agent-dealer 1.4.1 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -45,7 +45,7 @@ import { getWorkItem } from "../repository/work-items.js";
45
45
  import { listFindingsForIssue } from "../repository/findings.js";
46
46
  import { createIssueArtifact, latestIssueArtifact } from "../repository/artifacts.js";
47
47
  import { recordUsageEvent } from "../repository/usage-events.js";
48
- import { extractSpawnUsage } from "./usage.js";
48
+ import { extractSpawnUsage, totalTokensInFromMuseUsage } from "./usage.js";
49
49
  import { emitCheckpointObserved, emitRetryReuse } from "./checkpoint.js";
50
50
  import { syncIssueBaseBranch } from "./sync-issue-base-branch.js";
51
51
  import { emitAgentCompleted, emitAgentStarted } from "./agent-boundaries.js";
@@ -1566,7 +1566,8 @@ export async function runDeveloperEffect(ctx, deps = defaultDeps) {
1566
1566
  // null when it did not report them and cost is always null (Muse has no cost figure, NOT-177).
1567
1567
  const usage = spawned.muse
1568
1568
  ? {
1569
- tokensIn: spawned.muse.usage.inputTokens,
1569
+ // Muse input excludes cache (same as Claude); sum so TOKENS IN matches volume.
1570
+ tokensIn: totalTokensInFromMuseUsage(spawned.muse.usage),
1570
1571
  tokensOut: spawned.muse.usage.outputTokens,
1571
1572
  costUsd: spawned.muse.usage.costUsd,
1572
1573
  }
@@ -325,7 +325,8 @@ test("usage events record muse_code, the confirmed model, duration and tokens; c
325
325
  assert.equal(usage.runtime, "muse_code");
326
326
  assert.equal(usage.model, MUSE_CODE_CONTRIBUTOR_MODEL);
327
327
  assert.ok(usage.durationMs > 0);
328
- assert.equal(usage.tokensIn, 1000);
328
+ // fake-muse success USAGE: input 1000 + cache_read 50 + cache_write 0 (disjoint, like Claude).
329
+ assert.equal(usage.tokensIn, 1050);
329
330
  assert.equal(usage.tokensOut, 200);
330
331
  assert.equal(usage.costUsd, null);
331
332
  });
@@ -2,6 +2,39 @@ import { extractResultText, parseNdjsonFile } from "../runners/stream-json.js";
2
2
  import { normalizeCodexEvents, parseCodexJsonl } from "../runners/codex-jsonl.js";
3
3
  import fs from "node:fs";
4
4
  const EMPTY = { tokensIn: null, tokensOut: null, costUsd: null };
5
+ function asFiniteNumber(v) {
6
+ return typeof v === "number" && Number.isFinite(v) ? v : null;
7
+ }
8
+ /**
9
+ * Total input tokens for `usage_events.tokens_in` / execution reports.
10
+ *
11
+ * Providers disagree on whether cache is inside `input_tokens`:
12
+ * - Codex: `input_tokens` already includes `cached_input_tokens` (subset) — use input alone.
13
+ * - Claude / Cursor / Muse: uncached `input_tokens` plus cache read/write are disjoint — sum them.
14
+ *
15
+ * Without the sum, Claude sessions store ~tens of input tokens while cost/`tokens_out`
16
+ * look normal, so the report's TOKENS IN column reads as nonsense (e.g. 16.9K vs 7.7M out).
17
+ */
18
+ export function totalTokensInFromUsage(usage, runtime) {
19
+ const input = asFiniteNumber(usage.input_tokens ?? usage.inputTokens);
20
+ if (input === null)
21
+ return null;
22
+ if (runtime === "codex_local")
23
+ return input;
24
+ const cacheRead = asFiniteNumber(usage.cache_read_input_tokens ?? usage.cacheReadTokens) ?? 0;
25
+ const cacheWrite = asFiniteNumber(usage.cache_creation_input_tokens ??
26
+ usage.cache_write_input_tokens ??
27
+ usage.cacheWriteTokens ??
28
+ usage.cacheCreationInputTokens) ?? 0;
29
+ return input + cacheRead + cacheWrite;
30
+ }
31
+ /** Muse's parsed usage: same additive cache rule as Claude (input excludes cache). */
32
+ export function totalTokensInFromMuseUsage(usage) {
33
+ const parts = [usage.inputTokens, usage.cacheReadTokens, usage.cacheWriteTokens];
34
+ if (parts.every((p) => p === null))
35
+ return null;
36
+ return parts.reduce((sum, p) => sum + (p ?? 0), 0);
37
+ }
5
38
  /** Best-effort: a missing/unparseable log never blocks recording the session's duration. */
6
39
  export function extractSpawnUsage(logPath, runtime) {
7
40
  if (!fs.existsSync(logPath))
@@ -12,8 +45,8 @@ export function extractSpawnUsage(logPath, runtime) {
12
45
  const result = events.find((e) => e.type === "result");
13
46
  const usage = result?.usage ?? {};
14
47
  return {
15
- tokensIn: usage.input_tokens ?? usage.inputTokens ?? null,
16
- tokensOut: usage.output_tokens ?? usage.outputTokens ?? null,
48
+ tokensIn: totalTokensInFromUsage(usage, runtime),
49
+ tokensOut: asFiniteNumber(usage.output_tokens ?? usage.outputTokens),
17
50
  costUsd: typeof result?.total_cost_usd === "number" ? result.total_cost_usd : null,
18
51
  };
19
52
  }
@@ -4,7 +4,7 @@ import assert from "node:assert/strict";
4
4
  import fs from "node:fs";
5
5
  import os from "node:os";
6
6
  import path from "node:path";
7
- import { extractResultTranscript, extractSpawnUsage } from "./usage.js";
7
+ import { extractResultTranscript, extractSpawnUsage, totalTokensInFromMuseUsage, totalTokensInFromUsage, } from "./usage.js";
8
8
  function writeLog(lines) {
9
9
  const file = path.join(fs.mkdtempSync(path.join(os.tmpdir(), "dealer-usage-")), "session.ndjson");
10
10
  fs.writeFileSync(file, lines.map((l) => JSON.stringify(l)).join("\n"));
@@ -21,15 +21,63 @@ test("extractSpawnUsage reads claude-shaped stream-json usage/cost", () => {
21
21
  assert.equal(usage.tokensOut, 200);
22
22
  assert.equal(usage.costUsd, 0.0421);
23
23
  });
24
+ test("extractSpawnUsage sums Claude cache read/creation into tokensIn", () => {
25
+ // Real Claude Code result shape: input_tokens is uncached-only; almost all volume is cache.
26
+ const logPath = writeLog([
27
+ {
28
+ type: "result",
29
+ total_cost_usd: 0.700155,
30
+ usage: {
31
+ input_tokens: 22,
32
+ cache_creation_input_tokens: 55468,
33
+ cache_read_input_tokens: 629415,
34
+ output_tokens: 6522,
35
+ },
36
+ },
37
+ ]);
38
+ const usage = extractSpawnUsage(logPath, "claude_code");
39
+ assert.equal(usage.tokensIn, 22 + 55468 + 629415);
40
+ assert.equal(usage.tokensOut, 6522);
41
+ assert.equal(usage.costUsd, 0.700155);
42
+ });
43
+ test("extractSpawnUsage sums Cursor camelCase cache fields into tokensIn", () => {
44
+ const logPath = writeLog([
45
+ {
46
+ type: "result",
47
+ usage: {
48
+ inputTokens: 86839,
49
+ cacheReadTokens: 1314304,
50
+ cacheWriteTokens: 0,
51
+ outputTokens: 14718,
52
+ },
53
+ },
54
+ ]);
55
+ const usage = extractSpawnUsage(logPath, "cursor_local");
56
+ assert.equal(usage.tokensIn, 86839 + 1314304);
57
+ assert.equal(usage.tokensOut, 14718);
58
+ });
24
59
  test("extractSpawnUsage reads codex-shaped jsonl usage (no cost field)", () => {
25
60
  const logPath = writeLog([
26
61
  { type: "turn.completed", usage: { input_tokens: 500, cached_input_tokens: 100, output_tokens: 50 } },
27
62
  ]);
28
63
  const usage = extractSpawnUsage(logPath, "codex_local");
64
+ // Codex input_tokens already includes cached_input_tokens — do not double-count.
29
65
  assert.equal(usage.tokensIn, 500);
30
66
  assert.equal(usage.tokensOut, 50);
31
67
  assert.equal(usage.costUsd, null);
32
68
  });
69
+ test("totalTokensInFromUsage does not double-count Codex cache after normalize", () => {
70
+ // After normalizeCodexEvents, cached_input_tokens becomes cache_read_input_tokens on the result.
71
+ assert.equal(totalTokensInFromUsage({ input_tokens: 24763, cache_read_input_tokens: 24448, output_tokens: 122 }, "codex_local"), 24763);
72
+ });
73
+ test("totalTokensInFromMuseUsage sums disjoint cache fields", () => {
74
+ assert.equal(totalTokensInFromMuseUsage({
75
+ inputTokens: 17770,
76
+ cacheReadTokens: 5105,
77
+ cacheWriteTokens: 100,
78
+ }), 17770 + 5105 + 100);
79
+ assert.equal(totalTokensInFromMuseUsage({ inputTokens: null, cacheReadTokens: null, cacheWriteTokens: null }), null);
80
+ });
33
81
  test("extractSpawnUsage returns nulls for a missing log file", () => {
34
82
  const usage = extractSpawnUsage("/does/not/exist.ndjson", "claude_code");
35
83
  assert.deepEqual(usage, { tokensIn: null, tokensOut: null, costUsd: null });
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-dealer/server",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -23,7 +23,7 @@
23
23
  "test": "AGENT_DEALER_SKIP_GITHUB_HEALTH=1 AGENT_DEALER_SKIP_AGENT_HEALTH=1 npm run build -w @agent-dealer/shared && AGENT_DEALER_SKIP_GITHUB_HEALTH=1 AGENT_DEALER_SKIP_AGENT_HEALTH=1 node ../../scripts/run-workspace-tests.mjs"
24
24
  },
25
25
  "dependencies": {
26
- "@agent-dealer/shared": "1.4.1",
26
+ "@agent-dealer/shared": "1.4.2",
27
27
  "@fastify/cors": "^11.0.1",
28
28
  "@fastify/static": "^8.2.0",
29
29
  "@modelcontextprotocol/sdk": "^1.29.0",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-dealer/shared",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-dealer",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "description": "Human control plane for agent execution — queue, plan approval, audit",
5
5
  "license": "MIT",
6
6
  "type": "module",