@loucompanion/forge-bridge 0.1.3-dev.fbe15023cdfe → 0.1.4-dev.028808a841c8
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.
- package/README.md +89 -3
- package/dist/bridge/bridge-chat.types.d.ts +111 -0
- package/dist/bridge/bridge-chat.types.js +401 -0
- package/dist/bridge/bridge.base.d.ts +4 -1
- package/dist/bridge/bridge.service.d.ts +83 -3
- package/dist/bridge/bridge.service.js +1609 -152
- package/dist/bridge/bridge.types.d.ts +52 -1
- package/dist/bridge/dispatch-result-outbox.d.ts +35 -0
- package/dist/bridge/dispatch-result-outbox.js +87 -0
- package/dist/bridge/dispatch-result-worker.d.ts +2 -0
- package/dist/bridge/dispatch-result-worker.js +122 -0
- package/dist/bridge/internals.d.ts +2 -0
- package/dist/bridge/internals.js +1 -0
- package/dist/bridge/readiness-result-outbox.d.ts +21 -0
- package/dist/bridge/readiness-result-outbox.js +54 -0
- package/dist/bridge/readiness-result-worker.d.ts +2 -0
- package/dist/bridge/readiness-result-worker.js +121 -0
- package/dist/bridge/runner-host-capabilities.d.ts +15 -0
- package/dist/bridge/runner-host-capabilities.js +93 -0
- package/dist/cleanup/cleanup.service.d.ts +1 -0
- package/dist/cleanup/cleanup.service.js +70 -9
- package/dist/cli/cli.help.js +4 -4
- package/dist/cli/cli.service.d.ts +14 -6
- package/dist/cli/cli.service.js +75 -16
- package/dist/cli/local-config.d.ts +1 -0
- package/dist/cli/local-config.js +4 -0
- package/dist/runtime/aspnet-runtime-prebuild.d.ts +36 -0
- package/dist/runtime/aspnet-runtime-prebuild.js +341 -0
- package/dist/runtime/attempt-database-broker.d.ts +89 -0
- package/dist/runtime/attempt-database-broker.js +390 -0
- package/dist/runtime/attempt-node-runtime-launcher.d.ts +2 -1
- package/dist/runtime/attempt-node-runtime-launcher.js +57 -18
- package/dist/runtime/attempt-path-operation.d.ts +21 -0
- package/dist/runtime/attempt-path-operation.js +94 -0
- package/dist/runtime/attempt-private-directory.d.ts +6 -0
- package/dist/runtime/attempt-private-directory.js +54 -0
- package/dist/runtime/attempt-process-supervisor.d.ts +35 -0
- package/dist/runtime/attempt-process-supervisor.js +144 -23
- package/dist/runtime/canonical-source-file.d.ts +3 -0
- package/dist/runtime/canonical-source-file.js +54 -0
- package/dist/runtime/docker-engine-probe.d.ts +90 -0
- package/dist/runtime/docker-engine-probe.js +333 -0
- package/dist/runtime/generated-output-reset.d.ts +10 -0
- package/dist/runtime/generated-output-reset.js +62 -8
- package/dist/runtime/host-database-capability-boundary.d.ts +16 -0
- package/dist/runtime/host-database-capability-boundary.js +63 -0
- package/dist/runtime/host-diagnostics.d.ts +11 -0
- package/dist/runtime/host-diagnostics.js +192 -0
- package/dist/runtime/host-execution-root.d.ts +4 -0
- package/dist/runtime/host-execution-root.js +39 -0
- package/dist/runtime/host-facts.d.ts +35 -0
- package/dist/runtime/host-facts.js +95 -0
- package/dist/runtime/host-listener-inspector.d.ts +8 -1
- package/dist/runtime/host-listener-inspector.js +121 -60
- package/dist/runtime/host-load-sampler.d.ts +83 -0
- package/dist/runtime/host-load-sampler.js +164 -0
- package/dist/runtime/host-pressure-stall.d.ts +41 -0
- package/dist/runtime/host-pressure-stall.js +121 -0
- package/dist/runtime/host-runtime-contract.d.ts +13 -1
- package/dist/runtime/host-runtime-contract.js +170 -55
- package/dist/runtime/host-runtime-lease.d.ts +92 -8
- package/dist/runtime/host-runtime-lease.js +1193 -120
- package/dist/runtime/host-runtime-promotion.d.ts +12 -3
- package/dist/runtime/host-runtime-promotion.js +59 -12
- package/dist/runtime/managed-command-teardown.d.ts +62 -0
- package/dist/runtime/managed-command-teardown.js +607 -0
- package/dist/runtime/memory-capacity.d.ts +70 -0
- package/dist/runtime/memory-capacity.js +123 -0
- package/dist/runtime/native-test-postgres-fallback.d.ts +25 -0
- package/dist/runtime/native-test-postgres-fallback.js +91 -0
- package/dist/runtime/node-host-runtime-adapter.d.ts +3 -0
- package/dist/runtime/node-host-runtime-adapter.js +29 -11
- package/dist/runtime/pinned-directory-process.d.ts +9 -0
- package/dist/runtime/pinned-directory-process.js +61 -0
- package/dist/runtime/pinned-source-directory.d.ts +17 -0
- package/dist/runtime/pinned-source-directory.js +94 -0
- package/dist/runtime/playwright-browser-cache.d.ts +39 -0
- package/dist/runtime/playwright-browser-cache.js +227 -0
- package/dist/runtime/playwright-host-runtime-adapter.d.ts +12 -0
- package/dist/runtime/playwright-host-runtime-adapter.js +100 -58
- package/dist/runtime/prepared-runtime-identity.d.ts +6 -0
- package/dist/runtime/prepared-runtime-identity.js +218 -0
- package/dist/runtime/prepared-runtime-readiness.d.ts +30 -0
- package/dist/runtime/prepared-runtime-readiness.js +285 -0
- package/dist/runtime/runtime-isolation-lease.js +4 -1
- package/dist/runtime/workspace-resource-estimator.d.ts +17 -0
- package/dist/runtime/workspace-resource-estimator.js +174 -0
- package/dist/sandbox/preflight.d.ts +31 -1
- package/dist/sandbox/preflight.js +237 -51
- package/dist/sandbox/repository-lock.d.ts +2 -1
- package/dist/sandbox/repository-lock.js +19 -9
- package/dist/sandbox/runtime.d.ts +6 -1
- package/dist/sandbox/runtime.js +137 -107
- package/dist/sandbox/sandbox.types.d.ts +7 -0
- package/dist/sandbox/workspace.service.js +2 -0
- package/dist/session/managed-attempt-tools.d.ts +42 -1
- package/dist/session/managed-attempt-tools.js +692 -40
- package/dist/session/managed-build-coordinator.d.ts +5 -0
- package/dist/session/managed-build-coordinator.js +109 -0
- package/dist/session/managed-build-receipt.d.ts +21 -0
- package/dist/session/managed-build-receipt.js +337 -0
- package/dist/session/managed-verification-timeouts.d.ts +8 -0
- package/dist/session/managed-verification-timeouts.js +17 -0
- package/dist/session/phase-completion.artifacts.d.ts +17 -0
- package/dist/session/phase-completion.artifacts.js +228 -22
- package/dist/session/phase-completion.d.ts +11 -1
- package/dist/session/phase-completion.helpers.d.ts +5 -0
- package/dist/session/phase-completion.helpers.js +30 -7
- package/dist/session/phase-completion.js +14 -3
- package/dist/session/phase-completion.prompts.js +17 -18
- package/dist/session/phase-completion.runtime.d.ts +3 -1
- package/dist/session/phase-completion.runtime.js +148 -34
- package/dist/session/phase-completion.schemas.js +49 -2
- package/dist/session/phase-completion.validation.d.ts +1 -0
- package/dist/session/phase-completion.validation.js +111 -21
- package/dist/session/pre-publication-process.d.ts +20 -0
- package/dist/session/pre-publication-process.js +97 -0
- package/dist/session/pre-publication-skipped-tests.d.ts +21 -0
- package/dist/session/pre-publication-skipped-tests.js +109 -0
- package/dist/session/pre-publication-validation.d.ts +75 -0
- package/dist/session/pre-publication-validation.js +631 -0
- package/dist/session/provider-cli/bridge-cli.conformance-fixtures.d.ts +72 -0
- package/dist/session/provider-cli/bridge-cli.conformance-fixtures.js +462 -0
- package/dist/session/provider-cli/ci-repair-readiness.d.ts +8 -0
- package/dist/session/provider-cli/ci-repair-readiness.js +23 -3
- package/dist/session/provider-cli/provider-cli.artifacts.d.ts +6 -3
- package/dist/session/provider-cli/provider-cli.artifacts.js +62 -22
- package/dist/session/provider-cli/provider-cli.deterministic.js +34 -2
- package/dist/session/provider-cli/provider-cli.merge-integrity.d.ts +89 -0
- package/dist/session/provider-cli/provider-cli.merge-integrity.js +310 -0
- package/dist/session/provider-cli/provider-cli.process.d.ts +80 -0
- package/dist/session/provider-cli/provider-cli.process.js +694 -28
- package/dist/session/provider-cli/provider-cli.prompts.d.ts +10 -0
- package/dist/session/provider-cli/provider-cli.prompts.js +151 -16
- package/dist/session/provider-cli/provider-cli.repair-epoch.d.ts +6 -0
- package/dist/session/provider-cli/provider-cli.repair-epoch.js +11 -0
- package/dist/session/provider-cli/provider-cli.runtime.d.ts +14 -1
- package/dist/session/provider-cli/provider-cli.runtime.js +54 -3
- package/dist/session/provider-cli/provider-cli.service.d.ts +30 -5
- package/dist/session/provider-cli/provider-cli.service.js +858 -77
- package/dist/session/provider-cli/provider-cli.types.d.ts +2 -0
- package/dist/session/provider-cli/provider-in-flight-work.d.ts +6 -0
- package/dist/session/provider-cli/provider-in-flight-work.js +47 -0
- package/dist/session/provider-cli/provider-output-state.d.ts +16 -0
- package/dist/session/provider-cli/provider-output-state.js +370 -17
- package/dist/session/provider-cli/provider-usage.d.ts +9 -1
- package/dist/session/provider-cli/provider-usage.js +28 -5
- package/dist/session/publishable-worktree-changes.d.ts +54 -0
- package/dist/session/publishable-worktree-changes.js +193 -0
- package/dist/session/session-redaction.js +434 -9
- package/dist/session/session.service.js +145 -23
- package/dist/session/session.types.d.ts +147 -2
- package/dist/session/session.types.js +599 -7
- package/dist/session/unreferenced-exports.d.ts +12 -0
- package/dist/session/unreferenced-exports.js +59 -0
- package/dist/shared/format/format-bytes.d.ts +1 -0
- package/dist/shared/format/format-bytes.js +9 -0
- package/dist/shared/git/git.js +2 -1
- package/dist/shared/path/path.d.ts +1 -0
- package/dist/shared/path/path.js +18 -0
- package/dist/shared/process/dotnet-toolchain.d.ts +17 -0
- package/dist/shared/process/dotnet-toolchain.js +307 -0
- package/dist/shared/process/host-process-recovery.js +3 -2
- package/dist/shared/process/host-process-supervisor.d.ts +2 -0
- package/dist/shared/process/host-process-supervisor.js +60 -11
- package/dist/shared/process/process.d.ts +2 -0
- package/dist/shared/process/process.js +12 -7
- package/dist/shared/provider-capabilities.d.ts +3 -0
- package/dist/shared/provider-capabilities.js +85 -0
- package/dist/shared/redaction/redaction.d.ts +1 -0
- package/dist/shared/redaction/redaction.js +39 -3
- package/dist/shared/time/monotonic.d.ts +1 -0
- package/dist/shared/time/monotonic.js +8 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/worktree/dependency-warmer.d.ts +41 -9
- package/dist/worktree/dependency-warmer.js +571 -22
- package/dist/worktree/index.d.ts +1 -1
- package/dist/worktree/story-qa-reference-hydrator.d.ts +4 -0
- package/dist/worktree/story-qa-reference-hydrator.js +253 -0
- package/dist/worktree/worktree.service.d.ts +7 -0
- package/dist/worktree/worktree.service.js +253 -21
- package/dist/worktree/worktree.types.d.ts +7 -1
- package/package.json +7 -5
- package/runtime/windows-job-supervisor.ps1 +380 -14
|
@@ -19,6 +19,8 @@ export interface ProviderCliDraftContext {
|
|
|
19
19
|
}
|
|
20
20
|
export interface ProviderCliRunResult {
|
|
21
21
|
readonly summary: string;
|
|
22
|
+
readonly resultCommitSha?: string | null;
|
|
23
|
+
readonly publishedCommitSha?: string | null;
|
|
22
24
|
readonly spec?: RunnerDispatchSpec | null;
|
|
23
25
|
readonly artifact?: RunnerDispatchArtifact | null;
|
|
24
26
|
readonly artifacts?: readonly RunnerDispatchArtifact[] | null;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* True while an attempt-owned managed command (a receipt under
|
|
3
|
+
* `<FORGE_LOG_DIR>/managed-commands/`) is still starting, queued or running.
|
|
4
|
+
* Unreadable or stale receipts do not count.
|
|
5
|
+
*/
|
|
6
|
+
export declare function hasNonterminalManagedCommand(logDirectory: string | undefined, nowMs?: number): Promise<boolean>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
const nonterminalManagedCommandStatuses = new Set(["starting", "running", "waiting_for_build_slot"]);
|
|
4
|
+
// A running receipt is rewritten every 5 s. One that has not changed for a minute
|
|
5
|
+
// belongs to a command whose supervisor is gone, so it no longer holds the provider open.
|
|
6
|
+
const runningReceiptStaleMs = 60_000;
|
|
7
|
+
// Starting and queued receipts are written once. They hold the provider open for at most
|
|
8
|
+
// the command's own timeout (the managed-command maximum when absent) plus a margin.
|
|
9
|
+
const maximumManagedCommandTimeoutMs = 2 * 60 * 60_000;
|
|
10
|
+
const queuedReceiptMarginMs = 60_000;
|
|
11
|
+
/**
|
|
12
|
+
* True while an attempt-owned managed command (a receipt under
|
|
13
|
+
* `<FORGE_LOG_DIR>/managed-commands/`) is still starting, queued or running.
|
|
14
|
+
* Unreadable or stale receipts do not count.
|
|
15
|
+
*/
|
|
16
|
+
export async function hasNonterminalManagedCommand(logDirectory, nowMs = Date.now()) {
|
|
17
|
+
if (!logDirectory?.trim())
|
|
18
|
+
return false;
|
|
19
|
+
const directory = path.join(logDirectory, "managed-commands");
|
|
20
|
+
const names = await fs.readdir(directory).catch(() => []);
|
|
21
|
+
for (const name of names) {
|
|
22
|
+
if (!name.endsWith(".receipt.json"))
|
|
23
|
+
continue;
|
|
24
|
+
if (await receiptHoldsProvider(path.join(directory, name), nowMs))
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
async function receiptHoldsProvider(receiptPath, nowMs) {
|
|
30
|
+
try {
|
|
31
|
+
const [stat, content] = await Promise.all([fs.stat(receiptPath), fs.readFile(receiptPath, "utf8")]);
|
|
32
|
+
const receipt = JSON.parse(content);
|
|
33
|
+
const status = typeof receipt.status === "string" ? receipt.status : "";
|
|
34
|
+
if (!nonterminalManagedCommandStatuses.has(status))
|
|
35
|
+
return false;
|
|
36
|
+
const ageMs = nowMs - stat.mtimeMs;
|
|
37
|
+
if (status === "running")
|
|
38
|
+
return ageMs <= runningReceiptStaleMs;
|
|
39
|
+
const timeoutMs = typeof receipt.timeout_ms === "number" && Number.isFinite(receipt.timeout_ms)
|
|
40
|
+
? receipt.timeout_ms
|
|
41
|
+
: maximumManagedCommandTimeoutMs;
|
|
42
|
+
return ageMs <= timeoutMs + queuedReceiptMarginMs;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -4,15 +4,31 @@ export declare function messageFromError(error: unknown): string;
|
|
|
4
4
|
export declare function clip(value: string, maxLength: number): string;
|
|
5
5
|
export interface ProviderOutputState {
|
|
6
6
|
readonly sessionLabel: string | null;
|
|
7
|
+
/** Stable provider conversation identifier used for an in-place repair turn. */
|
|
8
|
+
readonly providerThreadId: string | null;
|
|
7
9
|
finalMessage: string | null;
|
|
8
10
|
/** True only while the provider has reported a tool/command start without a matching completion. */
|
|
9
11
|
readonly hasActiveToolCall: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Forgets tool calls a previous provider generation started but never completed. A
|
|
14
|
+
* generation that exits mid-call reports no completion, and the next generation must not
|
|
15
|
+
* inherit that call as still in flight.
|
|
16
|
+
*/
|
|
17
|
+
resetActiveToolCalls(): void;
|
|
10
18
|
/** Accumulated provider-reported token usage for this run, or null when none was reported. */
|
|
11
19
|
readonly usage: ProviderUsage | null;
|
|
12
20
|
consume(line: string): readonly RunnerDispatchEvent[];
|
|
13
21
|
}
|
|
14
22
|
export declare function createProviderOutputState(provider: string): ProviderOutputState;
|
|
15
23
|
export declare function parseJsonLine(line: string): Record<string, unknown> | null;
|
|
24
|
+
/**
|
|
25
|
+
* Claude Code exposes MCP tools as `mcp__<server>__<tool>` and replaces characters outside
|
|
26
|
+
* `[A-Za-z0-9_-]` with `_`, so `forge.complete_qa` arrives as `mcp__forge__forge_complete_qa`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function parseClaudeMcpToolName(name: string): {
|
|
29
|
+
readonly server: string;
|
|
30
|
+
readonly tool: string;
|
|
31
|
+
} | null;
|
|
16
32
|
export declare function formatCodexMcpToolCallEvents(type: string, event: Record<string, unknown>): readonly RunnerDispatchEvent[];
|
|
17
33
|
export declare function objectField(source: Record<string, unknown>, key: string): Record<string, unknown>;
|
|
18
34
|
export declare function stringField(source: Record<string, unknown>, key: string): string | null;
|
|
@@ -16,9 +16,14 @@ export function clip(value, maxLength) {
|
|
|
16
16
|
return `${value.slice(0, maxLength - ellipsis.length)}${ellipsis}`;
|
|
17
17
|
}
|
|
18
18
|
export function createProviderOutputState(provider) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
const normalized = provider.trim().toLowerCase();
|
|
20
|
+
if (normalized === "cursor") {
|
|
21
|
+
return new CursorStreamJsonState();
|
|
22
|
+
}
|
|
23
|
+
if (normalized === "claude") {
|
|
24
|
+
return new ClaudeStreamJsonState();
|
|
25
|
+
}
|
|
26
|
+
return new CodexJsonState();
|
|
22
27
|
}
|
|
23
28
|
class CursorStreamJsonState {
|
|
24
29
|
sessionId = null;
|
|
@@ -29,12 +34,18 @@ class CursorStreamJsonState {
|
|
|
29
34
|
get sessionLabel() {
|
|
30
35
|
return this.sessionId ? `session ${this.sessionId}` : null;
|
|
31
36
|
}
|
|
37
|
+
get providerThreadId() {
|
|
38
|
+
return this.sessionId;
|
|
39
|
+
}
|
|
32
40
|
get usage() {
|
|
33
41
|
return this.accumulatedUsage;
|
|
34
42
|
}
|
|
35
43
|
get hasActiveToolCall() {
|
|
36
44
|
return this.activeToolCallCount > 0;
|
|
37
45
|
}
|
|
46
|
+
resetActiveToolCalls() {
|
|
47
|
+
this.activeToolCallCount = 0;
|
|
48
|
+
}
|
|
38
49
|
consume(line) {
|
|
39
50
|
const event = parseJsonLine(line);
|
|
40
51
|
if (!event) {
|
|
@@ -49,7 +60,8 @@ class CursorStreamJsonState {
|
|
|
49
60
|
role: "runner",
|
|
50
61
|
kind: "checkpoint",
|
|
51
62
|
content: `Cursor session started: ${this.sessionId}.`,
|
|
52
|
-
eventType: "provider_thread_started"
|
|
63
|
+
eventType: "provider_thread_started",
|
|
64
|
+
data: JSON.stringify({ threadId: this.sessionId })
|
|
53
65
|
}];
|
|
54
66
|
}
|
|
55
67
|
}
|
|
@@ -116,13 +128,19 @@ class CursorStreamJsonState {
|
|
|
116
128
|
if (Object.keys(usage).length === 0 && durationMs === 0) {
|
|
117
129
|
return;
|
|
118
130
|
}
|
|
131
|
+
const uncachedInputTokens = readUsageNumber(usage, "inputTokens");
|
|
132
|
+
const cacheReadTokens = readUsageNumber(usage, "cacheReadTokens");
|
|
133
|
+
const cacheCreationTokens = readUsageNumber(usage, "cacheWriteTokens");
|
|
119
134
|
this.accumulatedUsage = addUsage(this.accumulatedUsage ?? emptyUsage("provider_reported"), {
|
|
120
|
-
inputTokens:
|
|
135
|
+
inputTokens: uncachedInputTokens + cacheReadTokens + cacheCreationTokens,
|
|
136
|
+
uncachedInputTokens,
|
|
121
137
|
outputTokens: readUsageNumber(usage, "outputTokens"),
|
|
122
|
-
cacheReadTokens
|
|
123
|
-
cacheCreationTokens
|
|
138
|
+
cacheReadTokens,
|
|
139
|
+
cacheCreationTokens,
|
|
124
140
|
providerDurationMs: durationMs > 0 ? durationMs : null,
|
|
125
|
-
source: "provider_reported"
|
|
141
|
+
source: "provider_reported",
|
|
142
|
+
inputTokenSemantics: "excludes_cache_tokens",
|
|
143
|
+
rawUsage: [{ provider: "cursor", usage }]
|
|
126
144
|
});
|
|
127
145
|
}
|
|
128
146
|
}
|
|
@@ -225,6 +243,295 @@ export function parseJsonLine(line) {
|
|
|
225
243
|
? parsed
|
|
226
244
|
: null;
|
|
227
245
|
}
|
|
246
|
+
const claudeFileEditTools = new Set(["edit", "multiedit", "write", "notebookedit"]);
|
|
247
|
+
/**
|
|
248
|
+
* Parses `claude -p --output-format stream-json --verbose` output. Each line is one of:
|
|
249
|
+
* `system/init`, `assistant` (text + tool_use blocks), `user` (tool_result blocks), or the
|
|
250
|
+
* terminal `result` event that carries the aggregated usage for the whole run.
|
|
251
|
+
*/
|
|
252
|
+
class ClaudeStreamJsonState {
|
|
253
|
+
sessionId = null;
|
|
254
|
+
finalMessage = null;
|
|
255
|
+
emittedSession = false;
|
|
256
|
+
accumulatedUsage = null;
|
|
257
|
+
activeToolUses = new Map();
|
|
258
|
+
get sessionLabel() {
|
|
259
|
+
return this.sessionId ? `session ${this.sessionId}` : null;
|
|
260
|
+
}
|
|
261
|
+
get providerThreadId() {
|
|
262
|
+
return this.sessionId;
|
|
263
|
+
}
|
|
264
|
+
get usage() {
|
|
265
|
+
return this.accumulatedUsage;
|
|
266
|
+
}
|
|
267
|
+
get hasActiveToolCall() {
|
|
268
|
+
return this.activeToolUses.size > 0;
|
|
269
|
+
}
|
|
270
|
+
resetActiveToolCalls() {
|
|
271
|
+
this.activeToolUses.clear();
|
|
272
|
+
}
|
|
273
|
+
consume(line) {
|
|
274
|
+
const event = parseJsonLine(line);
|
|
275
|
+
if (!event) {
|
|
276
|
+
return [];
|
|
277
|
+
}
|
|
278
|
+
const type = stringField(event, "type");
|
|
279
|
+
if (type === "system" && stringField(event, "subtype") === "init") {
|
|
280
|
+
return this.consumeInit(event);
|
|
281
|
+
}
|
|
282
|
+
if (type === "assistant") {
|
|
283
|
+
return this.consumeAssistant(event);
|
|
284
|
+
}
|
|
285
|
+
if (type === "user") {
|
|
286
|
+
return this.consumeToolResults(event);
|
|
287
|
+
}
|
|
288
|
+
if (type === "result") {
|
|
289
|
+
return this.consumeResult(event, line);
|
|
290
|
+
}
|
|
291
|
+
if (type === "error") {
|
|
292
|
+
return [providerCliError(line)];
|
|
293
|
+
}
|
|
294
|
+
return [];
|
|
295
|
+
}
|
|
296
|
+
consumeInit(event) {
|
|
297
|
+
const events = [];
|
|
298
|
+
this.sessionId = stringField(event, "session_id") ?? this.sessionId;
|
|
299
|
+
if (this.sessionId && !this.emittedSession) {
|
|
300
|
+
this.emittedSession = true;
|
|
301
|
+
events.push({
|
|
302
|
+
role: "runner",
|
|
303
|
+
kind: "checkpoint",
|
|
304
|
+
content: `Claude session started: ${this.sessionId}.`,
|
|
305
|
+
eventType: "provider_thread_started"
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
const servers = Array.isArray(event.mcp_servers) ? event.mcp_servers : [];
|
|
309
|
+
for (const entry of servers) {
|
|
310
|
+
if (!entry || typeof entry !== "object") {
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
const server = entry;
|
|
314
|
+
const name = stringField(server, "name");
|
|
315
|
+
const status = stringField(server, "status");
|
|
316
|
+
if (name && status && status !== "connected") {
|
|
317
|
+
events.push({
|
|
318
|
+
role: "runner",
|
|
319
|
+
kind: "checkpoint",
|
|
320
|
+
content: `Claude MCP server ${name} is ${status}.`,
|
|
321
|
+
eventType: "provider_mcp_server_status",
|
|
322
|
+
data: JSON.stringify({ server_name: name, status })
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return events;
|
|
327
|
+
}
|
|
328
|
+
consumeAssistant(event) {
|
|
329
|
+
const events = [];
|
|
330
|
+
const text = assistantMessageText(event);
|
|
331
|
+
if (text) {
|
|
332
|
+
this.finalMessage = text;
|
|
333
|
+
events.push({
|
|
334
|
+
role: "assistant",
|
|
335
|
+
kind: "message",
|
|
336
|
+
content: clip(text, 4000),
|
|
337
|
+
eventType: "provider_message"
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
for (const block of messageContentBlocks(event)) {
|
|
341
|
+
if (stringField(block, "type") !== "tool_use") {
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
const id = stringField(block, "id");
|
|
345
|
+
const name = stringField(block, "name");
|
|
346
|
+
if (!id || !name) {
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
const toolUse = { name, input: objectField(block, "input") };
|
|
350
|
+
this.activeToolUses.set(id, toolUse);
|
|
351
|
+
events.push(...formatClaudeToolUseStarted(toolUse));
|
|
352
|
+
}
|
|
353
|
+
return events;
|
|
354
|
+
}
|
|
355
|
+
consumeToolResults(event) {
|
|
356
|
+
const events = [];
|
|
357
|
+
for (const block of messageContentBlocks(event)) {
|
|
358
|
+
if (stringField(block, "type") !== "tool_result") {
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
const id = stringField(block, "tool_use_id");
|
|
362
|
+
const toolUse = id ? this.activeToolUses.get(id) : undefined;
|
|
363
|
+
if (!id || !toolUse) {
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
this.activeToolUses.delete(id);
|
|
367
|
+
events.push(...formatClaudeToolUseCompleted(toolUse, block.is_error === true, toolResultText(block)));
|
|
368
|
+
}
|
|
369
|
+
return events;
|
|
370
|
+
}
|
|
371
|
+
consumeResult(event, line) {
|
|
372
|
+
this.activeToolUses.clear();
|
|
373
|
+
this.accumulateUsage(event);
|
|
374
|
+
const result = stringField(event, "result");
|
|
375
|
+
const subtype = stringField(event, "subtype") ?? "";
|
|
376
|
+
const failed = event.is_error === true || subtype.startsWith("error");
|
|
377
|
+
if (!failed) {
|
|
378
|
+
if (result) {
|
|
379
|
+
this.finalMessage = result;
|
|
380
|
+
}
|
|
381
|
+
return [{
|
|
382
|
+
role: "runner",
|
|
383
|
+
kind: "checkpoint",
|
|
384
|
+
content: "Claude turn completed.",
|
|
385
|
+
eventType: "provider_turn_completed"
|
|
386
|
+
}];
|
|
387
|
+
}
|
|
388
|
+
return [providerCliError(line)];
|
|
389
|
+
}
|
|
390
|
+
accumulateUsage(event) {
|
|
391
|
+
const usage = objectField(event, "usage");
|
|
392
|
+
const durationMs = readUsageNumber(event, "duration_ms") || readUsageNumber(event, "duration_api_ms");
|
|
393
|
+
if (Object.keys(usage).length === 0 && durationMs === 0) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
// Anthropic's input_tokens excludes cache reads and writes; normalize to a cache-inclusive total.
|
|
397
|
+
const uncachedInputTokens = readUsageNumber(usage, "input_tokens");
|
|
398
|
+
const cacheReadTokens = readUsageNumber(usage, "cache_read_input_tokens");
|
|
399
|
+
const cacheCreationTokens = readUsageNumber(usage, "cache_creation_input_tokens");
|
|
400
|
+
this.accumulatedUsage = addUsage(this.accumulatedUsage ?? emptyUsage("provider_reported"), {
|
|
401
|
+
inputTokens: uncachedInputTokens + cacheReadTokens + cacheCreationTokens,
|
|
402
|
+
uncachedInputTokens,
|
|
403
|
+
outputTokens: readUsageNumber(usage, "output_tokens"),
|
|
404
|
+
cacheReadTokens,
|
|
405
|
+
cacheCreationTokens,
|
|
406
|
+
providerDurationMs: durationMs > 0 ? durationMs : null,
|
|
407
|
+
source: "provider_reported",
|
|
408
|
+
inputTokenSemantics: "excludes_cache_tokens",
|
|
409
|
+
rawUsage: [{ provider: "claude", usage }]
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
function providerCliError(line) {
|
|
414
|
+
return {
|
|
415
|
+
role: "runner",
|
|
416
|
+
kind: "error",
|
|
417
|
+
content: clip(line.trim(), 1000),
|
|
418
|
+
eventType: "provider_cli_error"
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
function messageContentBlocks(event) {
|
|
422
|
+
const content = objectField(event, "message").content;
|
|
423
|
+
if (!Array.isArray(content)) {
|
|
424
|
+
return [];
|
|
425
|
+
}
|
|
426
|
+
return content.filter((entry) => Boolean(entry) && typeof entry === "object" && !Array.isArray(entry));
|
|
427
|
+
}
|
|
428
|
+
function toolResultText(block) {
|
|
429
|
+
const content = block.content;
|
|
430
|
+
if (typeof content === "string") {
|
|
431
|
+
return content.trim() || null;
|
|
432
|
+
}
|
|
433
|
+
if (!Array.isArray(content)) {
|
|
434
|
+
return null;
|
|
435
|
+
}
|
|
436
|
+
const text = content
|
|
437
|
+
.map((entry) => entry && typeof entry === "object" ? stringField(entry, "text") : null)
|
|
438
|
+
.filter((part) => Boolean(part))
|
|
439
|
+
.join("\n")
|
|
440
|
+
.trim();
|
|
441
|
+
return text || null;
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Claude Code exposes MCP tools as `mcp__<server>__<tool>` and replaces characters outside
|
|
445
|
+
* `[A-Za-z0-9_-]` with `_`, so `forge.complete_qa` arrives as `mcp__forge__forge_complete_qa`.
|
|
446
|
+
*/
|
|
447
|
+
export function parseClaudeMcpToolName(name) {
|
|
448
|
+
const match = /^mcp__(.+?)__(.+)$/u.exec(name);
|
|
449
|
+
if (!match) {
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
const server = match[1] ?? "";
|
|
453
|
+
const rawTool = match[2] ?? "";
|
|
454
|
+
const tool = server === "forge" && rawTool.startsWith("forge_") ? `forge.${rawTool.slice("forge_".length)}` : rawTool;
|
|
455
|
+
return server && tool ? { server, tool } : null;
|
|
456
|
+
}
|
|
457
|
+
function claudeMcpEvents(toolUse, phase, errorText) {
|
|
458
|
+
const mcp = parseClaudeMcpToolName(toolUse.name);
|
|
459
|
+
if (!mcp) {
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
const item = {
|
|
463
|
+
type: "mcp_tool_call",
|
|
464
|
+
server: mcp.server,
|
|
465
|
+
tool: mcp.tool,
|
|
466
|
+
status: phase
|
|
467
|
+
};
|
|
468
|
+
if (phase === "failed") {
|
|
469
|
+
item.error = errorText ? clip(errorText, 600) : "MCP tool call failed.";
|
|
470
|
+
}
|
|
471
|
+
return formatCodexMcpToolCallEvents(phase === "started" ? "item.started" : "item.completed", { item });
|
|
472
|
+
}
|
|
473
|
+
function formatClaudeToolUseStarted(toolUse) {
|
|
474
|
+
const mcp = claudeMcpEvents(toolUse, "started", null);
|
|
475
|
+
if (mcp) {
|
|
476
|
+
return mcp;
|
|
477
|
+
}
|
|
478
|
+
const normalized = toolUse.name.toLowerCase();
|
|
479
|
+
const filePath = stringField(toolUse.input, "file_path") ?? stringField(toolUse.input, "notebook_path") ?? stringField(toolUse.input, "path");
|
|
480
|
+
let content;
|
|
481
|
+
if (normalized === "bash") {
|
|
482
|
+
const command = stringField(toolUse.input, "command");
|
|
483
|
+
content = command ? `Shell command started: ${clip(command, 500)}` : "Shell command started.";
|
|
484
|
+
}
|
|
485
|
+
else if (normalized === "read" && filePath) {
|
|
486
|
+
content = `Reading ${filePath}`;
|
|
487
|
+
}
|
|
488
|
+
else if (claudeFileEditTools.has(normalized) && filePath) {
|
|
489
|
+
content = `Writing ${filePath}`;
|
|
490
|
+
}
|
|
491
|
+
else {
|
|
492
|
+
content = `Tool call started: ${toolUse.name}`;
|
|
493
|
+
}
|
|
494
|
+
return [{
|
|
495
|
+
role: "worker",
|
|
496
|
+
kind: "checkpoint",
|
|
497
|
+
content,
|
|
498
|
+
eventType: normalized === "bash" ? "provider_command" : "provider_tool_call"
|
|
499
|
+
}];
|
|
500
|
+
}
|
|
501
|
+
function formatClaudeToolUseCompleted(toolUse, isError, resultText) {
|
|
502
|
+
const mcp = claudeMcpEvents(toolUse, isError ? "failed" : "completed", resultText);
|
|
503
|
+
if (mcp) {
|
|
504
|
+
return mcp;
|
|
505
|
+
}
|
|
506
|
+
const normalized = toolUse.name.toLowerCase();
|
|
507
|
+
const filePath = stringField(toolUse.input, "file_path") ?? stringField(toolUse.input, "notebook_path") ?? stringField(toolUse.input, "path");
|
|
508
|
+
if (claudeFileEditTools.has(normalized) && filePath && !isError) {
|
|
509
|
+
return [{
|
|
510
|
+
role: "worker",
|
|
511
|
+
kind: "file_edit",
|
|
512
|
+
content: `Wrote ${filePath}`,
|
|
513
|
+
eventType: "provider_file_edit"
|
|
514
|
+
}];
|
|
515
|
+
}
|
|
516
|
+
const status = isError ? "failed" : "completed";
|
|
517
|
+
let content;
|
|
518
|
+
if (normalized === "bash") {
|
|
519
|
+
const command = stringField(toolUse.input, "command");
|
|
520
|
+
content = command ? `Shell command ${status}: ${clip(command, 500)}` : `Shell command ${status}.`;
|
|
521
|
+
}
|
|
522
|
+
else if (normalized === "read" && filePath) {
|
|
523
|
+
content = isError ? `Read failed: ${filePath}` : `Read ${filePath}`;
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
content = `Tool call ${status}: ${toolUse.name}`;
|
|
527
|
+
}
|
|
528
|
+
return [{
|
|
529
|
+
role: "worker",
|
|
530
|
+
kind: "checkpoint",
|
|
531
|
+
content,
|
|
532
|
+
eventType: normalized === "bash" ? "provider_command" : "provider_tool_call"
|
|
533
|
+
}];
|
|
534
|
+
}
|
|
228
535
|
class CodexJsonState {
|
|
229
536
|
threadId = null;
|
|
230
537
|
finalMessage = null;
|
|
@@ -234,12 +541,18 @@ class CodexJsonState {
|
|
|
234
541
|
get sessionLabel() {
|
|
235
542
|
return this.threadId ? `thread ${this.threadId}` : null;
|
|
236
543
|
}
|
|
544
|
+
get providerThreadId() {
|
|
545
|
+
return this.threadId;
|
|
546
|
+
}
|
|
237
547
|
get usage() {
|
|
238
548
|
return this.accumulatedUsage;
|
|
239
549
|
}
|
|
240
550
|
get hasActiveToolCall() {
|
|
241
551
|
return this.activeToolCallCount > 0;
|
|
242
552
|
}
|
|
553
|
+
resetActiveToolCalls() {
|
|
554
|
+
this.activeToolCallCount = 0;
|
|
555
|
+
}
|
|
243
556
|
consume(line) {
|
|
244
557
|
const event = parseJsonLine(line);
|
|
245
558
|
if (!event) {
|
|
@@ -255,7 +568,8 @@ class CodexJsonState {
|
|
|
255
568
|
role: "runner",
|
|
256
569
|
kind: "checkpoint",
|
|
257
570
|
content: `Codex thread started: ${this.threadId}.`,
|
|
258
|
-
eventType: "provider_thread_started"
|
|
571
|
+
eventType: "provider_thread_started",
|
|
572
|
+
data: JSON.stringify({ threadId: this.threadId })
|
|
259
573
|
}];
|
|
260
574
|
}
|
|
261
575
|
}
|
|
@@ -332,12 +646,17 @@ class CodexJsonState {
|
|
|
332
646
|
if (Object.keys(usage).length === 0) {
|
|
333
647
|
return;
|
|
334
648
|
}
|
|
649
|
+
const inputTokens = readUsageNumber(usage, "input_tokens");
|
|
650
|
+
const cacheReadTokens = readUsageNumber(usage, "cached_input_tokens");
|
|
335
651
|
this.accumulatedUsage = addUsage(this.accumulatedUsage ?? emptyUsage("provider_reported"), {
|
|
336
|
-
inputTokens
|
|
652
|
+
inputTokens,
|
|
653
|
+
uncachedInputTokens: Math.max(0, inputTokens - cacheReadTokens),
|
|
337
654
|
outputTokens: readUsageNumber(usage, "output_tokens"),
|
|
338
|
-
cacheReadTokens
|
|
655
|
+
cacheReadTokens,
|
|
339
656
|
reasoningOutputTokens: readUsageNumber(usage, "reasoning_output_tokens"),
|
|
340
|
-
source: "provider_reported"
|
|
657
|
+
source: "provider_reported",
|
|
658
|
+
inputTokenSemantics: "includes_cached_input",
|
|
659
|
+
rawUsage: [{ provider: "codex", usage }]
|
|
341
660
|
});
|
|
342
661
|
}
|
|
343
662
|
}
|
|
@@ -425,11 +744,45 @@ function mcpErrorSummary(item) {
|
|
|
425
744
|
if (direct) {
|
|
426
745
|
return clip(direct, 600);
|
|
427
746
|
}
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
747
|
+
const messages = uniqueErrorMessages([
|
|
748
|
+
item.error,
|
|
749
|
+
item.result,
|
|
750
|
+
item.content,
|
|
751
|
+
]);
|
|
752
|
+
return messages.length > 0 ? clip(messages.join(" Caused by: "), 600) : null;
|
|
753
|
+
}
|
|
754
|
+
function uniqueErrorMessages(values) {
|
|
755
|
+
const messages = [];
|
|
756
|
+
const seenMessages = new Set();
|
|
757
|
+
const seenObjects = new Set();
|
|
758
|
+
const visit = (value, depth) => {
|
|
759
|
+
if (depth > 8 || value === null || value === undefined)
|
|
760
|
+
return;
|
|
761
|
+
if (typeof value === "string") {
|
|
762
|
+
const message = value.trim();
|
|
763
|
+
if (message.length > 0 && !seenMessages.has(message)) {
|
|
764
|
+
seenMessages.add(message);
|
|
765
|
+
messages.push(message);
|
|
766
|
+
}
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
if (typeof value !== "object" || seenObjects.has(value))
|
|
770
|
+
return;
|
|
771
|
+
seenObjects.add(value);
|
|
772
|
+
if (Array.isArray(value)) {
|
|
773
|
+
for (const entry of value)
|
|
774
|
+
visit(entry, depth + 1);
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
const record = value;
|
|
778
|
+
for (const key of ["message", "summary", "error", "cause", "data", "details", "result", "content"]) {
|
|
779
|
+
if (key in record)
|
|
780
|
+
visit(record[key], depth + 1);
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
for (const value of values)
|
|
784
|
+
visit(value, 0);
|
|
785
|
+
return messages;
|
|
433
786
|
}
|
|
434
787
|
function safeProviderMcpData(item, serverName, toolName, status) {
|
|
435
788
|
const elapsedMs = numericField(item, "elapsed_ms") ?? numericField(item, "duration_ms");
|
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
import type { RunOnceDispatchPacket, RunnerDispatchEvent } from "../session.types.js";
|
|
2
2
|
export type UsageSource = "provider_reported" | "cli_reported" | "estimated" | "unknown";
|
|
3
|
+
export type InputTokenSemantics = "includes_cached_input" | "excludes_cache_tokens" | "unknown";
|
|
4
|
+
export declare const PROVIDER_USAGE_SCHEMA_VERSION = 1;
|
|
3
5
|
/**
|
|
4
6
|
* Normalized, provider-agnostic token usage accumulated for a dispatch. Cost is never computed
|
|
5
|
-
* here; the API recomputes cost from these tokens x model rates (
|
|
7
|
+
* here; the API recomputes cost from these tokens x model rates (legacy
|
|
8
|
+
* ADR-0006, .forge-designer/decisions/legacy/0006-cost-capture.md).
|
|
6
9
|
*/
|
|
7
10
|
export interface ProviderUsage {
|
|
11
|
+
/** Normalized total input. Cached input is a subset, never an additive peer. */
|
|
8
12
|
readonly inputTokens: number;
|
|
13
|
+
readonly uncachedInputTokens: number | null;
|
|
9
14
|
readonly outputTokens: number;
|
|
10
15
|
readonly cacheReadTokens: number;
|
|
11
16
|
readonly cacheCreationTokens: number;
|
|
12
17
|
readonly reasoningOutputTokens: number;
|
|
13
18
|
readonly providerDurationMs: number | null;
|
|
14
19
|
readonly source: UsageSource;
|
|
20
|
+
readonly inputTokenSemantics: InputTokenSemantics;
|
|
21
|
+
/** Exact provider usage objects retained for audit/re-normalization. */
|
|
22
|
+
readonly rawUsage: readonly Record<string, unknown>[];
|
|
15
23
|
}
|
|
16
24
|
export declare function emptyUsage(source?: UsageSource): ProviderUsage;
|
|
17
25
|
export declare function addUsage(base: ProviderUsage, next: Partial<ProviderUsage>): ProviderUsage;
|
|
@@ -1,27 +1,38 @@
|
|
|
1
|
+
export const PROVIDER_USAGE_SCHEMA_VERSION = 1;
|
|
1
2
|
export function emptyUsage(source = "unknown") {
|
|
2
3
|
return {
|
|
3
4
|
inputTokens: 0,
|
|
5
|
+
uncachedInputTokens: 0,
|
|
4
6
|
outputTokens: 0,
|
|
5
7
|
cacheReadTokens: 0,
|
|
6
8
|
cacheCreationTokens: 0,
|
|
7
9
|
reasoningOutputTokens: 0,
|
|
8
10
|
providerDurationMs: null,
|
|
9
|
-
source
|
|
11
|
+
source,
|
|
12
|
+
inputTokenSemantics: "unknown",
|
|
13
|
+
rawUsage: []
|
|
10
14
|
};
|
|
11
15
|
}
|
|
12
16
|
export function addUsage(base, next) {
|
|
17
|
+
const nextUncached = next.uncachedInputTokens;
|
|
18
|
+
const uncachedInputTokens = base.uncachedInputTokens === null || nextUncached === null
|
|
19
|
+
? null
|
|
20
|
+
: base.uncachedInputTokens + nonNegative(nextUncached);
|
|
13
21
|
return {
|
|
14
22
|
inputTokens: base.inputTokens + nonNegative(next.inputTokens),
|
|
23
|
+
uncachedInputTokens,
|
|
15
24
|
outputTokens: base.outputTokens + nonNegative(next.outputTokens),
|
|
16
25
|
cacheReadTokens: base.cacheReadTokens + nonNegative(next.cacheReadTokens),
|
|
17
26
|
cacheCreationTokens: base.cacheCreationTokens + nonNegative(next.cacheCreationTokens),
|
|
18
27
|
reasoningOutputTokens: base.reasoningOutputTokens + nonNegative(next.reasoningOutputTokens),
|
|
19
28
|
providerDurationMs: next.providerDurationMs ?? base.providerDurationMs,
|
|
20
|
-
source: next.source ?? base.source
|
|
29
|
+
source: next.source ?? base.source,
|
|
30
|
+
inputTokenSemantics: mergeSemantics(base.inputTokenSemantics, next.inputTokenSemantics),
|
|
31
|
+
rawUsage: [...base.rawUsage, ...(next.rawUsage ?? [])]
|
|
21
32
|
};
|
|
22
33
|
}
|
|
23
34
|
export function totalTokens(usage) {
|
|
24
|
-
return usage.inputTokens + usage.outputTokens
|
|
35
|
+
return usage.inputTokens + usage.outputTokens;
|
|
25
36
|
}
|
|
26
37
|
export function hasUsage(usage) {
|
|
27
38
|
return usage !== null && totalTokens(usage) > 0;
|
|
@@ -37,20 +48,26 @@ export function buildProviderUsageEvent(dispatch, usage) {
|
|
|
37
48
|
lane: dispatch.execution.lane,
|
|
38
49
|
phase: dispatch.cycleRun.phase,
|
|
39
50
|
attempt: dispatch.cycleRun.attempt,
|
|
51
|
+
usage_schema_version: PROVIDER_USAGE_SCHEMA_VERSION,
|
|
52
|
+
input_token_semantics: usage.inputTokenSemantics,
|
|
40
53
|
input_tokens: usage.inputTokens,
|
|
54
|
+
uncached_input_tokens: usage.uncachedInputTokens,
|
|
41
55
|
output_tokens: usage.outputTokens,
|
|
42
56
|
cache_read_tokens: usage.cacheReadTokens,
|
|
43
57
|
cache_creation_tokens: usage.cacheCreationTokens,
|
|
44
58
|
reasoning_output_tokens: usage.reasoningOutputTokens,
|
|
45
59
|
metered: dispatch.execution.authKind === "api_key",
|
|
46
60
|
usage_source: usage.source,
|
|
47
|
-
provider_duration_ms: usage.providerDurationMs
|
|
61
|
+
provider_duration_ms: usage.providerDurationMs,
|
|
62
|
+
normalized_total_tokens: totalTokens(usage),
|
|
63
|
+
normalized_total_formula: "total_input + output; cached_input is a subset of total_input",
|
|
64
|
+
raw_usage: usage.rawUsage
|
|
48
65
|
};
|
|
49
66
|
const total = totalTokens(usage);
|
|
50
67
|
return {
|
|
51
68
|
role: "runner",
|
|
52
69
|
kind: "checkpoint",
|
|
53
|
-
content: `Provider usage: ${total} tokens (${usage.inputTokens} in / ${usage.outputTokens} out) for ${dispatch.execution.model}.`,
|
|
70
|
+
content: `Provider usage: ${total} tokens (${usage.inputTokens} total in, including ${usage.cacheReadTokens} cached / ${usage.outputTokens} out) for ${dispatch.execution.model}.`,
|
|
54
71
|
eventType: "provider_usage",
|
|
55
72
|
data: JSON.stringify(data)
|
|
56
73
|
};
|
|
@@ -62,3 +79,9 @@ export function readUsageNumber(source, key) {
|
|
|
62
79
|
function nonNegative(value) {
|
|
63
80
|
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
|
|
64
81
|
}
|
|
82
|
+
function mergeSemantics(base, next) {
|
|
83
|
+
if (!next || next === "unknown") {
|
|
84
|
+
return base;
|
|
85
|
+
}
|
|
86
|
+
return base === "unknown" || base === next ? next : "unknown";
|
|
87
|
+
}
|