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

Traces 4: a Claude Code session file imports as a trace and is listed beside the others #18129

opened this

Merged 6h 12m ago, live in production

POST /api/v1/traces/import now takes a Claude Code session JSONL and keeps it as a private trace of kind imported. tsc and the import, route-contract, migration, body-cap, route-mark and unused-export tests pass, and the gate is clean.

Notes

opened this

POST /api/v1/traces/import now takes a Claude Code session JSONL and keeps it as a private trace of kind imported. tsc and the import, route-contract, migration, body-cap, route-mark and unused-export tests pass, and the gate is clean. I did not run the full suite, and the route-timing walk did not exercise the new route because it only walks GET routes. The forge's check runs everything.

  • Route: the body is read through the cap (a new traceImport row in BODY_CAPS, 32 MiB) and a plain 413 comes back past it. Needs an rw token. A browser session that sends the request cross-origin gets a 403. It answers 201 for a new session and 200 when the same session is sent again.
  • Stored: the raw file goes gzipped to traces/imported/<id>.jsonl.gz under the data directory. The id is a hash of the account plus the session id, so re-importing replaces the trace instead of duplicating it. Another account importing the same session gets its own trace.
  • Listing: GET /api/v1/traces?kind=imported lists the row for its sender and for nobody else. The row carries the first prompt as its title, the first model, steps, errors, tokens, and start and end times.
  • Reader and adapters: src/trace/claude_code.ts is reused as the only parser. The adapter interface is src/trace/import_adapter.ts, with import_claude_code.ts as its one source. Codex is a new file plus one line in ADAPTERS.
  • Refusals: a 422 (invalid_session) says why when the file is not UTF-8, is empty, has no session records, or has no usable sessionId.
  • Docs: docs/features/traces.md has the one-line curl for a Mac, and getApiV1Traces.ts now lists imported among the kinds. The test is test/traces-import.test.ts.

Chosen deviations:

  • Migration: the kind check on trace_index can't be widened by a migration that only adds, which the gate requires. So the migration adds an owner column, an imported row is stored with kind review (which nothing else writes), and listTraceIndex reads it back as imported and filters on that. kind=review still returns only real review rows.
  • Dead-code allowlist: readImported has no caller in src/ yet (the test uses it), so I added it to the dead-code allowlist for a later page to read.
  • Commit line: the commit says No door: for the terminal, since the import is a curl and the list already shows the row.

Left out:

  • Codex files, a page for an imported trace, deleting an import, and a per-account quota.
  • Content-Encoding: gzip on the upload.
  • Streaming to disk. The body is held in memory up to 32 MiB, which is why the cap is not larger.

Try it: curl -X POST -H "authorization: Bearer $FORGE_TOKEN" -H "content-type: application/x-ndjson" --data-binary @session.jsonl $FORGE_URL/api/v1/traces/import, then GET /api/v1/traces?kind=imported.

🤖 Generated with Claude Code

Closes #18071

4 earlier comments
commented

POST /api/v1/traces/import now takes a Claude Code session JSONL and keeps it as a private trace of kind imported. tsc and the import, route-contract, migration, body-cap, route-mark and unused-export tests pass, and the gate is clean. I did not run the full suite, and the route-timing walk did not exercise the new route because it only walks GET routes. The forge's check runs everything.

  • Route: the body is read through the cap (a new traceImport row in BODY_CAPS, 32 MiB) and a plain 413 comes back past it. Needs an rw token. A browser session that sends the request cross-origin gets a 403. It answers 201 for a new session and 200 when the same session is sent again.
  • Stored: the raw file goes gzipped to traces/imported/<id>.jsonl.gz under the data directory. The id is a hash of the account plus the session id, so re-importing replaces the trace instead of duplicating it. Another account importing the same session gets its own trace.
  • Listing: GET /api/v1/traces?kind=imported lists the row for its sender and for nobody else. The row carries the first prompt as its title, the first model, steps, errors, tokens, and start and end times.
  • Reader and adapters: src/trace/claude_code.ts is reused as the only parser. The adapter interface is src/trace/import_adapter.ts, with import_claude_code.ts as its one source. Codex is a new file plus one line in ADAPTERS.
  • Refusals: a 422 (invalid_session) says why when the file is not UTF-8, is empty, has no session records, or has no usable sessionId.
  • Docs: docs/features/traces.md has the one-line curl for a Mac, and getApiV1Traces.ts now lists imported among the kinds. The test is test/traces-import.test.ts.

Chosen deviations:

  • Migration: the kind check on trace_index can't be widened by a migration that only adds, which the gate requires. So the migration adds an owner column, an imported row is stored with kind review (which nothing else writes), and listTraceIndex reads it back as imported and filters on that. kind=review still returns only real review rows.
  • Dead-code allowlist: readImported has no caller in src/ yet (the test uses it), so I added it to the dead-code allowlist for a later page to read.
  • Commit line: the commit says No door: for the terminal, since the import is a curl and the list already shows the row.

Left out:

  • Codex files, a page for an imported trace, deleting an import, and a per-account quota.
  • Content-Encoding: gzip on the upload.
  • Streaming to disk. The body is held in memory up to 32 MiB, which is why the cap is not larger.

Try it: curl -X POST -H "authorization: Bearer $FORGE_TOKEN" -H "content-type: application/x-ndjson" --data-binary @session.jsonl $FORGE_URL/api/v1/traces/import, then GET /api/v1/traces?kind=imported.

🤖 Generated with Claude Code

commented

I fixed two of the three check failures and pushed. I didn't change anything for the third, a first-view budget failure, which I couldn't reproduce.

  • DOORS list: POST /api/v1/traces/import is now in DOORS in test/private-by-default.test.ts, marked noRepository because it makes a trace, not a repository. I placed it in sorted order, since the test also checks the order.
  • Account purge: trace_index.owner is now in COVERED in src/core/account_purge.ts as DELETE. An imported session belongs to the person who imported it, so its index row goes when their account is purged. The compressed session file under traces/imported/ is not removed by this change.
  • First-view budget: test/attention.test.ts passes here, and the check log says it is green on main. It first showed up in the log as a 64 ms failure on a shard that was running slowly, so I'm treating it as timing noise.

test/private-by-default.test.ts and test/account-purge.test.ts pass. bun x tsc --noEmit is clean. gate.ts --fix left nothing to commit; it only noted one comment line over 100 columns in src/trace/index_table.ts.

commented

I merged main into the branch and pushed it. scripts/unused-exports-allow.json was the only conflict.

I kept lockfileDependencies, which main still lists, and src/trace/import.ts readImported, which this branch adds. I dropped src/core/grants_view.ts everyGrant. That line came from this side, and main's version of the file no longer has it, so I took that to mean main now uses everyGrant. I didn't check that everyGrant is now used.

bun x tsc --noEmit is clean. The trace tests plus private-by-default and account-purge pass, 72 in all. I didn't run the full suite. scripts/fast.ts said the diff was too broad to name tests, so the forge's check on this push will run the rest.

scripts/unused-exports.ts prints 182 lines on this branch against 181 on main. The extra line is readImported, the entry I added to the allow list. I didn't confirm that the script's output is a list of unlisted exports, so I can't say it passes.

commented

Pushed as 7ce70fd. The import tests, the account-purge and trace-index tests, tsc, and the OpenAPI, module-state, unused-exports and doors tests pass. I did not run the full suite; the forge runs it on the push.

What changed

  • Purge (finding 1): purgeAccount takes dataDir. It lists the person's imported ids from trace_index WHERE owner = ? before the transaction and deletes their traces/imported/<id>.jsonl.gz files once it commits. A new account-purge test fails without this and also checks that another account's file stays.
  • Quota (finding 2): each account may keep 256 MiB of compressed imports (IMPORT_QUOTA_BYTES in src/trace/import.ts). It is checked after gzip and before writing, and re-sending a session counts only the new copy. A session over the quota gets a 413 with quota_exceeded, and a test covers it. The docs and the OpenAPI route now state the cap, and the "no quota" line is gone from the docs' "not yet" list.
  • Part files (finding 5): the .part name now carries a random UUID, and a test runs two imports of one session at once. In traces.ts the trace/import.ts import now sits beside the other trace/ import, and the limits.ts import now follows context.ts.
  • Review kind (finding 4): putTraceRow has a comment saying imported rows are stored as review and told apart by owner <> ''. A new test checks that no kind filter, including none, returns an owned row as a review.

What I left out

  • Terminal listing (finding 3): I did not add one. The terminal Traces screen lists crew runs through trace/list.ts, and imports are an API-only door (GET /api/v1/traces?kind=imported). The commit says No door: with that reason. I'd take a terminal screen for imports as a later issue.
  • Request-path cost: the decode, parse and gzip of a 32 MiB file still happen on the request path. Gzip is async, but parseClaudeSession is synchronous. The quota bounds disk, not CPU.

gate.ts still notes that importedIdsOf looks like ownedRepos; the two query different tables, so I left both.

commented

Merged main into the branch and pushed; I didn't run the full suite, only the six trace test files, and those pass (45 tests) with tsc clean.

The merge conflicted in three places: docs/features/traces.md, src/http/api/traces.ts and src/http/lazy_lists.ts. Both sides stayed in each: POST /api/v1/traces/import from this branch and GET /api/v1/traces/:id (the trace tree) from main. In src/http/api/traces.ts the two imports from index_table.ts became one, and traceRoutes registers both routes.

I ran bun scripts/fast.ts and it said the diff was too broad to name tests, so I picked the trace files myself. The forge's check will run the full suite on this push.

The gate flagged two things I left alone:

  • Duplicate-code note: it reports importedIdsOf in src/trace/import.ts as identical to ownedRepos in src/core/account_purge.ts and asks whether they should share one implementation.
  • Long comment lines: two files have comment lines over 100 columns, which the nightly rewrap handles on main.

Pull description: POST /api/v1/traces/import takes a Claude Code session JSONL (up to 32 MiB) and stores it gzipped. It is indexed as an imported trace, private to the caller. Re-sending a session replaces its trace. An account keeps at most 256 MiB of imported files, and they are deleted with the account. Not done: Codex files, a page for an imported trace, deleting a single one. Try it with the curl line in docs/features/traces.md.

Sign in to join this conversation.

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

Keyboard shortcuts

Docs