Skip to content

Commit 451a317

Browse files
fix(core): stop forks from replaying superseded instructions
1 parent fe04656 commit 451a317

5 files changed

Lines changed: 112 additions & 14 deletions

File tree

‎packages/core/src/session.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ const layer = Layer.effect(
367367
)
368368
.pipe(Effect.orDie)
369369
// The fork adopts the parent's newest instruction values rather than the
370-
// values in effect at the boundary; copied history may contain frozen
371-
// instruction-update text the initial baseline already reflects.
370+
// values in effect at the boundary; their chronological updates are
371+
// superseded by that baseline and omitted from the model context.
372372
// The parent's location, not the caller's, decides whether the copy's media is stored as blobs.
373373
const media = yield* mediaBlob(parent)
374374
yield* bus

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,15 @@ const messageEntries = Effect.fnUntraced(function* (
208208
// epoch: the baseline supersedes the chronological updates before it. Forks seed their baseline
209209
// at sequence 0 but retain parent sequences, so the copied checkpoint still retires them.
210210
const native = entries.findLast((entry) => isNativeCompaction(entry.message))
211+
// Forked instruction updates remain addressable in storage, but the inherited baseline supersedes them.
211212
// Skipped native checkpoints are not textual summaries. Their original transcript remains available.
212213
return entries.filter(
213214
(entry) =>
214-
!(entry.message.type === "system" && native && entry.seq < native.seq) && replayable(entry.message, boundary),
215+
!(
216+
entry.message.type === "system" &&
217+
((native && entry.seq < native.seq) ||
218+
(entry.message.metadata?.notice === "instructions" && entry.message.metadata.instructionSuperseded === true))
219+
) && replayable(entry.message, boundary),
215220
)
216221
})
217222

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@ const projectFork = Effect.fn("SessionProjector.projectFork")(function* (
222222
time_updated: row.time_updated,
223223
data: media ? yield* SessionBlob.externalizeRow(db, row.data) : row.data,
224224
}
225+
// Keep message IDs available as fork/revert boundaries while the inherited baseline supersedes their updates.
226+
if (
227+
event.data.instructions !== undefined &&
228+
row.type === "system" &&
229+
row.data.metadata?.notice === "instructions"
230+
)
231+
return {
232+
...copy,
233+
data: { ...copy.data, metadata: { ...copy.data.metadata, instructionSuperseded: true } },
234+
}
225235
// A summary references the first message it kept, which the fork copied under a new ID.
226236
const keepID = row.type === "compaction" ? row.data.metadata?.keepID : undefined
227237
if (!Schema.is(SessionMessage.ID)(keepID)) return copy

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,81 @@ describe("SessionProjector", () => {
16831683
}),
16841684
)
16851685

1686+
it.effect("forks retain instruction notices as history but supersede them when they inherit a baseline", () =>
1687+
Effect.gen(function* () {
1688+
const db = yield* seedSession()
1689+
const bus = yield* Bus.Service
1690+
const store = yield* SessionStore.Service
1691+
const boundary = SessionMessage.ID.make("msg_fork_boundary")
1692+
const messages = [
1693+
...[
1694+
{ text: "Legacy tool compatibility notice" },
1695+
{ text: "Other system notice", metadata: { notice: "legacy-tools" } },
1696+
{ text: "Superseded instruction update", metadata: { notice: "instructions" } },
1697+
].map((message, index) =>
1698+
SessionMessage.System.make({
1699+
...message,
1700+
id: SessionMessage.ID.make(`msg_system_${index}`),
1701+
type: "system",
1702+
time: { created },
1703+
}),
1704+
),
1705+
SessionMessage.User.make({
1706+
id: boundary,
1707+
type: "user",
1708+
text: "Continue",
1709+
time: { created },
1710+
}),
1711+
]
1712+
yield* db
1713+
.insert(SessionMessageTable)
1714+
.values(
1715+
messages.map((message, seq) => {
1716+
const { id, type, ...data } = encodeMessage(message)
1717+
return { id: SessionMessage.ID.make(id), session_id: sessionID, type, seq, time_created: 0, data }
1718+
}),
1719+
)
1720+
.run()
1721+
.pipe(Effect.orDie)
1722+
1723+
for (const inherited of [false, true]) {
1724+
const childID = Session.ID.make(`ses_instruction_fork_${inherited}`)
1725+
yield* bus.publish(SessionEvent.Forked, {
1726+
sessionID: childID,
1727+
parentID: sessionID,
1728+
boundary: { type: "through", messageID: boundary },
1729+
...(inherited ? { instructions: {} } : {}),
1730+
})
1731+
const copied = yield* store.messages({ sessionID: childID, order: "asc" })
1732+
expect(copied).toMatchObject([
1733+
{ type: "system", text: "Legacy tool compatibility notice" },
1734+
{ type: "system", text: "Other system notice", metadata: { notice: "legacy-tools" } },
1735+
{ type: "system", text: "Superseded instruction update", metadata: { notice: "instructions" } },
1736+
{ type: "user", text: "Continue" },
1737+
])
1738+
expect(copied[2].metadata?.instructionSuperseded).toBe(inherited ? true : undefined)
1739+
expect(yield* store.context(childID)).toMatchObject([
1740+
{ type: "system", text: "Legacy tool compatibility notice" },
1741+
{ type: "system", text: "Other system notice", metadata: { notice: "legacy-tools" } },
1742+
...(inherited ? [] : [{ type: "system", text: "Superseded instruction update" }]),
1743+
{ type: "user", text: "Continue" },
1744+
])
1745+
const grandchildID = Session.ID.make(`ses_instruction_grandfork_${inherited}`)
1746+
yield* bus.publish(SessionEvent.Forked, {
1747+
sessionID: grandchildID,
1748+
parentID: childID,
1749+
boundary: { type: "before", messageID: copied[2].id },
1750+
instructions: {},
1751+
})
1752+
expect(yield* store.messages({ sessionID: grandchildID, order: "asc" })).toMatchObject([
1753+
{ type: "system", text: "Legacy tool compatibility notice" },
1754+
{ type: "system", text: "Other system notice" },
1755+
])
1756+
}
1757+
expect(withoutIDs(yield* store.messages({ sessionID, order: "asc" }))).toEqual(withoutIDs(messages))
1758+
}),
1759+
)
1760+
16861761
it.effect("forks a settled message whose parts are in session_part with its content", () =>
16871762
Effect.gen(function* () {
16881763
const db = yield* seedSession()

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,9 +1769,7 @@ describe("SessionRunnerLLM", () => {
17691769
yield* s.session.resume(forked.id)
17701770
expect(s.requests.at(-1)?.messages[0]).toEqual(replacement[0])
17711771
expect(s.requests.at(-1)?.system.map((part) => part.text)).toContain("Newest instructions")
1772-
expect(s.requests.at(-1)?.messages.filter((message) => message.role === "system")).toEqual([
1773-
Message.system("Newest instructions"),
1774-
])
1772+
expect(systemTexts(s.requests.at(-1)!)).toEqual([])
17751773
expect(
17761774
(yield* s.session.messages({ sessionID: forked.id })).find((message) => message.type === "compaction"),
17771775
).toMatchObject({ native })
@@ -1781,9 +1779,7 @@ describe("SessionRunnerLLM", () => {
17811779
yield* s.session.prompt({ sessionID: forked.id, text: "Switched fork", resume: false })
17821780
yield* s.session.resume(forked.id)
17831781
expect(s.requests.at(-1)?.messages[0]?.content).toEqual([Message.text("Original request")])
1784-
expect(s.requests.at(-1)?.messages.filter((message) => message.role === "system")).toEqual([
1785-
Message.system("Newest instructions"),
1786-
])
1782+
expect(systemTexts(s.requests.at(-1)!)).toEqual([])
17871783
s.currentModel = original
17881784

17891785
yield* s.bus.publish(SessionEvent.RevertEvent.Committed, { sessionID, to: checkpoint.id })
@@ -1819,9 +1815,13 @@ describe("SessionRunnerLLM", () => {
18191815
yield* s.session.resume(forked.id)
18201816

18211817
expect(s.requests.at(-1)?.system.map((part) => part.text)).toEqual([defaultSystem, fakeIdentity, "Latest context"])
1822-
// Copied history keeps the frozen chronological update; no new update is emitted.
1823-
expect(systemTexts(s.requests.at(-1)!)).toContain("Changed context")
1824-
expect(systemTexts(s.requests.at(-1)!)).not.toContain("Latest context")
1818+
expect(systemTexts(s.requests.at(-1)!)).toEqual([])
1819+
1820+
s.systemBaseline = "Fork context"
1821+
yield* s.session.prompt({ sessionID: forked.id, text: "Continue fork", resume: false })
1822+
yield* s.session.resume(forked.id)
1823+
expect(s.requests.at(-1)?.system.map((part) => part.text)).toContain("Latest context")
1824+
expect(systemTexts(s.requests.at(-1)!)).toEqual(["Fork context"])
18251825

18261826
const recorded = yield* s.db
18271827
.select()
@@ -1845,7 +1845,11 @@ describe("SessionRunnerLLM", () => {
18451845
)
18461846
expect(
18471847
yield* s.db.select().from(InstructionStateTable).where(eq(InstructionStateTable.session_id, forked.id)).get(),
1848-
).toMatchObject({ current_values: { "test/context": Instructions.hash("Latest context") } })
1848+
).toMatchObject({ current_values: { "test/context": Instructions.hash("Fork context") } })
1849+
yield* s.session.prompt({ sessionID: forked.id, text: "Replayed fork", resume: false })
1850+
yield* s.session.resume(forked.id)
1851+
expect(s.requests.at(-1)?.system.map((part) => part.text)).toContain("Latest context")
1852+
expect(systemTexts(s.requests.at(-1)!)).toEqual(["Fork context"])
18491853
})
18501854

18511855
scenario("keeps nested forks self-contained", function* (s) {
@@ -2380,7 +2384,11 @@ describe("SessionRunnerLLM", () => {
23802384
const fork = yield* s.session.fork({ sessionID, before: latest.id })
23812385
expect(
23822386
(yield* s.session.context(fork.id)).flatMap((message) => (message.type === "system" ? [message.text] : [])),
2383-
).toEqual(["Replacement context"])
2387+
).toEqual([])
2388+
yield* s.session.prompt({ sessionID: fork.id, text: "Continue fork", resume: false })
2389+
yield* s.session.resume(fork.id)
2390+
expect(s.requests.at(-1)?.system.map((part) => part.text)).toContain("Replacement context")
2391+
expect(systemTexts(s.requests.at(-1)!)).toEqual([])
23842392
})
23852393

23862394
for (const order of ["before", "between", "after"] as const) {

0 commit comments

Comments
 (0)