Skip to content

Commit a027586

Browse files
fix: report an attach again after a relink; keep the session on a create's add
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent 82f88d8 commit a027586

5 files changed

Lines changed: 45 additions & 21 deletions

File tree

‎packages/opencode/src/altimate/tools/datamate.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,12 @@ async function handleAdd(
496496

497497
async function handleCreate(
498498
args: {
499-
name?: string
500-
description?: string
501-
integration_ids?: string[]
502-
memory_enabled?: boolean
503-
privacy?: string
504-
scope?: "project" | "global"
499+
name?: string
500+
description?: string
501+
integration_ids?: string[]
502+
memory_enabled?: boolean
503+
privacy?: string
504+
scope?: "project" | "global"
505505
},
506506
sessionID?: string,
507507
) {
@@ -540,6 +540,7 @@ async function handleCreate(
540540
const added = await handleAdd(
541541
{ datamate_id: created.id, name: `datamate-${slugify(args.name)}`, scope: args.scope },
542542
"create",
543+
sessionID,
543544
)
544545
if (!("managedBy" in added.metadata)) return added
545546
const rolledBack = await AltimateApi.deleteDatamate(created.id, creds).then(

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import {
4444
} from "./engine-seams"
4545
import { declaredBounded, fingerprint, notify, printLine, resolveBinding, versionOf, which } from "./engine-probes"
4646
import { OFFER_RECHECK_MS, OFFER_SKIP_TTL_MS, installCommand, offerOrNotify, type EngineOffer } from "./engine-offer"
47-
import { trackAttach, trackEngineError } from "./engine-telemetry"
47+
import { attachKey, trackAttach, trackEngineError } from "./engine-telemetry"
4848
import {
4949
ENGINE_BINARY,
5050
INSTALL_HELPS,
@@ -360,8 +360,9 @@ type SessionRecord = {
360360
/** The workspace the directory was linked to when the boundary settled this
361361
* outcome. */
362362
linked?: { id: string; name: string } | null
363-
/** The last outcome reported to telemetry; a transient record carries the previous one. */
364-
tracked?: Outcome
363+
/** The key of the last attach reported to telemetry (workspace and outcome); a transient record
364+
* carries the previous one. */
365+
tracked?: string
365366
}
366367
const sessions = new Map<string, SessionRecord>()
367368
const declaredCache = new Map<string, { value: Declared | null; at: number }>()
@@ -374,13 +375,12 @@ function record(
374375
extra: { engineVersion?: string | null; transient?: boolean } = {},
375376
): SessionRecord {
376377
const previous = sessions.get(sessionID)
377-
// telemetry: a settled outcome, when it changed, named for the directory's workspace. A transient
378-
// record (the retry sentinel) reports nothing and keeps the last reported outcome for the next
378+
// telemetry: a settled outcome, when it or the workspace it settled for changed. A transient
379+
// record (the retry sentinel) reports nothing and keeps the last reported key for the next
379380
// comparison.
380-
const tracked = extra.transient ? previous?.tracked : outcome
381-
if (!extra.transient) {
382-
trackAttach(outcome, previous?.tracked, { ...extra, workspaceId: Number(linkedWorkspace()?.id) }, sessionID)
383-
}
381+
const workspaceId = Number(linkedWorkspace()?.id) || undefined
382+
const tracked = extra.transient ? previous?.tracked : attachKey(outcome, workspaceId)
383+
if (!extra.transient) trackAttach(outcome, previous?.tracked, { ...extra, workspaceId }, sessionID)
384384
sessions.delete(sessionID)
385385
const next: SessionRecord = {
386386
outcome,

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,21 @@ export function attachEvent(
8686
}
8787

8888
/** Track the attach when the session's outcome changed (not on every turn boundary). */
89+
/** What an attach row is deduped on: the directory's workspace and the outcome's signature, so the
90+
* same-shaped attach to another workspace after a relink is a change. */
91+
export function attachKey(outcome: Outcome, workspaceId?: number): string {
92+
return `${workspaceId ?? ""}|${attachSignature(outcome)}`
93+
}
94+
95+
/** Track a session's attach when its key differs from the last one reported for the session. */
8996
export function trackAttach(
9097
outcome: Outcome,
91-
previous: Outcome | undefined,
98+
previousKey: string | undefined,
9299
extra: { engineVersion?: string | null; workspaceId?: number } = {},
93100
sessionID?: string,
94101
): boolean {
95102
try {
96-
if (previous && attachSignature(previous) === attachSignature(outcome)) return false
103+
if (previousKey !== undefined && previousKey === attachKey(outcome, extra.workspaceId)) return false
97104
Telemetry.track(attachEvent(outcome, extra, sessionID))
98105
return true
99106
} catch {

‎packages/opencode/test/altimate/workspace/engine-overlay.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,18 @@ describe("telemetry — the attach", () => {
13211321
expect(attaches().map((r) => r.outcome)).toEqual(["connect_failed"])
13221322
})
13231323

1324+
test("a relink to another workspace reports the attach again, named for the new workspace", async () => {
1325+
const h = install({})
1326+
await beforeTurn("s1")
1327+
h.binding = bound(7, "growth")
1328+
await beforeTurn("s1")
1329+
await beforeTurn("s1")
1330+
expect(attaches().map((r) => [r.outcome, r.workspace_id])).toEqual([
1331+
["attached", 42],
1332+
["attached", 7],
1333+
])
1334+
})
1335+
13241336
test("an engine below the floor reports the version it found", async () => {
13251337
install({ version: "0.1.0" })
13261338
await beforeTurn("s1")

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"
22
import { Telemetry } from "../../../src/altimate/telemetry"
33
import {
44
attachEvent,
5+
attachKey,
56
attachSignature,
67
connectErrorClass,
78
installPromptEvent,
@@ -59,10 +60,13 @@ describe("workspace engine telemetry builders", () => {
5960
test("an attach is tracked when the outcome changed, not on every turn boundary", () => {
6061
const attached = { kind: "attached", available: 2 } as const
6162
expect(trackAttach(attached, undefined)).toBe(true)
62-
expect(trackAttach(attached, attached)).toBe(false)
63-
expect(trackAttach({ kind: "attached", available: 3 }, attached)).toBe(true)
64-
expect(trackAttach({ kind: "engine-missing" }, { kind: "attached", available: 3 })).toBe(true)
65-
expect(events.map((e) => e.outcome)).toEqual(["attached", "attached", "engine_missing"])
63+
expect(trackAttach(attached, attachKey(attached))).toBe(false)
64+
expect(trackAttach({ kind: "attached", available: 3 }, attachKey(attached))).toBe(true)
65+
expect(trackAttach({ kind: "engine-missing" }, attachKey({ kind: "attached", available: 3 }))).toBe(true)
66+
// The same-shaped attach to another workspace (a relink) is a change.
67+
expect(trackAttach(attached, attachKey(attached, 42), { workspaceId: 42 })).toBe(false)
68+
expect(trackAttach(attached, attachKey(attached, 42), { workspaceId: 7 })).toBe(true)
69+
expect(events.map((e) => e.outcome)).toEqual(["attached", "attached", "engine_missing", "attached"])
6670
expect(attachSignature({ kind: "connect-failed", error: "a" })).toBe(
6771
attachSignature({ kind: "connect-failed", error: "b" }),
6872
)

0 commit comments

Comments
 (0)