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
HTML agent-harnessagent-orchestrationagentsai-agentsautonomous-agentsclaude-codecodexcoding-agentsdesktop-appdeveloper-toolselectrongemini-cliharnessharness-engineeringlocal-firstmemorymulti-agentopencodeorchestrationtypescript
feat(7A): live OTel telemetry collector + usage-provider seam + per-model cost
Chaitanya Girifd05989cd782
diff --git a/src/main/hive.ts b/src/main/hive.tsindex 00a4646b..f041cfce 100644--- a/src/main/hive.ts+++ b/src/main/hive.ts@@ -137,6 +137,20 @@ export class HiveManager {private routerTimer: NodeJS.Timeout | null = null;+ /** The embedded OTLP collector's loopback URL, set by the main process once the+ * collector is bound (telemetry.ts). null = telemetry off → no OTel env is+ * injected at spawn (the transcript reconciler remains the cost source). */+ private _otelEndpoint: string | null = null;+ /** Point newly-spawned agents at the live telemetry collector. Call after the+ * collector starts; only affects spawns made afterwards. */+ setOtelEndpoint(url: string | null): void {+ this._otelEndpoint = url;+ }+ /** The collector URL agents are pointed at, or null when telemetry is off. */+ otelEndpoint(): string | null {+ return this._otelEndpoint;+ }+// — paths —root(): string | null {const home = this.getHome();@@ -250,6 +264,24 @@ export class HiveManager {HIVE_ROOT: root,AGENT_DIR: dir};++ // Stage 7A — first-party Claude Code telemetry → the embedded loopback OTLP+ // collector (telemetry.ts). Pure env, no --settings change. Only injected+ // once the collector is up (otelEndpoint set), so telemetry-off installs and+ // tests spawn exactly as before. http/json (no protobuf dep). The resource+ // attrs are the agent↔event join key the collector reads. NOTE: Claude Code+ // reads these at process start, so they apply only to NEW spawns — an agent+ // already running won't emit until respawned.+ if (this._otelEndpoint) {+ env.CLAUDE_CODE_ENABLE_TELEMETRY = '1';+ env.OTEL_METRICS_EXPORTER = 'otlp';+ env.OTEL_LOGS_EXPORTER = 'otlp';+ env.OTEL_EXPORTER_OTLP_PROTOCOL = 'http/json';+ env.OTEL_EXPORTER_OTLP_ENDPOINT = this._otelEndpoint;+ env.OTEL_METRIC_EXPORT_INTERVAL = '5000'; // 5s — near-live without spamming+ env.OTEL_LOGS_EXPORT_INTERVAL = '2000';+ env.OTEL_RESOURCE_ATTRIBUTES = `agent.id=${meta.id},agent.name=${meta.name}`;+ }const args = ['--append-system-prompt', this.injectedPrompt(meta, dir, root, opts.semanticMemory ?? false)];// Phase 1 — autonomy: attach lifecycle hooks via --settings (no edits to the@@ -301,7 +333,11 @@ export class HiveManager {PostToolUse: [entry('*')],UserPromptSubmit: [entry()],Notification: [entry()],- SessionStart: [entry()]+ SessionStart: [entry()],+ // #5C: surface mid-`/compact` so an agent boxing up its context reads as+ // 'compacting' on the floor instead of looking frozen.+ PreCompact: [entry()],+ PostCompact: [entry()]}};}diff --git a/src/main/index.ts b/src/main/index.tsindex c96a5e50..9ee39cd6 100644--- a/src/main/index.ts+++ b/src/main/index.ts@@ -19,6 +19,7 @@ import { enrichMessage } from './assistant';import { readAgentUsage } from './transcript';import { listIssues, listCIRuns } from './github';import { SlackWebhookServer } from './slack';+import { TelemetryCollector } from './telemetry';const isDev = !!process.env.ELECTRON_RENDERER_URL;const ptyManager = new PtyManager();@@ -30,6 +31,13 @@ const hive = new HiveManager((channel, payload) => { try { liveWebContents()?.send(channel, payload); } catch { /* window tore down */ } });const hookServer = new HookServer(hive, () => liveWebContents(), () => readConfig());+// Stage 7A — the live observability tap. Receives Claude Code's first-party OTel+// over loopback OTLP/JSON and exposes the locked usage-provider seam. resolveCwd+// 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+});const memory = new MemoryManager(() => readConfig().harnessHome,() => { const c = readConfig(); return { enabled: c.semanticMemory !== false, model: c.embeddingModel ?? 'minilm' }; }@@ -499,6 +507,7 @@ ipcMain.handle('app:confirmClose', () => {try { clearMissionTimers(); } catch (e) { console.error('[quit] clearMissionTimers:', e); }try { hive.stopRouter(); } catch (e) { console.error('[quit] stopRouter:', e); }try { hookServer.stop(); } catch (e) { console.error('[quit] hookServer.stop:', e); }+ try { telemetry.stop(); } catch (e) { console.error('[quit] telemetry.stop:', e); }try { stopSlackServer(); } catch (e) { console.error('[quit] slack.stop:', e); }try { memory.stop(); } catch (e) { console.error('[quit] memory.stop:', e); }try { ptyManager.killAll(); } catch (e) { console.error('[quit] killAll:', e); }@@ -515,6 +524,7 @@ ipcMain.handle('app:resetAll', () => {try { clearMissionTimers(); } catch (e) { console.error('[reset] clearMissionTimers:', e); }try { hive.stopRouter(); } catch (e) { console.error('[reset] stopRouter:', e); }try { hookServer.stop(); } catch (e) { console.error('[reset] hookServer.stop:', e); }+ try { telemetry.stop(); } catch (e) { console.error('[reset] telemetry.stop:', e); }try { stopSlackServer(); } catch (e) { console.error('[reset] slack.stop:', e); }try { memory.stop(); } catch (e) { console.error('[reset] memory.stop:', e); }try { ptyManager.killAll(); } catch (e) { console.error('[reset] killAll:', e); }@@ -534,9 +544,30 @@ ipcMain.handle('app:resetAll', () => {});// ─── IPC: token telemetry (real usage + est. cost from CC transcripts) ───────+// Reconciler/fallback path: per-cwd transcript sum, now priced PER MODEL (cost+// bug #1 fixed in pricing.ts). Kept for back-compat with the existing UsageRow.ipcMain.handle('hive:agentUsage', (_evt, cwd: unknown) =>typeof cwd === 'string' ? readAgentUsage(cwd) : null);+// ─── IPC: live telemetry (the OTel collector — the locked usage-provider seam) ─+// The fleet grid + span waterfall (#7B) read these; Lane A's breaker (#6)+// consumes getAgentUsage in-process via the provider, not over IPC.+ipcMain.handle('telemetry:usage', (_evt, agentId: unknown) =>+ typeof agentId === 'string' ? telemetry.getAgentUsage(agentId) : null);+ipcMain.handle('telemetry:spans', (_evt, agentId: unknown) =>+ typeof agentId === 'string' ? telemetry.getSpans(agentId) : []);+ipcMain.handle('telemetry:snapshot', () => telemetry.snapshot());++// ─── IPC: circuit-breaker state (Lane A #6 policy → this lane's avatars/meter) ─+// Lane A's breaker calls this with a BreakerState; we fan it out to the renderer+// on `control:breakerState`, where the avatar adapter gives it precedence over+// hook-derived status (#5C looping/zombie). Defined here so the channel exists+// before Jim's policy lands; he produces, this lane consumes.+ipcMain.handle('control:setBreakerState', (_evt, state: unknown) => {+ try { liveWebContents()?.send('control:breakerState', state); } catch { /* window tore down */ }+ return { ok: true };+});+// ─── IPC: scheduled missions (recurring auto-dispatch) ──────────────────────ipcMain.handle('missions:list', () => readConfig().missions ?? []);ipcMain.handle('missions:save', (_evt, missions) => {@@ -636,6 +667,13 @@ app.whenReady().then(() => {ensureDefaultMissions(); // one-time: seed the built-in hourly ops standupsyncMissions(); // arm recurring auto-dispatch missions now the router is livehookServer.start();+ // Bind the telemetry collector BEFORE the renderer spawns any agent, then+ // point the hive at it so every subsequent spawn is instrumented. Best-effort+ // — a bind failure just leaves telemetry off (transcript reconciler stays).+ void telemetry.start().then((r) => {+ if (r.ok && r.endpoint) { hive.setOtelEndpoint(r.endpoint); console.log('[telemetry] collector listening', r.endpoint); }+ else console.error('[telemetry] collector failed to start:', r.error);+ });memory.start(); // init shared palace + mine loop (no-op without mempalace)}createWindow();diff --git a/src/main/pricing.ts b/src/main/pricing.tsnew file mode 100644index 00000000..0f1a5a42--- /dev/null+++ b/src/main/pricing.ts@@ -0,0 +1,73 @@+/**+ * Fallback-only model → price table (USD per million tokens).+ *+ * The LIVE telemetry path does NOT use this. Claude Code emits a pre-computed,+ * per-model `cost_usd` on every `api_request` log and a `claude_code.cost.usage`+ * metric (verified by the 7A.1 spike), so the collector (`telemetry.ts`) trusts+ * Claude's own figure. This table exists solely for the OFFLINE transcript+ * reconciler (`transcript.ts`), which runs when telemetry is off and must+ * estimate cost from raw token counts.+ *+ * It supersedes the old hard-coded Sonnet-for-everyone constants that lived in+ * `transcript.ts` (cost bug #1 — Opus undercosted ~5×, Haiku overcosted). Prices+ * are now matched per model family. This is the ONE place per-model pricing+ * lives; both the transcript backend and the collector's fallback import it.+ */++/** USD per million tokens for one model family. */+export interface ModelPrice {+ inputPerM: number;+ outputPerM: number;+ cacheReadPerM: number;+ cacheWritePerM: number;+}++// Anthropic list prices, USD per million tokens. Approximate, fallback-only —+// the live path uses Claude's own per-model cost, so drift here is harmless.+const OPUS: ModelPrice = { inputPerM: 15, outputPerM: 75, cacheReadPerM: 1.5, cacheWritePerM: 18.75 };+const SONNET: ModelPrice = { inputPerM: 3, outputPerM: 15, cacheReadPerM: 0.3, cacheWritePerM: 3.75 };+const HAIKU: ModelPrice = { inputPerM: 0.8, outputPerM: 4, cacheReadPerM: 0.08, cacheWritePerM: 1.0 };++/** When the model id is unknown, assume Sonnet (the historical default). */+const DEFAULT_PRICE: ModelPrice = SONNET;++/**+ * Strip Claude Code's variant suffix so `claude-opus-4-8[1m]` (the form the+ * `token.usage` metric carries) and `claude-opus-4-8` (the base id the+ * `api_request` log carries) resolve to the same family. Case is preserved;+ * matching is done case-insensitively in `priceFor`.+ */+export function normalizeModel(model: string | undefined | null): string {+ return (model ?? '').trim().replace(/\[[^\]]*\]\s*$/, '');+}++/** Resolve a model id to its price row by family, falling back to Sonnet. */+export function priceFor(model: string | undefined | null): ModelPrice {+ const m = normalizeModel(model).toLowerCase();+ if (m.includes('opus')) return OPUS;+ if (m.includes('haiku')) return HAIKU;+ if (m.includes('sonnet')) return SONNET;+ return DEFAULT_PRICE;+}++/** Token split used by the cost estimator (matches `AgentUsage` token fields). */+export interface TokenSplit {+ inputTokens: number;+ outputTokens: number;+ cacheReadTokens: number;+ cacheWriteTokens: number;+}++/**+ * Estimate USD cost for a token split using the model's fallback price row.+ * Used only by the transcript reconciler; the live path trusts Claude's cost.+ */+export function estimateCostUsd(model: string | undefined | null, tokens: TokenSplit): number {+ const p = priceFor(model);+ return (+ (tokens.inputTokens / 1_000_000) * p.inputPerM ++ (tokens.outputTokens / 1_000_000) * p.outputPerM ++ (tokens.cacheReadTokens / 1_000_000) * p.cacheReadPerM ++ (tokens.cacheWriteTokens / 1_000_000) * p.cacheWritePerM+ );+}diff --git a/src/main/telemetry.ts b/src/main/telemetry.tsnew file mode 100644index 00000000..df3efd61--- /dev/null+++ b/src/main/telemetry.ts@@ -0,0 +1,461 @@+/**+ * TelemetryCollector — the live, first-party observability tap for the hive.+ *+ * Every spawned `claude` is launched with `CLAUDE_CODE_ENABLE_TELEMETRY=1` and+ * `OTEL_EXPORTER_OTLP_ENDPOINT` pointed here (see hive.ts `ensureAgent`). Claude+ * Code then PUSHES OpenTelemetry over plain OTLP/HTTP JSON to this embedded+ * collector — no protobuf, no external process, loopback only. We decode it into+ * two products:+ *+ * 1. The usage PROVIDER (the locked cross-lane seam) — `getAgentUsage(agentId)`+ * (pull, primary) + `onAgentUsage(cb)` (push). Returns `AgentUsageSample`,+ * a PII-free cumulative cost/token snapshot. Lane A's circuit breaker (#6)+ * consumes this; the swap between the OTel backend and the transcript+ * fallback is hidden here so the breaker never changes.+ * 2. An EPHEMERAL ring buffer of rich tool spans (`tool_result` durations ++ * success) per agent, for the per-agent span waterfall (#7B.2).+ *+ * 🔒 PII: raw OTel records carry `user.email`, `user.account_id/uuid`,+ * `organization.id` and a hashed `user.id`. We read ONLY an allowlist of keys+ * ({agent.id, session.id, model, token type, cost, tool fields}) and never+ * persist a raw record — so everything this module emits is PII-free BY+ * CONSTRUCTION. Downstream durable stores (Lane A's cost-ledger, Lane B's+ * SQLite) inherit that guarantee and must never persist a raw record either.+ *+ * Transport posture mirrors `slack.ts`: the local handler bound to 127.0.0.1 is+ * the security boundary. Runs in the Electron main process; deliberately free of+ * any `electron` import so it can be smoke-tested as a plain Node module.+ */+import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';+import { readAgentUsage } from './transcript';+import { normalizeModel } from './pricing';++// ─── The locked cross-lane contract (do not change without re-agreeing) ───────++/** A cumulative cost/token snapshot for one agent. The shared row consumed by+ * Lane A's breaker (#6) and persisted by Lane A's cost-ledger / Lane B's SQLite+ * (#4). PII-free by construction (see file header). `usd` is Claude's own+ * per-model cost on the live path, the fallback estimate on the transcript+ * path — never recomputed downstream. */+export interface AgentUsageSample {+ agentId: string;+ /** Dedup/accounting key — present on every OTel record; fixes the cwd+ * double-count. Empty string on the transcript fallback when unknown. */+ sessionId: string;+ ts: number;+ input: number;+ output: number;+ cacheRead: number;+ cacheCreation: number;+ /** Normalized model id (`claude-opus-4-8`, no `[1m]` suffix). */+ model: string;+ usd: number;+}++/** Breaker state, emitted by Lane A's policy on `control:breakerState` and+ * consumed by this lane's avatar adapter (#5C) + cost meter. Defined here as+ * the shared type so both lanes import one shape. */+export interface BreakerState {+ agentId: string;+ level: 'healthy' | 'steering' | 'constrained' | 'stopped';+ reason: string;+ ts: number;+}++// ─── Internal, lane-owned shapes ──────────────────────────────────────────────++/** A single tool invocation, for the per-agent span waterfall. Ephemeral — kept+ * only in the in-memory ring buffer, never persisted. */+export interface ToolSpan {+ agentId: string;+ sessionId: string;+ ts: number;+ tool: string;+ success: boolean;+ durationMs: number;+ decision?: 'accept' | 'reject';+ error?: string;+}++/** The normalized event pushed to the renderer over `telemetry:event`. */+export type TelemetryEvent =+ | { kind: 'usage'; sample: AgentUsageSample }+ | { kind: 'tool_result'; span: ToolSpan }+ | { kind: 'api_error'; agentId: string; sessionId: string; ts: number; error: string };++/** Cold-start backfill returned by `snapshot()`. */+export interface TelemetrySnapshot {+ usage: AgentUsageSample[];+ spans: Record<string, ToolSpan[]>;+}++/** Per-session running accumulation (token.usage / cost.usage are DELTA ++ * monotonic, so we sum each export rather than treating it as a total). */+interface SessionAccum {+ agentId: string;+ model: string;+ ts: number;+ input: number;+ output: number;+ cacheRead: number;+ cacheCreation: number;+ usd: number;+}++const MAX_BODY_BYTES = 8 * 1024 * 1024; // OTLP batches are small; cap unauth peers.+const SPAN_RING_CAP = 200; // rich spans retained per agent for the waterfall.++export interface TelemetryCollectorOptions {+ /** Loopback host to bind. Defaults to 127.0.0.1 (the trust boundary). */+ host?: string;+ /** TCP port. Defaults to 0 → OS-assigned ephemeral port (avoids clashing with+ * a user's own collector on 4318); the chosen port is read back from the+ * bound socket and exposed via `endpoint()`. */+ port?: number;+ /** Sink for renderer-facing events (set to `webContents.send`). No-op in tests. */+ emit?: (channel: string, payload: unknown) => void;+ /** Resolve an agent's cwd (from the hive registry) for the transcript fallback. */+ resolveCwd?: (agentId: string) => string | null;+}++export class TelemetryCollector {+ private server: Server | null = null;+ private boundPort: number | null = null;+ private readonly host: string;+ private readonly port: number;+ private readonly emit?: (channel: string, payload: unknown) => void;+ private readonly resolveCwd?: (agentId: string) => string | null;++ /** sessionId → running accumulation. */+ private readonly sessions = new Map<string, SessionAccum>();+ /** agentId → its sessionIds (lets getAgentUsage aggregate across --resume). */+ private readonly agentSessions = new Map<string, Set<string>>();+ /** agentId → ring buffer of recent tool spans. */+ private readonly spans = new Map<string, ToolSpan[]>();+ /** Push subscribers (Lane A breaker + dashboard). */+ private readonly usageSubs = new Set<(s: AgentUsageSample) => void>();++ constructor(opts: TelemetryCollectorOptions = {}) {+ this.host = opts.host ?? '127.0.0.1';+ this.port = opts.port ?? 0;+ this.emit = opts.emit;+ this.resolveCwd = opts.resolveCwd;+ }++ /** Bind the loopback OTLP listener. The handler is live the instant this+ * resolves; `endpoint()` then returns the URL to inject into agent env. */+ async start(): Promise<{ ok: boolean; endpoint?: string; error?: string }> {+ if (this.server) return { ok: true, endpoint: this.endpoint() ?? undefined };+ try {+ await this.listen();+ return { ok: true, endpoint: this.endpoint() ?? undefined };+ } catch (e) {+ this.stop();+ return { ok: false, error: errMsg(e) };+ }+ }++ /** Close the listener. Idempotent and best-effort. Accumulated state is kept+ * (it's ephemeral anyway) so a restart doesn't lose live agents' totals. */+ stop(): void {+ try { this.server?.close(); } catch { /* noop */ }+ this.server = null;+ this.boundPort = null;+ }++ /** The bound loopback URL agents export to, or null until started. */+ endpoint(): string | null {+ return this.boundPort ? `http://${this.host}:${this.boundPort}` : null;+ }++ // ─── The locked provider seam ──────────────────────────────────────────────++ /** Pull (contract primary). OTel-live aggregate preferred; transcript fallback+ * when an agent has no live telemetry yet (e.g. spawned before the feature, or+ * telemetry off). Returns null only when neither source has anything. */+ getAgentUsage(agentId: string): AgentUsageSample | null {+ const live = this.aggregateLive(agentId);+ if (live) return live;+ return this.transcriptFallback(agentId);+ }++ /** Push (additive, OTel-only). Fires the agent's fresh aggregate whenever new+ * telemetry lands. Returns an unsubscribe fn. */+ onAgentUsage(cb: (s: AgentUsageSample) => void): () => void {+ this.usageSubs.add(cb);+ return () => this.usageSubs.delete(cb);+ }++ /** Recent tool spans for the per-agent waterfall (#7B.2), oldest→newest. */+ getSpans(agentId: string): ToolSpan[] {+ return this.spans.get(agentId)?.slice() ?? [];+ }++ /** Everything the renderer needs on cold start (it missed the live pushes). */+ snapshot(): TelemetrySnapshot {+ const usage: AgentUsageSample[] = [];+ for (const agentId of this.agentSessions.keys()) {+ const s = this.aggregateLive(agentId);+ if (s) usage.push(s);+ }+ const spans: Record<string, ToolSpan[]> = {};+ for (const [agentId, ring] of this.spans) spans[agentId] = ring.slice();+ return { usage, spans };+ }++ // ─── HTTP plumbing (mirrors slack.ts) ──────────────────────────────────────++ private listen(): Promise<void> {+ return new Promise<void>((resolve, reject) => {+ const server = createServer((req, res) => this.handleRequest(req, res));+ const onError = (e: Error): void => reject(e);+ server.once('error', onError);+ server.listen(this.port, this.host, () => {+ server.off('error', onError);+ const addr = server.address();+ this.boundPort = addr && typeof addr === 'object' ? addr.port : null;+ this.server = server;+ resolve();+ });+ });+ }++ private handleRequest(req: IncomingMessage, res: ServerResponse): void {+ if (req.method !== 'POST') { res.writeHead(405); res.end(); return; }+ const chunks: Buffer[] = [];+ let size = 0;+ let aborted = false;+ req.on('data', (c: Buffer) => {+ if (aborted) return;+ size += c.length;+ if (size > MAX_BODY_BYTES) {+ aborted = true;+ res.writeHead(413); res.end();+ req.destroy();+ return;+ }+ chunks.push(c);+ });+ req.on('end', () => {+ if (aborted) return;+ const url = req.url ?? '';+ try {+ const body = JSON.parse(Buffer.concat(chunks).toString('utf8'));+ if (url.includes('/v1/metrics')) this.ingestMetrics(body);+ else if (url.includes('/v1/logs')) this.ingestLogs(body);+ } catch { /* malformed batch — drop it, never throw into the socket */ }+ // OTLP success response is an empty JSON ExportServiceResponse.+ res.writeHead(200, { 'content-type': 'application/json' });+ res.end('{}');+ });+ req.on('error', () => {+ if (aborted) return;+ try { res.writeHead(400); res.end(); } catch { /* socket gone */ }+ });+ }++ // ─── OTLP decode → normalize → accumulate ──────────────────────────────────++ private ingestMetrics(body: unknown): void {+ const root = body as { resourceMetrics?: ResourceMetrics[] };+ if (!Array.isArray(root?.resourceMetrics)) return;+ const touched = new Set<string>(); // agentIds with new data this batch+ for (const rm of root.resourceMetrics) {+ const resAttrs = flattenAttrs(rm.resource?.attributes);+ for (const sm of rm.scopeMetrics ?? []) {+ for (const metric of sm.metrics ?? []) {+ const points = metric.sum?.dataPoints ?? metric.gauge?.dataPoints ?? [];+ for (const dp of points) {+ const attrs = flattenAttrs(dp.attributes);+ const agentId = str(attrs['agent.id']) || str(resAttrs['agent.id']);+ const sessionId = str(attrs['session.id']);+ if (!agentId || !sessionId) continue;+ const accum = this.session(agentId, sessionId);+ const model = normalizeModel(str(attrs['model']));+ if (model) accum.model = model;+ accum.ts = Date.now();+ const value = pointValue(dp);+ if (metric.name === 'claude_code.token.usage') {+ switch (str(attrs['type'])) {+ case 'input': accum.input += value; break;+ case 'output': accum.output += value; break;+ case 'cacheRead': accum.cacheRead += value; break;+ case 'cacheCreation': accum.cacheCreation += value; break;+ }+ touched.add(agentId);+ } else if (metric.name === 'claude_code.cost.usage') {+ accum.usd += value;+ touched.add(agentId);+ }+ }+ }+ }+ }+ for (const agentId of touched) this.publishUsage(agentId);+ }++ private ingestLogs(body: unknown): void {+ const root = body as { resourceLogs?: ResourceLogs[] };+ if (!Array.isArray(root?.resourceLogs)) return;+ for (const rl of root.resourceLogs) {+ const resAttrs = flattenAttrs(rl.resource?.attributes);+ for (const sl of rl.scopeLogs ?? []) {+ for (const lr of sl.logRecords ?? []) {+ const attrs = flattenAttrs(lr.attributes);+ const name = str(attrs['event.name']) || str(lr.body?.stringValue);+ const agentId = str(attrs['agent.id']) || str(resAttrs['agent.id']);+ const sessionId = str(attrs['session.id']);+ if (!agentId) continue;+ if (name === 'tool_result') {+ const span: ToolSpan = {+ agentId,+ sessionId,+ ts: Date.now(),+ tool: str(attrs['tool_name']) || 'tool',+ success: truthy(attrs['success']),+ durationMs: numAttr(attrs['duration_ms']),+ decision: undefined+ };+ this.pushSpan(span);+ this.emit?.('telemetry:event', { kind: 'tool_result', span } satisfies TelemetryEvent);+ } else if (name === 'tool_decision') {+ // Attach the accept/reject decision to the most recent span, and emit.+ const decision = str(attrs['decision']) === 'reject' ? 'reject' : 'accept';+ const ring = this.spans.get(agentId);+ if (ring?.length) ring[ring.length - 1].decision = decision;+ } else if (name === 'api_error' || (name && name.includes('error'))) {+ const error = str(attrs['error']) || str(attrs['message']) || name;+ this.emit?.('telemetry:event', { kind: 'api_error', agentId, sessionId, ts: Date.now(), error } satisfies TelemetryEvent);+ }+ }+ }+ }+ }++ // ─── Accumulation helpers ──────────────────────────────────────────────────++ private session(agentId: string, sessionId: string): SessionAccum {+ let accum = this.sessions.get(sessionId);+ if (!accum) {+ accum = { agentId, model: '', ts: Date.now(), input: 0, output: 0, cacheRead: 0, cacheCreation: 0, usd: 0 };+ this.sessions.set(sessionId, accum);+ }+ let set = this.agentSessions.get(agentId);+ if (!set) { set = new Set(); this.agentSessions.set(agentId, set); }+ set.add(sessionId);+ return accum;+ }++ private pushSpan(span: ToolSpan): void {+ let ring = this.spans.get(span.agentId);+ if (!ring) { ring = []; this.spans.set(span.agentId, ring); }+ ring.push(span);+ if (ring.length > SPAN_RING_CAP) ring.splice(0, ring.length - SPAN_RING_CAP);+ }++ /** Sum an agent's live sessions into one cumulative sample (sessionId/model =+ * the most recently active session). Null if the agent has no live data. */+ private aggregateLive(agentId: string): AgentUsageSample | null {+ const set = this.agentSessions.get(agentId);+ if (!set || set.size === 0) return null;+ const out: AgentUsageSample = {+ agentId, sessionId: '', ts: 0, input: 0, output: 0, cacheRead: 0, cacheCreation: 0, model: '', usd: 0+ };+ for (const sid of set) {+ const a = this.sessions.get(sid);+ if (!a) continue;+ out.input += a.input;+ out.output += a.output;+ out.cacheRead += a.cacheRead;+ out.cacheCreation += a.cacheCreation;+ out.usd += a.usd;+ if (a.ts >= out.ts) { out.ts = a.ts; out.sessionId = sid; out.model = a.model; }+ }+ return out;+ }++ private transcriptFallback(agentId: string): AgentUsageSample | null {+ const cwd = this.resolveCwd?.(agentId);+ if (!cwd) return null;+ const u = readAgentUsage(cwd);+ if (!u.inputTokens && !u.outputTokens && !u.cacheReadTokens && !u.cacheWriteTokens) return null;+ return {+ agentId,+ sessionId: '',+ ts: Date.now(),+ input: u.inputTokens,+ output: u.outputTokens,+ cacheRead: u.cacheReadTokens,+ cacheCreation: u.cacheWriteTokens,+ model: u.model ?? '',+ usd: u.estimatedCostUsd+ };+ }++ private publishUsage(agentId: string): void {+ const sample = this.aggregateLive(agentId);+ if (!sample) return;+ for (const cb of this.usageSubs) { try { cb(sample); } catch { /* subscriber threw */ } }+ this.emit?.('telemetry:event', { kind: 'usage', sample } satisfies TelemetryEvent);+ }+}++// ─── OTLP/JSON attribute decoding ─────────────────────────────────────────────++interface OtelKV { key?: string; value?: OtelAnyValue }+interface OtelAnyValue {+ stringValue?: string;+ intValue?: string | number;+ doubleValue?: number;+ boolValue?: boolean;+}+interface OtelDataPoint { attributes?: OtelKV[]; asInt?: string | number; asDouble?: number; timeUnixNano?: string }+interface OtelMetric { name?: string; sum?: { dataPoints?: OtelDataPoint[] }; gauge?: { dataPoints?: OtelDataPoint[] } }+interface ResourceMetrics { resource?: { attributes?: OtelKV[] }; scopeMetrics?: { metrics?: OtelMetric[] }[] }+interface OtelLogRecord { attributes?: OtelKV[]; body?: { stringValue?: string } }+interface ResourceLogs { resource?: { attributes?: OtelKV[] }; scopeLogs?: { logRecords?: OtelLogRecord[] }[] }++/** Allowlist of attribute keys we ever read — anything else (notably the PII:+ * user.email, user.account_id/uuid, organization.id, user.id) is ignored, so+ * nothing this module emits can carry identity. */+const ATTR_ALLOWLIST = new Set([+ 'agent.id', 'agent.name', 'session.id', 'model', 'type',+ 'tool_name', 'success', 'duration_ms', 'decision', 'event.name', 'error', 'message'+]);++/** Flatten an OTLP KeyValue[] to a plain object, keeping only allowlisted keys. */+function flattenAttrs(attrs: OtelKV[] | undefined): Record<string, string | number | boolean> {+ const out: Record<string, string | number | boolean> = {};+ if (!Array.isArray(attrs)) return out;+ for (const kv of attrs) {+ if (!kv?.key || !ATTR_ALLOWLIST.has(kv.key)) continue;+ const v = kv.value;+ if (!v) continue;+ if (typeof v.stringValue === 'string') out[kv.key] = v.stringValue;+ else if (v.intValue !== undefined) out[kv.key] = Number(v.intValue);+ else if (typeof v.doubleValue === 'number') out[kv.key] = v.doubleValue;+ else if (typeof v.boolValue === 'boolean') out[kv.key] = v.boolValue;+ }+ return out;+}++/** A metric data point's numeric value (int counters arrive as strings in JSON). */+function pointValue(dp: OtelDataPoint): number {+ if (dp.asInt !== undefined) return Number(dp.asInt) || 0;+ if (typeof dp.asDouble === 'number') return dp.asDouble;+ return 0;+}++function str(v: unknown): string {+ return typeof v === 'string' ? v : v === undefined || v === null ? '' : String(v);+}+function numAttr(v: unknown): number {+ const n = typeof v === 'number' ? v : Number(v);+ return Number.isFinite(n) ? n : 0;+}+function truthy(v: unknown): boolean {+ return v === true || v === 'true' || v === 1 || v === '1';+}+function errMsg(e: unknown): string {+ return e instanceof Error ? e.message : String(e);+}diff --git a/src/main/transcript.ts b/src/main/transcript.tsindex 3dcccdc8..ee76119b 100644--- a/src/main/transcript.ts+++ b/src/main/transcript.ts@@ -1,6 +1,7 @@import { existsSync, readdirSync, readFileSync } from 'node:fs';import os from 'node:os';import path from 'node:path';+import { estimateCostUsd, normalizeModel } from './pricing';/** Resolve the Claude Code transcript directory for a given working directory.* Claude Code stores per-project transcripts under ~/.claude/projects, keying@@ -16,57 +17,83 @@ export interface AgentUsage {cacheReadTokens: number;cacheWriteTokens: number;estimatedCostUsd: number;+ /** The most-recently-seen model id (normalized, e.g. `claude-opus-4-8`), or+ * undefined if no priced record was found. Lets the UI label the row. */+ model?: string;}-// Sonnet pricing, USD per million tokens.-const PRICE_INPUT_PER_M = 3;-const PRICE_OUTPUT_PER_M = 15;-const PRICE_CACHE_READ_PER_M = 0.3;-const PRICE_CACHE_WRITE_PER_M = 3.75;-function zero(): AgentUsage {return { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0, estimatedCostUsd: 0 };}-/** Sum real token usage across every Claude Code transcript for `cwd` and- * estimate the dollar cost using Sonnet pricing. Resilient by design: any- * unreadable file or malformed line is skipped, and any unexpected failure- * yields a zeroed result rather than throwing into the IPC handler. */-export function readAgentUsage(cwd: string): AgentUsage {+export interface ReadUsageOptions {+ /** When set, only transcripts whose records carry this `sessionId` are summed.+ * This is how the co-located-agents double-count (bug #2) is avoided: two+ * agents sharing a cwd each filter to their own session instead of summing+ * every `.jsonl` under the shared project dir. When unset, all are summed+ * (the legacy behavior, used when the agent's session id isn't yet known). */+ sessionId?: string;+}++/** Sum real token usage across Claude Code transcripts for `cwd`, pricing each+ * assistant record by ITS OWN model (fixes cost bug #1 — no more Sonnet for+ * everyone) via the fallback price table. Optionally filtered to one session+ * (fixes bug #2). Resilient by design: any unreadable file or malformed line is+ * skipped, and any unexpected failure yields a zeroed result rather than+ * throwing into the IPC handler. This is the OFFLINE reconciler / fallback —+ * the live source is the OTel collector (`telemetry.ts`). */+export function readAgentUsage(cwd: string, opts: ReadUsageOptions = {}): AgentUsage {const usage = zero();try {const dir = projectDir(cwd);if (!existsSync(dir)) return usage;const files = readdirSync(dir).filter((f) => f.endsWith('.jsonl'));+ let lastModel: string | undefined;for (const file of files) {try {const text = readFileSync(path.join(dir, file), 'utf8');for (const line of text.split('\n')) {const trimmed = line.trim();if (!trimmed) continue;- let rec: { type?: unknown; message?: { usage?: Record<string, unknown> } };+ let rec: {+ type?: unknown;+ sessionId?: unknown;+ message?: { model?: unknown; usage?: Record<string, unknown> };+ };try {rec = JSON.parse(trimmed);} catch {continue;}if (rec.type !== 'assistant') continue;+ // Session filter: skip records that aren't this agent's session.+ if (opts.sessionId && rec.sessionId !== opts.sessionId) continue;const u = rec.message?.usage;if (!u) continue;- usage.inputTokens += num(u.input_tokens);- usage.outputTokens += num(u.output_tokens);- usage.cacheWriteTokens += num(u.cache_creation_input_tokens);- usage.cacheReadTokens += num(u.cache_read_input_tokens);+ const model = typeof rec.message?.model === 'string' ? normalizeModel(rec.message.model) : undefined;+ if (model) lastModel = model;+ const rIn = num(u.input_tokens);+ const rOut = num(u.output_tokens);+ const rCacheWrite = num(u.cache_creation_input_tokens);+ const rCacheRead = num(u.cache_read_input_tokens);+ usage.inputTokens += rIn;+ usage.outputTokens += rOut;+ usage.cacheWriteTokens += rCacheWrite;+ usage.cacheReadTokens += rCacheRead;+ // Price THIS record by its own model, then accumulate — so a mixed-model+ // agent (rare) is still costed correctly rather than at one flat rate.+ usage.estimatedCostUsd += estimateCostUsd(model, {+ inputTokens: rIn,+ outputTokens: rOut,+ cacheReadTokens: rCacheRead,+ cacheWriteTokens: rCacheWrite+ });}} catch {// Skip this file; keep accumulating across the rest.}}- usage.estimatedCostUsd =- (usage.inputTokens / 1_000_000) * PRICE_INPUT_PER_M +- (usage.outputTokens / 1_000_000) * PRICE_OUTPUT_PER_M +- (usage.cacheReadTokens / 1_000_000) * PRICE_CACHE_READ_PER_M +- (usage.cacheWriteTokens / 1_000_000) * PRICE_CACHE_WRITE_PER_M;+ if (lastModel) usage.model = lastModel;return usage;} catch {return zero();diff --git a/src/preload/index.ts b/src/preload/index.tsindex 0323c403..b069da62 100644--- a/src/preload/index.ts+++ b/src/preload/index.ts@@ -128,13 +128,62 @@ export interface GitStatusEntry { path: string; index: string; worktree: stringexport interface GitStatus { staged: GitStatusEntry[]; unstaged: GitStatusEntry[]; untracked: string[] }/** Real token usage + estimated USD cost summed from an agent's Claude Code- * transcripts under ~/.claude/projects (Sonnet pricing). */+ * transcripts under ~/.claude/projects. Reconciler/fallback path — now priced+ * PER MODEL (not Sonnet-for-everyone). The live path uses AgentUsageSample. */export interface AgentUsage {inputTokens: number;outputTokens: number;cacheReadTokens: number;cacheWriteTokens: number;estimatedCostUsd: number;+ /** Most-recently-seen model id (normalized), if any priced record was found. */+ model?: string;+}++/** Live cumulative cost/token snapshot from the OTel collector (the locked+ * cross-lane seam). PII-free by construction. Mirrors telemetry.ts. */+export interface AgentUsageSample {+ agentId: string;+ sessionId: string;+ ts: number;+ input: number;+ output: number;+ cacheRead: number;+ cacheCreation: number;+ model: string;+ usd: number;+}++/** One tool invocation for the per-agent span waterfall (#7B.2). Ephemeral. */+export interface ToolSpan {+ agentId: string;+ sessionId: string;+ ts: number;+ tool: string;+ success: boolean;+ durationMs: number;+ decision?: 'accept' | 'reject';+ error?: string;+}++/** Circuit-breaker state (Lane A #6 → this lane's avatars/meter). */+export interface BreakerState {+ agentId: string;+ level: 'healthy' | 'steering' | 'constrained' | 'stopped';+ reason: string;+ ts: number;+}++/** Live telemetry push payload (channel `telemetry:event`). */+export type TelemetryEvent =+ | { kind: 'usage'; sample: AgentUsageSample }+ | { kind: 'tool_result'; span: ToolSpan }+ | { kind: 'api_error'; agentId: string; sessionId: string; ts: number; error: string };++/** Cold-start backfill from the collector. */+export interface TelemetrySnapshot {+ usage: AgentUsageSample[];+ spans: Record<string, ToolSpan[]>;}/** A GitHub issue, normalized for the renderer (labels/assignees flattened to names). */@@ -279,10 +328,38 @@ const api = {// ─── Token telemetry (real usage + est. cost from CC transcripts) ──────────/** Sum input/output/cache tokens + estimated USD cost for an agent from its- * Claude Code transcripts. Returns null for an invalid cwd. */+ * Claude Code transcripts (reconciler/fallback). Returns null for an invalid cwd. */agentUsage: (cwd: string): Promise<AgentUsage | null> =>ipcRenderer.invoke('hive:agentUsage', cwd),+ // ─── Live telemetry (OTel collector — the usage-provider seam + spans) ──────+ /** Live cumulative usage for an agent (OTel-preferred, transcript fallback). */+ telemetryUsage: (agentId: string): Promise<AgentUsageSample | null> =>+ ipcRenderer.invoke('telemetry:usage', agentId),+ /** Recent tool spans for an agent's waterfall (#7B.2). */+ telemetrySpans: (agentId: string): Promise<ToolSpan[]> =>+ ipcRenderer.invoke('telemetry:spans', agentId),+ /** Cold-start backfill of all agents' usage + recent spans. */+ telemetrySnapshot: (): Promise<TelemetrySnapshot> =>+ ipcRenderer.invoke('telemetry:snapshot'),+ /** Subscribe to live telemetry pushes; returns an unsubscribe fn. */+ onTelemetryEvent: (cb: (e: TelemetryEvent) => void): (() => void) => {+ const listener = (_e: IpcRendererEvent, payload: TelemetryEvent) => cb(payload);+ ipcRenderer.on('telemetry:event', listener);+ return () => ipcRenderer.removeListener('telemetry:event', listener);+ },++ // ─── Circuit breaker (Lane A #6 state → avatars/meter) ──────────────────────+ /** Subscribe to breaker-state changes; returns an unsubscribe fn. */+ onBreakerState: (cb: (s: BreakerState) => void): (() => void) => {+ const listener = (_e: IpcRendererEvent, payload: BreakerState) => cb(payload);+ ipcRenderer.on('control:breakerState', listener);+ return () => ipcRenderer.removeListener('control:breakerState', listener);+ },+ /** Push a breaker state to the renderer (Lane A's policy / interim glue calls this). */+ setBreakerState: (state: BreakerState): Promise<{ ok: boolean }> =>+ ipcRenderer.invoke('control:setBreakerState', state),+// ─── Task kanban (hive/tasks.json) ───────────────────────────────────────/** Overwrite the hive task ledger with the full task list and commit it. */hiveWriteTasks: (tasks: HiveTask[]): Promise<{ ok: boolean; error?: string }> =>