Skip to content

Small auto-search decisions rewrite large session_meta rows #1091

Backlog

Description

@iceteaSA

Short description
Small auto-search decisions rewrite large session_meta rows

What problem does this solve?
Auto-search saves a decision for a new prompt with or without a hint. The runner saves the decision synchronously on the plugin thread. The UPDATE changes only the decision column. When its length changes, SQLite can write the full row to the write-ahead log (WAL). This writes pages with unrelated data.

The largest rows in one database held most of their data in merged_reasoning_stripped_ids. The append path does not limit or prune these replay entries during a session. Of 13,255 metadata rows, 35 held 256 KiB or more of column values and five held 1 MiB or more. In a replay on wide rows, 120 hint decisions wrote 190 MB to the WAL. Linux throttled some writes to limit dirty pages in memory. This can stall prompt processing without a SQLite lock wait.

Reproduce on v0.49.119 (4d4bded91):

  1. Make a database with the plugin schema. Seed one session_meta row with 32,000 synthetic assistant IDs and 8,000 frozen part decisions in merged_reasoning_stripped_ids.
  2. Disable WAL auto-checkpoints and truncate the WAL. Make one call to appendAutoSearchHintDecision with a new no-hint decision. Read back the decision and measure the WAL file size increase.
  3. Run the test again with a new database and an empty stripped-ID set. Use the same decision and a limit of 64 KiB for the two cases.

With Bun 1.4.5, SQLite 3.54.1, and schema 95, the wide row added 2,459,672 bytes of WAL, above the limit. The control added 4,152 bytes, below the limit. The test checks bytes, not time.

Proposed solution
Store hint decisions in their own small table or row. This lets the plugin write a new decision without a rewrite of the larger row. A different option is to limit or compact merged_reasoning_stripped_ids. Keep the decisions necessary to replay the same prompt. Move fields that change size frequently out of wide metadata rows. Keep the stored hint and no-hint results stable across restarts.

Area
OpenCode plugin

Additional context
Related: #864 (WAL checkpoint cost inside main-thread commits). This report is about the size of each write, not the checkpoint.

I can send a PR with the WAL regression test and replay checks when the issue has the design-approved label.

AI helped write this draft.

Activity

  1. magic-alfonso commented on Dec 6, 2026

    @magic-alfonso
    Contributor

    Confirmed from the code, both parts.

    appendAutoSearchHintDecision rewrites the whole auto_search_hint_decisions array in session_meta for every new prompt, on the plugin thread, and the array is never pruned. addMergedReasoningStrippedIds appends to merged_reasoning_stripped_ids in the same row without a bound (the placeholder ids next to it are capped; these are not). So the row grows for the life of the session, and every size-changing write to it puts the whole record in the WAL. Migration 94 fixed the same thing for the last-good prefix and the trailing-blank decisions; these two columns were left behind.

    Thank you for the byte-based reproduction. Measuring WAL growth instead of time is the right test for this, and we will keep it.

    Two things before a design answer:

    1. No new table on this release line. The 0.49 store stays at its current schema version, so "its own small table" is out. What exists already is session_replay_decisions (one small row per message, from migration 94) and pruning. Which of the two fits depends on point 2.
    2. We are measuring the rest of the row first. A hint decision is one of several size-changing writes to session_meta, and the row also carries other large values. Before choosing, we want the WAL bytes per ordinary pass on a typical row and on a wide one, by column, and for each of the two columns the answer to "which entries can never be read again" on each host (entries for messages behind the history cut look removable, but a revert across a checkpoint has to be checked).

    We run that now and come back here with the design and the conditions for a PR. Please hold the PR until then; your WAL regression test is wanted either way.

  2. magic-alfonso commented on Dec 6, 2026

    @magic-alfonso
    Contributor

    Correcting my earlier reply: I wrote that the hint-decision array is never pruned. That is wrong. pruneAutoSearchHintDecisions and the queue beside it (storage-meta-persisted.ts, about lines 1884-1980) already remove decisions for messages that a history summary removed or the host deleted, on a pass that may rebuild, and drop the oldest hidden no-hint entries above 2,000. What has no such rule is merged_reasoning_stripped_ids, and that is where your wide rows come from.

    We measured the row (script and numbers are ours to keep with the fix; one session row at three widths, auto-checkpoint off, WAL truncated before each write):

    Write fresh row 150 KB history head 150 KB head + 32,000 ids
    append one hint decision (hint or no-hint) 4,152 B 181,312 B 2,080,632 B
    append one merged-reasoning id 4,152 B 181,312 B 2,080,632 B
    transform error text, note anchor, tool watermark 0 → 110, system hash 4,152 B 181,312 B 2,080,632 B
    response clock (same encoded width), historian flag 0 → 1 4,152 B 4,152 B 4,152 B
    a trailing-blank decision (its own row since migration 94) 4,152 B 4,152 B 4,152 B
    whole pass for a new prompt 74,192 B 251,352 B 2,150,672 B
    settled repeat pass 0 0 0

    So the hint decision is one of about thirty writes that put the whole record in the WAL when a length changes, an integer crossing an encoded width included. Moving only the hint decisions out would leave the other twenty-nine. What makes a row pathological is its width, and the unbounded part of the width is the merged-reasoning list.

    What we will not do on this line

    • No new table and no new column: the 0.49 store stays at schema 95.
    • No second kind of row in session_replay_decisions. We tried it against the real readers: the strict full reader throws on a row it cannot parse, and the clone path calls it before filtering by message id; writing a clone's replay document deletes every row of the session; a row of any kind counts as "previously managed" evidence. Released versions read that table the same way, so rows of another kind are not safe next to them.
    • No move of hint decisions out of their column. An older plugin version on the same store would see an empty column, drop the saved hint text from answered prompts and change bytes in front of kept reasoning.

    The design: bound the merged-reasoning list by the same rule the hint decisions already have

    1. When a history summary removes messages from the request, the message-scoped entries of merged_reasoning_stripped_ids for those messages are queued for removal, at the same point and from the same set of removed ids as the hint retirement (queueAutoSearchHintDecisionRetirement), and flushed on a pass that has rebuild permission, keeping any id that is visible again at the flush. One retirement path for both lists, not a second mechanism.
    2. Message-scoped means exactly two forms: a bare assistant message id, and a __merged_reasoning_parts_v1__ entry for that assistant. Every other string in that column stays untouched: binding_mismatch:, compaction_off_thinking:, the @tool-sweep markers, the signed-fit refusal marker, pi-content-replay-v1: entries. A test lists every prefix the column can hold and fails on an unknown one, so a new kind cannot be pruned by accident.
    3. Never the complement of today's visible ids. An id that is absent from this request without proof (another Pi branch, a staged revert) keeps its entry. The proof is the one the hint rule uses: removed under a summary boundary, or deleted by the host.
    4. A pass that may not rebuild writes nothing for this. Requests stay byte-identical on every pass for sessions where nothing was removed, and after a removal the request is the same as today's (the removed entries belong to messages that are not sent). Show both with recorded requests, not by reasoning.
    5. The case that decides whether this is safe, per host: messages behind the boundary come back (OpenCode 1 marker removed, OpenCode 2 revert across a checkpoint as in issue 1047, a Pi branch without the checkpoint). With their entries gone, the pass that sends them again must be a full rebuild that, on the newer Claude models whose reasoning is bound to the bytes before it, sends no kept reasoning behind them, and on other models runs reasoning clearing again. One test per host that proves this with a recorded request. If it cannot be shown on a host, that host does not prune.
    6. Existing wide rows shrink at their first rebuild after the update without a migration. Say per host how: where the host hands over the whole history every pass, the removed set already holds every summarized message; on OpenCode 1, where the host no longer loads messages behind the marker, state the rule you use and why it is not "the complement".
    7. Your WAL test, in bytes: the wide fixture, one rebuild, then one hint decision; the WAL growth is under 256 KiB. And the control you already have.
    8. The write that prunes is itself one whole-row write per rebuild. No other write is added to a pass.
    9. Clone and fork inheritance copy the same entries as today; a test.
    10. Pi and Oh My Pi: say whether your PR covers them. If not, we mirror it, as with issue 1083.

    What this does not fix, and the release notes will say so: every length-changing write still logs the record including the cached history head (about 180 KB on a typical session, 250 KB for a pass with a new prompt). Taking the head out of the record needs a schema change, which this line does not get.

    design-approved for this shape. The usual rules apply: reference this issue without a closing keyword, and the PR goes through an independent review, because the replay state for reasoning is the part of the plugin where a missing entry can cost a rejected request.

  3. added
    design-approved
    on Dec 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    design-approved

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions