1a9c722ad341c178dc418034e51a1d015507db9b
markd
  Fri Oct 2 20:23:47 2026 -0700
Let superTracks nest inside superTracks. refs #38460

A superTrack given a parent used to pass tdbQuery -check -strict and then be
dropped at load, with no warning: the browser reported the outer folder as not
found. Seven places each looked exactly one level up the parent chain, which is
what trackDb.h means by "Folders are superTracks. Currently only one level deep".

trackDbSuperMarkup   skipped any track that was itself a superTrack when
resolving parent, so the link was never made
rFindTrack           looked "to the sky" one level, so the outer super could
not be found by name
addChildRefsToParents only walked tdbList, which excludes superTracks, so an
inner super never reached its parent's children list
findSuperTrack       matched tdb->parent->track only, so hgTrackUi could not
find the outer folder of a hub assembly
flatten              rescued the immediate super parent only, so the outer one
was never written to the table
polishSupers         polished the immediate parent only, so the outer super
reached the write with a NULL shortLabel and segfaulted
groupTrackListAddSuper  made a track for one super above a member
setSuperTrackHasVisibleMembers  marked one level visible

Each now walks the chain. The group list still shows only the outermost folder,
since superTrack members are not listed there; the inner folders appear on its
page with their own controls, as members always have.

Walking the chain makes a parent loop fatal where it used to be harmless, so
trackDbSuperMarkup now checks that the supertracks form a tree once every link
is set, and breaks a loop at the supertrack that closes it, naming that track.
Verified by planting a loop: before, hgTrackDb spun in trackDbSetting and had to
be killed; now it warns and loads. The same hazard in composite parents is
untouched and filed as #38471.

Regression: trackDb built with this and with the released hgTrackDb is
byte-identical on mm39 (20377 rows) and, on a tree with no nesting, on hg38
(55629) and hs1 (782). With a nested trackDb the only difference on hg38 is the
one rescued container row. hgTracks on hg38 renders the same 32 rows and the
same 10 supertrack controls as dev, and the wgEncodeReg member list is
unchanged.

Note for whoever deploys this: trackDbToTxt also has to be rebuilt. It writes
the curated hub trackDb.txt from the table, and a released one emits a member
whose parent stanza is missing, which breaks the hub.

diff --git src/hg/lib/hubConnect.c src/hg/lib/hubConnect.c
index 219ffc10a3a..af9b83a9a9f 100644
--- src/hg/lib/hubConnect.c
+++ src/hg/lib/hubConnect.c
@@ -480,35 +480,44 @@
 }
 
 static struct trackDb *findSuperTrack(struct trackDb *tdbList, char *trackName)
 /*  discover any supertracks, and if there are some add them 
  *  to the subtrack list of the supertrack */
 {
 struct trackDb *tdb;
 struct trackDb *p = NULL;
 struct trackDb *next;
 
 for(tdb = tdbList; tdb; tdb = next)
     {
     /* save away the next pointer becuase we may detach this node and
      * add it to its supertrack parent */
     next = tdb->next;
-    if (tdb->parent != NULL && sameString(trackName, tdb->parent->track))
+    /* Walk up the chain: a supertrack may sit inside another supertrack, and
+     * neither is in tdbList, so an outer one is only named by an ancestor of a
+     * track that is.  Only an immediate child is added as a subtrack. */
+    struct trackDb *ancestor;
+    for (ancestor = tdb->parent; ancestor != NULL; ancestor = ancestor->parent)
 	{
-	/* found a supertrack with the right name, add this child */
-	p = tdb->parent;
+	if (sameString(trackName, ancestor->track))
+	    {
+	    /* found a supertrack with the right name */
+	    p = ancestor;
+	    if (ancestor == tdb->parent)
 		slAddHead(&p->subtracks, tdb);
+	    break;
+	    }
 	}
     }
 
 return p;
 }
 
 
 void hubConnectAddDescription(char *database, struct trackDb *tdb)
 /* Fetch tdb->track's html description (or nearest ancestor's non-empty description)
  * and store in tdb->html. */
 {
 unsigned hubId = hubIdFromTrackName(tdb->track);
 struct hubConnectStatus *hub = hubFromId(hubId);
 struct trackHubGenome *hubGenome = trackHubFindGenome(hub->trackHub, database);
 trackHubPolishTrackNames(hub->trackHub, tdb);