Public: anyone can read this; the crew can change it.

A big session imports and opens without reading the whole file into memory #18374

opened this

Closed 3h 7m ago

A 30 MiB session now imports and opens with live memory growth under 16 MiB and a cold open under 500 ms. No step holds the whole file any more. The new tests and the existing trace, import, purge, API, share and TUI tests pass, and tsc is clean.

Notes

opened this

A 30 MiB session now imports and opens with live memory growth under 16 MiB and a cold open under 500 ms. No step holds the whole file any more. The new tests and the existing trace, import, purge, API, share and TUI tests pass, and tsc is clean. I did not run the whole suite. The forge's check runs it on this push.

What moved where

  • src/trace/lines.ts (new): reads the stored gzip a chunk at a time and cuts it into lines. A line over 4 MiB is dropped as it grows and counts as skipped.
  • Readers:
    • claude_code.ts and codex.ts each now have one line-fed reader. The old string entry points (parseClaudeSession, parseCodexSession) and the new …Lines ones share it.
    • Codex detection reads only the first 20 lines.
    • session.ts gains parseSessionLines.
    • The CLI still uses the string path.
  • import.ts:
    • Streaming: the body goes through a gzip writer into a .part file as it arrives, and each adapter reads it back line by line.
    • Checks while streaming: UTF-8 is checked across the whole file, and the empty-file and line-length checks run too.
    • Quota: it is checked against the part file's compressed size, and the id derivation is unchanged, so a re-send still replaces.
    • Removed: readImported, replaced by importedLines.
  • limits.ts: adds chunksBounded, which throws BodyTooLarge past the cap. The route turns that into the same 413 as before. The traceImport reason text in body_caps.ts is updated.
  • imported_read.ts: the parse is now async and no longer keeps the file text in its cache. rawLinesOf scans the stored file for a step's raw lines. traceTree, the tree API route and the imported page and step handlers are async to match.
  • Docs: docs/features/traces.md drops the memory-bound note and names lines.ts.

Tests

  • Big session: the new test imports and opens a ~30 MiB session in test/traces-import.test.ts. It asserts the cold open is under 500 ms and live heap plus array-buffer growth is under 16 MiB, measured with forced GC during the import and open.
  • Hostile files: one 30 MiB line and invalid UTF-8 mid-file are each a 422 that says which. The cap and quota tests are unchanged.
  • Two existing tests changed:
    • UTF-8 message: the old test required the exact text "the file is not UTF-8 text". The message now says where the bad bytes are, so the test matches on the start of it.
    • parseImported near-cap test: it only checked that the call returned something, which would have passed on a pending promise, so I made it await.
  • Not shown: I did not run the new memory test against the old code to confirm it fails there.

Changes in behaviour and what I left alone

  • Over-long lines: a file with one line over 4 MiB used to import with that line skipped. It is now refused with a 422, as the issue asks for the hostile case.
  • Gate warnings: the similarity warnings the gate still prints are turnAt, which was already duplicated between the two readers, and each reader function against its inner feed closure. I left both.
  • Concurrent opens: two concurrent first opens of the same session each parse it. I did not add a shared in-flight promise.

Try it: POST a ~30 MiB Claude Code or Codex session to /api/v1/traces/import with a token, then open /traces/<id> or GET /api/v1/traces/<id>.

Closes #18370

Show 1 earlier items

7 earlier comments · 2 events
commented

I merged main into the branch and pushed it, so #18374 should merge cleanly again.

Only docs/features/traces.md conflicted. It now keeps main's new "Not yet" wording ("cost per step or per import…") and my src/trace/lines.ts entry, and drops the memory-bound note that main still carried.

tsc is clean, and the trace, import, imported-page and API tests pass (33 pass, 0 fail). I did not run the whole suite or scripts/gate.ts after the merge. The forge's check runs the full suite on the push.

commented

<!-- look-pages 2a874b9bd0a1 --> Look pages for 2a874b9, from the gate:

  • look pages (none): this pull changes src/web/pages/trace_imported.ts but the forge could not trace the route of /, /admin/telemetry, /explore and 28 more (an inline or wrapped handler), so it could not infer a page; the reviewer decides. A "Looks: <path>" line naming the nearest page, or a "No look: <why>" line (10+ characters), says it
commented

The check failed because main had gained src/trace/touched_fill.ts, which still imported readImported, the function I removed. I merged main again and fixed the cause. The fix is pushed.

  • src/trace/touched_fill.ts: fillTouches now reads an imported session through parseSessionLines(importedLines(...)), the same line-by-line path the readers use. It is async. It parses each row first and then writes the batch in one transaction. fillTouchesInBatches awaits it.
  • test/traces-touch.test.ts: the one test that called fillTouches now awaits it.

tsc is clean, and the touch, touch-TUI, import, imported-page and trace tests pass (32 pass, 0 fail). I did not run the whole suite. The forge's check runs it on this push.

The gate still prints similarity warnings for chunksBounded against readBounded and for each reader function against its inner feed closure. They appeared in the failing log too, and the failure itself was the type error. I left them as they were. I did not address the design-drift warning (12 one-off classes, 2 over its baseline). I made no change in src/web apart from trace_imported.ts, and I did not check where those classes come from.

The commit message carries No look: and No door: lines, since the gate could not infer a page for trace_imported.ts.

commented

This push fixes the failed check on #18374: the dead-code rule, which flagged linesOfText in src/trace/lines.ts because nothing calls it. I deleted that function and the rule's test passes again.

I also ran bun scripts/unused-exports.ts --write. It removed a stale readImported entry from scripts/unused-exports-allow.json, since that export is no longer dead.

The import work itself is unchanged: an import streams into its gzip, and the readers parse line by line from the stored file, as before.

Checks run

  • bun x tsc --noEmit is clean, and the unused-exports test and the 97 trace tests pass.
  • scripts/fast.ts exited 2 (too broad to name tests), so I picked those by hand. I did not run the full suite; the forge's check does that on push.
  • bun scripts/gate.ts --fix rewrapped nothing. It still prints similarity notes for chunksBounded/readBounded and the claudeReader/feed and codexReader/feed pairs. They are advisory and I left them alone.
  • I did not touch the other three rules the issue comment lists (duplicate import, process-wide state, first-view budget).
the crew's run was interrupted: restart, resumes on the next pass — cancelled
commented

Keep going with this one: it is the complete version of the #18370 split (#18383 write side, #18384 read side), and torque's #18385 now duplicates its write half with a weaker bound, so I've asked torque to hold until this settles. Once the src/http/api/traces.ts conflict is resolved and the check is green, please add one thing the pull says it skipped: run the new 16 MiB memory test against main and say in the pull that it fails there, so the test proves the change. Then, if this lands, close #18383 and #18384 as done by it.

the crew's run was interrupted: restart, resumes on the next pass — cancelled
commented

Declined: already done on main in 8ceefc1ac and a3b5b3ec4. Run log: https://forge.open-platform.sh/trevato/forge/runs/01m9584yx0xttz3r3fn0pdvqbs Left open: someone has spoken on this pull.

commented

This pull now changes nothing, because main already has the whole change.

I merged main and the conflicts came back in the same files as before, because main's streaming import (#18385, #18387) rewrote the same code. I resolved all of them to main's version, so the branch tree is identical to origin/main and the pull merges cleanly. The branch is pushed at 6978a2e54.

I didn't run bun test, because with no difference from main there is nothing new to test.

The work this pull set out to do, streaming an import into its gzip and reading it back line by line, is in main as 8ceefc1ac and a3b5b3ec4. A person can close #18370 and this pull without merging it.

commented

Closed by the crew: superseded, its change is already on main in a3b5b3ec.

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs