Skip to content

Commit 3ce788b

Browse files
fix: the tool hook input carries the turn's workspace; batch and warehouse rows name it
`tool.execute.after` gains `workspaceId`, set by the three trigger sites from the turn that ran the tool, so `first_job_completed` names it without asking the session for its current turn. `batch` passes `workspaceId`/`turn` to its inner contexts; `warehouse_add`'s suggestion failure names the turn's workspace. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent be3b516 commit 3ce788b

9 files changed

Lines changed: 65 additions & 17 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
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"
1615
// altimate_change start — AI-8398 workspaces trigger. Reaches into the same
1716
// EventV2 bridge the server/routes/tui.ts uses to publish TuiEvent.CommandExecute
1817
// so the workspace TuiPlugin (packages/opencode/src/plugin/tui/altimate/workspace.tsx)
@@ -262,10 +261,11 @@ export async function OnboardingTelemetryPlugin(_input: PluginInput): Promise<Ho
262261
OnboardingTelemetry.isSelectedJob(input.sessionID, job) &&
263262
OnboardingTelemetry.claimFirstJobCompleted(input.sessionID)
264263
) {
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.
264+
// altimate_change — the workspace of the turn that ran the tool, carried by the hook input
265+
// from the trigger site: a tool returning after its turn was cancelled and replaced still
266+
// names its own turn's workspace, not the replacement's.
267267
void OnboardingTelemetry.emit(
268-
{ type: "first_job_completed", job, workspace_id: Telemetry.turnWorkspaceOf(input.sessionID) },
268+
{ type: "first_job_completed", job, workspace_id: input.workspaceId },
269269
input.sessionID,
270270
)
271271
}

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,9 +1995,6 @@ export namespace Telemetry {
19951995
// mid-turn; an entry goes at the turn's end.
19961996
const sessionTurns = new Map<string, number>()
19971997
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>()
20011998
// altimate_change end
20021999
let appInsights: AppInsightsConfig | undefined
20032000
let droppedEvents = 0
@@ -2303,12 +2300,6 @@ export namespace Telemetry {
23032300
workspaceUse.delete(useKey(sessionId, turn))
23042301
if (turn !== undefined && sessionTurns.get(sessionId) !== turn) return
23052302
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)
23122303
}
23132304
// altimate_change end
23142305

@@ -2319,7 +2310,6 @@ export namespace Telemetry {
23192310
workspaceId = opts.workspaceId
23202311
// altimate_change start — a turn token for the session's counters and its end
23212312
if (!opts.sessionId) return 0
2322-
turnWorkspaces.set(opts.sessionId, opts.workspaceId)
23232313
const turn = ++nextTurn
23242314
sessionTurns.set(opts.sessionId, turn)
23252315
return turn

‎packages/opencode/src/altimate/tools/warehouse-add.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ IMPORTANT: For private key file paths, always use "private_key_path" (not "priva
121121
type: "core_failure",
122122
timestamp: Date.now(),
123123
session_id: Telemetry.getContext().sessionId || "unknown-session",
124+
workspace_id: ctx.workspaceId,
124125
tool_name: "warehouse_add",
125126
tool_category: "warehouse",
126127
error_class: "internal",

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,7 @@ export namespace SessionPrompt {
10851085
sessionID,
10861086
callID: part.id,
10871087
args: taskArgs,
1088+
workspaceId: boundWorkspace?.datamateId, // altimate_change — the turn's workspace, for plugin rows
10881089
},
10891090
result,
10901091
)
@@ -2483,6 +2484,7 @@ export namespace SessionPrompt {
24832484
sessionID: ctx.sessionID,
24842485
callID: ctx.callID,
24852486
args,
2487+
workspaceId: input.workspaceId, // altimate_change — the turn's workspace, for plugin rows
24862488
},
24872489
// altimate_change start — plugins observe the source-stamped output
24882490
stamped,
@@ -2585,6 +2587,7 @@ export namespace SessionPrompt {
25852587
sessionID: ctx.sessionID,
25862588
callID: opts.toolCallId,
25872589
args,
2590+
workspaceId: input.workspaceId, // altimate_change — the turn's workspace, for plugin rows
25882591
},
25892592
result,
25902593
)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ function toEffectContext(ctx: LegacyContext, callID: string): Tool.Context {
2323
agent: ctx.agent,
2424
abort: ctx.abort,
2525
callID,
26+
// the turn's workspace and token, so the inner tools' rows name it
27+
workspaceId: ctx.workspaceId,
28+
turn: ctx.turn,
2629
extra: ctx.extra,
2730
messages: ctx.messages,
2831
metadata: (input) => Effect.sync(() => ctx.metadata(input)),

‎packages/opencode/test/altimate/telemetry/onboarding.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,25 @@ describe("activation events", () => {
215215
expect((events.find((e) => e.type === "first_job_completed") as any).job).toBe("sample_duck_db")
216216
})
217217

218+
test("the first job's row names the workspace the hook input carries", async () => {
219+
const events = captureEvents()
220+
const hooks = await plugin()
221+
await startOnboarding(hooks, "skip")
222+
223+
// The trigger site sets `workspaceId` from the turn that ran the tool; the hook never asks the
224+
// session for its current turn (a replacement's, when a cancelled turn's tool returns late).
225+
await runTool(
226+
hooks,
227+
{ tool: "sample_setup", sessionID: SESSION, callID: "c3w", args: {}, workspaceId: 7 },
228+
{ title: "", output: "", metadata: { success: true } },
229+
)
230+
await settle()
231+
232+
const completed = events.find((e) => e.type === "first_job_completed") as any
233+
expect(completed.workspace_id).toBe(7)
234+
expect("workspace_id" in (events.find((e) => e.type === "activation_job_selected") as any)).toBe(false)
235+
})
236+
218237
test("a failed sample setup counts as selected but not completed", async () => {
219238
const events = captureEvents()
220239
const hooks = await plugin()

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ describe("workspace id on the telemetry context", () => {
9090
Telemetry.noteWorkspaceUse("s7", "memory", 2)
9191
// Counts are per turn: the cancelled turn's rows read its own, the replacement's its own.
9292
expect(Telemetry.workspaceUseOf("s7", first)).toEqual({ skills: 1, memory: 0, integrations: 0 })
93-
expect(Telemetry.turnWorkspaceOf("s7")).toBe(42) // an explicit lookup for in-turn emitters
9493
expect(Telemetry.workspaceUseOf("s7", second)).toEqual({ skills: 0, memory: 2, integrations: 0 })
9594
Telemetry.endSession("s7", first) // the old generation, after the replacement started
9695
expect(Telemetry.workspaceUseOf("s7", first)).toEqual({ skills: 0, memory: 0, integrations: 0 })
@@ -102,7 +101,6 @@ describe("workspace id on the telemetry context", () => {
102101
expect(Telemetry.workspaceUseOf("s7").integrations).toBe(1)
103102
Telemetry.endSession("s7", second)
104103
expect(Telemetry.workspaceUseOf("s7")).toEqual({ skills: 0, memory: 0, integrations: 0 })
105-
expect(Telemetry.turnWorkspaceOf("s7")).toBeUndefined()
106104
// Tokens never repeat, so a stale end can never hit a later turn.
107105
const stale = Telemetry.setContext({ sessionId: "s8", projectId: "p1", workspaceId: 5 })
108106
Telemetry.endSession("s8", stale)

‎packages/opencode/test/tool/batch.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,36 @@ describe("BatchTool: file lessons", () => {
6666
})
6767
})
6868

69+
describe("BatchTool: inner tool context", () => {
70+
test("carries the turn's workspace and token to the inner tools", async () => {
71+
const tool = await getToolInfo()
72+
let seen: { workspaceId?: number; turn?: number } | undefined
73+
const registry = spyOn(ToolRegistry, "tools").mockResolvedValue([{
74+
id: "probe",
75+
registrySource: undefined,
76+
description: "captures its context",
77+
parameters: Schema.Unknown,
78+
execute: (_args: unknown, ctx: { workspaceId?: number; turn?: number }) => {
79+
seen = { workspaceId: ctx.workspaceId, turn: ctx.turn }
80+
return Effect.succeed({ title: "ok", output: "ok", metadata: {} })
81+
},
82+
}] as any)
83+
const updatePart = Object.assign(async (part: Parameters<typeof Session.updatePart>[0]) => part, Session.updatePart)
84+
const updates = spyOn(Session, "updatePart").mockImplementation(updatePart)
85+
try {
86+
const result = await tool.execute({ tool_calls: [{ tool: "probe", parameters: {} }] }, {
87+
sessionID: SessionID.make("ses_batch_ctx"), messageID: MessageID.make("msg_batch_ctx"), agent: "build",
88+
abort: AbortSignal.any([]), messages: [], extra: {}, workspaceId: 7, turn: 3,
89+
} as any)
90+
expect(result.metadata.successful).toBe(1)
91+
expect(seen).toEqual({ workspaceId: 7, turn: 3 })
92+
} finally {
93+
updates.mockRestore()
94+
registry.mockRestore()
95+
}
96+
})
97+
})
98+
6999
describe("BatchTool: schema validation", () => {
70100
test("rejects empty tool_calls array", async () => {
71101
const tool = await getToolInfo()

‎packages/plugin/src/index.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,18 @@ export interface Hooks {
271271
input: { cwd: string; sessionID?: string; callID?: string },
272272
output: { env: Record<string, string> },
273273
) => Promise<void>
274+
// altimate_change start — `workspaceId`: the workspace of the turn the tool ran in, set by the
275+
// trigger site, so a plugin's own rows name it without consulting the session's current turn
276+
// (which may be a replacement by the time a cancelled turn's tool returns)
274277
"tool.execute.after"?: (
275-
input: { tool: string; sessionID: string; callID: string; args: any },
278+
input: { tool: string; sessionID: string; callID: string; args: any; workspaceId?: number },
276279
output: {
277280
title: string
278281
output: string
279282
metadata: any
280283
},
281284
) => Promise<void>
285+
// altimate_change end
282286
"experimental.chat.messages.transform"?: (
283287
input: {},
284288
output: {

0 commit comments

Comments
 (0)