Skip to content

Commit f9c1531

Browse files
fix: an attached run reports the bus offer it prints as shown
The offer command carries the offer's telemetry fields and the server's node major (`arguments`, JSON); `run --attach` reports `shown` with surface `bus` from them when it prints the line. The server's publish is not a display. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent 589f845 commit f9c1531

5 files changed

Lines changed: 120 additions & 11 deletions

File tree

‎packages/opencode/src/altimate/workspace/engine-offer.ts‎

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,65 @@ export async function installEngine(): Promise<InstallResult> {
273273
return { ok: false, error: detail || `npm exited with code ${run.code ?? "unknown"}` }
274274
}
275275

276+
/** What the bus carries with the offer command: the offer's telemetry fields plus the server's
277+
* node major (the install happens where the server runs), so the surface that prints the offer in
278+
* another process (an attached run) reports `shown` with them. */
279+
export type OfferDetail = Pick<EngineOffer, "reason" | "workspaceId" | "declared" | "found"> & {
280+
nodeMajor: number | null
281+
}
282+
283+
export function encodeOfferDetail(offer: EngineOffer, nodeMajor: number | null): string {
284+
const detail: OfferDetail = {
285+
reason: offer.reason,
286+
workspaceId: offer.workspaceId,
287+
declared: offer.declared,
288+
found: offer.found,
289+
nodeMajor,
290+
}
291+
return JSON.stringify(detail)
292+
}
293+
294+
/** The detail back from the bus, or undefined for anything that is not one. */
295+
export function decodeOfferDetail(raw: unknown): OfferDetail | undefined {
296+
if (typeof raw !== "string") return undefined
297+
try {
298+
const v = JSON.parse(raw) as Partial<Record<keyof OfferDetail, unknown>>
299+
const reason = v.reason === "engine-missing" || v.reason === "engine-too-old" ? v.reason : undefined
300+
const workspaceId = typeof v.workspaceId === "string" ? v.workspaceId : undefined
301+
const declared = typeof v.declared === "number" ? v.declared : v.declared === undefined ? undefined : NaN
302+
const found = typeof v.found === "string" ? v.found : v.found === undefined ? undefined : ""
303+
const nodeMajor = v.nodeMajor === null ? null : typeof v.nodeMajor === "number" ? v.nodeMajor : undefined
304+
if (!reason || workspaceId === undefined || Number.isNaN(declared) || found === "" || nodeMajor === undefined) {
305+
return undefined
306+
}
307+
return { reason, workspaceId, declared, found, nodeMajor }
308+
} catch {
309+
return undefined
310+
}
311+
}
312+
313+
/** An attached run printed the offer it read off the bus: that process is the surface that showed
314+
* it, so it reports `shown` (surface `bus`) from the fields the event carried. False when the
315+
* event carried none that decode, and nothing is reported. */
316+
export function trackPrintedOffer(raw: unknown, sessionID: string): boolean {
317+
const detail = decodeOfferDetail(raw)
318+
if (!detail) return false
319+
trackInstallPrompt("shown", detail, { surface: "bus", nodeMajor: detail.nodeMajor }, sessionID)
320+
return true
321+
}
322+
276323
/** Ask the TUI to raise the offer. False when the bus is unavailable. The
277324
* session is carried so an attached headless run, which reads the same event
278-
* stream, prints the offer raised for its own session only. */
279-
async function publishOffer(sessionID: string): Promise<boolean> {
280-
if (syncInternals.publishOffer) return syncInternals.publishOffer(sessionID)
325+
* stream, prints the offer raised for its own session only; the offer's fields
326+
* ride along so that run reports `shown` from them (the TUI dialog re-derives
327+
* the offer and reports its own). */
328+
async function publishOffer(offer: EngineOffer, sessionID: string): Promise<boolean> {
329+
const detail = encodeOfferDetail(offer, await nodeMajor().catch(() => null))
330+
if (syncInternals.publishOffer) return syncInternals.publishOffer(sessionID, detail)
281331
try {
282332
await AppRuntime.runPromise(
283333
EventV2Bridge.Service.use((events) =>
284-
events.publish(TuiEvent.CommandExecute, { command: OFFER_COMMAND, sessionID }),
334+
events.publish(TuiEvent.CommandExecute, { command: OFFER_COMMAND, sessionID, arguments: detail }),
285335
),
286336
)
287337
return true
@@ -335,6 +385,6 @@ export async function offerOrNotify(offer: EngineOffer, toast: Toast, sessionID:
335385
// may hold the offer back (its 7-day latch), so dispatch is not display. The toast reports here
336386
// only when it was shown.
337387
if (offerInstall(offer)) return
338-
if (await publishOffer(sessionID)) return
388+
if (await publishOffer(offer, sessionID)) return
339389
if (await notify(toast)) shown("toast")
340390
}

‎packages/opencode/src/altimate/workspace/engine-seams.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const syncInternals: {
3333
printLine?: (line: string) => void
3434
/** Install-offer seams (see engine-offer.ts). */
3535
offer?: (offer: EngineOffer) => boolean
36-
publishOffer?: (sessionID: string) => Promise<boolean>
36+
publishOffer?: (sessionID: string, detail: string) => Promise<boolean>
3737
runInstall?: (
3838
argv: string[],
3939
timeoutMs: number,

‎packages/opencode/src/cli/cmd/run.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import { SessionTermination } from "../../session/termination"
6262
// altimate_change start — upstream_fix: type-only import for the tracing-config cast (see tracer setup below)
6363
import type { ConfigV1 } from "@opencode-ai/core/v1/config/config"
6464
// altimate_change - render the workspace engine offer in an attached run
65-
import { OFFER_COMMAND, installCommand } from "@/altimate/workspace/engine-offer"
65+
import { OFFER_COMMAND, installCommand, trackPrintedOffer } from "@/altimate/workspace/engine-offer"
6666
// altimate_change end
6767

6868
// When a tool's parameters can't be statically inferred (legacy fork tools whose
@@ -998,6 +998,9 @@ You are speaking to a non-technical business executive. Follow these rules stric
998998
`This workspace's integration tools need the local engine on the server. Install it there with: ${installCommand()}` +
999999
EOL,
10001000
)
1001+
// This process is the surface that showed the offer, so it reports `shown` (surface
1002+
// `bus`) from the fields the event carried; the server's publish is not a display.
1003+
if (sessionID) trackPrintedOffer((event.properties as { arguments?: unknown }).arguments, sessionID)
10011004
continue
10021005
}
10031006
// altimate_change end

‎packages/opencode/src/server/tui-event.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export const TuiEvent = {
3636
// same stream: the session it was raised for lets that run print only its
3737
// own offer, not another session's in the same directory.
3838
sessionID: Schema.optional(Schema.String),
39+
// The offer's telemetry fields (JSON), for the surface that prints it to report `shown` with.
40+
arguments: Schema.optional(Schema.String),
3941
// altimate_change end
4042
},
4143
}),

‎packages/opencode/test/altimate/workspace/engine-install-offer.test.ts‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ import {
2222
type EngineOffer,
2323
type Toast,
2424
} from "../../../src/altimate/workspace/engine-overlay"
25-
import { OFFER_RECHECK_MS, OFFER_SKIP_TTL_MS } from "../../../src/altimate/workspace/engine-offer"
25+
import {
26+
OFFER_RECHECK_MS,
27+
OFFER_SKIP_TTL_MS,
28+
decodeOfferDetail,
29+
trackPrintedOffer,
30+
} from "../../../src/altimate/workspace/engine-offer"
2631
import type { CachedBinding } from "../../../src/altimate/workspace/state"
2732
import { Telemetry } from "../../../src/altimate/telemetry"
2833

@@ -38,7 +43,14 @@ const binding: CachedBinding = {
3843
linkedAt: 0,
3944
} as CachedBinding
4045

41-
type Harness = { offers: EngineOffer[]; toasts: Toast[]; printed: string[]; published: number; publishedFor: string[] }
46+
type Harness = {
47+
offers: EngineOffer[]
48+
toasts: Toast[]
49+
printed: string[]
50+
published: number
51+
publishedFor: string[]
52+
publishedDetail: string[]
53+
}
4254

4355
/** No engine on PATH (or an old one) plus captured surfaces. */
4456
function install(opts: {
@@ -50,7 +62,7 @@ function install(opts: {
5062
surface?: boolean
5163
bound?: boolean
5264
}): Harness {
53-
const h: Harness = { offers: [], toasts: [], printed: [], published: 0, publishedFor: [] }
65+
const h: Harness = { offers: [], toasts: [], printed: [], published: 0, publishedFor: [], publishedDetail: [] }
5466
delete process.env.ALTIMATE_DISABLE_WORKSPACE
5567
syncInternals.serve = () => false
5668
syncInternals.headless = () => opts.headless === true
@@ -68,10 +80,11 @@ function install(opts: {
6880
syncInternals.printLine = (line) => {
6981
h.printed.push(line)
7082
}
71-
syncInternals.publishOffer = async (sessionID) => {
83+
syncInternals.publishOffer = async (sessionID, detail) => {
7284
if (opts.bus === false) return false
7385
h.published += 1
7486
h.publishedFor.push(sessionID)
87+
h.publishedDetail.push(detail)
7588
return true
7689
}
7790
if (opts.surface) {
@@ -161,6 +174,47 @@ describe("offer routing — engine missing", () => {
161174
expect(h.toasts).toEqual([])
162175
expect(h.printed).toEqual([])
163176
})
177+
test("the published offer carries its fields, and the surface that prints them reports shown", async () => {
178+
const h = install({})
179+
syncInternals.nodeMajor = async () => 22
180+
await beforeTurn("s1")
181+
expect(decodeOfferDetail(h.publishedDetail[0])).toEqual({
182+
reason: "engine-missing",
183+
workspaceId: "42",
184+
declared: 2,
185+
found: undefined,
186+
nodeMajor: 22,
187+
})
188+
const rows: Record<string, unknown>[] = []
189+
const spy = spyOn(Telemetry, "track").mockImplementation((e) => {
190+
rows.push(e as Record<string, unknown>)
191+
})
192+
try {
193+
// An attached run prints the offer from this event in its own process and reports from it.
194+
expect(trackPrintedOffer(h.publishedDetail[0], "s1")).toBe(true)
195+
expect(trackPrintedOffer(undefined, "s1")).toBe(false)
196+
expect(trackPrintedOffer("not json", "s1")).toBe(false)
197+
expect(trackPrintedOffer(JSON.stringify({ reason: "other", workspaceId: "42", nodeMajor: null }), "s1")).toBe(
198+
false,
199+
)
200+
expect(trackPrintedOffer(JSON.stringify({ reason: "engine-missing", workspaceId: "42" }), "s1")).toBe(false)
201+
} finally {
202+
spy.mockRestore()
203+
}
204+
expect(rows).toEqual([
205+
expect.objectContaining({
206+
type: "workspace_engine_install_prompt",
207+
action: "shown",
208+
surface: "bus",
209+
session_id: "s1",
210+
workspace_id: 42,
211+
reason: "engine-missing",
212+
declared: 2,
213+
node_detected: true,
214+
node_major: 22,
215+
}),
216+
])
217+
})
164218
test("falls back to the toast only when the bus is unavailable", async () => {
165219
const h = install({ bus: false })
166220
await beforeTurn("s1")

0 commit comments

Comments
 (0)