Skip to content

Commit 7a4364a

Browse files
feat: usage rows of a linked workspace
Workspace skills, memory and integration calls carry a workspace value and are counted per session for session_end and task_outcome_signal; new starter_shown, workspace_memory_contributed and workspace_sync_failed; skill_published carries the workspace id. Closes #1510 Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent d1fc903 commit 7a4364a

16 files changed

Lines changed: 483 additions & 103 deletions

File tree

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

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ export namespace Telemetry {
258258
total_tokens: number
259259
tool_call_count: number
260260
duration_ms: number
261+
// altimate_change — what of the bound workspace this session used (see noteWorkspaceUse)
262+
workspace_skills_used?: number
263+
workspace_documents_used?: number
264+
workspace_memory_blocks?: number
265+
workspace_integration_calls?: number
261266
}
262267
| {
263268
type: "generation"
@@ -316,6 +321,8 @@ export namespace Telemetry {
316321
previous_tool: string | null
317322
input_signature?: string
318323
error?: string
324+
// altimate_change — the call went to the bound workspace (a datamate MCP server)
325+
via_workspace?: boolean
319326
}
320327
| {
321328
type: "native_call"
@@ -596,6 +603,34 @@ export namespace Telemetry {
596603
status?: number
597604
workspace_id?: number
598605
}
606+
| {
607+
/** The post-link starter message was served (TUI or extension chat); what the workspace provides. */
608+
type: "starter_shown"
609+
timestamp: number
610+
session_id: string
611+
workspace_id: number
612+
skills: number
613+
integrations: number
614+
knowledge: string
615+
memory_enabled: boolean
616+
}
617+
| {
618+
/** A memory block was written back to the bound workspace's cloud memory. */
619+
type: "workspace_memory_contributed"
620+
timestamp: number
621+
session_id: string
622+
workspace_id: number
623+
block_count: number
624+
}
625+
| {
626+
/** A kind's sync status flipped to error. */
627+
type: "workspace_sync_failed"
628+
timestamp: number
629+
session_id: string
630+
workspace_id: number
631+
kind: "skills" | "memory"
632+
reason: "auth" | "not_found" | "network" | "other"
633+
}
599634
// altimate_change end
600635
| {
601636
type: "mcp_server_census"
@@ -633,6 +668,8 @@ export namespace Telemetry {
633668
total_chars: number
634669
budget: number
635670
scopes_used: string[]
671+
/** Blocks that came from the bound workspace's cloud memory (overlay). */
672+
workspace_blocks?: number
636673
}
637674
| {
638675
// Sent before connecting, so an attempt that hangs or ends the process is still counted.
@@ -701,7 +738,7 @@ export namespace Telemetry {
701738
session_id: string
702739
message_id: string
703740
skill_name: string
704-
skill_source: "builtin" | "global" | "project"
741+
skill_source: "builtin" | "global" | "project" | "workspace"
705742
duration_ms: number
706743
// altimate_change start — skill trigger classification for discovery analytics
707744
trigger: "user_command" | "llm_selected" | "auto_suggested" | "unknown"
@@ -815,6 +852,8 @@ export namespace Telemetry {
815852
file_count: number
816853
// "serve": published from the IDE extension over the serve route
817854
source: "cli" | "tui" | "serve"
855+
// altimate_change — the workspace the skill was attached to
856+
workspace_id?: number
818857
}
819858
// altimate_change end
820859
// altimate_change start — plan refinement telemetry event
@@ -928,6 +967,8 @@ export namespace Telemetry {
928967
duration_ms: number
929968
/** Last tool category the agent used (or "none") */
930969
last_tool_category: string
970+
// altimate_change — true when the session had used workspace skills, memory, documents or integrations
971+
workspace_context_used?: boolean
931972
}
932973
// task intent classification for understanding DE problem distribution
933974
| {
@@ -2199,6 +2240,26 @@ export namespace Telemetry {
21992240
}
22002241
}
22012242

2243+
// altimate_change start — per-session use of the bound workspace. The skill, memory and tool
2244+
// emit sites note what they used; session_end reports the counts and task_outcome_signal
2245+
// whether any workspace context was in play. Keyed by session so a serve process with several
2246+
// sessions keeps them apart; cleared when the session ends.
2247+
export type WorkspaceUse = { skills: number; documents: number; memory: number; integrations: number }
2248+
const workspaceUse = new Map<string, WorkspaceUse>()
2249+
export function noteWorkspaceUse(sessionId: string, kind: keyof WorkspaceUse, n = 1) {
2250+
if (!sessionId || n <= 0) return
2251+
const use = workspaceUse.get(sessionId) ?? { skills: 0, documents: 0, memory: 0, integrations: 0 }
2252+
use[kind] += n
2253+
workspaceUse.set(sessionId, use)
2254+
}
2255+
export function workspaceUseOf(sessionId: string): WorkspaceUse {
2256+
return { skills: 0, documents: 0, memory: 0, integrations: 0, ...workspaceUse.get(sessionId) }
2257+
}
2258+
export function clearWorkspaceUse(sessionId: string) {
2259+
workspaceUse.delete(sessionId)
2260+
}
2261+
// altimate_change end
2262+
22022263
export function setContext(opts: { sessionId: string; projectId: string; workspaceId?: number }) {
22032264
sessionId = opts.sessionId
22042265
projectId = opts.projectId

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

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// Nothing is written to disk: local files are the source of truth, and a cloud
1717
// record can have been edited elsewhere by a client that does not preserve this
1818
// CLI's metadata.
19+
import { Telemetry } from "@/altimate/telemetry"
1920
import { createHash } from "node:crypto"
2021
import { Flag as CoreFlag } from "@opencode-ai/core/flag/flag"
2122
import { Flag } from "@/flag/flag"
@@ -379,11 +380,7 @@ function stripTrainingMeta(content: string): string {
379380
/** Fingerprint of everything about a block that reaches the store. Tags and
380381
* expiry are included because both are mirrored. */
381382
function contentHash(block: MemoryBlock): string {
382-
const payload = JSON.stringify([
383-
stripTrainingMeta(block.content),
384-
[...block.tags].sort(),
385-
block.expires ?? "",
386-
])
383+
const payload = JSON.stringify([stripTrainingMeta(block.content), [...block.tags].sort(), block.expires ?? ""])
387384
// sha-256, not a 32-bit non-cryptographic hash. This value is the single
388385
// gate deciding whether a save is sent at all: on a collision ``push``
389386
// returns "unchanged" and a real edit is silently never mirrored, with no
@@ -702,7 +699,9 @@ async function reapArchivedBlocks(
702699
try {
703700
const gone = syncInternals.removeBlock
704701
? await syncInternals.removeBlock(scope, blockId, directory ?? undefined, block.updated)
705-
: await (await import("@/memory/store")).MemoryStore.remove(scope, blockId, directory ?? undefined, {
702+
: await (
703+
await import("@/memory/store")
704+
).MemoryStore.remove(scope, blockId, directory ?? undefined, {
706705
expectUpdated: block.updated,
707706
})
708707
if (gone) {
@@ -770,9 +769,7 @@ async function push(
770769
// user recreated under an old id gets a fresh record instead of un-archiving
771770
// the old one (`MemoryApi.update` replaces metadata wholesale, which would
772771
// drop the archived marker).
773-
const indexed = existing?.memoryId
774-
? view.records.find((r) => r.id === existing.memoryId)
775-
: undefined
772+
const indexed = existing?.memoryId ? view.records.find((r) => r.id === existing.memoryId) : undefined
776773
// A record the index names but a COMPLETE record set does not hold is gone —
777774
// deleted in the web app, by another client, or by a wipe. Absence used to
778775
// read as "still ours", so `match` stayed pointing at a dead id and every
@@ -816,6 +813,7 @@ async function push(
816813
const others = copies.filter((r) => r.id !== match).map((r) => r.id)
817814
await archiveDuplicates(others, block, metadata, "archiving a second live record of one block")
818815
await recordIndexEntry(key, { memoryId: match, contentHash: hash, syncedAt: Date.now() })
816+
if (binding) trackContributed(binding)
819817
return "stored"
820818
}
821819

@@ -848,6 +846,7 @@ async function push(
848846
// leaves the block unindexed, and the next save finds the extras by identity.
849847
await archiveDuplicates(extras, block, metadata, "archiving an extra record produced by one create")
850848
await recordIndexEntry(key, { memoryId: primary, contentHash: hash, syncedAt: Date.now() })
849+
if (binding) trackContributed(binding)
851850
return "stored"
852851
}
853852

@@ -866,6 +865,22 @@ async function archiveDuplicates(
866865
}
867866
}
868867

868+
// altimate_change start — a successful write-back is the workspace getting something back
869+
function trackContributed(binding: CachedBinding) {
870+
try {
871+
Telemetry.track({
872+
type: "workspace_memory_contributed",
873+
timestamp: Date.now(),
874+
session_id: Telemetry.getContext().sessionId,
875+
workspace_id: binding.datamateId,
876+
block_count: 1,
877+
})
878+
} catch {
879+
// telemetry never blocks a mirror
880+
}
881+
}
882+
// altimate_change end
883+
869884
/** Cloud operations for one logical block, run one at a time.
870885
*
871886
* Both callers are fire-and-forget from the store's write and delete paths, so
@@ -992,11 +1007,7 @@ export async function mirrorBlock(block: MemoryBlock, directory?: string): Promi
9921007
/** Archive a block's cloud record rather than deleting it, so the workspace
9931008
* keeps the history. Only this client filters the marker — other readers do
9941009
* not — so an archived record stays visible elsewhere. */
995-
export async function archiveBlock(
996-
scope: "global" | "project",
997-
blockId: string,
998-
directory?: string,
999-
): Promise<void> {
1010+
export async function archiveBlock(scope: "global" | "project", blockId: string, directory?: string): Promise<void> {
10001011
if (!isEnabled()) return
10011012
// Queued behind any in-flight mirror for the same block, so a delete cannot
10021013
// run before the create it is meant to undo. The binding lookup happens
@@ -1015,11 +1026,7 @@ export async function archiveBlock(
10151026
)
10161027
}
10171028

1018-
async function archiveNow(
1019-
scope: "global" | "project",
1020-
blockId: string,
1021-
binding: CachedBinding,
1022-
): Promise<void> {
1029+
async function archiveNow(scope: "global" | "project", blockId: string, binding: CachedBinding): Promise<void> {
10231030
const key = indexKey({
10241031
scope,
10251032
blockId,
@@ -1193,9 +1200,7 @@ async function pollMemoKey(binding: CachedBinding): Promise<string> {
11931200
*
11941201
* An "unknown" is never memoized: the next tick should ask again rather than
11951202
* inherit a network blip. */
1196-
export async function memoryEnabledForPoller(
1197-
binding: CachedBinding,
1198-
): Promise<"enabled" | "disabled" | "unknown"> {
1203+
export async function memoryEnabledForPoller(binding: CachedBinding): Promise<"enabled" | "disabled" | "unknown"> {
11991204
// The scoped memo is the poller's only cache. `memoryEnabledCache` is keyed by
12001205
// bare workspace id — right for the write path, which is tenant-bound by its
12011206
// credentials — but consulting it here reopened a 60s cross-tenant window the
@@ -1204,8 +1209,7 @@ export async function memoryEnabledForPoller(
12041209
// minutes per tenant is the price, and `memoryStatus` still warms both.
12051210
const key = await pollMemoKey(binding)
12061211
const memo = pollMemo.get(key)
1207-
if (memo && Date.now() - memo.at < (memo.status === "unknown" ? POLL_UNKNOWN_TTL_MS : POLL_TTL_MS))
1208-
return memo.status
1212+
if (memo && Date.now() - memo.at < (memo.status === "unknown" ? POLL_UNKNOWN_TTL_MS : POLL_TTL_MS)) return memo.status
12091213
// The in-flight ask is memoized too, not only the settled answer. Two
12101214
// refreshes overlapping on a cold memo — a remount while a slow one is
12111215
// still out — both saw it empty and both put a request on the wire.
@@ -1340,7 +1344,8 @@ export async function backfill(
13401344
// ``gated`` says the sweep never ran, as opposed to running and storing
13411345
// nothing. A caller recording "this binding is seeded" must be able to tell
13421346
// those apart: memory being off is not a completed seed.
1343-
if (!isEnabled()) return { ok: 0, failed: 0, skipped: 0, declined: 0, deferred: 0, gated: true, gateReason: "local-off" }
1347+
if (!isEnabled())
1348+
return { ok: 0, failed: 0, skipped: 0, declined: 0, deferred: 0, gated: true, gateReason: "local-off" }
13441349
// The bind path passes the binding it just recorded; there is no ambient
13451350
// instance to resolve one from on the `link` subcommand.
13461351
const binding = explicitBinding ?? (await currentBinding())
@@ -1370,10 +1375,7 @@ export async function backfill(
13701375
log.info("workspace memory backfill starting", { pending: pending.length, skipped })
13711376
const result = await runQueue(
13721377
pending,
1373-
(item) =>
1374-
serialize(item.block.scope, item.block.id, () =>
1375-
push(item.block, item.binding, known, sweepDirectory),
1376-
),
1378+
(item) => serialize(item.block.scope, item.block.id, () => push(item.block, item.binding, known, sweepDirectory)),
13771379
BACKFILL_CONCURRENCY,
13781380
)
13791381
// `skipped` combines blocks filtered before the queue (already synced, or
@@ -1388,7 +1390,10 @@ export async function backfill(
13881390
function originLabel(meta: Record<string, unknown>): string | undefined {
13891391
const remote = typeof meta.repo_remote === "string" ? meta.repo_remote : undefined
13901392
if (remote) {
1391-
const trimmed = remote.replace(/[/]+$/, "").replace(/\.git$/, "").replace(/[/]+$/, "")
1393+
const trimmed = remote
1394+
.replace(/[/]+$/, "")
1395+
.replace(/\.git$/, "")
1396+
.replace(/[/]+$/, "")
13921397
const last = trimmed.split(/[/:]/).pop()
13931398
if (last) return last
13941399
}
@@ -1484,10 +1489,7 @@ export async function hydrate(sessionID: string): Promise<void> {
14841489

14851490
/** Wait for a session's in-flight hydration, capped. Resolves immediately once
14861491
* the fetch has settled, so only the first injection of a session pays. */
1487-
export async function whenHydrated(
1488-
sessionID: string,
1489-
timeoutMs: number = HYDRATION_WAIT_MS,
1490-
): Promise<void> {
1492+
export async function whenHydrated(sessionID: string, timeoutMs: number = HYDRATION_WAIT_MS): Promise<void> {
14911493
const state = sessions.get(sessionID)
14921494
const pending = state?.hydration
14931495
if (!pending || !state) return

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// `GET /altimate/workspace/starter`. Fixed in format and built from the same data as the prompt's
66
// "What this Altimate Workspace provides" section, so it costs no model call (none of the user's
77
// tokens) and reads the same every time. Anything not known is left out, never shown as "none".
8+
import { Telemetry } from "@/altimate/telemetry"
89
import { displayWorkspaceName } from "./workspace-name"
910
import { workspaceContents, type WorkspaceContents } from "./contents"
1011

@@ -33,10 +34,14 @@ function list(names: string[], max: number): string {
3334
}
3435

3536
function skillsLine(skills: WorkspaceContents["skills"]): string | null {
36-
if (skills === null) return "Skills: not synced to this project yet; run `altimate-code workspace refresh` to fetch them."
37+
if (skills === null)
38+
return "Skills: not synced to this project yet; run `altimate-code workspace refresh` to fetch them."
3739
if (skills === "unknown") return null
3840
if (skills.length === 0) return "Skills: none yet. Add them in the workspace and they sync here."
39-
return `Skills (${skills.length}): ${list(skills.map((s) => s.name), MAX_LISTED_SKILLS)}`
41+
return `Skills (${skills.length}): ${list(
42+
skills.map((s) => s.name),
43+
MAX_LISTED_SKILLS,
44+
)}`
4045
}
4146

4247
function knowledgeLine(k: WorkspaceContents["knowledge"]): string | null {
@@ -86,7 +91,9 @@ function summaryOf(contents: WorkspaceContents): string {
8691
else if (k?.kind === "selected") parts.push(plural(k.names?.length ?? k.selected, "knowledge document"))
8792
else if (k?.kind === "off") parts.push("knowledge off")
8893
if (contents.integrations !== null)
89-
parts.push(contents.integrations.length === 0 ? "no integrations" : `integrations: ${contents.integrations.join(", ")}`)
94+
parts.push(
95+
contents.integrations.length === 0 ? "no integrations" : `integrations: ${contents.integrations.join(", ")}`,
96+
)
9097
if (contents.memoryEnabled !== null) parts.push(contents.memoryEnabled ? "memory on" : "memory off")
9198
return parts.join(" · ")
9299
}
@@ -138,10 +145,33 @@ export function wrapStarter(text: string, width: number): string {
138145
}
139146

140147
/** The starter for a linked project. `directory` is the project key the link was recorded under. */
148+
// altimate_change start — the starter is the first thing a user sees of the workspace in a tool
149+
/** `starter_shown`, with what the workspace provides as counts. Never throws. */
150+
export function trackStarterShown(workspaceId: number, contents: WorkspaceContents): void {
151+
try {
152+
Telemetry.track({
153+
type: "starter_shown",
154+
timestamp: Date.now(),
155+
session_id: Telemetry.getContext().sessionId,
156+
workspace_id: workspaceId,
157+
skills: Array.isArray(contents.skills) ? contents.skills.length : 0,
158+
integrations: Array.isArray(contents.integrations) ? contents.integrations.length : 0,
159+
knowledge: contents.knowledge?.kind ?? "unknown",
160+
memory_enabled: contents.memoryEnabled === true,
161+
})
162+
} catch {
163+
// telemetry never blocks the starter
164+
}
165+
}
166+
// altimate_change end
167+
141168
export async function starterFor(
142169
directory: string,
143170
binding: { datamateId: number; datamateName: string },
144171
waitMs = STARTER_WAIT_MS,
145172
): Promise<Starter> {
146-
return renderStarter(binding.datamateName, await workspaceContents(directory, binding.datamateId, waitMs))
173+
const contents = await workspaceContents(directory, binding.datamateId, waitMs)
174+
const starter = renderStarter(binding.datamateName, contents)
175+
trackStarterShown(binding.datamateId, contents)
176+
return starter
147177
}

0 commit comments

Comments
 (0)