Skip to content

Commit 0eec72e

Browse files
fix: address cubic's review of the usage rows
Session entries end with the turn, not at shutdown; the engine key counts only in a bound turn; only project cloud blocks are workspace memory; skill source scoped to this project; unknown starter contents omitted; documents counter removed; failure text keeps the status and is redacted. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent 7a79820 commit 0eec72e

13 files changed

Lines changed: 160 additions & 74 deletions

File tree

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ export namespace Telemetry {
260260
duration_ms: number
261261
// altimate_change — what of the bound workspace this session used (see noteWorkspaceUse)
262262
workspace_skills_used?: number
263-
workspace_documents_used?: number
264263
workspace_memory_blocks?: number
265264
workspace_integration_calls?: number
266265
}
@@ -609,10 +608,11 @@ export namespace Telemetry {
609608
timestamp: number
610609
session_id: string
611610
workspace_id: number
612-
skills: number
613-
integrations: number
611+
/** Absent when the starter could not tell (unsynced, unreadable, timed out). */
612+
skills?: number
613+
integrations?: number
614614
knowledge: string
615-
memory_enabled: boolean
615+
memory_enabled?: boolean
616616
}
617617
| {
618618
/** A memory block was written back to the bound workspace's cloud memory. */
@@ -953,7 +953,7 @@ export namespace Telemetry {
953953
duration_ms: number
954954
/** Last tool category the agent used (or "none") */
955955
last_tool_category: string
956-
// altimate_change — true when the session had used workspace skills, memory, documents or integrations
956+
// altimate_change — true when the session had used workspace skills, memory or integrations
957957
workspace_context_used?: boolean
958958
}
959959
// task intent classification for understanding DE problem distribution
@@ -1962,9 +1962,10 @@ export namespace Telemetry {
19621962
let workspaceId: number | undefined
19631963
// altimate_change start — the workspace of every session that set the context, by session: a serve
19641964
// process runs sessions side by side, so the singleton above can belong to another session's turn
1965-
// when an event is tracked. Insertion-ordered; the oldest entry goes past the cap.
1965+
// when an event is tracked. An entry lives from the session's turn start to its end (endSession);
1966+
// the insertion-ordered cap only matters past that many sessions mid-turn at once.
19661967
const sessionWorkspaces = new Map<string, number | undefined>()
1967-
const MAX_SESSION_WORKSPACES = 256
1968+
const MAX_SESSION_WORKSPACES = 1024
19681969
// altimate_change end
19691970
let appInsights: AppInsightsConfig | undefined
19701971
let droppedEvents = 0
@@ -2233,20 +2234,29 @@ export namespace Telemetry {
22332234
// emit sites note what they used; session_end reports the counts and task_outcome_signal
22342235
// whether any workspace context was in play. Keyed by session so a serve process with several
22352236
// sessions keeps them apart; cleared when the session ends.
2236-
export type WorkspaceUse = { skills: number; documents: number; memory: number; integrations: number }
2237+
export type WorkspaceUse = { skills: number; memory: number; integrations: number }
22372238
const workspaceUse = new Map<string, WorkspaceUse>()
22382239
export function noteWorkspaceUse(sessionId: string, kind: keyof WorkspaceUse, n = 1) {
22392240
if (!sessionId || n <= 0) return
2240-
const use = workspaceUse.get(sessionId) ?? { skills: 0, documents: 0, memory: 0, integrations: 0 }
2241+
const use = workspaceUse.get(sessionId) ?? { skills: 0, memory: 0, integrations: 0 }
22412242
use[kind] += n
22422243
workspaceUse.set(sessionId, use)
22432244
}
22442245
export function workspaceUseOf(sessionId: string): WorkspaceUse {
2245-
return { skills: 0, documents: 0, memory: 0, integrations: 0, ...workspaceUse.get(sessionId) }
2246+
return { skills: 0, memory: 0, integrations: 0, ...workspaceUse.get(sessionId) }
22462247
}
22472248
export function clearWorkspaceUse(sessionId: string) {
22482249
workspaceUse.delete(sessionId)
22492250
}
2251+
/** The workspace a session's turn bound, or undefined when unbound or unknown. */
2252+
export function workspaceOf(sessionId: string): number | undefined {
2253+
return sessionWorkspaces.get(sessionId)
2254+
}
2255+
/** A session's turn ended: its use counts are read out by then, and its workspace entry goes. */
2256+
export function endSession(sessionId: string) {
2257+
workspaceUse.delete(sessionId)
2258+
sessionWorkspaces.delete(sessionId)
2259+
}
22502260
// altimate_change end
22512261

22522262
export function setContext(opts: { sessionId: string; projectId: string; workspaceId?: number }) {
@@ -2622,7 +2632,6 @@ export namespace Telemetry {
26222632
sessionId = ""
26232633
projectId = ""
26242634
workspaceId = undefined
2625-
sessionWorkspaces.clear() // altimate_change
26262635
machineId = ""
26272636
// doInit() only assigns userEmail when Account.active() returns one, so without this a
26282637
// logout followed by a re-init in the same process kept hashing the previous account into

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import {
3535
type CachedBinding,
3636
} from "./state"
3737
import { indexKey, readIndex, readIndexEntry, recordIndexEntry } from "./memory-index"
38-
import { WorkspaceApi } from "./api-client"
38+
import { WorkspaceApi, WorkspaceApiError } from "./api-client"
39+
import { redact } from "@/altimate/debug/report" // altimate_change — failure text in the sync record
3940
import { AltimateApi } from "@/altimate/api/client"
4041
import {
4142
LIST_LIMIT,
@@ -264,6 +265,16 @@ async function memoryEnabled(binding: CachedBinding): Promise<boolean> {
264265
/** A failed read says what failed, so the sync record can classify it. */
265266
type MemoryStatusOutcome = "enabled" | "disabled" | { status: "error"; error: string }
266267

268+
// altimate_change start — what failed, for the persisted sync record: the HTTP status when the API
269+
// answered one (so the reason class sees it whatever the detail says), redacted of secrets, URLs
270+
// and the user's identity before it is written.
271+
function failureText(err: unknown): string {
272+
const status = err instanceof WorkspaceApiError ? err.status : undefined
273+
const base = err instanceof Error ? `${err.name}: ${err.message}` : String(err)
274+
return redact(status !== undefined ? `HTTP ${status} ${base}` : base)
275+
}
276+
// altimate_change end
277+
267278
/** Three-way, because a read that cannot reach the service must not be reported
268279
* as "this workspace has no memory" — that reads as success while destroying
269280
* whatever the session already had. */
@@ -319,7 +330,7 @@ async function memoryStatus(
319330
log.warn("could not confirm workspace memory setting", { err: String(err) })
320331
// Not cached either way: a transient failure should neither disable the
321332
// mirror for a minute nor keep it enabled.
322-
return { status: "error", error: String(err) }
333+
return { status: "error", error: failureText(err) }
323334
}
324335
}
325336

@@ -677,6 +688,8 @@ async function push(
677688
binding: CachedBinding | null,
678689
known?: KnownRecords,
679690
directory?: string,
691+
/** The session that wrote the block, captured before the queue; the context's when absent. */
692+
sessionID?: string,
680693
): Promise<PushOutcome> {
681694
const key = indexKey({
682695
scope: block.scope,
@@ -760,7 +773,7 @@ async function push(
760773
}
761774
await MemoryApi.update(match, block.content, metadata)
762775
await recordIndexEntry(key, { memoryId: match, contentHash: hash, syncedAt: Date.now() })
763-
if (binding) trackContributed(binding)
776+
if (binding) trackContributed(binding, sessionID)
764777
return "stored"
765778
}
766779

@@ -795,17 +808,17 @@ async function push(
795808
log.warn("archiving an extra record produced by one create", { id: block.id, memoryId: extra })
796809
await MemoryApi.update(extra, "", { ...metadata, archived: "true", archived_at: new Date().toISOString() })
797810
}
798-
if (binding) trackContributed(binding)
811+
if (binding) trackContributed(binding, sessionID)
799812
return "stored"
800813
}
801814

802815
// altimate_change start — a successful write-back is the workspace getting something back
803-
function trackContributed(binding: CachedBinding) {
816+
function trackContributed(binding: CachedBinding, sessionID?: string) {
804817
try {
805818
Telemetry.track({
806819
type: "workspace_memory_contributed",
807820
timestamp: Date.now(),
808-
session_id: Telemetry.getContext().sessionId,
821+
session_id: sessionID ?? Telemetry.getContext().sessionId,
809822
workspace_id: binding.datamateId,
810823
block_count: 1,
811824
})
@@ -890,6 +903,9 @@ export async function flushPendingMirrors(timeoutMs = 30_000): Promise<void> {
890903
* disabled. */
891904
export async function mirrorBlock(block: MemoryBlock, directory?: string): Promise<void> {
892905
if (!isEnabled()) return
906+
// altimate_change — the writer's session, read before the queue: the context can be another
907+
// session's by the time the mirror lands.
908+
const sessionID = Telemetry.getContext().sessionId
893909
// Queued BEFORE the binding lookup, not after. Both are async, so resolving
894910
// them first let two operations on one block reach `serialize` in the
895911
// opposite order to the writes that triggered them.
@@ -903,7 +919,7 @@ export async function mirrorBlock(block: MemoryBlock, directory?: string): Promi
903919
const binding = await currentBinding(directory)
904920
if (!binding) return
905921
if (!(await memoryEnabled(binding))) return
906-
await push(block, binding, undefined, directory)
922+
await push(block, binding, undefined, directory, sessionID)
907923
})
908924
return tracked(task)
909925
}
@@ -1508,7 +1524,7 @@ async function loadWorkspaceMemory(directory?: string): Promise<LoadOutcome> {
15081524
log.warn("workspace memory load failed", { err: String(err) })
15091525
// Stamped like any other outcome: without an epoch the session would reload (and make
15101526
// the prompt wait) on every turn for as long as the service is down.
1511-
return { status: "error", error: String(err), epoch: vouched, dir, datamateId: resolved, ...stamp }
1527+
return { status: "error", error: failureText(err), epoch: vouched, dir, datamateId: resolved, ...stamp }
15121528
}
15131529
}
15141530

‎packages/opencode/src/altimate/workspace/starter.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,11 @@ export function trackStarterShown(workspaceId: number, contents: WorkspaceConten
154154
timestamp: Date.now(),
155155
session_id: Telemetry.getContext().sessionId,
156156
workspace_id: workspaceId,
157-
skills: Array.isArray(contents.skills) ? contents.skills.length : 0,
158-
integrations: Array.isArray(contents.integrations) ? contents.integrations.length : 0,
157+
// Unknown is left out, not reported as none: the starter's own rule.
158+
...(Array.isArray(contents.skills) ? { skills: contents.skills.length } : {}),
159+
...(Array.isArray(contents.integrations) ? { integrations: contents.integrations.length } : {}),
159160
knowledge: contents.knowledge?.kind ?? "unknown",
160-
memory_enabled: contents.memoryEnabled === true,
161+
...(typeof contents.memoryEnabled === "boolean" ? { memory_enabled: contents.memoryEnabled } : {}),
161162
})
162163
} catch {
163164
// telemetry never blocks the starter

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ function chain(file: string, work: () => Promise<void>, kind: SyncKind): Promise
279279
/** The reason class of a sync error message, for workspace_sync_failed. */
280280
export function syncFailureReason(error: string | undefined): "auth" | "not_found" | "network" | "other" {
281281
const e = (error ?? "").toLowerCase()
282-
if (/\b(401|403)\b|unauthori[sz]ed|forbidden|sign(ed)? in|credential/.test(e)) return "auth"
282+
if (/\b(401|403)\b|unauthori[sz]ed|forbidden|\bsign(ed)? in\b|credential/.test(e)) return "auth"
283283
if (/\b404\b|not found|no longer exists|unknown workspace/.test(e)) return "not_found"
284284
if (/timed? ?out|cannot reach|unreachable|econn|enotfound|network|socket/.test(e)) return "network"
285285
return "other"

‎packages/opencode/src/altimate/workspace/tool-call-telemetry.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
// altimate_change — tool_call rows for the bound workspace's integration calls.
22
import { Telemetry } from "@/altimate/telemetry"
3-
import { mcpToolSource } from "@/altimate/tool-source"
3+
import { DATAMATE_KEY } from "@/altimate/datamate-transport"
4+
import { redact } from "@/altimate/debug/report"
45

5-
/** True when the MCP client serving a tool is the bound workspace's datamate server. */
6+
/** True when the MCP client serving a tool is the engine the workspace overlay manages. */
67
export function isWorkspaceClient(clientName: string): boolean {
7-
return mcpToolSource(clientName) === "altimate"
8+
return clientName === DATAMATE_KEY
89
}
910

1011
/**
11-
* Track one MCP call that went through the bound workspace. MCP tools never pass through the
12-
* `Tool.define` wrapper that tracks registry tools, so this is where a workspace integration
12+
* Track one MCP call that went through the bound workspace's engine. MCP tools never pass through
13+
* the `Tool.define` wrapper that tracks registry tools, so this is where a workspace integration
1314
* call becomes a `tool_call` row; a successful one also counts as an integration use for the
14-
* session. Third-party MCP servers stay untracked and registry tools (`datamate_manager`
15-
* included) never reach here. Returns whether the call was tracked.
15+
* session. Only the managed engine client (`datamate`) in a session whose turn is bound counts:
16+
* a standalone `datamate-<name>` server, or the key in an unbound session, is not the workspace's.
17+
* Returns whether the call was tracked.
1618
*/
1719
export function trackWorkspaceToolCall(input: {
1820
key: string
@@ -24,6 +26,7 @@ export function trackWorkspaceToolCall(input: {
2426
error?: unknown
2527
}): boolean {
2628
if (!isWorkspaceClient(input.clientName)) return false
29+
if (Telemetry.workspaceOf(input.sessionID) === undefined) return false
2730
try {
2831
if (input.outcome === "success") Telemetry.noteWorkspaceUse(input.sessionID, "integrations")
2932
const message = input.error instanceof Error ? input.error.message : String(input.error)
@@ -40,7 +43,7 @@ export function trackWorkspaceToolCall(input: {
4043
duration_ms: Date.now() - input.startedAt,
4144
sequence_index: 0,
4245
previous_tool: null,
43-
...(input.error !== undefined && { error: Telemetry.maskString(message).slice(0, 500) }),
46+
...(input.error !== undefined && { error: redact(Telemetry.maskString(message)).slice(0, 500) }),
4447
})
4548
} catch {
4649
// telemetry never surfaces into the tool call

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,9 @@ export namespace MemoryPrompt {
246246
injectedCount++
247247
injectedTraining.push(item.block)
248248
scopesSeen.add(item.block.scope)
249-
// altimate_change start — a block from the bound workspace's cloud memory counts as workspace scope
250-
if (isRemote(item.block)) {
249+
// altimate_change start — a project-scope block from the bound workspace's cloud memory counts as
250+
// workspace scope; a global cloud block is the account's and applies everywhere
251+
if (isRemote(item.block) && item.block.scope === "project") {
251252
scopesSeen.add("workspace")
252253
workspaceBlocks++
253254
}
@@ -272,7 +273,7 @@ export namespace MemoryPrompt {
272273
injectedCount++
273274
scopesSeen.add(item.block.scope)
274275
// altimate_change start — as above, for memory blocks
275-
if (isRemote(item.block)) {
276+
if (isRemote(item.block) && item.block.scope === "project") {
276277
scopesSeen.add("workspace")
277278
workspaceBlocks++
278279
}
@@ -310,9 +311,7 @@ export namespace MemoryPrompt {
310311
// altimate_change start — workspace memory use, for memory_injection and the session's summary rows
311312
...(workspaceBlocks > 0 && { workspace_blocks: workspaceBlocks }),
312313
})
313-
if (workspaceBlocks > 0) {
314-
Telemetry.noteWorkspaceUse(ctx?.sessionID ?? Telemetry.getContext().sessionId, "memory", workspaceBlocks)
315-
}
314+
if (workspaceBlocks > 0 && ctx?.sessionID) Telemetry.noteWorkspaceUse(ctx.sessionID, "memory", workspaceBlocks)
316315
// altimate_change end
317316
}
318317

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,12 @@ import { Telemetry } from "@/telemetry" // altimate_change — session telemetry
110110
// altimate_change start — the session's bound workspace rides on every telemetry row
111111
import { readLocalBinding } from "@/altimate/workspace/state"
112112

113-
/** The session's workspace use, as session_end fields; counts are cleared with the read. */
113+
/** The session's workspace use, as session_end fields; the read ends the session's telemetry entries. */
114114
function workspaceUseFields(sessionID: string) {
115115
const use = Telemetry.workspaceUseOf(sessionID)
116-
Telemetry.clearWorkspaceUse(sessionID)
116+
Telemetry.endSession(sessionID)
117117
return {
118118
workspace_skills_used: use.skills,
119-
workspace_documents_used: use.documents,
120119
workspace_memory_blocks: use.memory,
121120
workspace_integration_calls: use.integrations,
122121
}
@@ -2132,7 +2131,7 @@ export namespace SessionPrompt {
21322131
step_count: step,
21332132
duration_ms: Date.now() - sessionStartTime,
21342133
last_tool_category: lastToolCategory || "none",
2135-
// altimate_change — whether workspace skills, memory, documents or integrations were in play
2134+
// altimate_change — whether workspace skills, memory or integrations were in play
21362135
workspace_context_used: Object.values(Telemetry.workspaceUseOf(sessionID)).some((n) => n > 0),
21372136
})
21382137
// Tool chain effectiveness — aggregated tool sequence + outcome

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ import { selectSkillsWithLLM } from "../altimate/skill-selector"
1919
import { Telemetry } from "../altimate/telemetry"
2020
import os from "os"
2121
import { isBuiltinSkillLocation } from "../altimate/skill-source"
22+
import { Instance } from "../project/instance"
2223

2324
const MAX_DISPLAY_SKILLS = 50
2425

2526
// altimate_change start — classifySkillSource helper for skill telemetry + source badge
26-
export function classifySkillSource(location: string): "builtin" | "global" | "project" | "workspace" {
27+
export function classifySkillSource(
28+
location: string,
29+
/** This project's root; with it, only its own managed folder is the workspace's. */
30+
projectRoot?: string,
31+
): "builtin" | "global" | "project" | "workspace" {
2732
// Normalize separators so `.altimate/builtin` / homedir prefix match on Windows too.
2833
const normalized = location.replace(/\\/g, "/")
2934
// Embedded skills load with a `builtin:<name>/SKILL.md` location and Altimate's
@@ -34,8 +39,12 @@ export function classifySkillSource(location: string): "builtin" | "global" | "p
3439
// altimate_change - the same predicate the skill listing labels with, so the two cannot drift apart
3540
if (isBuiltinSkillLocation(location)) return "builtin"
3641
// altimate_change start — skills synced from the bound workspace live under the managed
37-
// `_workspace` folder; they are the workspace's, not the project's.
38-
if (/\/\.altimate-code\/skill\/_workspace\//.test(normalized)) return "workspace"
42+
// `_workspace` folder of this project; they are the workspace's, not the project's. Another
43+
// project's managed folder is not this session's workspace.
44+
if (projectRoot !== undefined) {
45+
const root = projectRoot.replace(/\\/g, "/").replace(/\/+$/, "")
46+
if (normalized.startsWith(`${root}/.altimate-code/skill/_workspace/`)) return "workspace"
47+
} else if (/\/\.altimate-code\/skill\/_workspace\//.test(normalized)) return "workspace"
3948
// altimate_change end
4049
if (normalized.startsWith(os.homedir().replace(/\\/g, "/"))) return "global"
4150
return "project"
@@ -260,7 +269,7 @@ export const SkillTool = Tool.define("skill", async (ctx) => {
260269
// altimate_change end
261270

262271
// altimate_change start — classify origin once, reused for telemetry and the source badge
263-
const skillOrigin = classifySkillSource(skill.location)
272+
const skillOrigin = classifySkillSource(skill.location, Instance.directory)
264273
// altimate_change start — count workspace skill use for the session's summary rows
265274
if (skillOrigin === "workspace") Telemetry.noteWorkspaceUse(ctx.sessionID, "skills")
266275
// altimate_change end

0 commit comments

Comments
 (0)