Skip to content

an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions

fix(telemetry): scope the transcript usage fallback to the agent's own session (D11)

Ed Chan06879a3c9fb6
diff --git a/package.json b/package.json
index 58ddf586..7706850b 100644
--- a/package.json
+++ b/package.json
@@ -19,7 +19,7 @@
"typecheck:node": "tsc --noEmit -p tsconfig.node.json",
"typecheck:web": "tsc --noEmit -p tsconfig.web.json",
"typecheck": "npm run typecheck:node && npm run typecheck:web",
- "test:focused": "node --test test/codex-remote.test.cjs test/control.test.cjs test/provider-automation.test.cjs test/provider-config.test.cjs test/queue-delivery.test.cjs test/terminal-automation.test.cjs test/terminal-recovery.test.cjs test/roster.test.cjs test/proc-kill.test.cjs test/expand-tilde.test.cjs test/hive-cwd.test.cjs test/hive-hook-node.test.cjs test/hive-roster-injection.test.cjs test/hive-runtime-path.test.cjs test/cli-install-ladder.test.cjs test/node-install.test.cjs test/office-gl-recovery.test.cjs test/update-state.test.cjs test/transcript-project-dir.test.cjs test/commit-graph.test.cjs test/task-ledger.test.cjs test/memory-rearm.test.cjs test/worker-permission-parity.test.cjs",
+ "test:focused": "node --test test/codex-remote.test.cjs test/control.test.cjs test/provider-automation.test.cjs test/provider-config.test.cjs test/queue-delivery.test.cjs test/terminal-automation.test.cjs test/terminal-recovery.test.cjs test/roster.test.cjs test/proc-kill.test.cjs test/expand-tilde.test.cjs test/hive-cwd.test.cjs test/hive-hook-node.test.cjs test/hive-roster-injection.test.cjs test/hive-runtime-path.test.cjs test/cli-install-ladder.test.cjs test/node-install.test.cjs test/office-gl-recovery.test.cjs test/update-state.test.cjs test/transcript-project-dir.test.cjs test/commit-graph.test.cjs test/task-ledger.test.cjs test/memory-rearm.test.cjs test/worker-permission-parity.test.cjs test/telemetry-session-fallback.test.cjs",
"check:links": "node tools/check-release-links.cjs",
"dist": "npm run build && electron-builder",
"dist:mac": "npm run build && electron-builder --mac",
diff --git a/src/main/index.ts b/src/main/index.ts
index dba2b9a8..2a8393f1 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -236,7 +236,10 @@ const control = new ControlRegistry();
// lets the transcript fallback find an agent's cwd from the hive registry.
const telemetry = new TelemetryCollector({
emit: (channel, payload) => { try { liveWebContents()?.send(channel, payload); } catch { /* window tore down */ } },
- resolveCwd: (agentId) => hive.registry().agents[agentId]?.cwd ?? null
+ resolveCwd: (agentId) => hive.registry().agents[agentId]?.cwd ?? null,
+ // D11: scopes the transcript fallback to this agent's own session instead of
+ // summing every transcript in a (routinely shared) cwd.
+ resolveSessionId: (agentId) => hive.lastSession(agentId)
});
// Usage provider (Seam 1) — the INTEGRATION swap: Oscar's telemetry collector (#7)
// IS the provider, replacing Lane A's interim StubUsageProvider. Same
diff --git a/src/main/telemetry.ts b/src/main/telemetry.ts
index 2b71fcc3..d0c0928e 100644
--- a/src/main/telemetry.ts
+++ b/src/main/telemetry.ts
@@ -116,6 +116,13 @@ export interface TelemetryCollectorOptions {
emit?: (channel: string, payload: unknown) => void;
/** Resolve an agent's cwd (from the hive registry) for the transcript fallback. */
resolveCwd?: (agentId: string) => string | null;
+ /** Resolve an agent's CURRENT Claude Code session id (from the hive registry)
+ * for the transcript fallback (D11). Without this, the fallback can only
+ * fall back further to summing every `.jsonl` ever written in the agent's
+ * cwd — correct for a lone agent in its own directory, but a shared/reused
+ * cwd (the common case for hive workers) pulls in every other agent's and
+ * every past session's history too. */
+ resolveSessionId?: (agentId: string) => string | undefined;
}
export class TelemetryCollector {
@@ -125,6 +132,7 @@ export class TelemetryCollector {
private readonly port: number;
private readonly emit?: (channel: string, payload: unknown) => void;
private readonly resolveCwd?: (agentId: string) => string | null;
+ private readonly resolveSessionId?: (agentId: string) => string | undefined;
/** sessionId → running accumulation. */
private readonly sessions = new Map<string, SessionAccum>();
@@ -143,6 +151,7 @@ export class TelemetryCollector {
this.port = opts.port ?? 0;
this.emit = opts.emit;
this.resolveCwd = opts.resolveCwd;
+ this.resolveSessionId = opts.resolveSessionId;
}
/** Bind the loopback OTLP listener. The handler is live the instant this
@@ -386,10 +395,27 @@ export class TelemetryCollector {
return out;
}
+ /** D11: a cwd is routinely SHARED — every hive worker spawned against the same
+ * repo (isolation is best-effort and several call sites deliberately skip it)
+ * lands in the same `~/.claude/projects/<key>/` directory as every other
+ * agent that ever ran there, this app-run or a past one. Without a session
+ * filter, `readAgentUsage` sums ALL of it — confirmed live: a probe worker
+ * with zero LLM calls was reaped citing 143,369,766 tokens, which was in fact
+ * the shared project directory's entire history across other agents'
+ * sessions, not its own. So this fallback now filters to the agent's OWN
+ * current session id and, when that id isn't known yet (nothing has hooked
+ * in for this agent this run — the true zero-usage case for a just-spawned
+ * agent), reports "no data" rather than someone else's history. `sessionId`
+ * stays '' on the returned sample regardless — deliberately, so it keeps
+ * reading as "no live session" to the #56 cost-ledger dedup gate in
+ * index.ts (`if (sample?.sessionId) appendCostLedger(...)`); the resolved id
+ * is used only to filter the read, never surfaced as if it were live OTel. */
private transcriptFallback(agentId: string): AgentUsageSample | null {
const cwd = this.resolveCwd?.(agentId);
if (!cwd) return null;
- const u = readAgentUsage(cwd);
+ const sessionId = this.resolveSessionId?.(agentId);
+ if (!sessionId) return null;
+ const u = readAgentUsage(cwd, { sessionId });
if (!u.inputTokens && !u.outputTokens && !u.cacheReadTokens && !u.cacheWriteTokens) return null;
return {
agentId,
diff --git a/test/telemetry-session-fallback.test.cjs b/test/telemetry-session-fallback.test.cjs
new file mode 100644
index 00000000..e375b5d6
--- /dev/null
+++ b/test/telemetry-session-fallback.test.cjs
@@ -0,0 +1,106 @@
+'use strict';
+
+/**
+ * D11 — the circuit breaker's per-agent token cap is measured off
+ * TelemetryCollector.getAgentUsage(), which falls back to transcriptFallback()
+ * whenever an agent has no live OTel data yet (a just-spawned agent, above all).
+ * That fallback called readAgentUsage(cwd) with NO session filter, so it summed
+ * every `.jsonl` transcript ever written in that cwd's Claude project directory
+ * — every other agent's history, and every past run's, not just this agent's.
+ *
+ * Confirmed live and reproduced here: a probe worker with zero LLM calls was
+ * reaped citing 143,369,766 tokens (an equivalent worker's real usage, per
+ * fleet.json, was 251,901) because its cwd was a long-lived shared repo whose
+ * project directory carried tens of millions of tokens from unrelated past
+ * sessions. The fix scopes the fallback to the agent's OWN current session id
+ * (resolveSessionId, wired to hive.lastSession in index.ts) and, when no
+ * session id is known yet for that agent (the true zero-usage instant right
+ * after spawn), reports no data rather than someone else's history.
+ *
+ * Sandboxes HOME so projectDir() maps into a throwaway dir, never the real
+ * ~/.claude/projects (mirrors test/transcript-usage.test.cjs).
+ */
+
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const fs = require('node:fs');
+const os = require('node:os');
+const path = require('node:path');
+
+const FAKE_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'home-'));
+process.env.HOME = FAKE_HOME;
+process.env.USERPROFILE = FAKE_HOME;
+
+const loadTs = require('./load-ts.cjs');
+const { TelemetryCollector } = loadTs('src/main/telemetry.ts');
+const { projectDir, readAgentUsage } = loadTs('src/main/transcript.ts');
+
+function assistantRecord(sessionId, outputTokens) {
+ return JSON.stringify({
+ type: 'assistant',
+ sessionId,
+ message: {
+ model: 'claude-sonnet-5',
+ usage: {
+ input_tokens: 100,
+ output_tokens: outputTokens,
+ cache_read_input_tokens: outputTokens * 50, // dominates the total, like real turns do
+ cache_creation_input_tokens: 0
+ }
+ }
+ });
+}
+
+/** A fresh cwd with a project dir standing in for a long-lived, shared repo:
+ * one foreign/older session already left tens of millions of tokens behind. */
+function makeSharedProject() {
+ const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'proj-'));
+ const dir = projectDir(cwd);
+ fs.mkdirSync(dir, { recursive: true });
+ const foreignLines = Array.from({ length: 20 }, () => assistantRecord('foreign-session', 50_000));
+ fs.writeFileSync(path.join(dir, 'foreign-session.jsonl'), `${foreignLines.join('\n')}\n`);
+ return { cwd, dir };
+}
+
+test('D11: pre-fix fingerprint — an unfiltered read sums a shared cwd\'s whole history', () => {
+ const { cwd } = makeSharedProject();
+ const legacy = readAgentUsage(cwd); // no sessionId filter — the old transcriptFallback call
+ assert.equal(legacy.outputTokens, 1_000_000, 'the foreign session alone should already be huge');
+});
+
+test('D11: transcript fallback attributes only the agent\'s own session, not the shared cwd\'s history', () => {
+ const { cwd, dir } = makeSharedProject();
+ fs.writeFileSync(path.join(dir, 'my-session.jsonl'), `${assistantRecord('my-session', 10)}\n`);
+
+ const telemetry = new TelemetryCollector({
+ resolveCwd: () => cwd,
+ resolveSessionId: () => 'my-session'
+ });
+ const sample = telemetry.getAgentUsage('worker-x');
+ assert.ok(sample, 'expected a sample from the agent\'s own transcript');
+ assert.equal(sample.output, 10, 'must not include the foreign session\'s tokens');
+});
+
+test('D11: with no session id known yet, a brand-new agent reports no usage instead of the shared cwd\'s history', () => {
+ const { cwd } = makeSharedProject();
+
+ const telemetry = new TelemetryCollector({
+ resolveCwd: () => cwd,
+ resolveSessionId: () => undefined // no hook has fired for this agent yet
+ });
+ // Pre-fix this returned the foreign session's ~1,000,000-token pseudo-total —
+ // the exact shape of the "reaped citing 143,369,766 tokens" incident.
+ assert.equal(telemetry.getAgentUsage('worker-x'), null);
+});
+
+test('D11: a returned fallback sample keeps sessionId empty (preserves the #56 cost-ledger dedup gate)', () => {
+ const { cwd, dir } = makeSharedProject();
+ fs.writeFileSync(path.join(dir, 'my-session.jsonl'), `${assistantRecord('my-session', 10)}\n`);
+
+ const telemetry = new TelemetryCollector({
+ resolveCwd: () => cwd,
+ resolveSessionId: () => 'my-session'
+ });
+ const sample = telemetry.getAgentUsage('worker-x');
+ assert.equal(sample.sessionId, '', 'a transcript-fallback sample must never look like a live OTel session');
+});