Repository navigation
perf(gateway): keep artifact reads off the main thread - #158450
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed September 26, 2026, 12:50 AM ET / 04:50 UTC (Revision 7). ClawSweeper reviewWhat this changesMoves transcript artifact selection, image paging, and general history reads into the existing session-history worker while keeping artifact access checks in the Gateway. Merge readiness✅ Ready for maintainer review Current main still performs artifact scans on the Gateway thread, so this PR remains useful. The earlier missing-storage finding is repaired and covered by a focused test; this review found no new blocking defect. Priority: P2 Review scores
Verification
How this fits togetherGateway artifact requests read media references from session transcripts. The history worker selects transcript data, then the Gateway checks current access before returning results to the client. flowchart LR
A[Artifact request] --> B[Session access check]
B --> C[History worker read]
C --> D[Artifact selection]
D --> E[Current access recheck]
E --> F[Client response]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +405, Tests +72, Docs +9. Total +486 across 28 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Use the existing history worker as the single durable transcript-read owner, with connection cursors and final disclosure checks retained in the Gateway. Do we have a high-confidence way to reproduce the issue? Yes, at the source level: current main's artifact handler visits transcript messages on the Gateway thread, and the PR reports a before-and-after registered-handler benchmark. This read-only review did not rerun it. Is this the best way to solve the issue? Yes. The branch moves durable reads to the existing history worker and retains Gateway-owned authorization and cursors. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against dac6274dae58. LabelsLabel changes: No label changes. Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (6 earlier review cycles)
|
|
Merged via squash.
|
* perf(gateway): move artifact reads off the main thread * fix(gateway): preserve archive-only transcript reads * style(gateway): clarify captured worker reader inputs * fix(gateway): break artifact history type import cycles * fix(gateway): distinguish artifact selector from read facade * fix(gateway): preserve missing history storage errors * test(gateway): provide stored transcript for cached context report * test(agents): isolate nested tool history state
What Problem This Solves
Artifact browsing can pause Gateway chat streams because listing artifacts, paging images, and reading referenced images execute transcript SQLite queries and payload decoding on the Gateway thread.
User Impact
Artifact lists, image retrieval, public-share history, and anchored message visibility reads use the existing history worker while preserving IDs, ordering, pagination, filters, and access checks. No Gateway protocol, configuration, SQLite schema, retention, or update change is required. Process-held incognito data and existing native callback visitors retain their current owners.
Why This Change Was Made
The existing async transcript facade contained synchronous read callbacks. This moves artifact selection into its admitted worker, returns selected results, and keeps connection cursors, request authority, and post-await authorization in the Gateway. The shared pagination facade now dispatches durable reads through that owner. Read-only image discovery still refuses cold restoration. The artifact selector and managed-ID parser have one implementation each; history dispatch moves into a focused sibling to keep the worker within its line cap.
Evidence
node scripts/run-vitest.mjs src/gateway/server-methods/artifacts.images.test.ts --maxWorkers=1: 5 passed, 250.67 s command wall, including cold worker preparation. Existing cases now assert no caller-thread transcript payload SQL for full and image-only lists.node scripts/run-vitest.mjs src/gateway/server-methods/artifacts.transcript-images.test.ts --maxWorkers=1: 6 passed, 131.91 s command wall. Covers exact bytes, legacy duplicate IDs, replacement incarnations, post-await revocation, and no caller-thread transcript payload SQL.node scripts/run-vitest.mjs src/gateway/session-transcript-readers.test.ts --maxWorkers=1passed 16 tests in 140.87 s command wall, including extended recent/page coverage. The original mixed run took 579.15 s and failed only that corrected case.node scripts/check-changed.mjspassed, including production and all 25 core-test typecheck graphs, formatting, lint, dead-export, import-cycle, storage, and authentication guards. The initial run caught parameter-reassignment/shadowing lint issues; those were fixed before the passing final run.Landing CI repair
The original head's CI run 36204316844 failed
check-additional-runtime-topology-architecture: the all-import graph found a cycle through the new history request types, reader implementation, and session-accessor barrel. Other jobs were cancelled after that failure. The follow-up moves the message-by-ID result type alongside the reader's existing result contracts and points three type-only imports at their existing contract owners (six files, +13/-13; no runtime logic change).The full
pnpm check:architecturesuite passed, including zero runtime and all-import cycles. Core production types, targeted lint, formatting, and independent Codex review through P2 passed. Compiling each of the six repaired files with the installed TypeScript transform produced byte-identical JavaScript before and after the repair. The existing architecture gate covers the regression; no duplicative source-inspection test was added.The additional focused local test attempt was stopped during module loading on the heavily loaded host after a 742-second cold worker build; it returned exit 143 without test results. It is not counted as passing evidence. The earlier focused behavior proof above remains applicable to this type-only repair, and fresh CI is running on the new commit. An accidentally selected broad TypeScript config reported out-of-lane test errors; the correct
tsconfig.core.jsonproduction lane passed.The next exact-head CI run, 36208663918, reached the source-contract guard and found that the selector and async facade both exported
readSessionArtifacts. The internal selector is nowselectSessionArtifacts, with its worker, incognito, and fixture callers updated together; the facade contract stays unchanged.check-export-name-collisions.mts, core production types, and targeted lint passed after the rename. The run's source-contract failure cancelled remaining jobs; no unchanged CI rerun was requested.Final selector-rename proof:
node scripts/run-vitest.mjs src/gateway/server-methods/artifacts.test.ts --maxWorkers=1passed 53 tests, 545.19 s command wall on the loaded local host (468.96 s Vitest duration; worker preparation 347.96 s). This updates only the existing fixture's selector reference; no tests or assertions were added. Independent Codex review of the rename was scoped-clean through P2.ClawSweeper's missing-storage finding was reproduced through the real reader facade: with fallback disabled, paging resolved an empty history instead of rejecting (
290.37 scommand wall for the failing regression). The reader now suppresses missing SQLite for scoped history reads only when reset-archive fallback is explicitly enabled, preserving storage errors for public-share paging. Existing count and exact-ID missing-store behavior remains unchanged. The existing archive fixture now checks fallback false and omitted before its fallback-enabled reads; no additional test fixture or Gateway boot was introduced.After that fix, the complete reader-facade and public-share suites passed 22 tests in 713.18 s command wall (reader shard 94.13 s, public-share shard 193.72 s, cold worker preparation 368.38 s). The extended missing-store/archive case itself took 2.626 s. This uses real reader-worker admission and preserves the existing fixtures; most command time was cold preparation and transforms on the loaded host. Independent Codex review with the reader owner and public-share caller supplied as context was scoped-clean through P2.
CI run 36211910596 then exposed one invalid cached-context fixture: it supplied a session ID without creating SQLite and relied on the former main-thread reader opening an empty database. Replaying the same 16-file shard reproduced that one failure (220 passed, 1 failed). The test now uses its existing transcript fixture; its cached-token assertions and production behavior are unchanged. The same shard then passed 221 tests in 159.84 s Vitest duration (the repaired case took 866 ms), versus 522.04 s for the failing local replay and 22.62 s for the original CI shard. Targeted lint and an independent review through P2 passed. No unchanged CI rerun was requested.
The full changed-file check passed after the storage-error repair, including production types, all 25 core-test type graphs, formatting, lint, unused-export scans, and storage/auth guards. The subsequent fixture-only change also passed its messaging type graph and targeted lint.
CI run 36214029659 reported a session-store discovery change during the second history read in the nested Code Mode lifecycle fixture. Its initial 187-file single-worker replay passed 3,350 tests (22 existing skips) in 512.92 s, so the exact interfering registry writer was not reproduced or identified. Source inspection found that the fixture isolated only its transcript directory while sharing the process state registry and scheduling entry maintenance. It now uses the existing owned-state helper, a canonical store inside that state root, and the established maintenance-skipping seed pattern. All production discovery/authority checks and behavior assertions remain unchanged. The fixture's type graph, targeted lint, line-cap guard, and independent review through P2 passed.
Lifecycle-fixture verification completed 20 standalone file runs: 28 tests per run, 560 successful executions, zero failures (wrapper median 86.23 s, range 34.11–192.99 s on the loaded host). The first full 187-file replay at CI's eight-worker concurrency passed 3,350 tests with 22 existing skips in 198.66 s command wall. The second replay passed the lifecycle case but failed three unrelated real-time MCP polling/catalog-timeout tests in two unchanged files; both MCP files then passed all 153 tests in isolation. Those failures remain recorded and are not treated as fixed by the isolated pass. No production guard, assertion, baseline, timeout, or CI retry was weakened.
The third planned eight-worker shard replay passed all 3,350 tests (22 existing skips) in 82.54 s command wall. Thus the three replay outcomes were pass / unrelated MCP timing failures / pass; the failed middle replay is retained as a known local validation limitation, not replaced by additional attempts. Exact-head GitHub gates remain required for landing.
Final exact-head CI passed on
5bab1b983d41ebf62ef9f13c30e770e8c51c7a0c: run 36218607343. Aggregate test-job wall times were 439 s forchecks-node-changed-compact-large-9and 378 s forchecks-node-changed-compact-large-13, both successful.