Skip to content

Commit be3b516

Browse files
fix: name the workspace explicitly on every row; no ambient stamping
`track()` no longer stamps `workspace_id` from a process-wide or per-session fallback (five rounds of cancelled-turn attribution bugs came from it). `setContext` returns a turn token; loop, tool, skill, memory and first-job rows carry their turn's workspace explicitly; counters key on the turn. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent 6235bc5 commit be3b516

9 files changed

Lines changed: 104 additions & 97 deletions

File tree

‎packages/opencode/src/altimate/plugin/onboarding-telemetry.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// session loop.
1313
import type { Hooks, PluginInput } from "@opencode-ai/plugin"
1414
import * as OnboardingTelemetry from "../telemetry/onboarding"
15+
import { Telemetry } from "@/altimate/telemetry"
1516
// altimate_change start — AI-8398 workspaces trigger. Reaches into the same
1617
// EventV2 bridge the server/routes/tui.ts uses to publish TuiEvent.CommandExecute
1718
// so the workspace TuiPlugin (packages/opencode/src/plugin/tui/altimate/workspace.tsx)
@@ -261,7 +262,12 @@ export async function OnboardingTelemetryPlugin(_input: PluginInput): Promise<Ho
261262
OnboardingTelemetry.isSelectedJob(input.sessionID, job) &&
262263
OnboardingTelemetry.claimFirstJobCompleted(input.sessionID)
263264
) {
264-
void OnboardingTelemetry.emit({ type: "first_job_completed", job }, input.sessionID)
265+
// altimate_change — the current turn's workspace, asked for explicitly (this hook runs inside
266+
// the turn whose tool just finished); nothing is stamped from the context.
267+
void OnboardingTelemetry.emit(
268+
{ type: "first_job_completed", job, workspace_id: Telemetry.turnWorkspaceOf(input.sessionID) },
269+
input.sessionID,
270+
)
265271
}
266272
},
267273
}

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

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ export namespace Telemetry {
238238
// altimate_change start — add os/arch/node_version for environment segmentation
239239
| {
240240
type: "session_start"
241+
/** altimate_change — the turn's workspace, named by the emitter */
242+
workspace_id?: number
241243
timestamp: number
242244
session_id: string
243245
model_id: string
@@ -668,6 +670,8 @@ export namespace Telemetry {
668670
}
669671
| {
670672
type: "memory_injection"
673+
/** altimate_change — the turn's workspace, named by the emitter */
674+
workspace_id?: number
671675
timestamp: number
672676
session_id: string
673677
block_count: number
@@ -740,6 +744,8 @@ export namespace Telemetry {
740744
}
741745
| {
742746
type: "skill_used"
747+
/** altimate_change — the turn's workspace, named by the emitter */
748+
workspace_id?: number
743749
timestamp: number
744750
session_id: string
745751
message_id: string
@@ -851,6 +857,8 @@ export namespace Telemetry {
851857
// altimate_change start — plan refinement telemetry event
852858
| {
853859
type: "plan_revision"
860+
/** altimate_change — the turn's workspace, named by the emitter */
861+
workspace_id?: number
854862
timestamp: number
855863
session_id: string
856864
revision_number: number
@@ -879,6 +887,8 @@ export namespace Telemetry {
879887
// altimate_change end
880888
| {
881889
type: "core_failure"
890+
/** altimate_change — the turn's workspace, named by the emitter */
891+
workspace_id?: number
882892
timestamp: number
883893
session_id: string
884894
tool_name: string
@@ -933,6 +943,8 @@ export namespace Telemetry {
933943
// altimate_change start — sql quality telemetry for issue prevention metrics
934944
| {
935945
type: "sql_quality"
946+
/** altimate_change — the turn's workspace, named by the emitter */
947+
workspace_id?: number
936948
timestamp: number
937949
session_id: string
938950
tool_name: string
@@ -967,6 +979,8 @@ export namespace Telemetry {
967979
// task intent classification for understanding DE problem distribution
968980
| {
969981
type: "task_classified"
982+
/** altimate_change — the turn's workspace, named by the emitter */
983+
workspace_id?: number
970984
timestamp: number
971985
session_id: string
972986
/** Classified intent category */
@@ -1253,6 +1267,8 @@ export namespace Telemetry {
12531267
| {
12541268
/** derived — see activation_job_selected */
12551269
type: "first_job_completed"
1270+
/** altimate_change — the turn's workspace, named by the emitter */
1271+
workspace_id?: number
12561272
timestamp: number
12571273
session_id: string
12581274
job: "sample_duck_db" | "breaks_downstream" | "sql_review" | "cost" | "something_else"
@@ -1275,6 +1291,8 @@ export namespace Telemetry {
12751291
}
12761292
| {
12771293
type: "first_prompt_sent"
1294+
/** altimate_change — the turn's workspace, named by the emitter */
1295+
workspace_id?: number
12781296
timestamp: number
12791297
session_id: string
12801298
}
@@ -1968,21 +1986,18 @@ export namespace Telemetry {
19681986
let sessionId = ""
19691987
let projectId = ""
19701988
// altimate_change — the workspace the current session's project is linked to (the backend
1971-
// datamate id), set per prompt turn from the local binding cache; stamped on every envelope so
1972-
// the session, first-use, skill, memory, tool and outcome rows join the workspace without each
1973-
// emit site knowing about it.
1989+
// datamate id), set per prompt turn from the local binding cache and read back through
1990+
// `getContext()` by callers that ask for it; nothing is stamped from it (see `track`).
19741991
let workspaceId: number | undefined
1975-
// altimate_change start — the workspace of every session that set the context, by session: a serve
1976-
// process runs sessions side by side, so the singleton above can belong to another session's turn
1977-
// when an event is tracked. An entry lives from the session's turn start to its end (endSession);
1978-
// the insertion-ordered cap only matters past that many sessions mid-turn at once.
1979-
const sessionWorkspaces = new Map<string, number | undefined>()
1980-
const MAX_SESSION_WORKSPACES = 1024
1981-
/** The current turn token per session: `setContext` issues one, `endSession` ends only its own, so a
1982-
* cancelled turn unwinding late cannot end the replacement's entries. Tokens come from one
1983-
* process-wide counter, so an entry evicted and re-created never reuses a token a stale turn holds. */
1992+
// altimate_change start — the current turn token per session: `setContext` issues one, `endSession`
1993+
// ends only its own, so a cancelled turn unwinding late cannot end the replacement's counters.
1994+
// Tokens come from one process-wide counter, so a token is never reused. The map holds sessions
1995+
// mid-turn; an entry goes at the turn's end.
19841996
const sessionTurns = new Map<string, number>()
19851997
let nextTurn = 0
1998+
/** The workspace of each session's current turn, for an emitter that runs inside the turn but
1999+
* outside the loop (a plugin hook) and asks for it explicitly; nothing is stamped from it. */
2000+
const turnWorkspaces = new Map<string, number | undefined>()
19862001
// altimate_change end
19872002
let appInsights: AppInsightsConfig | undefined
19882003
let droppedEvents = 0
@@ -2281,22 +2296,19 @@ export namespace Telemetry {
22812296
export function clearWorkspaceUse(sessionId: string, turn?: number) {
22822297
workspaceUse.delete(useKey(sessionId, turn))
22832298
}
2284-
/** The workspace a session's turn bound, or undefined when unbound or unknown. */
2285-
export function workspaceOf(sessionId: string): number | undefined {
2286-
return sessionWorkspaces.get(sessionId)
2287-
}
2288-
/** A session's turn ended: its use counts are read out and its `session_end` tracked by then, and
2289-
* its workspace entry goes. With `turn` (from `setContext`), a turn that is no longer the session's
2290-
* current one ends nothing: the replacement's entries are the replacement's. */
2299+
/** A session's turn ended: its use counts are read out and its `session_end` tracked by then. With
2300+
* `turn` (from `setContext`), a turn that is no longer the session's current one ends only its own
2301+
* counts: the replacement's are the replacement's. */
22912302
export function endSession(sessionId: string, turn?: number) {
2292-
if (turn !== undefined && sessionTurns.get(sessionId) !== turn) {
2293-
// A turn that was replaced ends only its own counts; the replacement's entries stay.
2294-
workspaceUse.delete(useKey(sessionId, turn))
2295-
return
2296-
}
22972303
workspaceUse.delete(useKey(sessionId, turn))
2298-
sessionWorkspaces.delete(sessionId)
2304+
if (turn !== undefined && sessionTurns.get(sessionId) !== turn) return
22992305
sessionTurns.delete(sessionId)
2306+
turnWorkspaces.delete(sessionId)
2307+
}
2308+
/** The workspace of the session's current turn, for an emitter inside that turn; undefined when
2309+
* unbound or when no turn is running. An explicit lookup, never a stamp. */
2310+
export function turnWorkspaceOf(sessionId: string): number | undefined {
2311+
return turnWorkspaces.get(sessionId)
23002312
}
23012313
// altimate_change end
23022314

@@ -2305,15 +2317,9 @@ export namespace Telemetry {
23052317
sessionId = opts.sessionId
23062318
projectId = opts.projectId
23072319
workspaceId = opts.workspaceId
2308-
// altimate_change start — remembered per session for the stamping in track()
2320+
// altimate_change start — a turn token for the session's counters and its end
23092321
if (!opts.sessionId) return 0
2310-
sessionWorkspaces.delete(opts.sessionId)
2311-
sessionWorkspaces.set(opts.sessionId, opts.workspaceId)
2312-
if (sessionWorkspaces.size > MAX_SESSION_WORKSPACES) {
2313-
const oldest = sessionWorkspaces.keys().next().value!
2314-
sessionWorkspaces.delete(oldest)
2315-
sessionTurns.delete(oldest)
2316-
}
2322+
turnWorkspaces.set(opts.sessionId, opts.workspaceId)
23172323
const turn = ++nextTurn
23182324
sessionTurns.set(opts.sessionId, turn)
23192325
return turn
@@ -2471,16 +2477,10 @@ export namespace Telemetry {
24712477
const operationId = typeof eventSession === "string" ? TraceContext.activeTraceId(eventSession) : undefined
24722478
if (operationId) (event as any)._operation_id = operationId
24732479
// altimate_change end
2474-
// altimate_change start — the bound workspace is read now, not at flush, and for the event's own
2475-
// session: a serve process runs sessions side by side, so the process-wide context can belong to
2476-
// another session's turn. A row with no session takes the current context; a row naming a session
2477-
// that never set the context gains no key rather than another session's.
2478-
if (!("workspace_id" in event)) {
2479-
const own =
2480-
typeof eventSession === "string" && eventSession !== "" ? sessionWorkspaces.get(eventSession) : workspaceId
2481-
if (own !== undefined) (event as any).workspace_id = own
2482-
}
2483-
// altimate_change end
2480+
// altimate_change — no ambient workspace stamping: a row names its workspace itself (the prompt
2481+
// loop's rows, tool rows and memory rows carry the turn's; workspace rows carry their own) or it
2482+
// carries none. A process-wide or per-session fallback kept attributing cancelled turns' rows to
2483+
// their replacements.
24842484
buffer.push(event)
24852485
if (buffer.length > MAX_BUFFER_SIZE) {
24862486
buffer.shift()

‎packages/opencode/src/altimate/tool-zod-compat.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export type Metadata = { [key: string]: any; error?: string }
2828
export interface LegacyContext<M extends Metadata = Metadata> {
2929
sessionID: SessionID
3030
messageID: MessageID
31+
// altimate_change start — the calling turn's workspace and token, as on Tool.Context
32+
workspaceId?: number
33+
turn?: number
34+
// altimate_change end
3135
agent: string
3236
abort: AbortSignal
3337
callID?: string
@@ -221,6 +225,10 @@ function legacyDefToDef(legacy: LegacyToolDef): DefWithoutID {
221225
const legacyCtx: LegacyContext = {
222226
sessionID: ctx.sessionID,
223227
messageID: ctx.messageID,
228+
// altimate_change start — carried through for the rows a legacy tool emits
229+
workspaceId: ctx.workspaceId,
230+
turn: ctx.turn,
231+
// altimate_change end
224232
agent: ctx.agent,
225233
abort: ctx.abort,
226234
callID: ctx.callID,

‎packages/opencode/src/memory/prompt.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ export namespace MemoryPrompt {
305305
timestamp: Date.now(),
306306
// altimate_change — the injecting session, not the process context (another turn's in serve)
307307
session_id: ctx?.sessionID ?? Telemetry.getContext().sessionId,
308+
workspace_id: ctx?.workspaceId, // altimate_change — the turn's workspace, named explicitly
308309
block_count: injectedCount,
309310
total_chars: used,
310311
budget,
@@ -313,7 +314,9 @@ export namespace MemoryPrompt {
313314
...(workspaceBlocks > 0 && { workspace_blocks: workspaceBlocks }),
314315
})
315316
// The turn injects on every step: the count is the most blocks any step injected, not a sum.
316-
if (workspaceBlocks > 0 && ctx?.sessionID) Telemetry.noteWorkspaceUse(ctx.sessionID, "memory", workspaceBlocks, "max")
317+
if (workspaceBlocks > 0 && ctx?.sessionID) {
318+
Telemetry.noteWorkspaceUse(ctx.sessionID, "memory", workspaceBlocks, "max", ctx.turn)
319+
}
317320
// altimate_change end
318321
}
319322

‎packages/opencode/src/memory/types.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export const AGENT_TRAINING_RELEVANCE: Record<string, Partial<Record<string, num
4646
}
4747

4848
export interface InjectionContext {
49+
// altimate_change start — the prompt turn's workspace and token, for the injection row and its count
50+
workspaceId?: number
51+
turn?: number
52+
// altimate_change end
4953
agent?: string
5054
disableTraining?: boolean
5155
/** Identifies which session's workspace-memory overlay to merge. Omitted in

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,7 @@ export namespace SessionPrompt {
13101310
// Cap reached — track and inject a synthetic hint so the LLM informs the user
13111311
Telemetry.track({
13121312
type: "plan_revision",
1313+
...turnWorkspace, // altimate_change — this turn's workspace, named explicitly
13131314
timestamp: Date.now(),
13141315
session_id: sessionID,
13151316
revision_number: planRevisionCount,
@@ -1394,6 +1395,7 @@ export namespace SessionPrompt {
13941395
const action = isRejection ? "reject" : isApproval ? "approve" : "refine"
13951396
Telemetry.track({
13961397
type: "plan_revision",
1398+
...turnWorkspace, // altimate_change — this turn's workspace, named explicitly
13971399
timestamp: Date.now(),
13981400
session_id: sessionID,
13991401
revision_number: planRevisionCount,
@@ -1533,6 +1535,7 @@ export namespace SessionPrompt {
15331535
// "build" sessions). See the helper at the top of this namespace.
15341536
Telemetry.track({
15351537
type: "session_start",
1538+
...turnWorkspace, // altimate_change — this turn's workspace, named explicitly
15361539
timestamp: Date.now(),
15371540
session_id: sessionID,
15381541
model_id: model.id,
@@ -1584,6 +1587,7 @@ export namespace SessionPrompt {
15841587
) ?? "unknown"
15851588
Telemetry.track({
15861589
type: "task_classified",
1590+
...turnWorkspace, // altimate_change — this turn's workspace, named explicitly
15871591
timestamp: Date.now(),
15881592
session_id: sessionID,
15891593
intent: intent as any,
@@ -1617,7 +1621,7 @@ export namespace SessionPrompt {
16171621
OnboardingTelemetry.isOnboardingSession(sessionID) &&
16181622
OnboardingTelemetry.claimFirstPrompt(sessionID)
16191623
) {
1620-
void OnboardingTelemetry.emit({ type: "first_prompt_sent" }, sessionID)
1624+
void OnboardingTelemetry.emit({ type: "first_prompt_sent", ...turnWorkspace }, sessionID)
16211625
}
16221626
// altimate_change end
16231627
// altimate_change end — session start telemetry
@@ -1657,6 +1661,9 @@ export namespace SessionPrompt {
16571661
agent: agent.name,
16581662
disableTraining: Flag.ALTIMATE_DISABLE_TRAINING,
16591663
sessionID,
1664+
// altimate_change — the turn's workspace and token, for the injection row and its count
1665+
turn: telemetryTurn,
1666+
workspaceId: boundWorkspace?.datamateId,
16601667
})
16611668
// altimate_change end
16621669
// altimate_change start — workspace identity.
@@ -2006,6 +2013,7 @@ export namespace SessionPrompt {
20062013
// (verdict enums, counters, model names) passes through.
20072014
Telemetry.track({
20082015
type: "validator_check",
2016+
...turnWorkspace, // altimate_change — this turn's workspace, named explicitly
20092017
timestamp: Date.now(),
20102018
session_id: sessionID,
20112019
validator_name: validator.name,
@@ -2071,6 +2079,7 @@ export namespace SessionPrompt {
20712079
})
20722080
Telemetry.track({
20732081
type: "validator_retries_exhausted",
2082+
...turnWorkspace, // altimate_change — this turn's workspace, named explicitly
20742083
timestamp: Date.now(),
20752084
session_id: sessionID,
20762085
step,
@@ -2358,6 +2367,10 @@ export namespace SessionPrompt {
23582367
abort: options.abortSignal!,
23592368
messageID: input.processor.message.id,
23602369
callID: options.toolCallId,
2370+
// altimate_change start — the turn's workspace and token, for the rows the tool emits
2371+
workspaceId: input.workspaceId,
2372+
turn: input.turn,
2373+
// altimate_change end
23612374
// altimate_change start — batch inner calls reuse the prompt's lesson delivery state
23622375
extra: { model: input.model, bypassAgentCheck: input.bypassAgentCheck, lessons: input.lessons },
23632376
// altimate_change end

‎packages/opencode/src/tool/skill.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export const SkillTool = Tool.define("skill", async (ctx) => {
271271
// altimate_change start — classify origin once, reused for telemetry and the source badge
272272
const skillOrigin = classifySkillSource(skill.location, Instance.directory)
273273
// altimate_change start — count workspace skill use for the session's summary rows
274-
if (skillOrigin === "workspace") Telemetry.noteWorkspaceUse(ctx.sessionID, "skills")
274+
if (skillOrigin === "workspace") Telemetry.noteWorkspaceUse(ctx.sessionID, "skills", 1, "add", ctx.turn)
275275
// altimate_change end
276276
// altimate_change end
277277

@@ -281,6 +281,7 @@ export const SkillTool = Tool.define("skill", async (ctx) => {
281281
type: "skill_used",
282282
timestamp: Date.now(),
283283
session_id: ctx.sessionID,
284+
workspace_id: ctx.workspaceId, // altimate_change — the turn's workspace
284285
message_id: ctx.messageID,
285286
skill_name: skill.name,
286287
skill_source: skillOrigin,

0 commit comments

Comments
 (0)