@loucompanion/forge-bridge 0.1.1-dev.242bb53ef13f → 0.1.1-dev.32d4dd5874ee
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 +1 -0
- package/dist/bridge/bridge.service.js +16 -3
- package/dist/bridge/bridge.types.d.ts +7 -0
- package/dist/cli/cli.service.d.ts +27 -2
- package/dist/cli/cli.service.js +406 -16
- package/dist/cli/local-config.d.ts +4 -1
- package/dist/cli/local-config.js +42 -7
- package/dist/session/index.d.ts +2 -0
- package/dist/session/index.js +1 -0
- package/dist/session/internals.d.ts +4 -2
- package/dist/session/internals.js +2 -1
- package/dist/session/phase-completion.d.ts +54 -0
- package/dist/session/phase-completion.js +737 -0
- package/dist/session/provider-cli/index.d.ts +2 -2
- package/dist/session/provider-cli/index.js +1 -1
- package/dist/session/provider-cli/provider-cli.base.d.ts +2 -1
- package/dist/session/provider-cli/provider-cli.service.d.ts +45 -1
- package/dist/session/provider-cli/provider-cli.service.js +789 -15
- package/dist/session/provider-cli/provider-cli.types.d.ts +2 -0
- package/dist/session/session.service.js +55 -11
- package/dist/session/session.types.d.ts +13 -0
- package/dist/session/session.types.js +47 -1
- package/dist/shared/cursor-agent/cursor-agent.launch.d.ts +9 -0
- package/dist/shared/cursor-agent/cursor-agent.launch.js +66 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/worktree/artifact-injector.d.ts +11 -0
- package/dist/worktree/artifact-injector.js +107 -0
- package/dist/worktree/dependency-warmer.d.ts +32 -0
- package/dist/worktree/dependency-warmer.js +297 -0
- package/dist/worktree/index.d.ts +2 -0
- package/dist/worktree/index.js +1 -0
- package/dist/worktree/worktree.service.d.ts +4 -0
- package/dist/worktree/worktree.service.js +173 -11
- package/dist/worktree/worktree.types.d.ts +5 -1
- package/package.json +2 -2
|
@@ -1,7 +1,217 @@
|
|
|
1
1
|
import crypto from "node:crypto";
|
|
2
2
|
import fs from "node:fs/promises";
|
|
3
3
|
import path from "node:path";
|
|
4
|
+
import { execFile, spawn } from "node:child_process";
|
|
5
|
+
import readline from "node:readline";
|
|
6
|
+
import { promisify } from "node:util";
|
|
4
7
|
import { GitClient } from "../../shared/git/git.js";
|
|
8
|
+
import { resolveCursorAgentDirectLaunch } from "../../shared/cursor-agent/cursor-agent.launch.js";
|
|
9
|
+
import { buildSpecBody, selectLaneCommand } from "../../worktree/artifact-injector.js";
|
|
10
|
+
import { codexMcpConfigArgs, completionArtifactJsonPath, completionArtifactReportPath, completionPhaseFromDispatch, completionPromptForPhase, prepareCompletionToolRuntime, readCompletionArtifact } from "../phase-completion.js";
|
|
11
|
+
const fakeWorkerModel = "server-local-fake-worker";
|
|
12
|
+
const execFileAsync = promisify(execFile);
|
|
13
|
+
export class RoutingProviderCliService {
|
|
14
|
+
realProvider;
|
|
15
|
+
deterministicProvider;
|
|
16
|
+
constructor(realProvider = new ProcessBackedProviderCliService(), deterministicProvider = new DeterministicProviderCliService()) {
|
|
17
|
+
this.realProvider = realProvider;
|
|
18
|
+
this.deterministicProvider = deterministicProvider;
|
|
19
|
+
}
|
|
20
|
+
async runImplementation(context) {
|
|
21
|
+
return isFakeWorkerModel(context.dispatch.execution.model)
|
|
22
|
+
? await this.deterministicProvider.runImplementation(context)
|
|
23
|
+
: await this.realProvider.runImplementation(context);
|
|
24
|
+
}
|
|
25
|
+
async runConflictResolve(context) {
|
|
26
|
+
return isFakeWorkerModel(context.dispatch.execution.model)
|
|
27
|
+
? await this.deterministicProvider.runConflictResolve(context)
|
|
28
|
+
: await this.realProvider.runConflictResolve(context);
|
|
29
|
+
}
|
|
30
|
+
async runQa(dispatch, emit, signal) {
|
|
31
|
+
return isFakeWorkerModel(dispatch.execution.model)
|
|
32
|
+
? await this.deterministicProvider.runQa(dispatch, emit, signal)
|
|
33
|
+
: await this.realProvider.runQa(dispatch, emit, signal);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export class ProcessBackedProviderCliService {
|
|
37
|
+
processRunner;
|
|
38
|
+
git;
|
|
39
|
+
constructor(processRunner = new StreamingProviderProcessRunner(), git = new GitClient()) {
|
|
40
|
+
this.processRunner = processRunner;
|
|
41
|
+
this.git = git;
|
|
42
|
+
}
|
|
43
|
+
async runImplementation(context) {
|
|
44
|
+
const { dispatch, worktree } = context;
|
|
45
|
+
const phase = completionPhaseFromDispatch(dispatch);
|
|
46
|
+
const metadataRoot = path.join(worktree.worktreePath, ".forge", "work-items", dispatch.workItem.id);
|
|
47
|
+
await fs.mkdir(metadataRoot, { recursive: true });
|
|
48
|
+
const laneCommand = selectLaneCommand(dispatch.artifacts, dispatch.execution.lane, dispatch.cycleRun.phase);
|
|
49
|
+
if (!laneCommand) {
|
|
50
|
+
throw new Error(`Lane command for lane '${dispatch.execution.lane}' phase '${dispatch.cycleRun.phase}' was not resolved.`);
|
|
51
|
+
}
|
|
52
|
+
const specBody = buildSpecBody(laneCommand, dispatch.workItem, phase);
|
|
53
|
+
const specPath = path.join(metadataRoot, "spec.md");
|
|
54
|
+
await fs.writeFile(specPath, specBody, "utf8");
|
|
55
|
+
await fs.writeFile(path.join(metadataRoot, "spec.meta.json"), JSON.stringify({ schema_version: 1, status: "approved", produced_by: "forge-lane-command" }) + "\n", "utf8");
|
|
56
|
+
const completionTools = await prepareCompletionToolRuntime({ dispatch, worktree, phase });
|
|
57
|
+
const command = buildProviderCliCommand(dispatch, {
|
|
58
|
+
worktreePath: worktree.worktreePath,
|
|
59
|
+
prompt: buildImplementationPrompt(dispatch, phase),
|
|
60
|
+
completionTools
|
|
61
|
+
});
|
|
62
|
+
await context.emit({
|
|
63
|
+
role: "runner",
|
|
64
|
+
kind: "checkpoint",
|
|
65
|
+
content: `Starting provider CLI: ${formatProviderCliCommand(command)} in ${worktree.worktreePath}`,
|
|
66
|
+
eventType: "provider_cli_started"
|
|
67
|
+
});
|
|
68
|
+
await context.emit({
|
|
69
|
+
role: "runner",
|
|
70
|
+
kind: "checkpoint",
|
|
71
|
+
content: `Forge completion tools configured for ${phase}; provider must call the MCP completion tool before exit.`,
|
|
72
|
+
eventType: "completion_tools_configured"
|
|
73
|
+
});
|
|
74
|
+
const outputState = createProviderOutputState(dispatch.execution.provider);
|
|
75
|
+
let result;
|
|
76
|
+
try {
|
|
77
|
+
result = await this.processRunner.run({
|
|
78
|
+
command: command.command,
|
|
79
|
+
args: command.args,
|
|
80
|
+
cwd: worktree.worktreePath
|
|
81
|
+
}, {
|
|
82
|
+
onStdoutLine: async (line) => {
|
|
83
|
+
const event = outputState.consume(line);
|
|
84
|
+
if (event) {
|
|
85
|
+
await context.emit(event);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}, context.signal);
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
await cleanupCompletionTools(completionTools, context.emit);
|
|
92
|
+
}
|
|
93
|
+
if (result.exitCode !== 0) {
|
|
94
|
+
const stderr = clip(result.stderr.trim() || result.stdout.trim(), 1200);
|
|
95
|
+
throw new Error(`${command.command} exited ${result.exitCode}${stderr ? `: ${stderr}` : ""}`);
|
|
96
|
+
}
|
|
97
|
+
await context.emit({
|
|
98
|
+
role: "runner",
|
|
99
|
+
kind: "checkpoint",
|
|
100
|
+
content: `Provider CLI completed: ${command.command} exit ${result.exitCode}${outputState.sessionLabel ? ` ${outputState.sessionLabel}` : ""}.`,
|
|
101
|
+
eventType: "provider_cli_completed"
|
|
102
|
+
});
|
|
103
|
+
const committed = await commitWorktreeChanges(this.git, worktree.worktreePath, dispatch.workItem.id);
|
|
104
|
+
const completionArtifact = await readCompletionArtifact(worktree.worktreePath, dispatch.workItem.id, phase);
|
|
105
|
+
if (!completionArtifact) {
|
|
106
|
+
throw new Error(`Provider CLI completed but did not call the Forge ${phase} completion tool.`);
|
|
107
|
+
}
|
|
108
|
+
if (outputState.finalMessage) {
|
|
109
|
+
await context.emit({
|
|
110
|
+
role: "assistant",
|
|
111
|
+
kind: "message",
|
|
112
|
+
content: outputState.finalMessage,
|
|
113
|
+
eventType: "implementation_complete"
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
await context.emit({
|
|
118
|
+
role: "assistant",
|
|
119
|
+
kind: "message",
|
|
120
|
+
content: `${dispatch.execution.provider} CLI completed for ${worktree.branchName}.`,
|
|
121
|
+
eventType: "implementation_complete"
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
summary: `${dispatch.execution.provider} CLI completed ${worktree.branchName} (${phase}, ${committed.commitSha.slice(0, 12)}).`,
|
|
126
|
+
spec: {
|
|
127
|
+
repoPath: worktree.worktreePath,
|
|
128
|
+
path: specPath,
|
|
129
|
+
body: specBody,
|
|
130
|
+
producedBy: "forge-lane-command",
|
|
131
|
+
status: "approved"
|
|
132
|
+
},
|
|
133
|
+
artifact: completionArtifact
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
async runQa(dispatch, emit, signal) {
|
|
137
|
+
const command = buildProviderCliCommand(dispatch, {
|
|
138
|
+
prompt: buildQaPrompt(dispatch)
|
|
139
|
+
});
|
|
140
|
+
await emit({
|
|
141
|
+
role: "runner",
|
|
142
|
+
kind: "checkpoint",
|
|
143
|
+
content: `Starting provider CLI QA: ${formatProviderCliCommand(command)}`,
|
|
144
|
+
eventType: "provider_cli_started"
|
|
145
|
+
});
|
|
146
|
+
const outputState = createProviderOutputState(dispatch.execution.provider);
|
|
147
|
+
const result = await this.processRunner.run({
|
|
148
|
+
command: command.command,
|
|
149
|
+
args: command.args,
|
|
150
|
+
cwd: process.cwd()
|
|
151
|
+
}, {
|
|
152
|
+
onStdoutLine: async (line) => {
|
|
153
|
+
const event = outputState.consume(line);
|
|
154
|
+
if (event) {
|
|
155
|
+
await emit(event);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}, signal);
|
|
159
|
+
if (result.exitCode !== 0) {
|
|
160
|
+
const stderr = clip(result.stderr.trim() || result.stdout.trim(), 1200);
|
|
161
|
+
throw new Error(`${command.command} exited ${result.exitCode}${stderr ? `: ${stderr}` : ""}`);
|
|
162
|
+
}
|
|
163
|
+
await emit({
|
|
164
|
+
role: "runner",
|
|
165
|
+
kind: "checkpoint",
|
|
166
|
+
content: `Provider CLI QA completed: ${command.command} exit ${result.exitCode}${outputState.sessionLabel ? ` ${outputState.sessionLabel}` : ""}.`,
|
|
167
|
+
eventType: "provider_cli_completed"
|
|
168
|
+
});
|
|
169
|
+
return {
|
|
170
|
+
summary: `${dispatch.execution.provider} CLI QA passed.`
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
async runConflictResolve(context) {
|
|
174
|
+
const { dispatch, worktree } = context;
|
|
175
|
+
const command = buildProviderCliCommand(dispatch, {
|
|
176
|
+
worktreePath: worktree.worktreePath,
|
|
177
|
+
prompt: buildConflictResolvePrompt(dispatch, worktree.branchName)
|
|
178
|
+
});
|
|
179
|
+
await context.emit({
|
|
180
|
+
role: "runner",
|
|
181
|
+
kind: "checkpoint",
|
|
182
|
+
content: `Starting provider CLI conflict resolve: ${formatProviderCliCommand(command)} in ${worktree.worktreePath}`,
|
|
183
|
+
eventType: "provider_cli_started"
|
|
184
|
+
});
|
|
185
|
+
const outputState = createProviderOutputState(dispatch.execution.provider);
|
|
186
|
+
const result = await this.processRunner.run({
|
|
187
|
+
command: command.command,
|
|
188
|
+
args: command.args,
|
|
189
|
+
cwd: worktree.worktreePath
|
|
190
|
+
}, {
|
|
191
|
+
onStdoutLine: async (line) => {
|
|
192
|
+
const event = outputState.consume(line);
|
|
193
|
+
if (event) {
|
|
194
|
+
await context.emit(event);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}, context.signal);
|
|
198
|
+
if (result.exitCode !== 0) {
|
|
199
|
+
const stderr = clip(result.stderr.trim() || result.stdout.trim(), 1200);
|
|
200
|
+
throw new Error(`${command.command} exited ${result.exitCode}${stderr ? `: ${stderr}` : ""}`);
|
|
201
|
+
}
|
|
202
|
+
const committed = await commitWorktreeChanges(this.git, worktree.worktreePath, dispatch.workItem.id);
|
|
203
|
+
await this.git.run(worktree.worktreePath, ["push", "origin", `HEAD:${worktree.branchName}`]);
|
|
204
|
+
await context.emit({
|
|
205
|
+
role: "assistant",
|
|
206
|
+
kind: "message",
|
|
207
|
+
content: outputState.finalMessage || `${dispatch.execution.provider} CLI resolved merge conflicts for ${worktree.branchName}.`,
|
|
208
|
+
eventType: "conflict_resolve_complete"
|
|
209
|
+
});
|
|
210
|
+
return {
|
|
211
|
+
summary: `${dispatch.execution.provider} CLI resolved conflicts on ${worktree.branchName} (${committed.commitSha.slice(0, 12)}).`
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
}
|
|
5
215
|
export class DeterministicProviderCliService {
|
|
6
216
|
git;
|
|
7
217
|
constructor(git = new GitClient()) {
|
|
@@ -9,15 +219,26 @@ export class DeterministicProviderCliService {
|
|
|
9
219
|
}
|
|
10
220
|
async runImplementation(context) {
|
|
11
221
|
const { dispatch, worktree } = context;
|
|
222
|
+
const phase = completionPhaseFromDispatch(dispatch);
|
|
12
223
|
const providerCommand = buildProviderCliCommand(dispatch);
|
|
13
224
|
const providerCommandText = formatProviderCliCommand(providerCommand);
|
|
14
|
-
const metadataRoot = path.join(worktree.
|
|
225
|
+
const metadataRoot = path.join(worktree.worktreePath, ".forge", "work-items", dispatch.workItem.id);
|
|
15
226
|
const artifactRoot = path.join(metadataRoot, "artifacts");
|
|
16
227
|
await fs.mkdir(artifactRoot, { recursive: true });
|
|
17
|
-
const
|
|
228
|
+
const laneCommand = selectLaneCommand(dispatch.artifacts, dispatch.execution.lane, dispatch.cycleRun.phase);
|
|
229
|
+
if (!laneCommand) {
|
|
230
|
+
throw new Error(`Lane command for lane '${dispatch.execution.lane}' phase '${dispatch.cycleRun.phase}' was not resolved.`);
|
|
231
|
+
}
|
|
232
|
+
const specBody = `${buildSpecBody(laneCommand, dispatch.workItem, phase)}
|
|
233
|
+
## Execution
|
|
234
|
+
|
|
235
|
+
- Provider: ${singleLine(dispatch.execution.provider)}
|
|
236
|
+
- Model: ${singleLine(dispatch.execution.model)}
|
|
237
|
+
- Command: ${providerCommandText}
|
|
238
|
+
`;
|
|
18
239
|
const specPath = path.join(metadataRoot, "spec.md");
|
|
19
240
|
await fs.writeFile(specPath, specBody, "utf8");
|
|
20
|
-
await fs.writeFile(path.join(metadataRoot, "spec.meta.json"), JSON.stringify({ schema_version: 1, status: "approved", produced_by: "forge-
|
|
241
|
+
await fs.writeFile(path.join(metadataRoot, "spec.meta.json"), JSON.stringify({ schema_version: 1, status: "approved", produced_by: "forge-lane-command" }) + "\n", "utf8");
|
|
21
242
|
const changesetPath = path.join(worktree.worktreePath, "FORGE_PHASE5_CHANGESET.md");
|
|
22
243
|
const changesetBody = `# Forge Phase 5 bridge changeset\n\nWork item: ${dispatch.workItem.id}\nTitle: ${dispatch.workItem.title}\nBranch: ${worktree.branchName}\nProvider: ${singleLine(dispatch.execution.provider)}\nModel: ${singleLine(dispatch.execution.model)}\nCommand: ${providerCommandText}\n\nThis deterministic file is written by forge-bridge run-once through the shared session/worktree service path.\n`;
|
|
23
244
|
await fs.writeFile(changesetPath, changesetBody, "utf8");
|
|
@@ -31,17 +252,31 @@ export class DeterministicProviderCliService {
|
|
|
31
252
|
});
|
|
32
253
|
const verdict = {
|
|
33
254
|
schema_version: 1,
|
|
255
|
+
phase: "implementation",
|
|
34
256
|
status: "pass",
|
|
35
|
-
|
|
36
|
-
|
|
257
|
+
summary: "forge-bridge produced a deterministic implementation changeset.",
|
|
258
|
+
plan: [{ step: "Create deterministic changeset", status: "done" }],
|
|
259
|
+
changed_files: [{ path: "FORGE_PHASE5_CHANGESET.md", summary: "Recorded deterministic bridge evidence." }],
|
|
260
|
+
tests_added_or_updated: [{ name: "forge_bridge_run_once", evidence: "The bridge committed FORGE_PHASE5_CHANGESET.md." }],
|
|
261
|
+
docs_added_or_updated: [{ path: "FORGE_PHASE5_CHANGESET.md", summary: "Deterministic evidence document created for the run." }],
|
|
262
|
+
acceptance_criteria: [
|
|
263
|
+
{
|
|
264
|
+
criterion: dispatch.workItem.acceptanceCriteria || "Deterministic bridge changeset exists",
|
|
265
|
+
evidence: `FORGE_PHASE5_CHANGESET.md was committed with content hash ${sha256(changesetBody)}.`,
|
|
266
|
+
status: "satisfied"
|
|
267
|
+
}
|
|
268
|
+
],
|
|
269
|
+
known_gaps: [],
|
|
270
|
+
next_phase_context: "Review FORGE_PHASE5_CHANGESET.md and the bridge commit before validation.",
|
|
37
271
|
produced_by: "forge-bridge",
|
|
38
272
|
content_hash: sha256(changesetBody)
|
|
39
273
|
};
|
|
40
274
|
const verdictJson = JSON.stringify(verdict);
|
|
41
275
|
const report = `# Implementation report\n\nforge-bridge prepared \`${worktree.branchName}\` and created \`${changesetPath}\`.\n`;
|
|
42
|
-
const artifactPath =
|
|
276
|
+
const artifactPath = completionArtifactJsonPath(worktree.worktreePath, dispatch.workItem.id, "implementation");
|
|
277
|
+
const reportPath = completionArtifactReportPath(worktree.worktreePath, dispatch.workItem.id, "implementation");
|
|
43
278
|
await fs.writeFile(artifactPath, verdictJson + "\n", "utf8");
|
|
44
|
-
await fs.writeFile(
|
|
279
|
+
await fs.writeFile(reportPath, report, "utf8");
|
|
45
280
|
await context.emit({
|
|
46
281
|
role: "assistant",
|
|
47
282
|
kind: "message",
|
|
@@ -51,19 +286,20 @@ export class DeterministicProviderCliService {
|
|
|
51
286
|
return {
|
|
52
287
|
summary: `forge-bridge completed ${worktree.branchName}.`,
|
|
53
288
|
spec: {
|
|
54
|
-
repoPath: worktree.
|
|
289
|
+
repoPath: worktree.worktreePath,
|
|
55
290
|
path: specPath,
|
|
56
291
|
body: specBody,
|
|
57
|
-
producedBy: "forge-
|
|
292
|
+
producedBy: "forge-lane-command",
|
|
58
293
|
status: "approved"
|
|
59
294
|
},
|
|
60
295
|
artifact: {
|
|
61
|
-
repoPath: worktree.
|
|
296
|
+
repoPath: worktree.worktreePath,
|
|
62
297
|
path: artifactPath,
|
|
63
298
|
verdictJson,
|
|
64
299
|
report,
|
|
65
300
|
producedBy: "forge-bridge",
|
|
66
|
-
status: "pass"
|
|
301
|
+
status: "pass",
|
|
302
|
+
phase: "implementation"
|
|
67
303
|
}
|
|
68
304
|
};
|
|
69
305
|
}
|
|
@@ -84,6 +320,176 @@ export class DeterministicProviderCliService {
|
|
|
84
320
|
summary: "forge-bridge QA passed."
|
|
85
321
|
};
|
|
86
322
|
}
|
|
323
|
+
async runConflictResolve(context) {
|
|
324
|
+
const { dispatch, worktree } = context;
|
|
325
|
+
await this.git.run(worktree.worktreePath, ["commit", "--allow-empty", "-m", `forge bridge conflict resolve for ${dispatch.workItem.id.slice(0, 8)}`]);
|
|
326
|
+
await context.emit({
|
|
327
|
+
role: "assistant",
|
|
328
|
+
kind: "message",
|
|
329
|
+
content: `Deterministic conflict resolve completed for ${worktree.branchName}.`,
|
|
330
|
+
eventType: "conflict_resolve_complete"
|
|
331
|
+
});
|
|
332
|
+
return {
|
|
333
|
+
summary: `forge-bridge resolved conflicts for ${worktree.branchName}.`
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
export class StreamingProviderProcessRunner {
|
|
338
|
+
async run(request, handlers = {}, signal) {
|
|
339
|
+
const launch = await resolveProviderProcessLaunch(request.command, request.args);
|
|
340
|
+
return await new Promise((resolve, reject) => {
|
|
341
|
+
const child = spawn(launch.command, launch.args, {
|
|
342
|
+
cwd: request.cwd,
|
|
343
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
344
|
+
windowsHide: true
|
|
345
|
+
});
|
|
346
|
+
let stdout = "";
|
|
347
|
+
let stderr = "";
|
|
348
|
+
let settled = false;
|
|
349
|
+
const cleanup = () => {
|
|
350
|
+
signal?.removeEventListener("abort", abort);
|
|
351
|
+
};
|
|
352
|
+
const fail = (error) => {
|
|
353
|
+
if (settled) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
settled = true;
|
|
357
|
+
cleanup();
|
|
358
|
+
reject(error);
|
|
359
|
+
};
|
|
360
|
+
const abort = () => {
|
|
361
|
+
try {
|
|
362
|
+
child.kill("SIGTERM");
|
|
363
|
+
}
|
|
364
|
+
catch {
|
|
365
|
+
// Best effort; close/error will settle the promise.
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
if (signal?.aborted) {
|
|
369
|
+
abort();
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
373
|
+
}
|
|
374
|
+
child.on("error", fail);
|
|
375
|
+
const readers = [
|
|
376
|
+
readProcessLines(child.stdout, (line) => {
|
|
377
|
+
stdout += `${line}\n`;
|
|
378
|
+
return handlers.onStdoutLine?.(line);
|
|
379
|
+
}),
|
|
380
|
+
readProcessLines(child.stderr, (line) => {
|
|
381
|
+
stderr += `${line}\n`;
|
|
382
|
+
return handlers.onStderrLine?.(line);
|
|
383
|
+
})
|
|
384
|
+
];
|
|
385
|
+
child.on("close", (code) => {
|
|
386
|
+
Promise.allSettled(readers).then((readerResults) => {
|
|
387
|
+
const rejected = readerResults.find((result) => result.status === "rejected");
|
|
388
|
+
if (rejected?.status === "rejected") {
|
|
389
|
+
fail(rejected.reason);
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
if (settled) {
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
settled = true;
|
|
396
|
+
cleanup();
|
|
397
|
+
resolve({
|
|
398
|
+
exitCode: code ?? 1,
|
|
399
|
+
stdout,
|
|
400
|
+
stderr
|
|
401
|
+
});
|
|
402
|
+
}).catch(fail);
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
async function resolveProviderProcessLaunch(command, args) {
|
|
408
|
+
if (process.platform !== "win32") {
|
|
409
|
+
return { command, args };
|
|
410
|
+
}
|
|
411
|
+
const cursorDirect = await resolveCursorAgentDirectLaunch(command, args);
|
|
412
|
+
if (cursorDirect) {
|
|
413
|
+
return cursorDirect;
|
|
414
|
+
}
|
|
415
|
+
const resolved = await resolveWindowsCommand(command);
|
|
416
|
+
return resolved ? buildWindowsLaunchCommand(resolved, args) : { command, args };
|
|
417
|
+
}
|
|
418
|
+
async function resolveWindowsCommand(command) {
|
|
419
|
+
if (path.isAbsolute(command)) {
|
|
420
|
+
return command;
|
|
421
|
+
}
|
|
422
|
+
try {
|
|
423
|
+
const result = await execFileAsync("where.exe", [command], { timeout: 2000 });
|
|
424
|
+
return chooseWindowsCommandCandidate(result.stdout.split(/\r?\n/));
|
|
425
|
+
}
|
|
426
|
+
catch {
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
export function chooseWindowsCommandCandidate(candidates) {
|
|
431
|
+
const normalized = candidates
|
|
432
|
+
.map((candidate) => candidate.trim())
|
|
433
|
+
.filter((candidate) => candidate.length > 0);
|
|
434
|
+
if (normalized.length === 0) {
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
const preferredExtensions = [".exe", ".ps1", ".cmd", ".bat", ""];
|
|
438
|
+
for (const extension of preferredExtensions) {
|
|
439
|
+
const candidate = normalized.find((entry) => path.extname(entry).toLowerCase() === extension);
|
|
440
|
+
if (candidate) {
|
|
441
|
+
return candidate;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return normalized[0];
|
|
445
|
+
}
|
|
446
|
+
export function buildWindowsLaunchCommand(commandPath, args) {
|
|
447
|
+
const extension = path.extname(commandPath).toLowerCase();
|
|
448
|
+
if (extension === ".ps1") {
|
|
449
|
+
return {
|
|
450
|
+
command: powershellExecutable(),
|
|
451
|
+
args: ["-NoProfile", "-ExecutionPolicy", "Bypass", "-File", commandPath, ...args]
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
if (extension === ".cmd" || extension === ".bat") {
|
|
455
|
+
return {
|
|
456
|
+
command: process.env.ComSpec ?? "cmd.exe",
|
|
457
|
+
args: ["/d", "/s", "/c", windowsCommandLine(commandPath, args)]
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
return { command: commandPath, args };
|
|
461
|
+
}
|
|
462
|
+
function powershellExecutable() {
|
|
463
|
+
const systemRoot = process.env.SystemRoot;
|
|
464
|
+
return systemRoot
|
|
465
|
+
? path.join(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe")
|
|
466
|
+
: "powershell.exe";
|
|
467
|
+
}
|
|
468
|
+
function windowsCommandLine(command, args) {
|
|
469
|
+
return [command, ...args].map(quoteWindowsCommandLineArg).join(" ");
|
|
470
|
+
}
|
|
471
|
+
function quoteWindowsCommandLineArg(value) {
|
|
472
|
+
if (value.length > 0 && !/[\s"]/.test(value)) {
|
|
473
|
+
return value;
|
|
474
|
+
}
|
|
475
|
+
return `"${value.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\+)$/g, "$1$1")}"`;
|
|
476
|
+
}
|
|
477
|
+
async function fileExists(inputPath) {
|
|
478
|
+
return fs.stat(inputPath).then((stat) => stat.isFile()).catch(() => false);
|
|
479
|
+
}
|
|
480
|
+
async function readProcessLines(stream, onLine) {
|
|
481
|
+
const reader = readline.createInterface({ input: stream });
|
|
482
|
+
try {
|
|
483
|
+
for await (const line of reader) {
|
|
484
|
+
await onLine(line);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
finally {
|
|
488
|
+
reader.close();
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
function isFakeWorkerModel(model) {
|
|
492
|
+
return model.trim().toLowerCase() === fakeWorkerModel;
|
|
87
493
|
}
|
|
88
494
|
function sha256(content) {
|
|
89
495
|
return crypto.createHash("sha256").update(content).digest("hex");
|
|
@@ -97,15 +503,383 @@ function yamlQuotedScalar(value) {
|
|
|
97
503
|
function singleLine(value) {
|
|
98
504
|
return value.replace(/\s+/g, " ").trim();
|
|
99
505
|
}
|
|
100
|
-
export function buildProviderCliCommand(dispatch) {
|
|
506
|
+
export function buildProviderCliCommand(dispatch, run) {
|
|
101
507
|
const provider = dispatch.execution.provider.trim();
|
|
102
508
|
const normalizedProvider = provider.toLowerCase();
|
|
103
509
|
const model = dispatch.execution.model.trim();
|
|
104
510
|
if (normalizedProvider === "codex") {
|
|
105
|
-
|
|
511
|
+
const args = ["exec"];
|
|
512
|
+
if (run?.completionTools) {
|
|
513
|
+
args.push(...codexMcpConfigArgs(run.completionTools));
|
|
514
|
+
}
|
|
515
|
+
args.push("--model", model);
|
|
516
|
+
if (run) {
|
|
517
|
+
args.push("--json", "--dangerously-bypass-approvals-and-sandbox");
|
|
518
|
+
if (run.worktreePath) {
|
|
519
|
+
args.push("--cd", run.worktreePath);
|
|
520
|
+
}
|
|
521
|
+
args.push(run.prompt ?? "");
|
|
522
|
+
}
|
|
523
|
+
return { command: "codex", args };
|
|
106
524
|
}
|
|
107
525
|
if (normalizedProvider === "cursor") {
|
|
108
|
-
|
|
526
|
+
const args = ["--print", "--output-format", "stream-json", "--model", model];
|
|
527
|
+
if (run?.worktreePath) {
|
|
528
|
+
args.push("--workspace", run.worktreePath);
|
|
529
|
+
}
|
|
530
|
+
if (run?.prompt) {
|
|
531
|
+
args.push("--yolo", "--approve-mcps", "--trust", run.prompt);
|
|
532
|
+
}
|
|
533
|
+
return { command: "cursor-agent", args };
|
|
534
|
+
}
|
|
535
|
+
const args = ["--model", model];
|
|
536
|
+
if (run?.prompt) {
|
|
537
|
+
args.push(run.prompt);
|
|
538
|
+
}
|
|
539
|
+
return { command: provider, args };
|
|
540
|
+
}
|
|
541
|
+
function buildImplementationPrompt(dispatch, phase = completionPhaseFromDispatch(dispatch)) {
|
|
542
|
+
return `You are Forge's ${phase.replace(/_/g, " ")} phase runner running locally in a prepared git worktree.
|
|
543
|
+
|
|
544
|
+
Read .forge/work-items/${dispatch.workItem.id}/spec.md and any prior phase artifacts required by this phase.
|
|
545
|
+
${phaseWorkInstruction(phase)}
|
|
546
|
+
Use the repository's existing conventions. Run relevant tests when practical.
|
|
547
|
+
Forge owns protocol files: worktree prep, injected .forge commands/hooks/skills, phase verdict artifacts, reports, and commits.
|
|
548
|
+
Read Forge-provided context under .forge when needed, but do not edit .forge files or create Forge verdict/report artifacts by hand.
|
|
549
|
+
|
|
550
|
+
${completionPromptForPhase(phase)}
|
|
551
|
+
|
|
552
|
+
Finish with a concise summary of changed files and verification after calling the completion tool.`;
|
|
553
|
+
}
|
|
554
|
+
function phaseWorkInstruction(phase) {
|
|
555
|
+
if (phase === "self_review") {
|
|
556
|
+
return "Review the implementation diff, apply safe fixes when needed, and record remaining risks explicitly.";
|
|
109
557
|
}
|
|
110
|
-
|
|
558
|
+
if (phase === "validation") {
|
|
559
|
+
return "Run the validation commands and deterministic drift checks needed to prove the work is ready for Dev Test.";
|
|
560
|
+
}
|
|
561
|
+
return "Implement the work item and keep the change focused.";
|
|
562
|
+
}
|
|
563
|
+
function buildQaPrompt(dispatch) {
|
|
564
|
+
return `You are Forge's QA lane running locally.
|
|
565
|
+
|
|
566
|
+
Review the implementation for work item ${dispatch.workItem.id}: ${dispatch.workItem.title}
|
|
567
|
+
Run relevant verification when practical and finish with a concise QA verdict.`;
|
|
568
|
+
}
|
|
569
|
+
function buildConflictResolvePrompt(dispatch, branchName) {
|
|
570
|
+
const targetBranch = dispatch.cycleRun.requestedBase || dispatch.repo.defaultBranch || "main";
|
|
571
|
+
return `You are Forge's Conflict Resolve lane running locally in a prepared git worktree.
|
|
572
|
+
|
|
573
|
+
The current branch is the pull request branch ${branchName}. Resolve merge conflicts against ${targetBranch}.
|
|
574
|
+
Fetch origin, merge or rebase ${targetBranch} into ${branchName}, resolve conflicts conservatively, run relevant validation when practical, commit the resolution, and push ${branchName} to origin.
|
|
575
|
+
Keep the original work item intent intact:
|
|
576
|
+
- Work item: ${dispatch.workItem.id}
|
|
577
|
+
- Title: ${dispatch.workItem.title}
|
|
578
|
+
- Acceptance criteria: ${dispatch.workItem.acceptanceCriteria || "Not specified"}
|
|
579
|
+
|
|
580
|
+
Finish with a concise summary of the conflicts resolved and verification performed.`;
|
|
581
|
+
}
|
|
582
|
+
async function cleanupCompletionTools(completionTools, emit) {
|
|
583
|
+
try {
|
|
584
|
+
await completionTools.cleanup();
|
|
585
|
+
}
|
|
586
|
+
catch (error) {
|
|
587
|
+
await emit({
|
|
588
|
+
role: "runner",
|
|
589
|
+
kind: "checkpoint",
|
|
590
|
+
content: `Forge completion tool cleanup failed and was ignored: ${clip(messageFromError(error), 600)}`,
|
|
591
|
+
eventType: "completion_tools_cleanup_failed"
|
|
592
|
+
}).catch(() => undefined);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
async function commitWorktreeChanges(git, worktreePath, workItemId) {
|
|
596
|
+
await git.run(worktreePath, ["add", "-A"]);
|
|
597
|
+
await git.run(worktreePath, ["reset", "--", ".forge"]).catch(() => "");
|
|
598
|
+
const changedFiles = (await git.run(worktreePath, ["diff", "--cached", "--name-only"]))
|
|
599
|
+
.split(/\r?\n/)
|
|
600
|
+
.map((line) => line.trim())
|
|
601
|
+
.filter(Boolean);
|
|
602
|
+
if (changedFiles.length > 0) {
|
|
603
|
+
await git.run(worktreePath, ["commit", "-m", `provider cli changeset for ${workItemId.slice(0, 8)}`]);
|
|
604
|
+
}
|
|
605
|
+
else {
|
|
606
|
+
await git.run(worktreePath, ["commit", "--allow-empty", "-m", `provider cli completed for ${workItemId.slice(0, 8)}`]);
|
|
607
|
+
}
|
|
608
|
+
const commitSha = (await git.run(worktreePath, ["rev-parse", "HEAD"])).trim();
|
|
609
|
+
return { commitSha, changedFiles };
|
|
610
|
+
}
|
|
611
|
+
function messageFromError(error) {
|
|
612
|
+
return error instanceof Error ? error.message : String(error);
|
|
613
|
+
}
|
|
614
|
+
function clip(value, maxLength) {
|
|
615
|
+
if (value.length <= maxLength) {
|
|
616
|
+
return value;
|
|
617
|
+
}
|
|
618
|
+
return `${value.slice(0, maxLength)}...`;
|
|
619
|
+
}
|
|
620
|
+
function createProviderOutputState(provider) {
|
|
621
|
+
return provider.trim().toLowerCase() === "cursor"
|
|
622
|
+
? new CursorStreamJsonState()
|
|
623
|
+
: new CodexJsonState();
|
|
624
|
+
}
|
|
625
|
+
class CursorStreamJsonState {
|
|
626
|
+
sessionId = null;
|
|
627
|
+
finalMessage = null;
|
|
628
|
+
emittedSession = false;
|
|
629
|
+
get sessionLabel() {
|
|
630
|
+
return this.sessionId ? `session ${this.sessionId}` : null;
|
|
631
|
+
}
|
|
632
|
+
consume(line) {
|
|
633
|
+
const event = parseJsonLine(line);
|
|
634
|
+
if (!event) {
|
|
635
|
+
return null;
|
|
636
|
+
}
|
|
637
|
+
const type = stringField(event, "type");
|
|
638
|
+
if (type === "system" && stringField(event, "subtype") === "init") {
|
|
639
|
+
this.sessionId = stringField(event, "session_id");
|
|
640
|
+
if (this.sessionId && !this.emittedSession) {
|
|
641
|
+
this.emittedSession = true;
|
|
642
|
+
return {
|
|
643
|
+
role: "runner",
|
|
644
|
+
kind: "checkpoint",
|
|
645
|
+
content: `Cursor session started: ${this.sessionId}.`,
|
|
646
|
+
eventType: "provider_thread_started"
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
if (type === "assistant") {
|
|
651
|
+
const text = assistantMessageText(event);
|
|
652
|
+
if (text) {
|
|
653
|
+
this.finalMessage = clip(text, 4000);
|
|
654
|
+
return {
|
|
655
|
+
role: "assistant",
|
|
656
|
+
kind: "message",
|
|
657
|
+
content: this.finalMessage,
|
|
658
|
+
eventType: "provider_message"
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (type === "tool_call") {
|
|
663
|
+
const subtype = stringField(event, "subtype");
|
|
664
|
+
const toolCall = objectField(event, "tool_call");
|
|
665
|
+
if (subtype === "started") {
|
|
666
|
+
const summary = formatCursorToolCallStarted(toolCall);
|
|
667
|
+
if (summary) {
|
|
668
|
+
return {
|
|
669
|
+
role: "worker",
|
|
670
|
+
kind: "checkpoint",
|
|
671
|
+
content: summary,
|
|
672
|
+
eventType: "provider_tool_call"
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
if (subtype === "completed") {
|
|
677
|
+
const completed = formatCursorToolCallCompleted(toolCall);
|
|
678
|
+
if (completed) {
|
|
679
|
+
return {
|
|
680
|
+
role: "worker",
|
|
681
|
+
kind: completed.kind,
|
|
682
|
+
content: completed.content,
|
|
683
|
+
eventType: completed.kind === "file_edit" ? "provider_file_edit" : "provider_tool_call"
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
if (type === "result" && stringField(event, "subtype") === "success") {
|
|
689
|
+
const result = stringField(event, "result");
|
|
690
|
+
if (result) {
|
|
691
|
+
this.finalMessage = clip(result, 4000);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
if (type === "error" || (type === "result" && event.is_error === true)) {
|
|
695
|
+
return {
|
|
696
|
+
role: "runner",
|
|
697
|
+
kind: "error",
|
|
698
|
+
content: clip(line.trim(), 1000),
|
|
699
|
+
eventType: "provider_cli_error"
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
return null;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
function formatCursorToolCallStarted(toolCall) {
|
|
706
|
+
const readArgs = objectField(objectField(toolCall, "readToolCall"), "args");
|
|
707
|
+
const readPath = stringField(readArgs, "path");
|
|
708
|
+
if (readPath) {
|
|
709
|
+
return `Reading ${readPath}`;
|
|
710
|
+
}
|
|
711
|
+
const writeArgs = objectField(objectField(toolCall, "writeToolCall"), "args");
|
|
712
|
+
const writePath = stringField(writeArgs, "path");
|
|
713
|
+
if (writePath) {
|
|
714
|
+
return `Writing ${writePath}`;
|
|
715
|
+
}
|
|
716
|
+
const shellArgs = objectField(objectField(toolCall, "shellToolCall"), "args");
|
|
717
|
+
const command = stringField(shellArgs, "command");
|
|
718
|
+
if (command) {
|
|
719
|
+
return `Shell command started: ${clip(command, 500)}`;
|
|
720
|
+
}
|
|
721
|
+
const fn = objectField(toolCall, "function");
|
|
722
|
+
const name = stringField(fn, "name");
|
|
723
|
+
if (name) {
|
|
724
|
+
return `Tool call started: ${name}`;
|
|
725
|
+
}
|
|
726
|
+
return null;
|
|
727
|
+
}
|
|
728
|
+
function formatCursorToolCallCompleted(toolCall) {
|
|
729
|
+
const writeArgs = objectField(objectField(toolCall, "writeToolCall"), "args");
|
|
730
|
+
const writePath = stringField(writeArgs, "path");
|
|
731
|
+
const writeResult = objectField(objectField(objectField(toolCall, "writeToolCall"), "result"), "success");
|
|
732
|
+
const writtenPath = stringField(writeResult, "path") ?? writePath;
|
|
733
|
+
if (writtenPath) {
|
|
734
|
+
return {
|
|
735
|
+
kind: "file_edit",
|
|
736
|
+
content: `Wrote ${writtenPath}`
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
const readArgs = objectField(objectField(toolCall, "readToolCall"), "args");
|
|
740
|
+
const readPath = stringField(readArgs, "path");
|
|
741
|
+
if (readPath) {
|
|
742
|
+
const readResult = objectField(objectField(objectField(toolCall, "readToolCall"), "result"), "success");
|
|
743
|
+
const totalLines = readResult.totalLines;
|
|
744
|
+
const lineSummary = typeof totalLines === "number" ? ` (${totalLines} lines)` : "";
|
|
745
|
+
return {
|
|
746
|
+
kind: "checkpoint",
|
|
747
|
+
content: `Read ${readPath}${lineSummary}`
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
const shellArgs = objectField(objectField(toolCall, "shellToolCall"), "args");
|
|
751
|
+
const command = stringField(shellArgs, "command");
|
|
752
|
+
if (command) {
|
|
753
|
+
return {
|
|
754
|
+
kind: "checkpoint",
|
|
755
|
+
content: `Shell command completed: ${clip(command, 500)}`
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
const fn = objectField(toolCall, "function");
|
|
759
|
+
const name = stringField(fn, "name");
|
|
760
|
+
if (name) {
|
|
761
|
+
return {
|
|
762
|
+
kind: "checkpoint",
|
|
763
|
+
content: `Tool call completed: ${name}`
|
|
764
|
+
};
|
|
765
|
+
}
|
|
766
|
+
return null;
|
|
767
|
+
}
|
|
768
|
+
function assistantMessageText(event) {
|
|
769
|
+
const message = objectField(event, "message");
|
|
770
|
+
const content = message.content;
|
|
771
|
+
if (!Array.isArray(content)) {
|
|
772
|
+
return null;
|
|
773
|
+
}
|
|
774
|
+
const parts = content
|
|
775
|
+
.map((entry) => {
|
|
776
|
+
if (!entry || typeof entry !== "object") {
|
|
777
|
+
return null;
|
|
778
|
+
}
|
|
779
|
+
const block = entry;
|
|
780
|
+
return stringField(block, "text");
|
|
781
|
+
})
|
|
782
|
+
.filter((text) => Boolean(text));
|
|
783
|
+
if (parts.length === 0) {
|
|
784
|
+
return null;
|
|
785
|
+
}
|
|
786
|
+
return parts.join("");
|
|
787
|
+
}
|
|
788
|
+
function parseJsonLine(line) {
|
|
789
|
+
const trimmed = line.trim();
|
|
790
|
+
if (!trimmed.startsWith("{")) {
|
|
791
|
+
return null;
|
|
792
|
+
}
|
|
793
|
+
let parsed;
|
|
794
|
+
try {
|
|
795
|
+
parsed = JSON.parse(trimmed);
|
|
796
|
+
}
|
|
797
|
+
catch {
|
|
798
|
+
return null;
|
|
799
|
+
}
|
|
800
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
|
801
|
+
? parsed
|
|
802
|
+
: null;
|
|
803
|
+
}
|
|
804
|
+
class CodexJsonState {
|
|
805
|
+
threadId = null;
|
|
806
|
+
finalMessage = null;
|
|
807
|
+
emittedThread = false;
|
|
808
|
+
get sessionLabel() {
|
|
809
|
+
return this.threadId ? `thread ${this.threadId}` : null;
|
|
810
|
+
}
|
|
811
|
+
consume(line) {
|
|
812
|
+
const event = parseJsonLine(line);
|
|
813
|
+
if (!event) {
|
|
814
|
+
return null;
|
|
815
|
+
}
|
|
816
|
+
const type = stringField(event, "type") ?? "";
|
|
817
|
+
if (type === "thread.started") {
|
|
818
|
+
this.threadId = stringField(event, "thread_id");
|
|
819
|
+
if (this.threadId && !this.emittedThread) {
|
|
820
|
+
this.emittedThread = true;
|
|
821
|
+
return {
|
|
822
|
+
role: "runner",
|
|
823
|
+
kind: "checkpoint",
|
|
824
|
+
content: `Codex thread started: ${this.threadId}.`,
|
|
825
|
+
eventType: "provider_thread_started"
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
if (type === "turn.completed") {
|
|
830
|
+
return {
|
|
831
|
+
role: "runner",
|
|
832
|
+
kind: "checkpoint",
|
|
833
|
+
content: "Codex turn completed.",
|
|
834
|
+
eventType: "provider_turn_completed"
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
if (type === "turn.failed" || type === "error") {
|
|
838
|
+
return {
|
|
839
|
+
role: "runner",
|
|
840
|
+
kind: "error",
|
|
841
|
+
content: clip(line.trim(), 1000),
|
|
842
|
+
eventType: "provider_cli_error"
|
|
843
|
+
};
|
|
844
|
+
}
|
|
845
|
+
if (type === "item.completed" || type === "item.updated") {
|
|
846
|
+
const item = objectField(event, "item");
|
|
847
|
+
const itemType = stringField(item, "type");
|
|
848
|
+
if (itemType === "agent_message") {
|
|
849
|
+
const text = stringField(item, "text");
|
|
850
|
+
if (text) {
|
|
851
|
+
this.finalMessage = clip(text, 4000);
|
|
852
|
+
return {
|
|
853
|
+
role: "assistant",
|
|
854
|
+
kind: "message",
|
|
855
|
+
content: this.finalMessage,
|
|
856
|
+
eventType: "provider_message"
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
if (itemType === "command_execution") {
|
|
861
|
+
const command = stringField(item, "command");
|
|
862
|
+
const status = stringField(item, "status");
|
|
863
|
+
if (command) {
|
|
864
|
+
return {
|
|
865
|
+
role: "worker",
|
|
866
|
+
kind: "checkpoint",
|
|
867
|
+
content: `Codex command ${status || "completed"}: ${clip(command, 500)}`,
|
|
868
|
+
eventType: "provider_command"
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
return null;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
function objectField(source, key) {
|
|
877
|
+
const value = source[key];
|
|
878
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
879
|
+
? value
|
|
880
|
+
: {};
|
|
881
|
+
}
|
|
882
|
+
function stringField(source, key) {
|
|
883
|
+
const value = source[key];
|
|
884
|
+
return typeof value === "string" && value.trim().length > 0 ? value : null;
|
|
111
885
|
}
|