@loucompanion/forge-bridge 0.1.1-dev.615630fb4618 → 0.1.1-dev.65ea6889412c
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/dist/session/phase-completion.d.ts +31 -1
- package/dist/session/phase-completion.js +782 -78
- package/dist/session/provider-cli/provider-cli.base.d.ts +1 -2
- package/dist/session/provider-cli/provider-cli.service.d.ts +4 -4
- package/dist/session/provider-cli/provider-cli.service.js +612 -65
- package/dist/session/provider-cli/provider-usage.d.ts +25 -0
- package/dist/session/provider-cli/provider-usage.js +64 -0
- package/dist/session/session.service.js +9 -13
- package/dist/session/session.types.d.ts +14 -0
- package/dist/session/session.types.js +40 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { RunOnceDispatchPacket, RunnerDispatchEvent } from "../session.types.js";
|
|
2
|
+
export type UsageSource = "provider_reported" | "cli_reported" | "estimated" | "unknown";
|
|
3
|
+
/**
|
|
4
|
+
* 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 (ADR-0006).
|
|
6
|
+
*/
|
|
7
|
+
export interface ProviderUsage {
|
|
8
|
+
readonly inputTokens: number;
|
|
9
|
+
readonly outputTokens: number;
|
|
10
|
+
readonly cacheReadTokens: number;
|
|
11
|
+
readonly cacheCreationTokens: number;
|
|
12
|
+
readonly reasoningOutputTokens: number;
|
|
13
|
+
readonly providerDurationMs: number | null;
|
|
14
|
+
readonly source: UsageSource;
|
|
15
|
+
}
|
|
16
|
+
export declare function emptyUsage(source?: UsageSource): ProviderUsage;
|
|
17
|
+
export declare function addUsage(base: ProviderUsage, next: Partial<ProviderUsage>): ProviderUsage;
|
|
18
|
+
export declare function totalTokens(usage: ProviderUsage): number;
|
|
19
|
+
export declare function hasUsage(usage: ProviderUsage | null): usage is ProviderUsage;
|
|
20
|
+
/**
|
|
21
|
+
* Builds the durable `provider_usage` event. Token numbers travel as a structured `data` JSON
|
|
22
|
+
* string (snake_case) that the API parses into a usage_event row.
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildProviderUsageEvent(dispatch: RunOnceDispatchPacket, usage: ProviderUsage): RunnerDispatchEvent;
|
|
25
|
+
export declare function readUsageNumber(source: Record<string, unknown>, key: string): number;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export function emptyUsage(source = "unknown") {
|
|
2
|
+
return {
|
|
3
|
+
inputTokens: 0,
|
|
4
|
+
outputTokens: 0,
|
|
5
|
+
cacheReadTokens: 0,
|
|
6
|
+
cacheCreationTokens: 0,
|
|
7
|
+
reasoningOutputTokens: 0,
|
|
8
|
+
providerDurationMs: null,
|
|
9
|
+
source
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export function addUsage(base, next) {
|
|
13
|
+
return {
|
|
14
|
+
inputTokens: base.inputTokens + nonNegative(next.inputTokens),
|
|
15
|
+
outputTokens: base.outputTokens + nonNegative(next.outputTokens),
|
|
16
|
+
cacheReadTokens: base.cacheReadTokens + nonNegative(next.cacheReadTokens),
|
|
17
|
+
cacheCreationTokens: base.cacheCreationTokens + nonNegative(next.cacheCreationTokens),
|
|
18
|
+
reasoningOutputTokens: base.reasoningOutputTokens + nonNegative(next.reasoningOutputTokens),
|
|
19
|
+
providerDurationMs: next.providerDurationMs ?? base.providerDurationMs,
|
|
20
|
+
source: next.source ?? base.source
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function totalTokens(usage) {
|
|
24
|
+
return usage.inputTokens + usage.outputTokens + usage.cacheReadTokens + usage.cacheCreationTokens;
|
|
25
|
+
}
|
|
26
|
+
export function hasUsage(usage) {
|
|
27
|
+
return usage !== null && totalTokens(usage) > 0;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Builds the durable `provider_usage` event. Token numbers travel as a structured `data` JSON
|
|
31
|
+
* string (snake_case) that the API parses into a usage_event row.
|
|
32
|
+
*/
|
|
33
|
+
export function buildProviderUsageEvent(dispatch, usage) {
|
|
34
|
+
const data = {
|
|
35
|
+
provider: dispatch.execution.provider,
|
|
36
|
+
model: dispatch.execution.model,
|
|
37
|
+
lane: dispatch.execution.lane,
|
|
38
|
+
phase: dispatch.cycleRun.phase,
|
|
39
|
+
attempt: dispatch.cycleRun.attempt,
|
|
40
|
+
input_tokens: usage.inputTokens,
|
|
41
|
+
output_tokens: usage.outputTokens,
|
|
42
|
+
cache_read_tokens: usage.cacheReadTokens,
|
|
43
|
+
cache_creation_tokens: usage.cacheCreationTokens,
|
|
44
|
+
reasoning_output_tokens: usage.reasoningOutputTokens,
|
|
45
|
+
metered: dispatch.execution.authKind === "api_key",
|
|
46
|
+
usage_source: usage.source,
|
|
47
|
+
provider_duration_ms: usage.providerDurationMs
|
|
48
|
+
};
|
|
49
|
+
const total = totalTokens(usage);
|
|
50
|
+
return {
|
|
51
|
+
role: "runner",
|
|
52
|
+
kind: "checkpoint",
|
|
53
|
+
content: `Provider usage: ${total} tokens (${usage.inputTokens} in / ${usage.outputTokens} out) for ${dispatch.execution.model}.`,
|
|
54
|
+
eventType: "provider_usage",
|
|
55
|
+
data: JSON.stringify(data)
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export function readUsageNumber(source, key) {
|
|
59
|
+
const value = source[key];
|
|
60
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : 0;
|
|
61
|
+
}
|
|
62
|
+
function nonNegative(value) {
|
|
63
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
|
|
64
|
+
}
|
|
@@ -50,21 +50,17 @@ export class BridgeSessionService {
|
|
|
50
50
|
const role = dispatch.execution.role.toLowerCase();
|
|
51
51
|
const isQa = role === "qa" || lane === "qa";
|
|
52
52
|
const isConflictResolve = role === "conflict_resolve" || lane === "conflict_resolve";
|
|
53
|
+
const context = {
|
|
54
|
+
dispatch,
|
|
55
|
+
worktree,
|
|
56
|
+
emit,
|
|
57
|
+
signal
|
|
58
|
+
};
|
|
53
59
|
const run = isQa
|
|
54
|
-
? await this.providerCli.runQa(
|
|
60
|
+
? await this.providerCli.runQa(context)
|
|
55
61
|
: isConflictResolve
|
|
56
|
-
? await this.providerCli.runConflictResolve(
|
|
57
|
-
|
|
58
|
-
worktree,
|
|
59
|
-
emit,
|
|
60
|
-
signal
|
|
61
|
-
})
|
|
62
|
-
: await this.providerCli.runImplementation({
|
|
63
|
-
dispatch,
|
|
64
|
-
worktree,
|
|
65
|
-
emit,
|
|
66
|
-
signal
|
|
67
|
-
});
|
|
62
|
+
? await this.providerCli.runConflictResolve(context)
|
|
63
|
+
: await this.providerCli.runImplementation(context);
|
|
68
64
|
const phaseArtifacts = run.artifacts?.length ? run.artifacts : run.artifact ? [run.artifact] : [];
|
|
69
65
|
return redactSecrets({
|
|
70
66
|
schemaVersion: 1,
|
|
@@ -11,6 +11,15 @@ export interface RunOnceDispatchPacket {
|
|
|
11
11
|
readonly apiKeySecretGrant?: ApiKeySecretGrant | null;
|
|
12
12
|
readonly artifacts: readonly DispatchResolvedArtifact[];
|
|
13
13
|
readonly warmPathOverrides?: readonly string[];
|
|
14
|
+
readonly qaRework?: DispatchQaReworkContext | null;
|
|
15
|
+
}
|
|
16
|
+
export interface DispatchQaReworkContext {
|
|
17
|
+
readonly attempt: number;
|
|
18
|
+
readonly maxAttempts: number;
|
|
19
|
+
readonly packetPath: string;
|
|
20
|
+
readonly packetJson: string;
|
|
21
|
+
readonly findingIds: readonly string[];
|
|
22
|
+
readonly findings: readonly Record<string, unknown>[];
|
|
14
23
|
}
|
|
15
24
|
export interface DispatchResolvedArtifact {
|
|
16
25
|
readonly kind: string;
|
|
@@ -18,6 +27,7 @@ export interface DispatchResolvedArtifact {
|
|
|
18
27
|
readonly scope: string;
|
|
19
28
|
readonly body: string;
|
|
20
29
|
readonly contentHash: string;
|
|
30
|
+
readonly version: number;
|
|
21
31
|
readonly lane?: string | null;
|
|
22
32
|
readonly event?: string | null;
|
|
23
33
|
}
|
|
@@ -86,6 +96,10 @@ export interface RunnerDispatchEvent {
|
|
|
86
96
|
readonly kind: string;
|
|
87
97
|
readonly content: string;
|
|
88
98
|
readonly eventType: string;
|
|
99
|
+
/** Provider/runner-reported event time (ISO-8601) when available, preserved for accurate durations. */
|
|
100
|
+
readonly occurredAt?: string | null;
|
|
101
|
+
/** Optional structured telemetry payload (JSON string), e.g. token usage for provider_usage events. */
|
|
102
|
+
readonly data?: string | null;
|
|
89
103
|
}
|
|
90
104
|
export interface RunnerDispatchSpec {
|
|
91
105
|
readonly repoPath: string;
|
|
@@ -17,7 +17,8 @@ export function parseRunOnceDispatchPacket(value) {
|
|
|
17
17
|
serverLocal: parseServerLocal(packet.serverLocal),
|
|
18
18
|
apiKeySecretGrant: parseApiKeySecretGrant(packet.apiKeySecretGrant),
|
|
19
19
|
artifacts: parseArtifacts(packet.artifacts),
|
|
20
|
-
warmPathOverrides: parseWarmPathOverrides(packet.warmPathOverrides)
|
|
20
|
+
warmPathOverrides: parseWarmPathOverrides(packet.warmPathOverrides),
|
|
21
|
+
qaRework: parseQaRework(packet.qaRework)
|
|
21
22
|
};
|
|
22
23
|
}
|
|
23
24
|
export function failureResult(summary, events = []) {
|
|
@@ -138,6 +139,33 @@ function parseWarmPathOverride(entry, index) {
|
|
|
138
139
|
}
|
|
139
140
|
return segments.join("/");
|
|
140
141
|
}
|
|
142
|
+
function parseQaRework(value) {
|
|
143
|
+
if (value === undefined || value === null) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
const qaRework = asObject(value, "qaRework");
|
|
147
|
+
const findingIdsValue = qaRework.findingIds;
|
|
148
|
+
if (!Array.isArray(findingIdsValue)) {
|
|
149
|
+
throw new Error("qaRework.findingIds must be an array.");
|
|
150
|
+
}
|
|
151
|
+
const findingsValue = qaRework.findings;
|
|
152
|
+
if (!Array.isArray(findingsValue)) {
|
|
153
|
+
throw new Error("qaRework.findings must be an array.");
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
attempt: requiredNumber(qaRework, "attempt"),
|
|
157
|
+
maxAttempts: requiredNumber(qaRework, "maxAttempts"),
|
|
158
|
+
packetPath: requiredString(qaRework, "packetPath"),
|
|
159
|
+
packetJson: requiredString(qaRework, "packetJson"),
|
|
160
|
+
findingIds: findingIdsValue.map((entry, index) => {
|
|
161
|
+
if (typeof entry !== "string" || entry.trim().length === 0) {
|
|
162
|
+
throw new Error(`qaRework.findingIds[${index}] must be a non-empty string.`);
|
|
163
|
+
}
|
|
164
|
+
return entry;
|
|
165
|
+
}),
|
|
166
|
+
findings: findingsValue.map((entry, index) => asObject(entry, `qaRework.findings[${index}]`))
|
|
167
|
+
};
|
|
168
|
+
}
|
|
141
169
|
function parseArtifacts(value) {
|
|
142
170
|
if (value === undefined || value === null) {
|
|
143
171
|
return [];
|
|
@@ -153,6 +181,7 @@ function parseArtifacts(value) {
|
|
|
153
181
|
scope: requiredString(artifact, "scope"),
|
|
154
182
|
body: optionalString(artifact, "body") ?? "",
|
|
155
183
|
contentHash: requiredString(artifact, "contentHash"),
|
|
184
|
+
version: optionalPositiveInteger(artifact, "version") ?? 1,
|
|
156
185
|
lane: optionalString(artifact, "lane"),
|
|
157
186
|
event: optionalString(artifact, "event")
|
|
158
187
|
};
|
|
@@ -188,6 +217,16 @@ function requiredBoolean(source, key) {
|
|
|
188
217
|
}
|
|
189
218
|
return value;
|
|
190
219
|
}
|
|
220
|
+
function optionalPositiveInteger(source, key) {
|
|
221
|
+
const value = source[key];
|
|
222
|
+
if (value === undefined || value === null) {
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 1) {
|
|
226
|
+
throw new Error(`${key} must be a positive integer when provided.`);
|
|
227
|
+
}
|
|
228
|
+
return value;
|
|
229
|
+
}
|
|
191
230
|
function requiredNumber(source, key) {
|
|
192
231
|
const value = source[key];
|
|
193
232
|
if (typeof value !== "number" || !Number.isFinite(value)) {
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const forgeBridgeVersion = "0.1.1-dev.
|
|
1
|
+
export declare const forgeBridgeVersion = "0.1.1-dev.65ea6889412c";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const forgeBridgeVersion = "0.1.1-dev.
|
|
1
|
+
export const forgeBridgeVersion = "0.1.1-dev.65ea6889412c";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loucompanion/forge-bridge",
|
|
3
|
-
"version": "0.1.1-dev.
|
|
3
|
+
"version": "0.1.1-dev.65ea6889412c",
|
|
4
4
|
"description": "Forge runner bridge CLI for connecting local developer machines to Forge dispatch.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"type": "module",
|