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/hdb.c src/hg/lib/hdb.c
index 3ac8722e87c..6aa8b05af50 100644
--- src/hg/lib/hdb.c
+++ src/hg/lib/hdb.c
@@ -4330,40 +4330,45 @@
 }
 
 static struct trackDb *rFindTrack(int level, struct trackDb *tdbList, char *track)
 /* Look for named track in list or children. */
 {
 struct trackDb *tdb;
 for (tdb = tdbList; tdb != NULL; tdb = tdb->next)
     {
     if (sameString(track, tdb->track))
         return tdb;
     struct trackDb *matchingChild = rFindTrack(level+1, tdb->subtracks, track);
     if (matchingChild != NULL)
         return matchingChild;
     }
 
-/* Look "to the sky" in parents of root generation as well. */
+/* Look "to the sky" in parents of root generation as well.  Walk the whole
+ * chain: a supertrack may sit inside another supertrack, and neither is in the
+ * list itself, so the outer one is only reachable through the inner one. */
 if (level == 0)
     {
     for (tdb = tdbList; tdb != NULL; tdb = tdb->next)
 	{
-	struct trackDb *p = tdb->parent;
-	if (p != NULL && sameString(track, p->track))
+	struct trackDb *p;
+	for (p = tdb->parent; p != NULL; p = p->parent)
+	    {
+	    if (sameString(track, p->track))
 		return p;
 	    }
 	}
+    }
 
 return NULL;
 }
 
 #ifdef DEBUG
 static void dumpFlagStatus(struct trackDb *tdbList, char *tableName, char *label)
 /* Look for tdbList for track matching tableName.  Print out info on it starting with label. */
 {
 struct trackDb *tdb = rFindTrack(0, tdbList, tableName);
 if (tdb == NULL)
      printf("%s: nil<BR>\n", label);
 else
      printf("%s: treeNodeType %d, composite? %d, supertrack ? %d<BR>\n", label, tdb->treeNodeType, COMPOSITE_NODE(tdb->treeNodeType), SUPERTRACK_NODE(tdb->treeNodeType));
 }
 #endif /* DEBUG */
@@ -4372,30 +4377,35 @@
 /* Go through tdbList and set up the ->children field in parents with references
  * to their children. */
 {
 struct trackDb *tdb;
 
 /* Insert a little paranoid check here to make sure this doesn't get called twice. */
 for (tdb = tdbList; tdb != NULL; tdb = tdb->next)
     if (tdb->children !=  NULL)
         internalErr();
 
 for (tdb = tdbList; tdb != NULL; tdb = tdb->next)
     {
     struct trackDb *parent = tdb->parent;
     if (parent != NULL)
         refAdd(&parent->children, tdb);
+    /* A supertrack that is a member of another supertrack is not in tdbList
+     * itself, so the loop above never reaches it.  Add it to its own parent's
+     * children the first time one of its members brings us here. */
+    for (; parent != NULL && tdbIsSuperTrackChild(parent); parent = parent->parent)
+        refAddUnique(&parent->parent->children, parent);
     }
 }
 
 struct trackDb *trackDbPolishAfterLinkupKeepAll(struct trackDb *tdbList)
 /* The part of trackDbPolishAfterLinkup that every caller wants, without dropping
  * tracks whose data cannot be reached.  hubCheck needs this: a track pruned because
  * its bigDataUrl does not resolve is exactly the track hubCheck has to report on. */
 {
 addChildRefsToParents(tdbList);
 trackDbContainerMarkup(NULL, tdbList);
 rInheritFields(tdbList);
 slSort(&tdbList, trackDbCmp);
 return tdbList;
 }