Skip to content

Commit 88d648a

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 51bca9e commit 88d648a

16 files changed

Lines changed: 487 additions & 112 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"
@@ -801,6 +838,8 @@ export namespace Telemetry {
801838
file_count: number
802839
// "serve": published from the IDE extension over the serve route
803840
source: "cli" | "tui" | "serve"
841+
// altimate_change — the workspace the skill was attached to
842+
workspace_id?: number
804843
}
805844
// altimate_change end
806845
// altimate_change start — plan refinement telemetry event
@@ -914,6 +953,8 @@ export namespace Telemetry {
914953
duration_ms: number
915954
/** Last tool category the agent used (or "none") */
916955
last_tool_category: string
956+
// altimate_change — true when the session had used workspace skills, memory, documents or integrations
957+
workspace_context_used?: boolean
917958
}
918959
// task intent classification for understanding DE problem distribution
919960
| {
@@ -2183,6 +2224,26 @@ export namespace Telemetry {
21832224
}
21842225
}
21852226

2227+
// altimate_change start — per-session use of the bound workspace. The skill, memory and tool
2228+
// emit sites note what they used; session_end reports the counts and task_outcome_signal
2229+
// whether any workspace context was in play. Keyed by session so a serve process with several
2230+
// sessions keeps them apart; cleared when the session ends.
2231+
export type WorkspaceUse = { skills: number; documents: number; memory: number; integrations: number }
2232+
const workspaceUse = new Map<string, WorkspaceUse>()
2233+
export function noteWorkspaceUse(sessionId: string, kind: keyof WorkspaceUse, n = 1) {
2234+
if (!sessionId || n <= 0) return
2235+
const use = workspaceUse.get(sessionId) ?? { skills: 0, documents: 0, memory: 0, integrations: 0 }
2236+
use[kind] += n
2237+
workspaceUse.set(sessionId, use)
2238+
}
2239+
export function workspaceUseOf(sessionId: string): WorkspaceUse {
2240+
return { skills: 0, documents: 0, memory: 0, integrations: 0, ...workspaceUse.get(sessionId) }
2241+
}
2242+
export function clearWorkspaceUse(sessionId: string) {
2243+
workspaceUse.delete(sessionId)
2244+
}
2245+
// altimate_change end
2246+
21862247
export function setContext(opts: { sessionId: string; projectId: string; workspaceId?: number }) {
21872248
sessionId = opts.sessionId
21882249
projectId = opts.projectId

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

Lines changed: 39 additions & 39 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"
@@ -332,11 +333,7 @@ function stripTrainingMeta(content: string): string {
332333
/** Fingerprint of everything about a block that reaches the store. Tags and
333334
* expiry are included because both are mirrored. */
334335
function contentHash(block: MemoryBlock): string {
335-
const payload = JSON.stringify([
336-
stripTrainingMeta(block.content),
337-
[...block.tags].sort(),
338-
block.expires ?? "",
339-
])
336+
const payload = JSON.stringify([stripTrainingMeta(block.content), [...block.tags].sort(), block.expires ?? ""])
340337
// sha-256, not a 32-bit non-cryptographic hash. This value is the single
341338
// gate deciding whether a save is sent at all: on a collision ``push``
342339
// returns "unchanged" and a real edit is silently never mirrored, with no
@@ -656,7 +653,9 @@ async function reapArchivedBlocks(
656653
try {
657654
const gone = syncInternals.removeBlock
658655
? await syncInternals.removeBlock(scope, blockId, directory ?? undefined, block.updated)
659-
: await (await import("@/memory/store")).MemoryStore.remove(scope, blockId, directory ?? undefined, {
656+
: await (
657+
await import("@/memory/store")
658+
).MemoryStore.remove(scope, blockId, directory ?? undefined, {
660659
expectUpdated: block.updated,
661660
})
662661
if (gone) {
@@ -724,9 +723,7 @@ async function push(
724723
// user recreated under an old id gets a fresh record instead of un-archiving
725724
// the old one (`MemoryApi.update` replaces metadata wholesale, which would
726725
// drop the archived marker).
727-
const indexed = existing?.memoryId
728-
? view.records.find((r) => r.id === existing.memoryId)
729-
: undefined
726+
const indexed = existing?.memoryId ? view.records.find((r) => r.id === existing.memoryId) : undefined
730727
// A record the index names but a COMPLETE record set does not hold is gone —
731728
// deleted in the web app, by another client, or by a wipe. Absence used to
732729
// read as "still ours", so `match` stayed pointing at a dead id and every
@@ -740,7 +737,8 @@ async function push(
740737
// that already has one — the duplicate this function works hardest to avoid.
741738
const indexedGone = existing?.memoryId !== undefined && indexed === undefined && !view.truncated
742739
const indexUsable = existing?.memoryId && !indexedGone && (!indexed || !isArchived(indexed))
743-
const match = (indexUsable ? existing?.memoryId : undefined) ?? view.records.find((r) => isSameBlock(r, block, binding))?.id
740+
const match =
741+
(indexUsable ? existing?.memoryId : undefined) ?? view.records.find((r) => isSameBlock(r, block, binding))?.id
744742
if (match) {
745743
// Refuse to move a record backwards. Two machines editing the same block,
746744
// or a stale clone running a sweep, would otherwise overwrite a newer cloud
@@ -759,6 +757,7 @@ async function push(
759757
}
760758
await MemoryApi.update(match, block.content, metadata)
761759
await recordIndexEntry(key, { memoryId: match, contentHash: hash, syncedAt: Date.now() })
760+
if (binding) trackContributed(binding)
762761
return "stored"
763762
}
764763

@@ -793,9 +792,26 @@ async function push(
793792
log.warn("archiving an extra record produced by one create", { id: block.id, memoryId: extra })
794793
await MemoryApi.update(extra, "", { ...metadata, archived: "true", archived_at: new Date().toISOString() })
795794
}
795+
if (binding) trackContributed(binding)
796796
return "stored"
797797
}
798798

799+
// altimate_change start — a successful write-back is the workspace getting something back
800+
function trackContributed(binding: CachedBinding) {
801+
try {
802+
Telemetry.track({
803+
type: "workspace_memory_contributed",
804+
timestamp: Date.now(),
805+
session_id: Telemetry.getContext().sessionId,
806+
workspace_id: binding.datamateId,
807+
block_count: 1,
808+
})
809+
} catch {
810+
// telemetry never blocks a mirror
811+
}
812+
}
813+
// altimate_change end
814+
799815
/** Cloud operations for one logical block, run one at a time.
800816
*
801817
* Both callers are fire-and-forget from the store's write and delete paths, so
@@ -892,11 +908,7 @@ export async function mirrorBlock(block: MemoryBlock, directory?: string): Promi
892908
/** Archive a block's cloud record rather than deleting it, so the workspace
893909
* keeps the history. Only this client filters the marker — other readers do
894910
* not — so an archived record stays visible elsewhere. */
895-
export async function archiveBlock(
896-
scope: "global" | "project",
897-
blockId: string,
898-
directory?: string,
899-
): Promise<void> {
911+
export async function archiveBlock(scope: "global" | "project", blockId: string, directory?: string): Promise<void> {
900912
if (!isEnabled()) return
901913
// Queued behind any in-flight mirror for the same block, so a delete cannot
902914
// run before the create it is meant to undo. The binding lookup happens
@@ -915,11 +927,7 @@ export async function archiveBlock(
915927
)
916928
}
917929

918-
async function archiveNow(
919-
scope: "global" | "project",
920-
blockId: string,
921-
binding: CachedBinding,
922-
): Promise<void> {
930+
async function archiveNow(scope: "global" | "project", blockId: string, binding: CachedBinding): Promise<void> {
923931
const key = indexKey({
924932
scope,
925933
blockId,
@@ -1082,9 +1090,7 @@ async function pollMemoKey(binding: CachedBinding): Promise<string> {
10821090
*
10831091
* An "unknown" is never memoized: the next tick should ask again rather than
10841092
* inherit a network blip. */
1085-
export async function memoryEnabledForPoller(
1086-
binding: CachedBinding,
1087-
): Promise<"enabled" | "disabled" | "unknown"> {
1093+
export async function memoryEnabledForPoller(binding: CachedBinding): Promise<"enabled" | "disabled" | "unknown"> {
10881094
// The scoped memo is the poller's only cache. `memoryEnabledCache` is keyed by
10891095
// bare workspace id — right for the write path, which is tenant-bound by its
10901096
// credentials — but consulting it here reopened a 60s cross-tenant window the
@@ -1093,8 +1099,7 @@ export async function memoryEnabledForPoller(
10931099
// minutes per tenant is the price, and `memoryStatus` still warms both.
10941100
const key = await pollMemoKey(binding)
10951101
const memo = pollMemo.get(key)
1096-
if (memo && Date.now() - memo.at < (memo.status === "unknown" ? POLL_UNKNOWN_TTL_MS : POLL_TTL_MS))
1097-
return memo.status
1102+
if (memo && Date.now() - memo.at < (memo.status === "unknown" ? POLL_UNKNOWN_TTL_MS : POLL_TTL_MS)) return memo.status
10981103
// The in-flight ask is memoized too, not only the settled answer. Two
10991104
// refreshes overlapping on a cold memo — a remount while a slow one is
11001105
// still out — both saw it empty and both put a request on the wire.
@@ -1229,7 +1234,8 @@ export async function backfill(
12291234
// ``gated`` says the sweep never ran, as opposed to running and storing
12301235
// nothing. A caller recording "this binding is seeded" must be able to tell
12311236
// those apart: memory being off is not a completed seed.
1232-
if (!isEnabled()) return { ok: 0, failed: 0, skipped: 0, declined: 0, deferred: 0, gated: true, gateReason: "local-off" }
1237+
if (!isEnabled())
1238+
return { ok: 0, failed: 0, skipped: 0, declined: 0, deferred: 0, gated: true, gateReason: "local-off" }
12331239
// The bind path passes the binding it just recorded; there is no ambient
12341240
// instance to resolve one from on the `link` subcommand.
12351241
const binding = explicitBinding ?? (await currentBinding())
@@ -1259,10 +1265,7 @@ export async function backfill(
12591265
log.info("workspace memory backfill starting", { pending: pending.length, skipped })
12601266
const result = await runQueue(
12611267
pending,
1262-
(item) =>
1263-
serialize(item.block.scope, item.block.id, () =>
1264-
push(item.block, item.binding, known, sweepDirectory),
1265-
),
1268+
(item) => serialize(item.block.scope, item.block.id, () => push(item.block, item.binding, known, sweepDirectory)),
12661269
BACKFILL_CONCURRENCY,
12671270
)
12681271
// `skipped` combines blocks filtered before the queue (already synced, or
@@ -1277,7 +1280,10 @@ export async function backfill(
12771280
function originLabel(meta: Record<string, unknown>): string | undefined {
12781281
const remote = typeof meta.repo_remote === "string" ? meta.repo_remote : undefined
12791282
if (remote) {
1280-
const trimmed = remote.replace(/[/]+$/, "").replace(/\.git$/, "").replace(/[/]+$/, "")
1283+
const trimmed = remote
1284+
.replace(/[/]+$/, "")
1285+
.replace(/\.git$/, "")
1286+
.replace(/[/]+$/, "")
12811287
const last = trimmed.split(/[/:]/).pop()
12821288
if (last) return last
12831289
}
@@ -1291,10 +1297,7 @@ function originLabel(meta: Record<string, unknown>): string | undefined {
12911297

12921298
/** Map a cloud record back to an injectable block, or null if it is not a
12931299
* well-formed mirrored block. */
1294-
export function toBlock(
1295-
record: CloudMemoryRecord,
1296-
ownProjectKey: string | undefined,
1297-
): RemoteMemoryBlock | null {
1300+
export function toBlock(record: CloudMemoryRecord, ownProjectKey: string | undefined): RemoteMemoryBlock | null {
12981301
const meta = record.metadata
12991302
if (!meta || typeof meta !== "object") return null
13001303
const blockId = typeof meta.block_id === "string" ? meta.block_id : undefined
@@ -1368,10 +1371,7 @@ export async function hydrate(sessionID: string): Promise<void> {
13681371

13691372
/** Wait for a session's in-flight hydration, capped. Resolves immediately once
13701373
* the fetch has settled, so only the first injection of a session pays. */
1371-
export async function whenHydrated(
1372-
sessionID: string,
1373-
timeoutMs: number = HYDRATION_WAIT_MS,
1374-
): Promise<void> {
1374+
export async function whenHydrated(sessionID: string, timeoutMs: number = HYDRATION_WAIT_MS): Promise<void> {
13751375
const state = sessions.get(sessionID)
13761376
const pending = state?.hydration
13771377
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)