The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
feat(copilot): record AI-credit cost from session.shutdown totalNanoAiu
Claudea994bb7b0cde
diff --git a/CLAUDE.md b/CLAUDE.mdindex 20cbcfeb..752a0e0c 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -55,7 +55,7 @@ Supported runtime surfaces today:- fx (vercel-labs/fx) telemetry read from the session store fx commits under `~/.fx/sessions/`, covering session start, prompts, tool calls, commands with exit codes and output, file reads/creates/edits with diffs, MCP tool calls, agent messages, history compaction, and per-turn token usage plus reported cost. fx exposes no third-party hook, plugin, or OTLP surface -- its lifecycle hooks are compiled into the binary -- so collection is a read of fx's own records, run by `beacon endpoint fx sync`. Every event carries `harness.collection_method=poll`; approvals are not synthesized (fx persists permission feedback text, not a per-call decision, matching the Cline and Pi posture) and no session-end event is invented, since fx sessions are resumable. Implementation lives in `cli/beacon/internal/fxsession` and `cli/beacon/cmd/endpoint_fx.go`.- Claude Cowork admin-configured OpenTelemetry setup guidance and local validation.- OpenClaw Gateway uses a Beacon-managed plugin directory plus OpenClaw's own diagnostics OTLP guidance (`beacon endpoint integrations openclaw`). The plugin's `before_tool_call` hook is fail-closed, so the handler dispatches synchronously and returns without awaiting. Conversation hooks, including `llm_output`, require `plugins.entries.beacon-endpoint.hooks.allowConversationAccess`; without it, token and agent-message coverage is partial. Beacon reads OpenClaw JSON/JSON5 config for advice but does not rewrite it.-- GitHub Copilot CLI uses MDM/customer-managed OTLP HTTP for live spans and approval-like activity, plus `beacon endpoint copilot sync` to backfill local session records from `~/.copilot/session-state`. Poll events carry assistant text, complete tool results, command/file details, and token usage with `harness.collection_method=poll`; approvals still come only from the live OTLP path and are not synthesized from persisted records.+- GitHub Copilot CLI uses MDM/customer-managed OTLP HTTP for live spans and approval-like activity, plus `beacon endpoint copilot sync` to backfill local session records from `~/.copilot/session-state`. Poll events carry assistant text, complete tool results, command/file details, and token usage with `harness.collection_method=poll`, plus the shutdown's session-wide `totalNanoAiu` differenced into `gen_ai.usage.cost_usd` once per shutdown (10^9 nano-AIU = one AI credit = US$0.01), only for sessions started on or after the 2026-06-01 AI-credit cutover (`cli/beacon/internal/copilotsession/cost.go`); approvals still come only from the live OTLP path and are not synthesized from persisted records.- Runtimes that ride shared hook/OTLP paths without a special contract worth expanding here include Antigravity CLI, Devin CLI, Devin Desktop, Factory Droid, Gemini CLI, Grok Build, Hermes Agent, Qwen Code, and VS Code. Installed hook/endpoint support is governed by `cli/beacon/cmd/endpoint_targets.go` plus the install/status switches in `cli/beacon/cmd/endpoint_hooks.go`; discovery rows live in `cli/beacon/internal/endpoint/harness`.- Browser chat telemetry through an optional MV3 extension (`browser-extension/`; Chrome, with a beta Firefox build) covering claude.ai and chatgpt.com. A main-world interceptor tees the streamed response, a per-site adapter parses it into a normalized turn, and the service worker POSTs OTLP GenAI logs to the local collector on `127.0.0.1:4318`; the extension never writes files and never contacts a remote endpoint. `harness.name` resolves to `claude_web` or `chatgpt_web` (already handled by `NormalizeHarnessName` in `pkg/asymptoteobserve/harness.go`, where the browser cases must stay above the generic `claude` rule) and `harness.collection_method` is `otlp`. The one build serves every Chromium browser; each event names the browser it came from in the typed `user_agent.name`/`user_agent.version` fields (OTel semconv, promoted by the exporter so Metadata-only forwarding keeps them), resolved in `browser-extension/src/shared/browser-identity.ts` from UA Client Hints brands, with a UA-string fallback that also names Firefox. Scope is deliberately narrow: only those origins' chat streams, never other tabs or general browsing. Retention defaults to `full`, so prompt and response text is retained by default; this is documented rather than defaulted down, because browser chat telemetry without content has no investigative value. Treat the adapters as experimental, since both sites' stream formats are private and undocumented. Per-browser differences live only in `browser-extension/tools/targets.mjs` (one entry per target, deriving its manifest from `src/manifest.json`; Chrome ships that file verbatim to `dist/`, Firefox gets `dist-firefox/`), and every extension API call goes through the `src/shared/browser.ts` shim (a unit test fails on direct `chrome.*` use). The Firefox Gecko ID `[email protected]` is pinned and must never change.- `beaconjson` OpenTelemetry Collector exporter that converts OTLP logs, traces, metrics, and resource attributes into Beacon endpoint JSONL.diff --git a/cli/beacon/internal/copilotsession/cost.go b/cli/beacon/internal/copilotsession/cost.gonew file mode 100644index 00000000..7109be40--- /dev/null+++ b/cli/beacon/internal/copilotsession/cost.go@@ -0,0 +1,115 @@+package copilotsession++import (+ "encoding/json"+ "math"+ "strconv"+ "strings"+ "time"+)++// Copilot CLI reports what a session cost as totalNanoAiu on session.shutdown:+// the session-wide accumulated cost in nano AI units. The Copilot SDK defines+// one AI unit as 1e9 nano-AIU (NANO_AIU_PER_AIU, which it uses to turn a+// maxAiCredits limit into a nano-AIU ceiling), and GitHub bills Copilot in AI+// credits worth US$0.01 each. So one nano-AIU is US$1e-11.+const nanoAiuPerUSD = 100_000_000_000++// usageBillingCutover is when Copilot moved from premium requests to AI+// credits. A session started before it was billed per premium request, so+// whatever totalNanoAiu it carries is not the dollar charge and Beacon+// records no cost for it. Monthly allowances reset at 00:00 UTC, so the+// cutover is taken at midnight UTC on the first day of the new regime.+var usageBillingCutover = time.Date(2026, time.June, 1, 0, 0, 0, 0, time.UTC)++// maxExactNanoAiu keeps conversion exact: below 2^53 a float64 holds every+// integer, so the one division into dollars is the only rounding step.+const maxExactNanoAiu = 1 << 53++// nanoAiuValue reads a totalNanoAiu as a whole number of nano-AIU. Fractions+// of a nano-AIU (US$1e-11) are rounded to the nearest unit so differencing+// stays in integers.+func nanoAiuValue(value interface{}) (int64, bool) {+ var f float64+ switch v := value.(type) {+ case float64:+ f = v+ case int64:+ return v, v >= 0+ case int:+ return int64(v), v >= 0+ case json.Number:+ if n, err := v.Int64(); err == nil {+ return n, n >= 0+ }+ parsed, err := v.Float64()+ if err != nil {+ return 0, false+ }+ f = parsed+ case string:+ text := strings.TrimSpace(v)+ if n, err := strconv.ParseInt(text, 10, 64); err == nil {+ return n, n >= 0+ }+ parsed, err := strconv.ParseFloat(text, 64)+ if err != nil {+ return 0, false+ }+ f = parsed+ default:+ return 0, false+ }+ if math.IsNaN(f) || math.IsInf(f, 0) || f < 0 || f >= maxExactNanoAiu {+ return 0, false+ }+ return int64(math.Round(f)), true+}++func nanoAiuToUSD(n int64) float64 {+ return float64(n) / nanoAiuPerUSD+}++// noteSessionStart records when the session began, from session.start's own+// startTime or, failing that, the record's timestamp. It is what decides the+// billing regime, so a resume never moves it.+func (m *mapper) noteSessionStart(record Record) {+ if m.sessionStartMS > 0 {+ return+ }+ if ms := timestampMS(record.Data["startTime"], 0); ms > 0 {+ m.sessionStartMS = ms+ return+ }+ m.sessionStartMS = timestampMS(record.Time, 0)+}++// shutdownCostDelta returns the nano-AIU this shutdown adds to the session's+// reported cost, and advances the baseline. totalNanoAiu is cumulative and+// repeated on every shutdown, so only the rise since the last one is new. A+// total that falls is treated like the token totals beside it: the lower+// value becomes the baseline and nothing is emitted for it, so a reset can+// never count spend twice. omitted names why a positive rise was not recorded.+func (m *mapper) shutdownCostDelta(record Record) (delta int64, omitted string) {+ total, ok := nanoAiuValue(record.Data["totalNanoAiu"])+ if !ok {+ return 0, ""+ }+ if m.sessionStartMS <= 0 {+ // sessionStartTime is the runtime's own record of when the session+ // began; it covers a store whose session.start line is missing.+ m.sessionStartMS = timestampMS(record.Data["sessionStartTime"], 0)+ }+ previous := m.lastNanoAiu+ m.lastNanoAiu = total+ if total <= previous {+ return 0, ""+ }+ switch {+ case m.sessionStartMS <= 0:+ return 0, "session_start_unknown"+ case time.UnixMilli(m.sessionStartMS).Before(usageBillingCutover):+ return 0, "premium_request_billing"+ }+ return total - previous, ""+}diff --git a/cli/beacon/internal/copilotsession/cost_test.go b/cli/beacon/internal/copilotsession/cost_test.gonew file mode 100644index 00000000..fbf4b179--- /dev/null+++ b/cli/beacon/internal/copilotsession/cost_test.go@@ -0,0 +1,337 @@+package copilotsession++import (+ "bufio"+ "encoding/json"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/tokens"+)++// These fixtures follow the shape Copilot CLI commits to+// ~/.copilot/session-state/<id>/events.jsonl: ISO 8601 record timestamps,+// session.start carrying startTime, and session.shutdown carrying the+// session-wide cumulative totalNanoAiu next to per-model modelMetrics and+// sessionStartTime in Unix milliseconds.++const costSessionID = "4b0c9d52-6a1f-4e57-9d1e-2f7a3c8e6b10"++func costStart(startISO string) string {+ return `{"id":"e-start","parentId":null,"timestamp":"` + startISO + `","type":"session.start","data":{"sessionId":"` + costSessionID + `","version":1,"producer":"copilot-agent","copilotVersion":"1.0.40","startTime":"` + startISO + `","selectedModel":"claude-sonnet-4.6","context":{"cwd":"/repo","gitRoot":"/repo","repository":"Asymptote-Labs/agent-beacon","branch":"main"}}}`+}++func costPrompt(id, ts string) string {+ return `{"id":"` + id + `","timestamp":"` + ts + `","type":"user.message","data":{"content":"fix the failing test"}}`+}++func costResume(ts string) string {+ return `{"id":"e-resume","timestamp":"` + ts + `","type":"session.resume","data":{"resumeTime":"` + ts + `","eventCount":12,"selectedModel":"claude-sonnet-4.6","context":{"cwd":"/repo"}}}`+}++// costShutdown renders a session.shutdown whose modelMetrics holds one entry+// per model (model name -> input tokens) and whose session-wide total is+// totalNanoAiu.+func costShutdown(id, ts string, startMS string, totalNanoAiu string, models map[string]string) string {+ var metrics []string+ for model, input := range models {+ metrics = append(metrics, `"`+model+`":{"requests":{"count":3,"cost":3},"usage":{"inputTokens":`+input+`,"outputTokens":0,"cacheReadTokens":0,"cacheWriteTokens":0,"reasoningTokens":0}}`)+ }+ return `{"id":"` + id + `","timestamp":"` + ts + `","type":"session.shutdown","data":{"shutdownType":"routine","sessionStartTime":` + startMS + `,"totalApiDurationMs":4200,"totalPremiumRequests":3,"totalNanoAiu":` + totalNanoAiu + `,"codeChanges":{"linesAdded":4,"linesRemoved":1,"filesModified":["main.go"]},"currentModel":"claude-sonnet-4.6","modelMetrics":{` + strings.Join(metrics, ",") + `}}}`+}++// 2026-06-10T09:00:00Z, after the usage-based billing cutover.+const (+ postCutoverISO = "2026-06-10T09:00:00.000Z"+ postCutoverMS = "1781082000000"+ // 2026-05-20T09:00:00Z, billed per premium request.+ preCutoverISO = "2026-05-20T09:00:00.000Z"+ preCutoverMS = "1779267600000"+)++func writeCostSession(t *testing.T, root string, lines []string) string {+ t.Helper()+ dir := filepath.Join(root, "session-state", costSessionID)+ if err := os.MkdirAll(dir, 0o755); err != nil {+ t.Fatal(err)+ }+ path := filepath.Join(dir, EventsFile)+ if err := os.WriteFile(path, []byte(strings.Join(lines, "\n")+"\n"), 0o600); err != nil {+ t.Fatal(err)+ }+ return path+}++func mapCostSession(t *testing.T, root string) []MappedEvent {+ t.Helper()+ store, err := NewStore(root)+ if err != nil {+ t.Fatal(err)+ }+ refs, err := store.List()+ if err != nil || len(refs) != 1 {+ t.Fatalf("refs = %v, err = %v", refs, err)+ }+ records, _, err := store.Read(refs[0])+ if err != nil {+ t.Fatal(err)+ }+ return MapSession(refs[0], records, MapOptions{})+}++type costEvent struct {+ model string+ cost float64+ raw map[string]interface{}+}++func costEvents(events []MappedEvent) []costEvent {+ var out []costEvent+ for _, item := range events {+ ev := item.Event+ if ev.GenAI == nil || ev.GenAI.Usage == nil || ev.GenAI.Usage.CostUSD == nil {+ continue+ }+ raw, _ := ev.Raw["copilot"].(map[string]interface{})+ out = append(out, costEvent{model: ev.Model, cost: *ev.GenAI.Usage.CostUSD, raw: raw})+ }+ return out+}++func TestShutdownTotalNanoAiuIsRecordedAsReportedCost(t *testing.T) {+ root := t.TempDir()+ writeCostSession(t, root, []string{+ costStart(postCutoverISO),+ costPrompt("u1", "2026-06-10T09:00:05.000Z"),+ // 2.5 AI credits = $0.025.+ costShutdown("x1", "2026-06-10T09:10:00.000Z", postCutoverMS, "2500000000", map[string]string{"claude-sonnet-4.6": "1200"}),+ })+ got := costEvents(mapCostSession(t, root))+ if len(got) != 1 {+ t.Fatalf("cost events = %+v, want exactly one", got)+ }+ if got[0].cost != 0.025 {+ t.Fatalf("cost = %v, want 0.025", got[0].cost)+ }+ // One model in the shutdown: the session total is that model's, so it+ // rides that model's usage event and stays attributable by model.+ if got[0].model != "claude-sonnet-4.6" {+ t.Fatalf("cost model = %q, want claude-sonnet-4.6", got[0].model)+ }+ if got[0].raw["cumulative_total_nano_aiu"] == nil || got[0].raw["nano_aiu_delta"] == nil {+ t.Fatalf("raw nano-AIU values missing: %+v", got[0].raw)+ }+}++func TestResumedSessionCostIsDifferencedAcrossShutdowns(t *testing.T) {+ root := t.TempDir()+ writeCostSession(t, root, []string{+ costStart(postCutoverISO),+ costPrompt("u1", "2026-06-10T09:00:05.000Z"),+ costShutdown("x1", "2026-06-10T09:10:00.000Z", postCutoverMS, "2500000000", map[string]string{"claude-sonnet-4.6": "1200"}),+ costResume("2026-06-10T11:00:00.000Z"),+ costPrompt("u2", "2026-06-10T11:00:05.000Z"),+ // Cumulative: 4.1 credits in total, 1.6 of them new.+ costShutdown("x2", "2026-06-10T11:20:00.000Z", postCutoverMS, "4100000000", map[string]string{"claude-sonnet-4.6": "2000"}),+ // Repeated shutdown with no new spend must add nothing.+ costShutdown("x3", "2026-06-10T11:20:01.000Z", postCutoverMS, "4100000000", map[string]string{"claude-sonnet-4.6": "2000"}),+ })+ got := costEvents(mapCostSession(t, root))+ if len(got) != 2 {+ t.Fatalf("cost events = %+v, want two (one per shutdown with new spend)", got)+ }+ if got[0].cost != 0.025 || got[1].cost != 0.016 {+ t.Fatalf("costs = %v, %v, want 0.025, 0.016", got[0].cost, got[1].cost)+ }+}++func TestShutdownCostResetRebasesWithoutOverReporting(t *testing.T) {+ root := t.TempDir()+ writeCostSession(t, root, []string{+ costStart(postCutoverISO),+ costShutdown("x1", "2026-06-10T09:10:00.000Z", postCutoverMS, "3000000000", map[string]string{"claude-sonnet-4.6": "1200"}),+ costResume("2026-06-10T11:00:00.000Z"),+ // The cumulative total went backwards. Nothing is emitted for it,+ // and the lower value becomes the baseline.+ costShutdown("x2", "2026-06-10T11:20:00.000Z", postCutoverMS, "1000000000", map[string]string{"claude-sonnet-4.6": "1200"}),+ costResume("2026-06-10T12:00:00.000Z"),+ costShutdown("x3", "2026-06-10T12:20:00.000Z", postCutoverMS, "1700000000", map[string]string{"claude-sonnet-4.6": "1200"}),+ })+ got := costEvents(mapCostSession(t, root))+ if len(got) != 2 {+ t.Fatalf("cost events = %+v, want two", got)+ }+ if got[0].cost != 0.03 || got[1].cost != 0.007 {+ t.Fatalf("costs = %v, %v, want 0.03, 0.007", got[0].cost, got[1].cost)+ }+}++func TestPreCutoverSessionRecordsNoCost(t *testing.T) {+ root := t.TempDir()+ writeCostSession(t, root, []string{+ costStart(preCutoverISO),+ costPrompt("u1", "2026-05-20T09:00:05.000Z"),+ costShutdown("x1", "2026-05-20T09:10:00.000Z", preCutoverMS, "2500000000", map[string]string{"claude-sonnet-4.6": "1200"}),+ // Resumed after the cutover: still a premium-request session.+ costResume("2026-06-03T10:00:00.000Z"),+ costShutdown("x2", "2026-06-03T10:10:00.000Z", preCutoverMS, "5000000000", map[string]string{"claude-sonnet-4.6": "2400"}),+ })+ events := mapCostSession(t, root)+ if got := costEvents(events); len(got) != 0 {+ t.Fatalf("pre-cutover session recorded cost: %+v", got)+ }+ // Tokens are still recorded, and the raw billing total is kept.+ var sawRaw bool+ for _, item := range events {+ raw, _ := item.Event.Raw["copilot"].(map[string]interface{})+ if raw["type"] == "session.shutdown" && raw["cumulative_total_nano_aiu"] != nil {+ sawRaw = true+ }+ }+ if !sawRaw {+ t.Fatal("raw cumulative_total_nano_aiu missing on pre-cutover shutdown usage")+ }+}++func TestSessionWithoutKnownStartRecordsNoCost(t *testing.T) {+ root := t.TempDir()+ // No session.start and no sessionStartTime: the billing regime is unknown.+ writeCostSession(t, root, []string{+ `{"id":"x1","timestamp":"2026-06-10T09:10:00.000Z","type":"session.shutdown","data":{"shutdownType":"routine","totalNanoAiu":2500000000,"modelMetrics":{"claude-sonnet-4.6":{"requests":{"count":1,"cost":1},"usage":{"inputTokens":10,"outputTokens":0}}}}}`,+ })+ if got := costEvents(mapCostSession(t, root)); len(got) != 0 {+ t.Fatalf("cost recorded without a known session start: %+v", got)+ }+}++func TestMultiModelShutdownCountsSessionCostOnce(t *testing.T) {+ root := t.TempDir()+ writeCostSession(t, root, []string{+ costStart(postCutoverISO),+ costShutdown("x1", "2026-06-10T09:10:00.000Z", postCutoverMS, "7300000000", map[string]string{+ "claude-sonnet-4.6": "1200",+ "gpt-5.3-codex": "800",+ "claude-haiku-4.5": "300",+ }),+ })+ events := mapCostSession(t, root)+ got := costEvents(events)+ if len(got) != 1 {+ t.Fatalf("cost events = %+v, want exactly one for a multi-model shutdown", got)+ }+ if got[0].cost != 0.073 {+ t.Fatalf("cost = %v, want 0.073", got[0].cost)+ }+ // The total is session-level, so it is not pinned on any one model.+ if got[0].model != "" {+ t.Fatalf("multi-model session cost attributed to model %q", got[0].model)+ }+ var modelUsage int+ for _, item := range events {+ if item.Event.Event.Action == "token.usage" && item.Event.Model != "" {+ modelUsage+++ }+ }+ if modelUsage != 3 {+ t.Fatalf("per-model token usage events = %d, want 3", modelUsage)+ }+}++func TestNanoAiuParsingIsExact(t *testing.T) {+ for _, tc := range []struct {+ in interface{}+ want int64+ ok bool+ }{+ {float64(2500000000), 2500000000, true},+ {json.Number("123456789012"), 123456789012, true},+ {"42", 42, true},+ {float64(10.4), 10, true},+ {float64(-1), 0, false},+ {nil, 0, false},+ {"abc", 0, false},+ } {+ got, ok := nanoAiuValue(tc.in)+ if got != tc.want || ok != tc.ok {+ t.Fatalf("nanoAiuValue(%#v) = %d, %v; want %d, %v", tc.in, got, ok, tc.want, tc.ok)+ }+ }+ if got := nanoAiuToUSD(1); got != 1e-11 {+ t.Fatalf("1 nano-AIU = %v USD, want 1e-11", got)+ }+}++func readRuntimeLog(t *testing.T, path string) []schema.Event {+ t.Helper()+ f, err := os.Open(path)+ if err != nil {+ t.Fatal(err)+ }+ defer f.Close()+ var out []schema.Event+ scanner := bufio.NewScanner(f)+ scanner.Buffer(make([]byte, 0, 1024*1024), 8*1024*1024)+ for scanner.Scan() {+ var ev schema.Event+ if err := json.Unmarshal(scanner.Bytes(), &ev); err != nil {+ t.Fatalf("decode runtime log line: %v", err)+ }+ out = append(out, ev)+ }+ if err := scanner.Err(); err != nil {+ t.Fatal(err)+ }+ return out+}++func TestCopilotCostReachesTokenReportExactlyOnceAcrossResyncs(t *testing.T) {+ root := t.TempDir()+ path := writeCostSession(t, root, []string{+ costStart(postCutoverISO),+ costPrompt("u1", "2026-06-10T09:00:05.000Z"),+ costShutdown("x1", "2026-06-10T09:10:00.000Z", postCutoverMS, "2500000000", map[string]string{"claude-sonnet-4.6": "1200"}),+ })+ statePath := filepath.Join(root, "state.json")+ logPath := filepath.Join(root, "runtime.jsonl")+ opts := CollectOptions{CopilotDir: root, StatePath: statePath, LogPath: logPath, Write: true, UserMode: true}+ if _, err := CollectOnce(opts); err != nil {+ t.Fatal(err)+ }+ report := tokens.Aggregate(readRuntimeLog(t, logPath), tokens.Options{})+ if report.Totals.CostUSD != float64(2500000000)/1e11 {+ t.Fatalf("totals.cost_usd after first sync = %v, want %v", report.Totals.CostUSD, float64(2500000000)/1e11)+ }++ // Copilot resumes the session and appends a second, cumulative shutdown.+ f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0)+ if err != nil {+ t.Fatal(err)+ }+ for _, line := range []string{+ costResume("2026-06-10T11:00:00.000Z"),+ costPrompt("u2", "2026-06-10T11:00:05.000Z"),+ costShutdown("x2", "2026-06-10T11:20:00.000Z", postCutoverMS, "4000000000", map[string]string{"claude-sonnet-4.6": "2000", "gpt-5.3-codex": "500"}),+ } {+ if _, err := f.WriteString(line + "\n"); err != nil {+ t.Fatal(err)+ }+ }+ _ = f.Close()+ if _, err := CollectOnce(opts); err != nil {+ t.Fatal(err)+ }+ // A re-run with nothing new must not add cost.+ if summary, err := CollectOnce(opts); err != nil || summary.EventsEmitted != 0 {+ t.Fatalf("idle re-sync emitted %d events, err %v", summary.EventsEmitted, err)+ }+ report = tokens.Aggregate(readRuntimeLog(t, logPath), tokens.Options{})+ if report.Totals.CostUSD != float64(4000000000)/1e11 {+ t.Fatalf("totals.cost_usd after resume = %v, want %v", report.Totals.CostUSD, float64(4000000000)/1e11)+ }+ if report.Totals.InputTokens != 2500 {+ t.Fatalf("totals.input_tokens = %d, want 2500", report.Totals.InputTokens)+ }+}diff --git a/cli/beacon/internal/copilotsession/mapper.go b/cli/beacon/internal/copilotsession/mapper.goindex 6e8e536d..ff5f2fe1 100644--- a/cli/beacon/internal/copilotsession/mapper.go+++ b/cli/beacon/internal/copilotsession/mapper.go@@ -26,6 +26,11 @@ type mapper struct {lastUsage map[string]usageTotalsoutputSinceLastTotals map[string]int64++ // sessionStartMS decides whether the session is billed in AI credits;+ // lastNanoAiu is the cumulative totalNanoAiu of the last shutdown seen.+ sessionStartMS int64+ lastNanoAiu int64}type sessionContext struct {@@ -94,6 +99,9 @@ func sessionFromRef(ref SessionRef) sessionContext {func (m *mapper) consumeContext(record Record) {switch record.Type {case "session.start", "session.resume":+ if record.Type == "session.start" {+ m.noteSessionStart(record)+ }m.applySessionData(record.Data)case "session.model_change":if model := firstString(record.Data, "newModel", "selectedModel"); model != "" {@@ -360,10 +368,28 @@ func (m *mapper) rememberShutdownTotals(record Record) {m.lastUsage[model] = totalm.outputSinceLastTotals[model] = 0}+ m.shutdownCostDelta(record)}+// emitShutdownUsage differences the shutdown's cumulative totals into usage+// events. Tokens are per model. Cost is not: totalNanoAiu is one session-wide+// total, so it is recorded once per shutdown and never copied onto each model.+// When the shutdown names a single model the cost rides that model's event,+// because the whole session total is then that model's; otherwise it goes on+// one dedicated usage event with no model, so the totals still sum exactly.func (m *mapper) emitShutdownUsage(record Record) {models := shutdownUsage(record.Data)+ costNano, costOmitted := m.shutdownCostDelta(record)+ costRaw := func(raw map[string]interface{}) map[string]interface{} {+ if costNano > 0 {+ raw["cost_source"] = "session_shutdown_total_nano_aiu_delta"+ raw["nano_aiu_delta"] = costNano+ } else if costOmitted != "" {+ raw["cost_omitted"] = costOmitted+ }+ return raw+ }+ costOnModel := costNano > 0 && len(models) == 1for model, current := range models {previous := m.lastUsage[model]outputAlreadyReported := m.outputSinceLastTotals[model]@@ -383,6 +409,10 @@ func (m *mapper) emitShutdownUsage(record Record) {if n := delta(current.Reasoning, previous.Reasoning); n > 0 {usage.Reasoning = &schema.GenAIUsageReasoningInfo{OutputTokens: &n}}+ if costOnModel {+ cost := nanoAiuToUSD(costNano)+ usage.CostUSD = &cost+ }m.lastUsage[model] = currentm.outputSinceLastTotals[model] = 0if emptyUsage(usage) {@@ -391,14 +421,28 @@ func (m *mapper) emitShutdownUsage(record Record) {ev := m.base(record, "token.usage", "metric", schema.SeverityInfo, "GitHub Copilot CLI shutdown token usage")ev.Model = modelev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) { genAI.Usage = usage })- ev.Raw = m.raw(record, map[string]interface{}{+ ev.Raw = m.raw(record, costRaw(map[string]interface{}{"token_source": "session_shutdown_model_metrics_delta","output_tokens_already_reported": outputAlreadyReported,"cumulative_total_premium_requests": numberValue(record.Data["totalPremiumRequests"]),"cumulative_total_nano_aiu": numberValue(record.Data["totalNanoAiu"]),- })+ }))m.append(record, "shutdown.usage."+model, ev)}+ if costNano <= 0 || costOnModel {+ return+ }+ cost := nanoAiuToUSD(costNano)+ ev := m.base(record, "token.usage", "metric", schema.SeverityInfo, "GitHub Copilot CLI session cost")+ ev.Model = ""+ ev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) {+ genAI.Usage = &schema.GenAIUsageInfo{CostUSD: &cost}+ })+ ev.Raw = m.raw(record, costRaw(map[string]interface{}{+ "cumulative_total_premium_requests": numberValue(record.Data["totalPremiumRequests"]),+ "cumulative_total_nano_aiu": numberValue(record.Data["totalNanoAiu"]),+ }))+ m.append(record, "shutdown.cost", ev)}func (m *mapper) append(record Record, suffix string, ev schema.Event) {diff --git a/cli/beacon/internal/tokens/coverage.go b/cli/beacon/internal/tokens/coverage.goindex f64475cb..7d2026ac 100644--- a/cli/beacon/internal/tokens/coverage.go+++ b/cli/beacon/internal/tokens/coverage.go@@ -84,7 +84,7 @@ var usageExpectation = map[string]struct {"openclaw_gateway": {ExpectReported, "plugin reports usage per model response when conversation access is granted"},"vercel_fx": {ExpectReported, "session store carries cumulative usage and cost"},"deepseek_harness": {ExpectReported, "native session backfill reports usage when DeepSeek persists it"},- "copilot_cli": {ExpectReported, "session store carries output tokens and cumulative model usage"},+ "copilot_cli": {ExpectReported, "session store carries output tokens, cumulative model usage, and AI-credit cost"},"asymptote_observe": {ExpectReported, "SDK spans carry semconv usage"},"gemini_cli": {ExpectGenericOTLP, "only if it emits OTel GenAI semconv usage"},diff --git a/docs/cli/token-usage.mdx b/docs/cli/token-usage.mdxindex bf2e42b2..3c26469c 100644--- a/docs/cli/token-usage.mdx+++ b/docs/cli/token-usage.mdx@@ -49,12 +49,12 @@ from. A turn with no token attribution is a turn whose runtime never reported an| Prime Agent | input, output, cache read, cache creation, reasoning | Reported (`cost_usd`) | Yes | Yes || DeepSeek Harness | input, output, cache read, cache creation | Not reported | Yes, from committed session records | Yes || fx (Vercel Labs) | input, output, cache read, cache creation | Reported (`cost_usd`) | Yes | Yes |-| GitHub Copilot CLI | input, output, cache read, cache creation, reasoning | Not reported | Yes | Yes |+| GitHub Copilot CLI | input, output, cache read, cache creation, reasoning | Reported (`cost_usd`) for sessions started on or after 2026-06-01, from AI credits | Yes | Yes || OpenClaw Gateway | input, output, cache read, cache creation | Not reported on the hook | Yes, per model response | Yes |OpenClaw reports usage on its `llm_output` hook, which is a *conversation* hook -- OpenClaw withholds those from every non-bundled plugin until `plugins.entries.beacon-endpoint.hooks.allowConversationAccess` is set in its config. An OpenClaw endpoint collecting prompts, tools, commands and files while reporting no tokens at all is almost always that key, and `beacon endpoint hooks status --harness openclaw` says so. OpenClaw does compute a per-turn price, from an operator-configured cost table rather than from a provider report, so Beacon does not record it as a reported cost. See [OpenClaw Gateway](/runtimes/openclaw-gateway).-Cline reports usage once per task rather than per model call, because Beacon's rollups sum `gen_ai.usage` across events and reporting at both granularities would count every token twice. fx reports cumulative totals, which Beacon differences into per-turn deltas for the same reason. GitHub Copilot CLI reports assistant message output tokens and cumulative shutdown model totals; Beacon records the message output directly, then differences shutdown totals for input, cache, reasoning, and any output not already counted.+Cline reports usage once per task rather than per model call, because Beacon's rollups sum `gen_ai.usage` across events and reporting at both granularities would count every token twice. fx reports cumulative totals, which Beacon differences into per-turn deltas for the same reason. GitHub Copilot CLI reports assistant message output tokens and cumulative shutdown model totals; Beacon records the message output directly, then differences shutdown totals for input, cache, reasoning, and any output not already counted. Cost comes from the shutdown's session-wide `totalNanoAiu`, differenced the same way and converted at one AI credit = 10^9 nano-AIU = US$0.01. It is recorded once per shutdown: on the model's usage event when the shutdown names one model, otherwise on one usage event with no model, so a multi-model session is not counted once per model. Sessions started before GitHub moved Copilot to AI credits on 2026-06-01 were billed per premium request, so they record no cost; the raw nano-AIU total stays in `raw.copilot`. Annual Copilot Pro and Pro+ subscribers stay on premium-request billing until their plan renews, and the session record does not say which plan applies, so for them the recorded cost is what the session would cost in AI credits rather than their bill.**Collected through the generic OpenTelemetry path, not yet verified against captured payloads:**diff --git a/docs/runtimes/github-copilot-cli.mdx b/docs/runtimes/github-copilot-cli.mdxindex c7406429..131316f2 100644--- a/docs/runtimes/github-copilot-cli.mdx+++ b/docs/runtimes/github-copilot-cli.mdx@@ -64,6 +64,7 @@ For a one-command local validation flow, see [Test GitHub Copilot CLI telemetry]| Command, tool, and file telemetry | OTLP covers live tool and approval-like activity. Polling reconstructs complete tool calls/results, commands, and file reads or writes from session records || Approvals | Supported from live OTLP permission activity. Polling does not synthesize approval decisions || Token usage | Supported from local session polling. Assistant message output tokens are recorded directly; shutdown model totals are differenced for input, cache, and reasoning usage |+| Cost | Supported from local session polling for sessions started on or after 2026-06-01, when Copilot billing moved to AI credits. The shutdown's session-wide `totalNanoAiu` is differenced and recorded once as `gen_ai.usage.cost_usd` (10^9 nano-AIU = one AI credit = US$0.01). Earlier sessions were billed per premium request and record no cost. See [Token usage](/cli/token-usage) || Local JSONL and dashboard | Supported after events reach the local runtime log || MDM deployment | Supported for the endpoint agent. Copilot launch-environment configuration stays under MDM or customer policy control |