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/makeDb/hgTrackDb/hgTrackDb.c src/hg/makeDb/hgTrackDb/hgTrackDb.c
index bd8d9563bef..19687b0b51c 100644
--- src/hg/makeDb/hgTrackDb/hgTrackDb.c
+++ src/hg/makeDb/hgTrackDb/hgTrackDb.c
@@ -699,32 +699,36 @@
 {
 struct trackDb *tdb;
 for (tdb = tdbList; tdb != NULL; tdb = tdb->next)
     {
     trackDbPolish(tdb);
     rPolish(tdb->subtracks);
     }
 }
 
 static void polishSupers(struct trackDb *tdbList)
 /* Run polish on supertracks. */
 {
 struct trackDb *tdb;
 for (tdb = tdbList; tdb != NULL; tdb = tdb->next)
     {
-    struct trackDb *parent = tdb->parent;
-    if (parent != NULL)
+    /* Walk the whole chain of parents, not just the immediate one: a supertrack
+     * inside another supertrack is reached only through its child, and without
+     * its fields filled in from settings it reaches the write with a NULL
+     * shortLabel. */
+    struct trackDb *parent;
+    for (parent = tdb->parent; parent != NULL; parent = parent->parent)
 	{
 	trackDbFieldsFromSettings(parent);
 	trackDbPolish(parent);
 	}
     }
 }
 
 static struct trackDb *buildTrackDb(char *org, char *database, char *hgRoot, boolean strict)
 /* Build trackDb objects from files. */
 {
 struct hash *trackHash = newHash(0);
 char rootDir[PATH_LEN], orgDir[PATH_LEN], asmDir[PATH_LEN];
 
 /* Create track list from hgRoot and hgRoot/org and hgRoot/org/assembly
  * ra format database. */
@@ -773,41 +777,44 @@
 
 return tdbList;
 }
 
 static struct trackDb *flatten(struct trackDb *tdbForest)
 /* Convert our peculiar forest back to a list.
  * This for now rescues superTracks from the heavens. */
 {
 struct hash *superTrackHash = hashNew(0);
 struct slRef *ref, *refList = trackDbListGetRefsToDescendants(tdbForest);
 
 struct trackDb *tdbList = NULL;
 for (ref = refList; ref != NULL; ref = ref->next)
     {
     struct trackDb *tdb = ref->val;
-    struct trackDb *parent = tdb->parent;
-    if (parent != NULL && tdbIsSuperTrack(parent))
+    /* Walk up the chain of floating supertrack parents, not just the immediate
+     * one: a supertrack may itself sit inside another supertrack, and only the
+     * innermost is reachable from a track in the forest. */
+    struct trackDb *parent;
+    for (parent = tdb->parent; parent != NULL && tdbIsSuperTrack(parent);
+         parent = parent->parent)
 	{
 	/* The supertrack may appear as a 'floating' parent for multiple tracks.
 	 * Only put it on the list once. */
-	if (!hashLookup(superTrackHash, parent->track))
-	    {
+	if (hashLookup(superTrackHash, parent->track))
+	    break;
 	hashAdd(superTrackHash, parent->track, parent);
 	slAddHead(&tdbList, parent);
 	}
-	}
     slAddHead(&tdbList, tdb);
     }
 
 slFreeList(&refList);
 hashFree(&superTrackHash);
 slReverse(&tdbList);
 return tdbList;
 }
 
 static int findMaxCartVersion(struct trackDb *tdbList)
 /* Search the track list for the maximum cartVersion. */
 {
 struct trackDb *tdb;
 int maxVal = 0;
 for (tdb = tdbList; tdb != NULL; tdb = tdb->next)