Skip to content

Commit bb25f62

Browse files
fix: stamp the workspace id at track time; keep the memory failure detail
A serve process can move the context to another session before the batch is serialised, and the memory sync record flattened every failure to one text, so its reason class was always other. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent 1ef15a4 commit bb25f62

4 files changed

Lines changed: 29 additions & 17 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,6 @@ export namespace Telemetry {
20442044
// still says how this CLI was launched (cli, tui, serve, datamates).
20452045
client: "altimate-code",
20462046
source: clientSource,
2047-
...(workspaceId !== undefined && !("workspace_id" in fields) && { workspace_id: String(workspaceId) }),
20482047
project_id: fields.project_id ?? projectId,
20492048
...(machineId && { machine_id: machineId }),
20502049
// altimate_change — groups every event from one process launch. The onboarding funnel
@@ -2401,6 +2400,10 @@ export namespace Telemetry {
24012400
const operationId = typeof eventSession === "string" ? TraceContext.activeTraceId(eventSession) : undefined
24022401
if (operationId) (event as any)._operation_id = operationId
24032402
// altimate_change end
2403+
// altimate_change start — the bound workspace is read now, not at flush: a serve process can start
2404+
// another session's turn, and move the context, before this event is serialised.
2405+
if (workspaceId !== undefined && !("workspace_id" in event)) (event as any).workspace_id = workspaceId
2406+
// altimate_change end
24042407
buffer.push(event)
24052408
if (buffer.length > MAX_BUFFER_SIZE) {
24062409
buffer.shift()

‎packages/opencode/src/altimate/workspace/memory-sync.ts‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ async function memoryEnabled(binding: CachedBinding): Promise<boolean> {
261261
return (await memoryStatus(binding)) === "enabled"
262262
}
263263

264+
/** A failed read says what failed, so the sync record can classify it. */
265+
type MemoryStatusOutcome = "enabled" | "disabled" | { status: "error"; error: string }
266+
264267
/** Three-way, because a read that cannot reach the service must not be reported
265268
* as "this workspace has no memory" — that reads as success while destroying
266269
* whatever the session already had. */
@@ -273,7 +276,7 @@ async function memoryStatus(
273276
* inherit a positive written under a previous account. */
274277
fresh?: boolean
275278
} = {},
276-
): Promise<"enabled" | "disabled" | "error"> {
279+
): Promise<MemoryStatusOutcome> {
277280
const cached = opts.fresh ? undefined : memoryEnabledCache.get(binding.datamateId)
278281
if (cached && Date.now() - cached.checkedAt < MEMORY_ENABLED_TTL_MS) return "enabled"
279282
try {
@@ -289,7 +292,7 @@ async function memoryStatus(
289292
missingFromListWarned.add(binding.datamateId)
290293
log.warn("workspace missing from the workspace list; memory setting unknown", { workspace: binding.datamateId })
291294
}
292-
return "error"
295+
return { status: "error", error: "workspace missing from the workspace list" }
293296
}
294297
missingFromListWarned.delete(binding.datamateId)
295298
if (match.memoryEnabled === undefined && !missingFieldWarned.has(binding.datamateId)) {
@@ -316,7 +319,7 @@ async function memoryStatus(
316319
log.warn("could not confirm workspace memory setting", { err: String(err) })
317320
// Not cached either way: a transient failure should neither disable the
318321
// mirror for a minute nor keep it enabled.
319-
return "error"
322+
return { status: "error", error: String(err) }
320323
}
321324
}
322325

@@ -1108,7 +1111,7 @@ export async function memoryEnabledForPoller(binding: CachedBinding): Promise<"e
11081111
const ask = (async () => {
11091112
try {
11101113
const status = await memoryStatus(binding, { fresh: true })
1111-
const answer = status === "error" ? ("unknown" as const) : status
1114+
const answer = typeof status === "string" ? status : ("unknown" as const)
11121115
pollMemo.set(key, { at: Date.now(), status: answer })
11131116
return answer
11141117
} finally {
@@ -1242,8 +1245,10 @@ export async function backfill(
12421245
if (!binding)
12431246
return { ok: 0, failed: 0, skipped: blocks.length, declined: 0, deferred: 0, gated: true, gateReason: "unbound" }
12441247
const status = await memoryStatus(binding)
1245-
if (status !== "enabled")
1246-
return { ok: 0, failed: 0, skipped: blocks.length, declined: 0, deferred: 0, gated: true, gateReason: status }
1248+
if (status !== "enabled") {
1249+
const gateReason = typeof status === "string" ? status : status.status
1250+
return { ok: 0, failed: 0, skipped: blocks.length, declined: 0, deferred: 0, gated: true, gateReason }
1251+
}
12471252
const index = await readIndex()
12481253

12491254
const { pending, skipped } = partitionPending(blocks, binding, index)
@@ -1412,7 +1417,7 @@ type LoadOutcome = (
14121417
}
14131418
| { status: "unlinked"; /** The server confirmed it, rather than a lookup failing. */ confirmed: boolean }
14141419
| { status: "disabled" }
1415-
| { status: "error" }
1420+
| { status: "error"; /** What failed, for the sync record and its reason class. */ error: string }
14161421
) & {
14171422
epoch?: string
14181423
dir?: string | null
@@ -1454,12 +1459,12 @@ async function loadWorkspaceMemory(directory?: string): Promise<LoadOutcome> {
14541459
stable = before === epoch
14551460
}
14561461
vouched = epoch
1457-
if (!stable) return { status: "error", epoch, dir, ...stamp }
1462+
if (!stable) return { status: "error", error: "the workspace link changed while loading", epoch, dir, ...stamp }
14581463
if (!binding) return { status: "unlinked", confirmed, epoch, dir, ...stamp }
14591464
const datamateId = binding.datamateId
14601465
resolved = datamateId
14611466
const enabled = await memoryStatus(binding)
1462-
if (enabled === "error") return { status: "error", epoch, dir, datamateId, ...stamp }
1467+
if (typeof enabled !== "string") return { status: "error", error: enabled.error, epoch, dir, datamateId, ...stamp }
14631468
if (enabled === "disabled") return { status: "disabled", epoch, dir, datamateId, ...stamp }
14641469

14651470
const ownProjectKey = projectKeyFor(binding)
@@ -1503,7 +1508,7 @@ async function loadWorkspaceMemory(directory?: string): Promise<LoadOutcome> {
15031508
log.warn("workspace memory load failed", { err: String(err) })
15041509
// Stamped like any other outcome: without an epoch the session would reload (and make
15051510
// the prompt wait) on every turn for as long as the service is down.
1506-
return { status: "error", epoch: vouched, dir, datamateId: resolved, ...stamp }
1511+
return { status: "error", error: String(err), epoch: vouched, dir, datamateId: resolved, ...stamp }
15071512
}
15081513
}
15091514

@@ -1549,7 +1554,9 @@ function recordSyncState(outcome: LoadOutcome): void {
15491554
if (retire) return SyncState.forget(dir, "memory", { isCurrent, before: outcome.observedAt })
15501555
if (outcome.datamateId === undefined) return
15511556
const result =
1552-
outcome.status === "loaded" ? { items: outcome.items } : { error: "could not load workspace memory" }
1557+
outcome.status === "loaded"
1558+
? { items: outcome.items }
1559+
: { error: `could not load workspace memory: ${"error" in outcome ? outcome.error : outcome.status}` }
15531560
return SyncState.record(dir, outcome.datamateId, "memory", result, {
15541561
isCurrent,
15551562
scope: outcome.scope,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,16 @@ describe("workspace id on the telemetry context", () => {
7979
workspace_id: 7,
8080
how: "bind_existing",
8181
})
82+
// Another session's turn moves the context before the batch is serialised (a serve process):
83+
// the rows above keep the workspace they were tracked under.
84+
Telemetry.setContext({ sessionId: "s2", projectId: "p1" })
85+
Telemetry.track(startup("unbound"))
8286
await Telemetry.flush()
8387
const bound = envelopes().find((e) => e.data.baseData.properties.command === "bound")
8488
expect(bound.data.baseData.properties.workspace_id).toBe("42")
8589
expect(bound.data.baseData.properties.client).toBe("altimate-code")
8690
const linked = envelopes().find((e) => e.data.baseData.name === "workspace_linked")
8791
expect(linked.data.baseData.properties.workspace_id).toBe("7") // the row's own id, not the context's
88-
89-
Telemetry.setContext({ sessionId: "s2", projectId: "p1" })
90-
Telemetry.track(startup("unbound"))
91-
await Telemetry.flush()
9292
const unbound = envelopes().find((e) => e.data.baseData.properties.command === "unbound")
9393
expect("workspace_id" in unbound.data.baseData.properties).toBe(false)
9494
})

‎packages/opencode/test/altimate/workspace/memory-sync.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2237,7 +2237,9 @@ describe("sync state", () => {
22372237
listFails = false
22382238
}
22392239
const failed = await memoryState(dir, (e) => e.status === "error")
2240-
expect(failed).toMatchObject({ status: "error", count: 1, error: "could not load workspace memory" })
2240+
expect(failed).toMatchObject({ status: "error", count: 1 })
2241+
// The record keeps what failed, so the sync-failure reason class is real rather than "other".
2242+
expect(failed?.error).toMatch(/^could not load workspace memory: /)
22412243
// "memory loaded Xm ago" reads the last success, which the failure did not move.
22422244
expect(failed?.lastSucceededAt).toBe(loaded!.lastSucceededAt)
22432245
})

0 commit comments

Comments
 (0)