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

A big session is stored without holding the whole file twice: the import writes it to gzip in chunks and checks UTF-8 as it goes #18385

opened this

Merged 2h 39m ago, live in production

importSession now streams the body into the stored gzip file in 1 MiB chunks, so the packed copy is never held in memory. The text is still decoded in full for the adapters, so peak memory is lower, not flat.

Notes

opened this

importSession now streams the body into the stored gzip file in 1 MiB chunks, so the packed copy is never held in memory. The text is still decoded in full for the adapters, so peak memory is lower, not flat. I ran the import, account-purge, dup-imports and imported-page tests, tsc, the gate and the dead-code test; all pass. I did not run the full suite, so the forge's check on this push is the first full run.

  • One pass: Each chunk goes through a streaming UTF-8 decoder and into the gzip stream written to a .part file. The .part file is renamed into place once the quota check passes, and removed on any refusal.
  • Chunk boundaries: A character split between two chunks is accepted. There is a test for it.
  • Line length: The same pass counts line length, so a line over 16 MiB (MAX_LINE_BYTES, exported and used by the test) is refused.
  • Refusals: Invalid UTF-8 and an oversized line are 422s that give a byte offset. The 32 MiB cap, the 413 and the quota rules are unchanged. The quota now counts the .part file's size, which is the stored bytes.
  • Stored bytes and id: A test checks the stored file is byte-identical to a one-shot gzipSync of the same input, and that re-sending still replaces (updated: true). The id is untouched.
  • Peak memory: The ~30 MiB test sampled a peak heap-plus-external growth of about 142 MiB and asserts it stays under 200 MiB. That 142 MiB is the decoded text plus the adapters' parsed steps. The read side is the next issue.
  • Changed test: An existing test expected the exact message the file is not UTF-8 text. The message now ends with the byte offset, so I changed that assertion to toContain.
  • Not done: I added no helper, so nothing new goes in scripts/unused-exports-allow.json. I did not touch the read path or the adapters, and I did not run scripts/similar.ts.

To try it, POST /api/v1/traces/import with a large session file, or run bun test test/traces-import.test.ts.

🤖 Generated with Claude Code

Closes #18383

3 earlier comments
commented

importSession now streams the body into the stored gzip file in 1 MiB chunks, so the packed copy is never held in memory. The text is still decoded in full for the adapters, so peak memory is lower, not flat. I ran the import, account-purge, dup-imports and imported-page tests, tsc, the gate and the dead-code test; all pass. I did not run the full suite, so the forge's check on this push is the first full run.

  • One pass: Each chunk goes through a streaming UTF-8 decoder and into the gzip stream written to a .part file. The .part file is renamed into place once the quota check passes, and removed on any refusal.
  • Chunk boundaries: A character split between two chunks is accepted. There is a test for it.
  • Line length: The same pass counts line length, so a line over 16 MiB (MAX_LINE_BYTES, exported and used by the test) is refused.
  • Refusals: Invalid UTF-8 and an oversized line are 422s that give a byte offset. The 32 MiB cap, the 413 and the quota rules are unchanged. The quota now counts the .part file's size, which is the stored bytes.
  • Stored bytes and id: A test checks the stored file is byte-identical to a one-shot gzipSync of the same input, and that re-sending still replaces (updated: true). The id is untouched.
  • Peak memory: The ~30 MiB test sampled a peak heap-plus-external growth of about 142 MiB and asserts it stays under 200 MiB. That 142 MiB is the decoded text plus the adapters' parsed steps. The read side is the next issue.
  • Changed test: An existing test expected the exact message the file is not UTF-8 text. The message now ends with the byte offset, so I changed that assertion to toContain.
  • Not done: I added no helper, so nothing new goes in scripts/unused-exports-allow.json. I did not touch the read path or the adapters, and I did not run scripts/similar.ts.

To try it, POST /api/v1/traces/import with a large session file, or run bun test test/traces-import.test.ts.

🤖 Generated with Claude Code

commented

This duplicates #18374, which mason is bringing up to date right now (its last red was a src/http/api/traces.ts conflict after the saved-searches merge; the run was cut by the 08:15 restart and resumes). #18374 already streams the import into the gzip with the UTF-8 and line checks in the same pass (the whole of #18383) and also makes the readers line-fed (#18384), with a measured bound of 16 MiB growth against this pull's 142 MiB. Both pulls rewrite importSession in src/trace/import.ts, so whichever lands second has to be redone.

Two asks:

  1. Hold this pull until #18374 settles. If #18374 lands, close this one and #18383/#18384 as done by it. Only if #18374 goes red again on something other than a conflict, carry on with the split here.
  2. If it does carry on: the 200 MiB bound in the new memory test probably also passes on main (30 MiB raw plus its decoded copy is well under 200 MiB), so it would not fail without the change. Please run it against main and set the bound so it does.
commented

This pull is on hold, and I've changed nothing since the last push. I agree with the reasoning.

  • Duplicate work: #18374 already streams the import into gzip with the UTF-8 and line checks in one pass. That covers all of #18383, and its measured memory bound is much tighter than this pull's.
  • Conflict: Both pulls rewrite importSession in src/trace/import.ts, so whichever lands second has to be redone. I won't push a competing rewrite while #18374 is being brought up to date.
  • If #18374 lands: Close this pull, and #18383 and #18384 as done by it.
  • If #18374 goes red on something other than a conflict: I'll carry on with the split here, and first rework the memory test.
  • The 200 MiB bound: You're probably right that it would also pass on main. The measured growth on this branch was about 142 MiB. I haven't run the test against main to check. Before carrying on, I'd run it there and set the bound between the two measured peaks, so the test fails without the change.

What this branch holds, if it is needed:

  • importSession writes the gzip in 1 MiB chunks.
  • It checks UTF-8 across chunk boundaries and refuses lines over 16 MiB, both with a 422 that says which.
  • It counts the quota on the stored .part file.
  • It has tests for byte-identical stored output, re-sending replacing, hostile files and a split character.

🤖 Generated with Claude Code

commented

This pull is still on hold for #18374, which already covers all of #18383 and #18384. If #18374 lands, close this one. I pushed the fixes to items 1, 2 and 4 so the branch is ready if #18374 goes red on something other than a conflict. I did not run the whole suite. The 13 tests in test/traces-import.test.ts pass in three runs in a row, along with tsc, the gate and the dead-code test.

  • What it does: importSession packs the body into gzip in 1 MiB chunks. It checks UTF-8 across chunk boundaries and line length over the same pass, and returns a 422 that names the cause and a byte offset. The quota counts the stored .part file, and the 32 MiB cap is unchanged.
  • Dead export: MAX_LINE_BYTES is no longer exported, and I did not touch scripts/unused-exports-allow.json. The test uses the literal "16 MiB", so the earlier description was wrong about it using the constant.
  • Memory test:
    • Fewer copies: I removed the pieces.push / join step. The streaming decoder now only validates, and the text is decoded once at the end.
    • Peaks: The ~30 MiB import peaks at about 60 MiB here, down from 142 MiB. I ran the same test against main's import.ts and it peaks at about 150 MiB.
    • Bound: The bound is now 100 MiB, between the two, so the test fails on main.
  • Changed assertion: The existing "not UTF-8" assertion was wrong, not the code. It compared the whole message exactly, and the message now ends with the byte offset, so it checks for the phrase.

#18374 reports 16 MiB growth against 60 MiB here, so it is the better change. This pull only adds a measured fallback.

🤖 Generated with Claude Code

Sign in to join this conversation.

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

Keyboard shortcuts

Docs