Repository navigation
Description
Context
Part of #4278.
When a turn's journal finishes, one statement drops the turn's object references that no change row needs any more (finish, packages/ui-server/src/brain/turn-journal.ts:710; the statement at :725):
DELETE FROM turn_journal_object_refs WHERE turn_id=? AND NOT EXISTS (
SELECT 1 FROM turn_journal_changes c WHERE c.turn_id=? AND (
json_extract(c.before_json,'$.hash')=turn_journal_object_refs.hash OR
json_extract(c.after_json,'$.hash')=turn_journal_object_refs.hash))
AND NOT EXISTS (SELECT 1 FROM turn_journal_claude_memory m WHERE ...)For each reference it scans every change row of the turn and extracts two JSON fields from each. The cost is references times change rows.
On a copy of a recorded turn with 28,769 change rows and 27,889 references, the predicate took 139 to 150 s. A set-based form that returns the same rows took 35 to 39 ms (two independent local runs).
finish runs under the journal's hold (held, turn-journal.ts:293), which stops the workers of every active turn. So one large turn ending would stop every other session for minutes. This is a local timing of one historical input, not a recorded freeze; for an ordinary turn the difference is under a millisecond.
Scope
Replace the statement with a set-based one in the same transaction: compute the retained hashes once from the before and after versions of the turn's change rows and of its unsaved Claude-memory rows, exclude SQL NULL before the membership test, and delete only this turn's references that are absent from that set. A reviewed sketch:
DELETE FROM turn_journal_object_refs
WHERE turn_id = ?1
AND hash NOT IN (
SELECT h FROM (
SELECT json_extract(before_json, '$.hash') AS h FROM turn_journal_changes WHERE turn_id = ?1
UNION ALL
SELECT json_extract(after_json, '$.hash') FROM turn_journal_changes WHERE turn_id = ?1
UNION ALL
SELECT json_extract(before_json, '$.hash') FROM turn_journal_claude_memory WHERE turn_id = ?1 AND state != 'saved'
UNION ALL
SELECT json_extract(after_json, '$.hash') FROM turn_journal_claude_memory WHERE turn_id = ?1 AND state != 'saved'
)
WHERE h IS NOT NULL
)The NULL exclusion matters: NOT IN over a set that contains NULL deletes nothing.
Out of scope
- A schema change, a new index or a migration.
- Pin semantics, other turns' references, grace windows, crash and recovery ordering.
- Objects in the store that have no row at all: ui-server: journal store files that have no database row are never collected #4290.
- The other work an observation does under the hold: ui-server: an observation of twenty changed notes keeps every worker stopped for about four seconds #4294.
Acceptance criteria
- A test builds a turn with change rows and references covering: a hash referenced only by a before version, only by an after version, by an unsaved Claude-memory row, by a saved one, by no row, and a change row whose version has no hash. After
finish, the surviving references are exactly those the current statement leaves. - A mutation that drops the
NULLexclusion fails that test. - A test with 5,000 change rows and 5,000 references finishes its clean-up statement within a bound that the current statement misses by an order of magnitude on the same machine, asserted as a ratio between the two forms and not as an absolute time.
- The statement stays inside the existing finish transaction, shown by the existing crash-recovery test for finish.
Verification
bun run test packages/ui-server/tests
bun run typecheck
bun run lintContract impact
- This touches the integration contract.
- This changes a published package's behaviour, so it needs a changeset.
Activity
schlessera commentedon Nov 20, 2026
Claimed for implementation. Claim token:
claim-d3df916e. Branch:fix/4287-maintenance-storage.Worked as part of the #4278 epic integration branch epic/4278-host-overhead (one main landing for the epic). Collisions: none recorded at claim.
schlessera commentedon Nov 23, 2026
Closed by #4444, squash-merged to main as
a3395c4b2. The combined review found every non-native criterion met. Native-measurement criteria for this issue moved to #4420 by maintainer decision, because native proof cannot run on the integration host.