fix: restore message snapshot when tagging fails mid-pass

Closed
TR
5mo ago
+168-0

Problem

tagMessages() mutates message parts in place (e.g. §N§ text prefixing) after each DB write. If it throws mid-pass (DB error, etc.), the emitted messages array keeps the partial mutations — the LLM silently receives a prompt with partial tag prefixes that don't correspond to persisted state.

Fix

Snapshot { info, parts } (structuredClone) immediately before tagMessages(); on the tagging-failure catch path, restore via messages.splice(0, messages.length, ...snapshot). The existing taggingSucceeded gate and the documented no-outer-transaction design of tagMessages() are untouched — this only rolls back in-memory mutations, not durable DB semantics.

Tests

  • mid-pass failure: tagger throws on the 2nd message after the 1st was already prefixed → asserts no part contains a §N§ prefix and both messages match their pre-tag text
  • happy path: successful pass still persists source_contents and replays dropped statuses (snapshot/restore is a no-op on success)

View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.

Greptile Summary

This PR guards against partial in-memory corruption when tagMessages() throws mid-walk by taking a structuredClone snapshot of {info, parts} immediately before the call and splicing the snapshot back on the catch path. The existing taggingSucceeded gate and no-outer-transaction design of tagMessages are left intact.

  • snapshotMessagesForTagging clones every message's info and parts before tagging begins; on failure the messages array contents are restored so no stray §N§ prefixes reach the LLM.
  • Two new integration tests cover the rollback path (tagger forced to throw on the 2nd assignTag, asserts all text reverts to pre-tag content) and the happy path (source_contents and dropped-status replay still work correctly after the snapshot code was added).

Confidence Score: 4/5

Safe to merge; the fix correctly restores the message array on the failure path and is well-tested, with two minor robustness items worth a follow-up.

The snapshot/restore logic is sound and the taggingSucceeded gate prevents downstream stages from running on stale data. Two items are worth noting: structuredClone is called outside the try-catch so a DataCloneError from a non-cloneable part would bubble up as an unhandled transform failure, and the ...preTagSnapshot spread into splice has a theoretical argument-count ceiling. Neither is likely to trigger in practice given JSON-like parts and bounded message windows.

packages/plugin/src/hooks/magic-context/transform.ts — specifically the placement of snapshotMessagesForTagging relative to the try-catch and the splice restore pattern.

Important Files Changed

FilenameOverview
packages/plugin/src/hooks/magic-context/transform.tsAdds snapshotMessagesForTagging helper and a messages.splice restore on the tagging catch path; snapshot is taken outside the try-catch (structuredClone failure would be unhandled) and the spread-into-splice pattern has a theoretical argument-count ceiling.
packages/plugin/src/hooks/magic-context/transform.test.tsAdds two new integration tests: mid-pass rollback (tagger throws on 2nd assignTag, asserts no §N§ prefixes survive) and happy-path regression (source_contents persisted, dropped status replayed correctly after snapshot code was added).

Sequence Diagram

sequenceDiagram
    participant T as transform()
    participant S as snapshotMessagesForTagging()
    participant TM as tagMessages()
    participant DB as ContextDatabase

    T->>S: "structuredClone({info, parts}) for each message"
    S-->>T: preTagSnapshot[]

    T->>TM: tagMessages(sessionId, messages, tagger, db)
    loop per message part
        TM->>DB: tagger.assignTag() SAVEPOINT
        DB-->>TM: tag number
        TM->>TM: prependTag mutates part.text in place
    end

    alt tagging succeeds
        TM-->>T: TagMessagesResult
        T->>T: "taggingSucceeded = true"
        Note over T: downstream stages run normally
    else tagMessages throws mid-walk
        TM--xT: Error
        Note over T: messages[0] already has §1§ prefix
        T->>T: restore via splice
        Note over T: in-memory array restored to pre-tag state
        T->>T: tagger.cleanup(sessionId)
        Note over T: taggingSucceeded stays false
    end

Reviews (1): Last reviewed commit: "plugin: restore message snapshot when ta..." | Re-trigger Greptile

Greptile also left 2 inline comments on this PR.


Summary by cubic

Restores the pre-tag message snapshot when tagMessages() fails mid-pass, preventing partial §N§ prefixes from leaking into prompts. Keeps LLM input consistent with persisted state.

  • Bug Fixes
    • Take a deep snapshot of { info, parts } before tagging and splice it back on failure.
    • Keep taggingSucceeded gating and DB semantics unchanged; only in-memory mutations are rolled back.
    • Add integration tests for mid-pass rollback and the happy path (persisting source_contents and replaying dropped status).