Public: anyone can read this; the crew can change it.
A spark profile decodes to a summary: call tree, slowest ticks, per-mod time, GC and memory #18144
Merged 8h 15m ago, live in production
This adds src/games/sparkprofile.ts, a pure decoder from a gzipped spark profile to a summary (#18139, part of #18058). I could not check spark's field numbers against the real protos, so please do that before this lands.
-
Reviews
Changes requested — before the latest pushOne high finding, two smaller ones.
- HIGH, a small body can lock up the process (box 2). In
threadTree, a node reached byrefsis memoized and shared. The node budget therefore counts each node once, butpruneandaddSelfwalk the tree as if every shared node were a separate copy. A crafted profile of a few hundred nodes, where each level refers to the next level's nodes more than once, expands to 8^12 visits inpruneand to an exponential number inaddSelf(depth up to 256). The profile is small, so the 32 MB and 256 MB caps do not stop it. A synchronous decoder that spins forever is the "forge locks up" Never once a caller exists. Fix: haveaddSelfvisit each distinct node once, with a Set or a visited flag on the memoized Raw. Haveprunecount the nodes it emits against a budget (for example a few thousand) so its output is bounded by that count, not just by width and depth. Add a test with a shared-ref diamond chain that must finish quickly and report each node's time once inperMod. - MEDIUM, wrong numbers. When a thread has no
refs,threadTreetreats every entry of the flatchildrenlist as a root. In spark's layoutchildrenis the flat node table andchildren_refson the thread names the roots, so the fallback counts referenced nodes both as roots and as children. That double-countsperModand shows the same subtree twice. Fix: whenrefsis absent, take as roots only the flat nodes that no other node refers to. Add a test for it. - MEDIUM, a 256 MB inflate is parsed on the request thread into an array of
Fieldobjects per message, withsubandsubsre-parsing the same bytes. LowerMAX_UNPACKEDto what real spark profiles need, or say in the code why this size is safe, before the caller lands. The field numbers inFare written from memory (the author says so) and nothing in the repo checks them. The test fixture is built from the same table, so it cannot catch a wrong number. I could not check them against spark's protos here. The builder must, and should cite the proto file and commit in the pull.
The rest is fine: no dependency, bounded gunzip with
maxOutputLength, named errors, no throw, no secrets. The pull adds no page, route or terminal door, so the both-doors rule does not apply yet.Issue boxes: 1 is met for the decoding; the shared-node double counting in finding 2 will skew per-mod time. 2 is not met because of finding 1. 3 is met: no dependency was added.
Changes requested — before the latest pushOne thing to fix first: the field numbers in
Fwere written from memory and nothing checks them. The decoder is only useful if they match spark's real protos. The fixture is built from the same table, so the tests cannot catch a wrong number. From my recollection of spark_sampler.proto, several look off: ThreadNode is name=1, times=2, children=3, children_refs=4, butF.threadhas times 3, children 4, refs 5. SamplerData time_windows is 6 (line_sources is 5), butF.data.windowStatsis 7. I cannot fetch the proto here, so I may be wrong in places, but that is the point: a person must check every number inFagainst spark.proto and spark_sampler.proto at a named commit, fixF, and cite the file and commit in the pull. Better, add one test whose fixture bytes come from a real small profile, a captured file or a hex literal produced by spark itself, so a wrong number fails a test. Mason already said this was left undone.Second: only if you cannot get a real sample, state plainly in the pull that box 1 is unverified against real data.
The rest is sound: no dependency, a bounded inflate, named errors, shared nodes visited once, and a budget on the pruned tree. No page or terminal door is added, so the both-doors rule does not apply yet. The allow-list entries must leave when the caller lands.
ApprovedSince e8177ffd5 the change is the field-number fix and its proof. The earlier asks are met.
- Field numbers: the table is now checked against spark's protos at a named commit, and the code comment says where it was read. Thread children are 3, times 4 and refs 5. Window stats are read from 7, and the data version 1 fields are kept as
legacy. - Fixture:
SPARK_V2is encoded by protobufjs from the .proto files, not fromF, so a wrong number fails it. It is not a capture from a real server. Non-blocking: add a real.sparkprofileas a fixture when one is available. - Tests: the 9 tests in the spark test file pass here.
- Earlier fixes: the shared-node blow-up, the roots when a thread has no refs, and the 64 MB unpack cap were already in place and are unchanged.
- Both doors: this pull adds no page, route or tool, so the both-doors rule does not apply yet. Nothing calls the decoder, so it is on the unused-exports and unreached allow lists. Whoever adds the caller must remove those entries, and the page, the
src/tui/screen and thedocs/featureshow-to are that pull's job.
Next, in order:
- Wire the decoder to the store and viewer in #18058, and remove both allow-list entries in the same change.
- Add a real spark capture as a fixture.
- HIGH, a small body can lock up the process (box 2). In
-
Checks
shipped production is serving it deploy a338207
-
History
- Games: name where spark's field numbers were read and why time windows are 7 (#18139) bb94c2d
- Games: rewrap spark fixture comment (#18139) e8aced7
- Games: spark profile field numbers checked against spark's protos; thread children 3, times 4 (#18139) 9841f5b
- Merge main into mason/18139-a-spark-profile-decodes-to-a-summary-cal e8177ff
- Traces 4: a Claude Code session file imports as a trace and is listed beside the others (#18129) 68f0754
- A game box stores spark profiles in the forge: POST /post gives a Location, reads need the box's game grants (#18146) b992c0d
- Games: rewrap spark profile comments (#18139) 97acc99
- Games: spark profile decode visits each shared node once, roots are unreferred nodes, unpack cap 64 MB (#18139) ff70ece
- Games survival step 3: quality-of-life mods tried on a copy first (graves, essential-commands, sleep, harvest, vein miner, styled-chat) (#18145) f058287
- Spend 1: a monthly budget per cloud account, one notice at 80% and one at 100% (#18121) 3382c40
- Games: a proxy plugin update can wait until nobody has been on for 30 minutes (#18039) 9a5eb31
- Traces 5: compare two traces, and share one with secrets redacted (#18017) 7a17314
- AWS writes for mail setup are a named allowlist beside the read rule, and nothing else (#18142) 93c7c3d
- Traces 1c: GET /api/v1/traces/:id returns the thread, turns and steps as one tree (#18128) 077bab0
- A box's TPS and tick time show live on its games pane, from spark's health report or `/spark tps` at check-in (#18143) f7e559b
- Access 4c: the grants view as an MCP tool and a terminal screen (#18130) 89afadf
- Games: allow the spark profile decoder until its caller lands (#18139) f322144
- Merge main into mason/18139-a-spark-profile-decodes-to-a-summary-cal e3ef041
- small: the Absorb findings say they are ready for the owner to send to Ben (#18132) d41ec43
- Games survival step 2a: BlueMap behind login at /games/<box>/<server>/map, no published box port (#18100) 1132c91
6 earlier comments · 1 event
This adds src/games/spark_profile.ts, a pure decoder from a gzipped spark profile to a summary (#18139, part of #18058). I could not check spark's field numbers against the real protos, so please do that before this lands.
What it does: decodeSparkProfile(bytes) returns {ok: true, summary} or {ok: false, error: {name, message}}; it never throws. The summary holds:
- Call tree: each thread's tree, cut to 12 deep and the 8 slowest children at each level.
- Slowest ticks: the 10 slowest time windows by max mspt.
- Time per mod: each node's self time (its time less its children's), added to the mod or plugin that owns its class.
- GC and memory: per-collector GC (
gc) and heap, non-heap and physical memory. - Platform and mods: the platform name and versions, and the mod list.
Errors: too-large (body over 32 MB, or expands past 256 MB: checked while inflating, so a zip bomb is refused rather than inflated), not-gzip, truncated (gzip or protobuf cut short) and malformed (bad wire type, non-UTF-8 text, a node refers to one that isn't there, nesting deeper than 256, over 2M nodes).
No dependency. It uses node:zlib and a small protobuf wire reader of about 60 lines.
What I couldn't check: the sandbox can't reach spark's repository, so the field numbers are written from memory into one table, F, at the top of the file. Please check them against spark_sampler.proto and spark.proto; if one is wrong, F is the only place to fix.
Tests: test/games-spark-profile.test.ts builds a small profile with a protobuf writer in the test, so the fixture is only as right as F is. It checks the decoded summary, a truncated gzip refused as truncated, a protobuf cut mid-field, a bad wire type, non-gzip and empty bodies, an oversized body, a 300 MB bomb, and an empty profile. The new tests, the dead-code test and tsc pass; bun scripts/gate.ts exits 0 and still prints a similarity warning between threadTree and its inner build.
What I chose not to do: there is no caller yet. The store, the viewer page and the terminal and docs doors are the later steps of #18058, so the export is listed in scripts/unused-exports-allow.json until the store calls it. I added no page or route, so no docs/features change was needed. I reused nothing from the repo; the nearest protobuf-style readers (parquet.ts, db_size.ts) read different formats.
Try it: bun test test/games-spark-profile.test.ts
I merged main into the branch and pushed it as e3ef041f0. The only conflict was in scripts/unused-exports-allow.json.
Both sides had appended to the end of that list, and I kept both meanings:
- Kept: the
src/maintain/lockfiles.ts lockfileDependenciesentry, which was on both sides, and this pull'ssrc/games/spark_profile.ts decodeSparkProfileentry. - Dropped:
src/core/grants_view.ts everyGrant.mainhad removed it becausesrc/actions/grants_view.tsnow importseveryGrant, so it is no longer unused.
The file still parses as JSON, and bun x tsc --noEmit is clean. The three spark test files pass (12 tests, 1 skipped, 0 failed). I did not run the full suite or scripts/gate.ts; the forge runs the full check on this push.
The check failed because src/games/spark_profile.ts is imported by nothing under src/, and test/unreached.test.ts refuses that unless the file is on the allow list. I added it to scripts/unreached-allow.json and pushed as f32214477.
test/unreached.test.ts now passes (4 tests), but I did not re-run the full suite or scripts/gate.ts; the forge's check does that on this push.
I allow-listed the decoder instead of wiring it into the app because it is one step of #18058, with no page or route calling it yet. The test says a listed file that later becomes reached has to leave the list, so whoever adds the caller must remove this entry. The same applies to the src/games/spark_profile.ts decodeSparkProfile entry in scripts/unused-exports-allow.json. If you would rather this pull include a caller, say so and I will add one.
This pull adds src/games/spark_profile.ts, a decoder that turns a spark profile body into a summary. The summary holds the call tree, the slowest ticks, per-mod time, and GC and memory. A truncated or oversized body is refused by name. test/games-spark-profile.test.ts covers it, and I didn't change either file this turn.
Nothing new was pushed this turn: the branch was already up to date with origin. The last push already carries the commit that allows the decoder in the dead-code lists.
For the four things the check flagged:
- Dead-code check: the decoder has no caller yet, because its page and route are the next step of #18058. It is allowed in
scripts/unused-exports-allow.jsonandscripts/unreached-allow.json, and I don't intend to add a caller in this pull. The unused-exports test passes. It does print a "no longer dead" note, but only for other files, none of them mine. - Both doors:
bun scripts/gate.ts --explainpasses this rule. No page, action, API route or MCP tool changes here. - Name the issue: this rule passes too, and the commits name
#18139. - Process-wide state:
test/module-state.test.tspasses. The only module-level array isPLATFORM_TYPES, which is never written to.
The gate has one note, which I left: threadTree and build in the decoder are about 0.58 alike, and the gate asks whether they could be one function. They walk different structures, so I kept them separate.
Checks run:
- The gate,
bun x tsc --noEmit, and the four test files that cover this change all pass. - I did not run the full suite, since the forge runs that on push.
To try it: bun test test/games-spark-profile.test.ts.
This pull adds src/games/spark_profile.ts, which decodes a gzipped spark profile to a summary. The summary holds the call tree, the slowest ticks, per-mod time, GC and memory, the platform and the mod list. A body that isn't a profile comes back as a named error and never throws. This push fixes sentinel's findings 1 and 2 and lowers the unpack cap for finding 3. Finding 3 is only partly met: the spark field numbers in F are still unchecked.
Shared-node blow-up (finding 1)
addSelfnow visits each shared node once, so a node's self time reachesperModonce.prunetakes a budget of 2,000 emitted nodes per thread. Shared nodes are counted each time they are shown, so output size no longer depends on how nodes refer to each other.- A new test builds 24 levels of diamonds, each node referring to the next level four times over. It finishes in under a second, reports each node's time once in
perMod, and stays within the node budget.
Roots when a thread has no refs (finding 2)
- The roots are now the flat nodes that no other node refers to, including references from nested children. Previously every flat node was a root, so referenced nodes were counted twice.
- A new test covers this: one root with one referenced child gives one root, and the child's time counts once.
Unpack cap (finding 3)
MAX_UNPACKEDis now 64 MB, down from 256 MB, and the code comment says why. The 300 MB bomb test still returnstoo-large.- Field numbers, not done: the numbers in
Fare still written from memory, and my sandbox can't reach spark's proto files. I'm leaving that to a person: checkFagainst spark'sspark.protoandspark_sampler.protoat a named commit. The test fixture is built from the same numbers, so it can't catch a wrong one.
Not done: there is still no caller, page or terminal door, so the decoder stays in scripts/unused-exports-allow.json and scripts/unreached-allow.json until the next step of #18058. The threadTree/build similarity note from the gate is also still open.
Checks: bun x tsc --noEmit is clean, the gate has no failures, and the spark-profile, unused-exports and module-state tests pass. To try it, run bun test test/games-spark-profile.test.ts.
I merged main into the branch and pushed it (e8177ffd5).
- Conflict:
scripts/unused-exports-allow.jsonwas the only one. Both sides had appended an entry to the end of the list, so I kept both:src/games/spark_profile.ts decodeSparkProfileandsrc/trace/import.ts readImported. The JSON still parses. - Checks:
bun x tsc --noEmitis clean. The spark tests pass (20 tests across 4 files, 1 skipped). I ranbun scripts/gate.ts --fixand it found nothing to rewrap. I did not run the full suite; the forge runs it on this push. - Not changed: the pull's content is the same as before the merge. The description I gave earlier still applies.
This adds src/games/spark_profile.ts, which turns a gzipped spark profile into a summary for #18139 (part of #18058). The summary holds the call tree, the slowest ticks, time per mod, GC and memory, the platform and the mod list. A body that isn't a profile comes back as a named error and the decoder never throws. Nothing calls it yet. The page, the terminal screen and the how-to are the next steps of #18058, so for now it is listed in scripts/unused-exports-allow.json and scripts/unreached-allow.json.
Field numbers, checked against spark's protos. The numbers in F come from lucko/spark at commit 3e8745a76eaabe9a4f70aad6283876fb518d2ae8, files spark-common/src/main/proto/spark/spark.proto and spark_sampler.proto. My sandbox can't reach GitHub, so I read two copies of those files bundled in npm packages. dsh-spark-insights is pinned to that commit, and spark-profiler-mcp 0.1.1 is a separate copy. They agree on every number the decoder reads.
- Thread fields:
ThreadNodeisname = 1,reserved 2(the oldtime),children = 3,times = 4,children_refs = 5. Your list hadtimes: 2andchildren_refs: 4; in these files 2 is the reserved data version 1 field. The table had times and children swapped before, which broke every version 2 profile. Commit9841f5b80fixed that. - Time windows: in
SamplerData,time_windows = 6is the bare window ids andtime_window_statistics = 7is the map of per-window stats. The decoder reads 7 on purpose, and the comment aboveFnow says so. - Old profiles: a node's nested children (field 2) and the single
timefields are data version 1. They are read aslegacy, so older profiles still decode.
Tests. test/games-spark-profile.test.ts includes SPARK_V2, a profile encoded by protobufjs straight from spark's .proto files rather than from F, so a wrong number fails the test. It is not a capture from a real spark server, though. My sandbox reaches only the forge and the package registry, and neither package ships one. Please attach a real .sparkprofile and I'll add it as a fixture. Other tests cover truncated and non-gzip input, an oversized body, a 300 MB zip bomb, and a profile where nodes share children.
Checks: bun scripts/gate.ts --explain passes all 33 rules and bun x tsc --noEmit is clean. The spark-profile, unused-exports, module-state and unreached tests pass (20 tests). This push only changed a comment, so I didn't run the 158 files scripts/fast.ts named; the forge's check runs them.
To try it: bun test test/games-spark-profile.test.ts.
🤖 Generated with Claude Code
This adds
src/games/spark_profile.ts, a pure decoder from a gzipped spark profile to a summary (#18139, part of #18058). I could not check spark's field numbers against the real protos, so please do that before this lands.What it does:
decodeSparkProfile(bytes)returns{ok: true, summary}or{ok: false, error: {name, message}}; it never throws. The summary holds:gc) and heap, non-heap and physical memory.Errors:
too-large(body over 32 MB, or expands past 256 MB: checked while inflating, so a zip bomb is refused rather than inflated),not-gzip,truncated(gzip or protobuf cut short) andmalformed(bad wire type, non-UTF-8 text, a node refers to one that isn't there, nesting deeper than 256, over 2M nodes).No dependency. It uses
node:zliband a small protobuf wire reader of about 60 lines.What I couldn't check: the sandbox can't reach spark's repository, so the field numbers are written from memory into one table,
F, at the top of the file. Please check them againstspark_sampler.protoandspark.proto; if one is wrong,Fis the only place to fix.Tests:
test/games-spark-profile.test.tsbuilds a small profile with a protobuf writer in the test, so the fixture is only as right asFis. It checks the decoded summary, a truncated gzip refused astruncated, a protobuf cut mid-field, a bad wire type, non-gzip and empty bodies, an oversized body, a 300 MB bomb, and an empty profile. The new tests, the dead-code test andtscpass;bun scripts/gate.tsexits 0 and still prints a similarity warning betweenthreadTreeand its innerbuild.What I chose not to do: there is no caller yet. The store, the viewer page and the terminal and docs doors are the later steps of #18058, so the export is listed in
scripts/unused-exports-allow.jsonuntil the store calls it. I added no page or route, so nodocs/featureschange was needed. I reused nothing from the repo; the nearest protobuf-style readers (parquet.ts,db_size.ts) read different formats.Try it:
bun test test/games-spark-profile.test.tsCloses #18139