From b645291e9cb091e7d51a9caa489826b1bea34741 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Fri, 13 Nov 2026 00:07:59 -0600 Subject: [PATCH] fix(core): hide instruction updates a revert left behind (#63667) --- packages/core/src/session/history.ts | 7 ++++- .../core/src/session/instruction-state.ts | 8 ++++++ packages/core/test/session-runner.test.ts | 26 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/core/src/session/history.ts b/packages/core/src/session/history.ts index b25e5365085..8fa523245d6 100644 --- a/packages/core/src/session/history.ts +++ b/packages/core/src/session/history.ts @@ -226,10 +226,15 @@ const messageEntries = Effect.fnUntraced(function* ( // epoch: the baseline supersedes the chronological updates before it. Forks seed their baseline // at sequence 0 but retain parent sequences, so the copied checkpoint still retires them. const native = entries.findLast((entry) => isNativeCompaction(entry.message)) + // The instruction baseline covers every update before its epoch. A committed revert that removes an + // update clears the state, so the next baseline is rendered fresh and covers every surviving update. + const epoch = (yield* InstructionState.epochStart(db, sessionID)) ?? Number.POSITIVE_INFINITY // Skipped native checkpoints are not textual summaries. Their original transcript remains available. return entries.filter( (entry) => - !(entry.message.type === "system" && native && entry.seq < native.seq) && replayable(entry.message, boundary), + !(entry.message.type === "system" && native && entry.seq < native.seq) && + !(entry.message.type === "system" && entry.message.metadata?.notice === "instructions" && entry.seq < epoch) && + replayable(entry.message, boundary), ) }) diff --git a/packages/core/src/session/instruction-state.ts b/packages/core/src/session/instruction-state.ts index 7aa31118b76..9891f489bb7 100644 --- a/packages/core/src/session/instruction-state.ts +++ b/packages/core/src/session/instruction-state.ts @@ -176,6 +176,14 @@ export const current = Effect.fn("InstructionState.current")(function* ( return (yield* find(db, sessionID))?.current_values }) +/** Where the current baseline starts; updates before it are already folded into the baseline. */ +export const epochStart = Effect.fn("InstructionState.epochStart")(function* ( + db: DatabaseService, + sessionID: SessionSchema.ID, +) { + return (yield* find(db, sessionID))?.epoch_start +}) + export const preview = Effect.fn("InstructionState.preview")(function* ( db: DatabaseService, sessionID: SessionSchema.ID, diff --git a/packages/core/test/session-runner.test.ts b/packages/core/test/session-runner.test.ts index baf281f9a5d..be3f89a8ff3 100644 --- a/packages/core/test/session-runner.test.ts +++ b/packages/core/test/session-runner.test.ts @@ -2059,6 +2059,32 @@ describe("SessionRunnerLLM", () => { yield* s.runPrompt("Fourth") expect(s.requests.at(-1)?.system.map((part) => part.text)).toEqual([defaultSystem, fakeIdentity, "Latest context"]) + // The surviving "Changed context" update predates the fresh baseline and would contradict it. + expect(messageRoles(s.requests.at(-1))).toEqual(["user", "user"]) + expect((yield* s.context).some((message) => message.type === "system")).toBe(false) + }) + + scenario("hides an update a revert left behind once the source is removed", function* (s) { + s.systemRemoved = true + yield* s.runPrompt("First") + s.systemRemoved = false + s.systemBaseline = "Entered mode" + const second = yield* s.runPrompt("Second") + s.systemRemoved = true + yield* s.runPrompt("Third") + + // The removal is delivered with "Third"; the "Entered mode" update survives before "Second". + yield* s.bus.publish(SessionEvent.RevertEvent.Committed, { sessionID, to: second.id }) + yield* s.runPrompt("Fourth") + + const last = s.requests.at(-1)! + expect(systemTexts(last)).toEqual([]) + expect(userTexts(last)).toEqual(["First", "Fourth"]) + expect(last.system.map((part) => part.text).join("\n")).not.toContain("Entered mode") + // Clients still see every message; only the model context hides the stale update. + expect((yield* s.messages).filter((message) => message.type === "system")).toMatchObject([ + { text: "Entered mode" }, + ]) }) scenario("uses the selected model family prompt when the agent does not override it", function* (s) {