Skip to content

Commit b645291

Browse files
authored
fix(core): hide instruction updates a revert left behind (#63667)
1 parent e36f0c8 commit b645291

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎packages/core/src/session/history.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,15 @@ const messageEntries = Effect.fnUntraced(function* (
226226
// epoch: the baseline supersedes the chronological updates before it. Forks seed their baseline
227227
// at sequence 0 but retain parent sequences, so the copied checkpoint still retires them.
228228
const native = entries.findLast((entry) => isNativeCompaction(entry.message))
229+
// The instruction baseline covers every update before its epoch. A committed revert that removes an
230+
// update clears the state, so the next baseline is rendered fresh and covers every surviving update.
231+
const epoch = (yield* InstructionState.epochStart(db, sessionID)) ?? Number.POSITIVE_INFINITY
229232
// Skipped native checkpoints are not textual summaries. Their original transcript remains available.
230233
return entries.filter(
231234
(entry) =>
232-
!(entry.message.type === "system" && native && entry.seq < native.seq) && replayable(entry.message, boundary),
235+
!(entry.message.type === "system" && native && entry.seq < native.seq) &&
236+
!(entry.message.type === "system" && entry.message.metadata?.notice === "instructions" && entry.seq < epoch) &&
237+
replayable(entry.message, boundary),
233238
)
234239
})
235240

‎packages/core/src/session/instruction-state.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ export const current = Effect.fn("InstructionState.current")(function* (
176176
return (yield* find(db, sessionID))?.current_values
177177
})
178178

179+
/** Where the current baseline starts; updates before it are already folded into the baseline. */
180+
export const epochStart = Effect.fn("InstructionState.epochStart")(function* (
181+
db: DatabaseService,
182+
sessionID: SessionSchema.ID,
183+
) {
184+
return (yield* find(db, sessionID))?.epoch_start
185+
})
186+
179187
export const preview = Effect.fn("InstructionState.preview")(function* (
180188
db: DatabaseService,
181189
sessionID: SessionSchema.ID,

‎packages/core/test/session-runner.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,32 @@ describe("SessionRunnerLLM", () => {
20592059
yield* s.runPrompt("Fourth")
20602060

20612061
expect(s.requests.at(-1)?.system.map((part) => part.text)).toEqual([defaultSystem, fakeIdentity, "Latest context"])
2062+
// The surviving "Changed context" update predates the fresh baseline and would contradict it.
2063+
expect(messageRoles(s.requests.at(-1))).toEqual(["user", "user"])
2064+
expect((yield* s.context).some((message) => message.type === "system")).toBe(false)
2065+
})
2066+
2067+
scenario("hides an update a revert left behind once the source is removed", function* (s) {
2068+
s.systemRemoved = true
2069+
yield* s.runPrompt("First")
2070+
s.systemRemoved = false
2071+
s.systemBaseline = "Entered mode"
2072+
const second = yield* s.runPrompt("Second")
2073+
s.systemRemoved = true
2074+
yield* s.runPrompt("Third")
2075+
2076+
// The removal is delivered with "Third"; the "Entered mode" update survives before "Second".
2077+
yield* s.bus.publish(SessionEvent.RevertEvent.Committed, { sessionID, to: second.id })
2078+
yield* s.runPrompt("Fourth")
2079+
2080+
const last = s.requests.at(-1)!
2081+
expect(systemTexts(last)).toEqual([])
2082+
expect(userTexts(last)).toEqual(["First", "Fourth"])
2083+
expect(last.system.map((part) => part.text).join("\n")).not.toContain("Entered mode")
2084+
// Clients still see every message; only the model context hides the stale update.
2085+
expect((yield* s.messages).filter((message) => message.type === "system")).toMatchObject([
2086+
{ text: "Entered mode" },
2087+
])
20622088
})
20632089

20642090
scenario("uses the selected model family prompt when the agent does not override it", function* (s) {

0 commit comments

Comments
 (0)