Skip to content

ui-server: the turn-end reference clean-up rescans every change row for each object reference #4289

Closed

Description

@schlessera

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

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 NULL exclusion 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 lint

Contract impact

  • This touches the integration contract.
  • This changes a published package's behaviour, so it needs a changeset.

Activity

  1. added
    type: fix
    area: ui-server
    agent-ready
    on Nov 20, 2026
  2. added
    priority: p2
    on Nov 20, 2026
  3. self-assigned this
    on Nov 20, 2026
  4. schlessera commented on Nov 20, 2026

    @schlessera
    OwnerAuthor

    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.

  5. added this to the 0.45.0 milestone on Nov 21, 2026
  6. schlessera commented on Nov 23, 2026

    @schlessera
    OwnerAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

agent-readyarea: ui-serverpriority: p2type: fix

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions