Skip to content

Commit 20e3f31

Browse files
fix: issue turn tokens from one counter so an evicted session cannot reuse one
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent bd423b4 commit 20e3f31

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

‎packages/opencode/src/altimate/telemetry/index.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,9 +1966,11 @@ export namespace Telemetry {
19661966
// the insertion-ordered cap only matters past that many sessions mid-turn at once.
19671967
const sessionWorkspaces = new Map<string, number | undefined>()
19681968
const MAX_SESSION_WORKSPACES = 1024
1969-
/** The turn generation per session: `setContext` starts one, `endSession` ends only its own, so a
1970-
* cancelled turn unwinding late cannot end the replacement's entries. */
1969+
/** The current turn token per session: `setContext` issues one, `endSession` ends only its own, so a
1970+
* cancelled turn unwinding late cannot end the replacement's entries. Tokens come from one
1971+
* process-wide counter, so an entry evicted and re-created never reuses a token a stale turn holds. */
19711972
const sessionTurns = new Map<string, number>()
1973+
let nextTurn = 0
19721974
// altimate_change end
19731975
let appInsights: AppInsightsConfig | undefined
19741976
let droppedEvents = 0
@@ -2282,7 +2284,7 @@ export namespace Telemetry {
22822284
sessionWorkspaces.delete(oldest)
22832285
sessionTurns.delete(oldest)
22842286
}
2285-
const turn = (sessionTurns.get(opts.sessionId) ?? 0) + 1
2287+
const turn = ++nextTurn
22862288
sessionTurns.set(opts.sessionId, turn)
22872289
return turn
22882290
// altimate_change end

‎packages/opencode/test/altimate/telemetry/workspace-context.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe("workspace id on the telemetry context", () => {
8686
const first = Telemetry.setContext({ sessionId: "s7", projectId: "p1", workspaceId: 42 })
8787
Telemetry.noteWorkspaceUse("s7", "skills")
8888
const second = Telemetry.setContext({ sessionId: "s7", projectId: "p1", workspaceId: 42 })
89-
expect(second).toBe(first + 1)
89+
expect(second).toBeGreaterThan(first)
9090
Telemetry.noteWorkspaceUse("s7", "memory", 2)
9191
Telemetry.endSession("s7", first) // the old generation, after the replacement started
9292
expect(Telemetry.workspaceOf("s7")).toBe(42)
@@ -98,6 +98,16 @@ describe("workspace id on the telemetry context", () => {
9898
Telemetry.setContext({ sessionId: "s7", projectId: "p1", workspaceId: 9 })
9999
Telemetry.endSession("s7")
100100
expect(Telemetry.workspaceOf("s7")).toBeUndefined()
101+
// Evicted and re-created entries never reuse a token a stale turn still holds.
102+
const stale = Telemetry.setContext({ sessionId: "s8", projectId: "p1", workspaceId: 5 })
103+
for (let i = 0; i < 1100; i++) Telemetry.setContext({ sessionId: `evict-${i}`, projectId: "p1" })
104+
expect(Telemetry.workspaceOf("s8")).toBeUndefined() // evicted
105+
const fresh = Telemetry.setContext({ sessionId: "s8", projectId: "p1", workspaceId: 6 })
106+
expect(fresh).toBeGreaterThan(stale)
107+
Telemetry.endSession("s8", stale)
108+
expect(Telemetry.workspaceOf("s8")).toBe(6)
109+
Telemetry.endSession("s8", fresh)
110+
for (let i = 0; i < 1100; i++) Telemetry.endSession(`evict-${i}`)
101111
Telemetry.setContext({ sessionId: "", projectId: "" })
102112
})
103113

0 commit comments

Comments
 (0)