Skip to content

♻️ refactor(graph): extract prepareGraph and packBuffers from boot - #121

Merged
JadRizk merged 5 commits into
mainfrom
refactor/111-prepare-pack
Oct 11, 2026
Merged

JadRizk merged 5 commits into
mainfrom
refactor/111-prepare-pack

Conversation

@JadRizk

@JadRizk JadRizk commented Oct 10, 2026

Copy link
Copy Markdown
Owner

⏸ Held until 2.0 ships (see #107)

Closes #111
Part of #107

Summary

Moves boot's data prep and buffer packing out of GraphCanvas.tsx into two unexported modules. The code was copied as-is, then split and typed. Nothing else changed.

  • prepare.ts: prepareGraph covers validation, degree, displayed states, the layout seed and the solver's graph. buildPhysicsGraph builds the solver's node and edge lists, where a node's own values override its category's. It draws no random numbers.
  • buffers.ts: packBuffers calls packNodes and then packEdges, so every node seed is drawn before any edge seed. The iP2 slot constants A_TIER/A_HIDE/A_JIT/A_FRAY and EDGE_END_TRIM move with the code that writes them. GraphCanvas imports A_TIER/A_HIDE.
  • Left in boot(), as the scope asks: writing idToIndexRef, the dropped-edge warning, building visualRandom (passed in), and createPhysics + setParams. packBuffers is still called after the Scene and Camera are built.
  • Validation still throws out of prepareGraph into boot's catch, which calls onFatal.
  • buffers.ts imports { Color } only. A second * as THREE made the bundler rename GraphCanvas's namespace to THREE2, which turned the dist diff into noise.
  • The !s moved over unchanged, so 🏷️ types(graph): drop the non-null assertions the compiler doesn't need #118's removals still apply here if this PR merges second.
  • Patch changeset: .changeset/graph-prepare-pack.md.

Evidence

  • Harness (Differential harness for GraphCanvas at the three.js boundary #110): the full graph suite passes (322 tests) without -u, and git diff --exit-code packages/graph/src/__recordings__ is clean after every commit. As a mutation check, setting nodeSeeds[i] to a constant fails 21 of the 22 recordings.

  • Harness gap: flipping the arc's alternating sign does not change any recording, because the fixture's arcs never land on an odd edge index. buffers.test.ts covers that behaviour directly.

  • browser/graph.spec.ts: 14 passed in the pinned container. The three baselines are unchanged and the tree was clean afterwards.

  • dist: index.d.ts and index.d.cts are byte-identical to main, built from a git archive copy. The npm pack --dry-run file list is the same.

  • dist/index.js (prettier-formatted diff vs main): it only relocates code. The removed and added lines match except for:

    • the new function wrappers and the destructuring of their inputs and results;
    • validateGraph moving up next to prepareGraph;
    • const A_TIER = 0, … becoming var A_TIER = 0; and so on;
    • one edgeCount that now reads edgeCategoryIds.length, which is the same value.
    -      } = validateGraph(nodes, edges, nodeCategories, linkCategories, invalidEdges);
    +        nodeCount,
    +        edgeCount,
    +        …
    +        layoutSeed,
    +        physicsGraph,
    +      } = prepareGraph({ nodes, edges, nodeCategories, linkCategories, invalidEdges, seed });
           idToIndexRef.current = idToIndex;
    -      const nodeCount = nodes.length,
    -        edgeCount = liveEdges.length;
  • Lint: the graph package has 511 warnings, the same as main. The new functions raise no size, complexity or params warnings: each is ≤ 60 lines with complexity ≤ 12.

  • Unit tests: prepare.test.ts and buffers.test.ts test the pure functions directly. They cover the orphan, declared and default states, all three seed modes, dropped edges, the fray encoding (0/1/2), the alternating bow sign and default bow, radius growth and its cap, trims, gain and direction, defaults, and draw order: one draw per node, then one per edge.

  • CI test-job steps run locally, all passing: format:check, lint, typecheck, test, test:coverage, build, attw ×3, showcase build, the preview, tokens and styles drift checks, check-docs, peer-floor, check-consumer, check-changeset, and node scripts/test-react.mjs 18.

🤖 Generated with Claude Code

JadRizk and others added 4 commits October 11, 2026 01:44
Validation, degree, displayed states, the layout seed and the solver's
graph now come from a pure prepareGraph. boot keeps writing idToIndexRef,
the dropped-edge warning, visualRandom and createPhysics/setParams.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
packNodes then packEdges, so every node seed is drawn before any edge
seed. boot still calls it after the Scene and Camera, whose uuids draw
from Math.random when the graph is unseeded. The iP2 slot constants and
EDGE_END_TRIM move with the code that writes them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Covers the orphan state, the three seed modes, dropped edges, the fray
encoding, the alternating bow sign and the order of random draws, plus
the patch changeset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A second `* as THREE` namespace made the bundler rename GraphCanvas's to
THREE2, so dist/index.js changed far beyond the relocated code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tests

Split the shape/tier/colour and gain tests so each asserts one behaviour.
Add unit tests that fail if a node's category is looked up by position,
if the edge end trim drifts from 1.15, or if the iP2 slot constants move.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JadRizk
JadRizk merged commit ad4b637 into main Oct 11, 2026
4 checks passed
JadRizk added a commit that referenced this pull request Oct 11, 2026
main's tree is identical to #121's head, which this branch already
contains, so this branch's tree is kept unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
JadRizk added a commit that referenced this pull request Oct 11, 2026
#121 and #122 moved code out of GraphCanvas.tsx into prepare.ts, buffers.ts,
scene.ts, scene-meshes.ts and label-layer.ts, carrying 36 assertions the
compiler doesn't need. Drop them, and keep the 2d-context one in
label-layer.ts with the reason it had in GraphCanvas.tsx. Restore the reason
on the contrast test's kept assertion, lost when main's side of that hunk won.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
JadRizk added a commit that referenced this pull request Oct 11, 2026
…ed (#118)

* 🏷️ types(graph): drop the non-null assertions the compiler doesn't need

Graph builds under plain strict without noUncheckedIndexedAccess, so 371 of the 454 assertions inherited from qrntn's stricter config assert nothing: their operand's type already excludes null and undefined, or (one case) tsc accepts the nullable value as is. Each was removed as a single-character deletion; no other token changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* 🏷️ types(graph): justify the non-null assertions the compiler needs

83 assertions remain because their operand's type admits null or undefined and tsc rejects the code without them. Each holds by construction (a guard, a loop invariant, or a test's own setup), so each keeps its `!` with an eslint-disable-next-line that says why, as docs/code-style.md asks. The rest of the diff is prettier re-wrapping lines the first commit shortened.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* 📝 docs(changeset): empty changeset for the graph non-null clean-up

The built JS and .d.ts are byte-identical to main, so there is nothing to release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* 🏷️ types(graph): redo the non-null cleanup over the moved modules

#121 and #122 moved code out of GraphCanvas.tsx into prepare.ts, buffers.ts,
scene.ts, scene-meshes.ts and label-layer.ts, carrying 36 assertions the
compiler doesn't need. Drop them, and keep the 2d-context one in
label-layer.ts with the reason it had in GraphCanvas.tsx. Restore the reason
on the contrast test's kept assertion, lost when main's side of that hunk won.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

Extract prepareGraph and packBuffers

1 participant