Skip to content

Phase 3 (#64): progressive BIDS tree (skeleton + lazy per-subject) - #69

Merged
neuromechanist merged 3 commits into
feature/issue-66-epic-cold-paintfrom
feature/issue-64-progressive-bids-tree
May 26, 2026
Merged

neuromechanist merged 3 commits into
feature/issue-66-epic-cold-paintfrom
feature/issue-64-progressive-bids-tree

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

The biggest cold-paint win of epic #66. The current /api/dataset/<id>/tree?v=<v> endpoint emits a single 3.3 MB HTML blob for on005505 (5,409 paths / 136 subjects), blocking the file panel for seconds on low-bandwidth connections. This PR splits the endpoint into two response modes:

  • Skeleton (no subject= param): top-level non-subject entries rendered inline; every sub-XYZ directory becomes a closed <details data-subject="sub-XYZ"> carrying counts + size but no nested files, with a [data-subject-target] slot waiting for content. Target: < 50 KB for on005505.
  • Subtree (?subject=sub-NDARAC904DMU): returns just that subject's inner <ul class="tree__children"> markup, which the client drops into the matching slot on first <details> toggle.

Client-side glue is one new document.addEventListener("toggle", ..., true) in src/pages/dataset/[id].astro — capture phase because the toggle event does NOT bubble. The skeleton uses native <details> so keyboard nav (Tab + Enter/Space) works for free.

Validation: subject= must match ^sub-[A-Za-z0-9]+$ (rejects "", ../, sub-x/y); invalid values get a 400. A well-formed subject that doesn't exist in this version's tree gets a 404. Per-subject responses are edge-cached under PUBLISHED_CACHE — the existing URL-keyed Workers cache namespace already gives us per-(id, version, subject) entries for free.

Closes #64
Part of epic #66

Test plan

  • bun run typecheck — 0 errors
  • bun run lint — biome clean
  • bun run test — 386/386 (was 378 in Phase 2; +8 new in the new render-tree.test.ts — isSubjectDir traversal guard + skeleton/subtree rendering verification)
  • Manual dev verification on a 100+-subject dataset (on005505): cold paint shows top-level entries immediately; subject rows expand on click and on keyboard Enter; second expand of the same row does NOT re-fetch (data-loaded guard); collapsing and re-opening preserves the loaded subtree.
  • Devtools network panel: skeleton response < 50 KB; per-subject responses small; subject error case (e.g., ?subject=sub-INVALID) returns 404, not 500.
  • /browse against the Pages preview after merge — verify the UX under the real Worker runtime.

Files touched

  • src/lib/render-tree.ts — isSubjectDir predicate, renderBidsSubtree export, renderBidsTree modified to collapse subject branches.
  • src/pages/api/dataset/[id]/tree.ts — new ?subject= validation + dispatch; common TreeNode build path; subtree handler returns renderBidsSubtree(...) or 404.
  • src/pages/dataset/[id].astro — one capture-phase toggle listener for lazy subject expansion.
  • src/lib/render-tree.test.ts — new file, 8 tests.

Notes

  • The page-bundle consumer (nemar-cli#617) is still a follow-up. Will file a follow-up website issue after the epic lands.
  • Native <details> instead of a custom widget keeps the keyboard / screen reader story free.
  • Subject error UX inline-renders a <p class="tree__error" role="alert"> if the subtree fetch fails — collapse/re-open retries.

Splits /api/dataset/<id>/tree?v=<v> into two response modes via
the new optional subject= parameter. Without subject= the endpoint
emits a skeleton: top-level non-subject entries inline, each sub-XYZ
as a collapsed <details data-subject="..."> with a lazy slot. With
subject= it returns just that subject's inner subtree. The dataset
detail page picks up subtree expansions on the first <details>
toggle and drops the fragment into the matching slot.

Targets on005505 specifically: 5,409 paths / 136 subjects used to
ship 3.3 MB of HTML in one blob and block the file panel for
seconds. Skeleton stays under 50 KB; subtree fetches are scoped to
one subject at a time and edge-cached per (id, version, subject)
for free via the existing URL-keyed cache.

Adds isSubjectDir(name) predicate (used both for skeleton row
identification and as the param-validation gate on the endpoint),
renderBidsSubtree(node, basePath) for the subtree response, and 8
tests covering subject-vs-non-subject rendering plus the
traversal-guard cases on the predicate.

Closes #64
Part of epic #66
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented May 26, 2026 •

Copy link
Copy Markdown

Five sonnet reviewers ran in parallel. Critical and important fixes:

- silent-failure-hunter + code-reviewer: 503 response on the subtree
  branch had no Cache-Control. Add FALLBACK_CACHE (no-store) so a
  transient upstream failure doesn't pin at the edge, matching the
  skeleton's no-manifest treatment.
- silent-failure-hunter + comment-analyzer: the 404 (subject not
  found) inline comment promised a "short cache" the code never
  set. Add a NOT_FOUND_SUBJECT_CACHE constant (60s s-maxage) so
  the wire behavior matches the documented intent.
- code-reviewer: 400 responses (missing v=, invalid subject=)
  carried no Cache-Control. Add no-store on both paths so an
  intermediate proxy can't pin the error.
- code-reviewer: subject regex was alphanumeric-only; widen to
  /^sub-[A-Za-z0-9-]+$/ so BIDS 2.0 + real OpenNeuro datasets
  like sub-NDAR-AC904DMU don't silently regress to inline render.
  Update isSubjectDir tests; embedded slash still rejected.
- code-reviewer: .tree__error class had no CSS rule. Add a scoped
  rule in BidsTree.astro so the subtree-fetch-failure paragraph
  reads as a retry prompt, not body text.
- type-design-analyzer: replace inline import("...").TreeNode
  with a top-of-file import type so the dependency is legible.
- comment-analyzer: tighten the renderBidsSubtree docstring with
  the concrete renderDirChildren(node, basePath, 1) call site,
  and add a "keep in sync" warning to the subject-param
  validation comment.
- pr-test-analyzer: add four endpoint-validation tests (no v=,
  malformed subject, empty subject, underscore label) and two
  rendering edge cases (subjects-only tree, nested sub-* doesn't
  re-collapse).

393/393 tests (was 386).

Deferred to a follow-up issue: small-subject UX regression (2-3
subjects now require an extra click). Out of #64's stated scope
(100+ subject case).
CF Pages picks up *.test.ts under src/pages/** as a route entry
and breaks the build (same class as PR #62 / commit 3cbc89f for
the proxy test). Relocate to src/lib/tree-endpoint.test.ts; the
file still imports GET from the route source, so the wire-contract
coverage is unchanged. 393/393 tests pass; local astro build
completes clean.
@neuromechanist
neuromechanist merged commit 55f24ff into feature/issue-66-epic-cold-paint May 26, 2026
1 check passed
@neuromechanist
neuromechanist deleted the feature/issue-64-progressive-bids-tree branch June 21, 2026 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant