Skip to content

Commit c33f8cb

Browse files
committed
fix(plugin): self-heal legacy synthetic-todo rows with empty stateJson
Oracle final audit caught the only remaining HIGH-severity gap in the synthetic-todowrite feature: a legacy upgrade scenario where a row was written with (callId, messageId) only, then v11 migration's column DEFAULT '' added stateJson=''. On a cache-busting pass with matching callId, Branch 3 idempotently re-injected but didn't backfill the empty stateJson. The next defer pass would skip replay (line 770 guard fails on length===0), the synthetic would vanish from T1, and prefix bytes would diverge from T0 — exactly the cache-stability regression Council Finding #1's original fix was meant to prevent. Fix: in Branch 3 of B7 (transform-postprocess-phase.ts), backfill todo_synthetic_state_json with sessionMeta.lastTodoState whenever the persisted row has empty stateJson. Safe because callId equality under sha256 collision-resistance proves current snapshot equals what the old build hashed. Adds regression test seeding (callId, msg, stateJson='') → cache-bust T0 → assert anchor backfilled → defer T1 from fresh messages → assert T1 bytes equal T0 bytes. Without the fix this fails at the final expect (T1 omits the synthetic part).
1 parent 8b4dec8 commit c33f8cb

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

‎packages/plugin/src/hooks/magic-context/transform-postprocess-phase.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,27 @@ export async function runPostTransformPhase(args: RunPostTransformPhaseArgs): Pr
746746
// Snapshot unchanged AND persisted anchor message still
747747
// present — idempotent re-inject leaves DB and messages
748748
// byte-identical.
749+
//
750+
// Council Finding #1 v2 (Oracle final audit): if a legacy
751+
// row was upgraded with `stateJson=""` (default after v11
752+
// migration ran on a session that already had `callId` and
753+
// `messageId` from the pre-stateJson build), backfill the
754+
// snapshot now so subsequent defer passes have something
755+
// to replay. Without this, defer at line 770 skips on
756+
// `stateJson.length === 0` and the synthetic vanishes
757+
// from T1 — exactly the regression Finding #1 was meant
758+
// to prevent. callId equality (line 743) under sha256
759+
// collision-resistance guarantees current snapshot ===
760+
// what the old build hashed, so backfill is safe.
761+
if (persistedAnchor.stateJson.length === 0) {
762+
setPersistedTodoSyntheticAnchor(
763+
args.db,
764+
args.sessionId,
765+
persistedAnchor.callId,
766+
persistedAnchor.messageId,
767+
args.sessionMeta.lastTodoState,
768+
);
769+
}
749770
} else {
750771
const anchoredMessageId = injectToolPartIntoLatestAssistant(args.messages, part);
751772
if (anchoredMessageId) {

‎packages/plugin/src/hooks/magic-context/transform-todo-state.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ function runTodoSynthesis(args: {
107107
persistedAnchor.callId === part.callID &&
108108
injectToolPartIntoAssistantById(messages, persistedAnchor.messageId, part)
109109
) {
110+
// Mirror production: backfill stateJson if it's empty (legacy upgrade row).
111+
if (persistedAnchor.stateJson.length === 0) {
112+
setPersistedTodoSyntheticAnchor(
113+
db,
114+
sessionId,
115+
persistedAnchor.callId,
116+
persistedAnchor.messageId,
117+
sessionMeta.lastTodoState,
118+
);
119+
}
110120
return;
111121
}
112122
const anchoredMessageId = injectToolPartIntoLatestAssistant(messages, part);
@@ -544,6 +554,63 @@ describe("todo state synthesis — defer branches and byte stability", () => {
544554
// T1 must equal T0 byte-for-byte even though last_todo_state changed.
545555
expect(t1Bytes).toBe(t0Bytes);
546556
});
557+
558+
it("CACHE STABILITY: legacy row with empty stateJson self-heals on cache-bust + replays on next defer (Oracle final audit)", () => {
559+
// The exact upgrade scenario Oracle's final audit flagged:
560+
// a user on the original v0.17 build had `(callId, messageId, stateJson='')`
561+
// persisted because that build only stored callId+messageId. The v11
562+
// migration's DEFAULT '' added the column, but existing rows have
563+
// stateJson=''. Without self-heal, the next defer pass would skip
564+
// replay (line 770 gate fails on length===0) and the synthetic would
565+
// vanish from T1 — exactly the regression we're guarding against.
566+
useTempDataHome("todo-legacy-stateJson-");
567+
const db = openDatabase();
568+
updateSessionMeta(db, "ses-1", { lastTodoState: ACTIVE_TODOS_JSON });
569+
// Seed a legacy row: callId matches current snapshot, but stateJson is empty.
570+
const callId = computeSyntheticCallId(ACTIVE_TODOS_JSON);
571+
setPersistedTodoSyntheticAnchor(db, "ses-1", callId, "msg-asst-2", "");
572+
573+
// Pre-seed a synthetic part on the anchor message (simulates state
574+
// already injected on a previous pass before this build was loaded).
575+
const part = buildSyntheticTodoPart(ACTIVE_TODOS_JSON);
576+
if (!part) throw new Error("part null");
577+
const asst2 = buildMessages().find((m) => m.info.id === "msg-asst-2");
578+
if (!asst2) throw new Error("asst-2 missing");
579+
580+
// T0: cache-bust pass on a legacy row.
581+
const t0Messages = buildMessages();
582+
const t0Asst2 = t0Messages.find((m) => m.info.id === "msg-asst-2");
583+
if (!t0Asst2) throw new Error("t0 asst-2 missing");
584+
t0Asst2.parts.push(part);
585+
runTodoSynthesis({
586+
db,
587+
sessionId: "ses-1",
588+
messages: t0Messages,
589+
isCacheBustingPass: true,
590+
fullFeatureMode: true,
591+
});
592+
const t0Bytes = JSON.stringify(t0Messages);
593+
594+
// Backfill must have happened — anchor row now has stateJson.
595+
const after = getPersistedTodoSyntheticAnchor(db, "ses-1");
596+
expect(after?.callId).toBe(callId);
597+
expect(after?.messageId).toBe("msg-asst-2");
598+
expect(after?.stateJson).toBe(ACTIVE_TODOS_JSON);
599+
600+
// T1: defer pass from fresh messages (OpenCode rebuild). Without the
601+
// backfill, this would skip injection and produce different bytes.
602+
const t1Messages = buildMessages();
603+
runTodoSynthesis({
604+
db,
605+
sessionId: "ses-1",
606+
messages: t1Messages,
607+
isCacheBustingPass: false,
608+
fullFeatureMode: true,
609+
});
610+
const t1Bytes = JSON.stringify(t1Messages);
611+
612+
expect(t1Bytes).toBe(t0Bytes);
613+
});
547614
});
548615

549616
describe("todo state synthesis — feature gates", () => {

0 commit comments

Comments
 (0)