Skip to content

Commit 1c1c100

Browse files
anandgupta42claude
andcommitted
fix: [PR-968] address telemetry source-field review feedback
Resolves review findings on the telemetry `source` field: - Read `Flag.ALTIMATE_CLI_CLIENT` live at envelope-build time instead of a hardcoded `clientSource = "cli"` set only inside the prompt flow. Events emitted before the first prompt (startup, connection setup, standalone CLI subcommands) are now labelled with the real client instead of defaulting to `"cli"`. Removes the now-redundant `clientSource` variable, `setSource()`, and its call in `session/prompt.ts`. - Type-guard `session.metadata?.source` in the `session_start` emit (was an unchecked `as string`) so malformed client JSON falls back to the flag instead of being routed to `measurements` downstream. - Document `Session.Info.metadata` as an intentional open extension point. - Add tests: telemetry envelope `source` behaviour (env-flag default, `"cli"` fallback, per-event override, non-string routing contract) and session `metadata` round-trip through `create` -> `get`. Comments clarified per Codex review: events that carry their own `source` field (session_start, and pre-existing `skill_*`/connections events) override the injected process-level value. Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent d808982 commit 1c1c100

5 files changed

Lines changed: 192 additions & 10 deletions

File tree

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Account } from "@/account"
22
import { Config } from "@/config/config"
3+
import { Flag } from "@/flag/flag"
34
import { Installation } from "@/installation"
45
import { Log } from "@/util/log"
56
import { createHash, randomUUID } from "crypto"
@@ -1201,7 +1202,6 @@ export namespace Telemetry {
12011202
let machineId = ""
12021203
let sessionId = ""
12031204
let projectId = ""
1204-
let clientSource = "cli"
12051205
let appInsights: AppInsightsConfig | undefined
12061206
let droppedEvents = 0
12071207
let initPromise: Promise<void> | undefined
@@ -1222,6 +1222,14 @@ export namespace Telemetry {
12221222
}
12231223

12241224
function toAppInsightsEnvelopes(events: Event[], cfg: AppInsightsConfig): object[] {
1225+
// Process-level client, read live from the env flag (defaults to "cli") so events
1226+
// emitted before any prompt runs — startup, connection setup, standalone CLI
1227+
// subcommands — are labelled correctly instead of stuck at a hardcoded default.
1228+
// Any event that already carries its own `source` field overrides this via the field
1229+
// loop below — session_start (the per-session client, from session.metadata.source) and
1230+
// a few pre-existing events (skill_*, connections) whose `source` means something else
1231+
// (cli/tui, connection origin). Events without their own `source` report this value.
1232+
const clientSource = Flag.ALTIMATE_CLI_CLIENT
12251233
return events.map((event) => {
12261234
const { type, timestamp, ...fields } = event as any
12271235
const sid: string = fields.session_id ?? sessionId
@@ -1351,10 +1359,6 @@ export namespace Telemetry {
13511359
projectId = opts.projectId
13521360
}
13531361

1354-
export function setSource(s: string): void {
1355-
clientSource = s
1356-
}
1357-
13581362
export function getContext() {
13591363
return { sessionId, projectId }
13601364
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ export namespace Session {
151151
archived: z.number().optional(),
152152
}),
153153
permission: PermissionNext.Ruleset.optional(),
154+
// Open extension point for clients (e.g. VS Code extensions) to tag a session via
155+
// POST /session. Only `source` is consumed today (session_start telemetry); kept as an
156+
// open record so new keys need no schema change.
154157
metadata: z.record(z.string(), z.unknown()).optional(),
155158
revert: z
156159
.object({

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ export namespace SessionPrompt {
362362
// altimate_change end
363363
// altimate_change start — session telemetry tracking
364364
await Telemetry.init()
365-
Telemetry.setSource(Flag.ALTIMATE_CLI_CLIENT)
366365
Telemetry.setContext({ sessionId: sessionID, projectId: Instance.project?.id ?? "" })
367366
const sessionStartTime = Date.now()
368367
let sessionTotalCost = 0
@@ -926,10 +925,15 @@ export namespace SessionPrompt {
926925
// altimate_change start — per-session source override: VS Code extensions set
927926
// metadata.source (e.g. "datamates", "poweruser") when creating the session via POST /session.
928927
// This lets both extensions share the same altimate serve process while producing
929-
// distinguishable session_start telemetry. Falls back to the process-level
930-
// Flag.ALTIMATE_CLI_CLIENT (set by ALTIMATE_CLI_CLIENT env var, e.g. "vscode") if
931-
// the session carries no metadata source.
932-
source: (session.metadata?.source as string | undefined) ?? Flag.ALTIMATE_CLI_CLIENT,
928+
// distinguishable session_start telemetry. session_start carries this per-session value;
929+
// events without their own `source` fall back to the process-level Flag.ALTIMATE_CLI_CLIENT
930+
// injected in telemetry, so per-extension attribution of those events requires a join on
931+
// session_id. metadata is arbitrary client JSON, so type-guard source and fall back to the
932+
// flag if it is absent or not a string (a non-string would otherwise be routed to
933+
// measurements downstream instead of appearing as the source property).
934+
source:
935+
(typeof session.metadata?.source === "string" ? session.metadata.source : undefined) ??
936+
Flag.ALTIMATE_CLI_CLIENT,
933937
// altimate_change end
934938
})
935939
// altimate_change start — task intent classification (keyword/regex, zero LLM cost)

‎packages/opencode/test/session/session.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,35 @@ describe("step-finish token propagation via Bus event", () => {
140140
{ timeout: 30000 },
141141
)
142142
})
143+
144+
describe("session metadata", () => {
145+
test("persists and reads back metadata.source through create -> get", async () => {
146+
await Instance.provide({
147+
directory: projectRoot,
148+
fn: async () => {
149+
const session = await Session.create({ metadata: { source: "datamates" } })
150+
expect(session.metadata).toEqual({ source: "datamates" })
151+
152+
const loaded = await Session.get(session.id)
153+
expect(loaded.metadata).toEqual({ source: "datamates" })
154+
155+
await Session.remove(session.id)
156+
},
157+
})
158+
})
159+
160+
test("metadata is undefined when not provided", async () => {
161+
await Instance.provide({
162+
directory: projectRoot,
163+
fn: async () => {
164+
const session = await Session.create({})
165+
expect(session.metadata).toBeUndefined()
166+
167+
const loaded = await Session.get(session.id)
168+
expect(loaded.metadata).toBeUndefined()
169+
170+
await Session.remove(session.id)
171+
},
172+
})
173+
})
174+
})

‎packages/opencode/test/telemetry/telemetry.test.ts‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,145 @@ describe("telemetry.toAppInsightsEnvelopes (indirect)", () => {
715715
cleanup()
716716
}
717717
})
718+
719+
// --- source field (ALTIMATE_CLI_CLIENT injection + per-event override) ---
720+
721+
function withClientEnv(value: string | undefined, fn: () => Promise<void>): Promise<void> {
722+
const origAcc = process.env.ALTIMATE_CLI_CLIENT
723+
const origOc = process.env.OPENCODE_CLIENT
724+
delete process.env.OPENCODE_CLIENT
725+
if (value === undefined) delete process.env.ALTIMATE_CLI_CLIENT
726+
else process.env.ALTIMATE_CLI_CLIENT = value
727+
return fn().finally(() => {
728+
if (origAcc !== undefined) process.env.ALTIMATE_CLI_CLIENT = origAcc
729+
else delete process.env.ALTIMATE_CLI_CLIENT
730+
if (origOc !== undefined) process.env.OPENCODE_CLIENT = origOc
731+
else delete process.env.OPENCODE_CLIENT
732+
})
733+
}
734+
735+
test("source on a generic event reflects the ALTIMATE_CLI_CLIENT env flag", async () => {
736+
await withClientEnv("vscode", async () => {
737+
const { fetchCalls, cleanup } = await initWithMockedFetch()
738+
try {
739+
Telemetry.track({
740+
type: "session_end",
741+
timestamp: 1700000000000,
742+
session_id: "sess-1",
743+
total_cost: 0.05,
744+
total_tokens: 1500,
745+
tool_call_count: 10,
746+
duration_ms: 30000,
747+
})
748+
await Telemetry.flush()
749+
expect(JSON.parse(fetchCalls[0].body)[0].data.baseData.properties.source).toBe("vscode")
750+
} finally {
751+
cleanup()
752+
}
753+
})
754+
})
755+
756+
test('source defaults to "cli" when no client env is set', async () => {
757+
await withClientEnv(undefined, async () => {
758+
const { fetchCalls, cleanup } = await initWithMockedFetch()
759+
try {
760+
Telemetry.track({
761+
type: "session_end",
762+
timestamp: 1700000000000,
763+
session_id: "sess-1",
764+
total_cost: 0.01,
765+
total_tokens: 10,
766+
tool_call_count: 1,
767+
duration_ms: 100,
768+
})
769+
await Telemetry.flush()
770+
expect(JSON.parse(fetchCalls[0].body)[0].data.baseData.properties.source).toBe("cli")
771+
} finally {
772+
cleanup()
773+
}
774+
})
775+
})
776+
777+
test("per-event source (session_start) overrides the process-level flag", async () => {
778+
await withClientEnv("vscode", async () => {
779+
const { fetchCalls, cleanup } = await initWithMockedFetch()
780+
try {
781+
Telemetry.track({
782+
type: "session_start",
783+
timestamp: 1700000000000,
784+
session_id: "sess-1",
785+
model_id: "m",
786+
provider_id: "p",
787+
agent: "a",
788+
project_id: "proj",
789+
os: "linux",
790+
arch: "x64",
791+
node_version: "v22.0.0",
792+
source: "datamates",
793+
})
794+
await Telemetry.flush()
795+
// Event-level source wins over the global "vscode" flag.
796+
expect(JSON.parse(fetchCalls[0].body)[0].data.baseData.properties.source).toBe("datamates")
797+
} finally {
798+
cleanup()
799+
}
800+
})
801+
})
802+
803+
test("session_start without a source falls back to the process-level flag", async () => {
804+
await withClientEnv("vscode", async () => {
805+
const { fetchCalls, cleanup } = await initWithMockedFetch()
806+
try {
807+
Telemetry.track({
808+
type: "session_start",
809+
timestamp: 1700000000000,
810+
session_id: "sess-1",
811+
model_id: "m",
812+
provider_id: "p",
813+
agent: "a",
814+
project_id: "proj",
815+
os: "linux",
816+
arch: "x64",
817+
node_version: "v22.0.0",
818+
})
819+
await Telemetry.flush()
820+
expect(JSON.parse(fetchCalls[0].body)[0].data.baseData.properties.source).toBe("vscode")
821+
} finally {
822+
cleanup()
823+
}
824+
})
825+
})
826+
827+
test("a non-string source is routed to measurements, not properties (why session_start type-guards it)", async () => {
828+
await withClientEnv("vscode", async () => {
829+
const { fetchCalls, cleanup } = await initWithMockedFetch()
830+
try {
831+
// Simulates a malformed per-event source reaching the envelope (e.g. if the prompt
832+
// guard were removed and a client passed metadata.source as a number). The field loop
833+
// routes numbers to measurements, so `properties.source` would silently retain the
834+
// injected process-level flag rather than the intended value — hence the guard.
835+
Telemetry.track({
836+
type: "session_start",
837+
timestamp: 1700000000000,
838+
session_id: "sess-1",
839+
model_id: "m",
840+
provider_id: "p",
841+
agent: "a",
842+
project_id: "proj",
843+
os: "linux",
844+
arch: "x64",
845+
node_version: "v22.0.0",
846+
source: 42 as unknown as string,
847+
})
848+
await Telemetry.flush()
849+
const baseData = JSON.parse(fetchCalls[0].body)[0].data.baseData
850+
expect(baseData.measurements.source).toBe(42)
851+
expect(baseData.properties.source).toBe("vscode")
852+
} finally {
853+
cleanup()
854+
}
855+
})
856+
})
718857
})
719858

720859
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)