Skip to content

Commit acb4c71

Browse files
fix(workspace): record a delivered announcement against the session incarnation, not the snapshot
The identity guard checked the snapshot object, but a multi-step turn refreshes per step and each refresh replaces the snapshot while the same line is still being said — so a delivery landing after the second refresh was never recorded and the next turn repeated the line. A per-session incarnation token, minted when a session is first remembered or recreated after eviction and dropped with it, keeps both properties: ordinary refreshes still record, a completion for a session evicted and recreated mid-flight does not.
1 parent 915020f commit acb4c71

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎packages/opencode/src/altimate/workspace/precedence.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ const EMPTY = (reason: Precedence["disabledReason"], workspaceName = ""): Preced
153153
/** Per-session precedence, refreshed once per turn by the tool resolver and read
154154
* (never recomputed) by tool bodies mid-turn. */
155155
const bySession = new Map<string, Precedence>()
156+
/** One token per session incarnation: set when a session is first remembered or
157+
* recreated after eviction, dropped with it. A delivery records against the
158+
* incarnation it was published for, so ordinary refreshes (which replace the
159+
* snapshot but not the session) still count, while a stale completion for a
160+
* session evicted and recreated mid-flight does not. */
161+
const incarnations = new Map<string, object>()
156162

157163
/** Test seam. Production leaves every field unset. */
158164
export const precedenceInternals: {
@@ -178,6 +184,7 @@ export const precedenceInternals: {
178184
export const MAX_TRACKED_SESSIONS = 256
179185

180186
function remember(sessionID: string, value: Precedence): void {
187+
if (!bySession.has(sessionID)) incarnations.set(sessionID, {})
181188
bySession.delete(sessionID)
182189
bySession.set(sessionID, value)
183190
while (bySession.size > MAX_TRACKED_SESSIONS) {
@@ -187,6 +194,7 @@ function remember(sessionID: string, value: Precedence): void {
187194
announced.delete(oldest.value)
188195
publishing.delete(oldest.value)
189196
publishQueue.delete(oldest.value)
197+
incarnations.delete(oldest.value)
190198
unrecognisedWarned.delete(oldest.value)
191199
}
192200
}
@@ -445,6 +453,7 @@ export async function refresh(
445453
// Nothing reaches `announced` until the line actually arrives, so a failure leaves
446454
// the session's known state untouched and the next turn retries.
447455
const attempt = { line, routed }
456+
const incarnation = incarnations.get(sessionID)
448457
publishing.set(sessionID, attempt)
449458
const queued = (publishQueue.get(sessionID) ?? Promise.resolve()).then(async () => {
450459
const delivered = await announce(line)
@@ -453,10 +462,10 @@ export async function refresh(
453462
// eviction only ever walks `bySession`, so an entry recreated here after the
454463
// session left it could never be reclaimed, and the map would grow with the
455464
// lifetime session count rather than staying bounded. Nor may it write over a
456-
// session recreated in the meantime — only the snapshot this attempt was
457-
// published for may record it, or a stale completion could retain or repeat
458-
// an obsolete line over the newer publication.
459-
if (delivered && bySession.get(sessionID) === result) announced.set(sessionID, attempt)
465+
// session recreated in the meantime. The check is on the session incarnation,
466+
// not the snapshot: a later refresh in the same session replaces the snapshot
467+
// while this line is still the one being said for it.
468+
if (delivered && incarnations.get(sessionID) === incarnation) announced.set(sessionID, attempt)
460469
})
461470
publishQueue.set(sessionID, queued)
462471
void queued
@@ -545,6 +554,7 @@ export function announcedSessionCount(): number {
545554

546555
export function resetForTests(): void {
547556
bySession.clear()
557+
incarnations.clear()
548558
announced.clear()
549559
publishing.clear()
550560
publishQueue.clear()

‎packages/opencode/test/altimate/workspace/precedence.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,25 @@ describe("the per-session caches are bounded", () => {
296296
expect(announcedSessionCount()).toBeLessThanOrEqual(MAX_TRACKED_SESSIONS)
297297
})
298298

299+
test("two refreshes sharing one in-flight delivery record it once, not never", async () => {
300+
// A multi-step turn refreshes per step. The second refresh replaces the snapshot
301+
// but starts no new delivery (the line is already being said); when that delivery
302+
// lands it must still be recorded, or the next turn repeats the same line.
303+
const settle: Array<() => void> = []
304+
precedenceInternals.announce = () => new Promise<void>((resolve) => settle.push(resolve))
305+
await refresh(SESSION, SNOWFLAKE_TOOLS)
306+
await refresh(SESSION, SNOWFLAKE_TOOLS)
307+
expect(settle).toHaveLength(1)
308+
settle[0]()
309+
await tick()
310+
311+
const said: string[] = []
312+
precedenceInternals.announce = async (line) => void said.push(line)
313+
await refresh(SESSION, SNOWFLAKE_TOOLS)
314+
await tick()
315+
expect(said).toEqual([])
316+
})
317+
299318
test("a line delivered after its session was evicted and recreated does not overwrite the new record", async () => {
300319
// Eviction drops the session's publish chain, so a session recreated before its
301320
// old line lands has a second publication running unchained. If the stale

0 commit comments

Comments
 (0)