master-orchestrator 0.0.0-stage → 0.2.0
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/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +176 -2
- package/package.json +56 -4
- package/src/adapters/agy.ts +285 -0
- package/src/adapters/claude.ts +317 -0
- package/src/adapters/codex.ts +162 -0
- package/src/adapters/common.ts +211 -0
- package/src/adapters/dsh.ts +356 -0
- package/src/adapters/fake-agent.mjs +50 -0
- package/src/adapters/fake.ts +88 -0
- package/src/adapters/gemini.ts +239 -0
- package/src/adapters/grok.ts +328 -0
- package/src/adapters/omp.ts +119 -0
- package/src/adapters/opencode.ts +228 -0
- package/src/adapters/pi.ts +284 -0
- package/src/adapters/registry.ts +27 -0
- package/src/adapters/types.ts +201 -0
- package/src/config/load.ts +290 -0
- package/src/config/profiles.ts +64 -0
- package/src/config/schema.ts +158 -0
- package/src/config/write.ts +83 -0
- package/src/engine/artifacts.ts +145 -0
- package/src/engine/baseline.ts +132 -0
- package/src/engine/execute.ts +437 -0
- package/src/engine/git.ts +32 -0
- package/src/engine/hostenv.ts +54 -0
- package/src/engine/registry.ts +280 -0
- package/src/engine/runner.ts +249 -0
- package/src/engine/types.ts +55 -0
- package/src/engine/verify.ts +100 -0
- package/src/index.ts +602 -0
- package/src/prompts/envelope.ts +29 -0
- package/src/prompts/templates.ts +389 -0
- package/src/ui/configure.ts +174 -0
- package/src/ui/dialogs.ts +88 -0
- package/src/ui/doctor.ts +120 -0
- package/src/ui/format.ts +94 -0
- package/src/ui/host.ts +100 -0
- package/src/ui/render.ts +86 -0
- package/src/ui/widget.ts +70 -0
- package/src/workflows/budget.ts +66 -0
- package/src/workflows/common.ts +100 -0
- package/src/workflows/debate.ts +488 -0
- package/src/workflows/host.ts +78 -0
- package/src/workflows/plan-exec.ts +807 -0
- package/src/workflows/plan.ts +407 -0
- package/src/workflows/planfile.ts +380 -0
- package/src/workflows/planstate.ts +229 -0
- package/src/workflows/verdict.ts +74 -0
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
import {
|
|
4
|
+
classifyFailureText,
|
|
5
|
+
clip,
|
|
6
|
+
detectByVersion,
|
|
7
|
+
getNumber,
|
|
8
|
+
getString,
|
|
9
|
+
isObject,
|
|
10
|
+
parseJsonLine,
|
|
11
|
+
recordDenied,
|
|
12
|
+
recordSession,
|
|
13
|
+
recordText,
|
|
14
|
+
recordUsage,
|
|
15
|
+
standardFinalize,
|
|
16
|
+
} from "./common.ts";
|
|
17
|
+
import type {
|
|
18
|
+
AdapterOutcome,
|
|
19
|
+
AgentAdapter,
|
|
20
|
+
HostEnv,
|
|
21
|
+
Invocation,
|
|
22
|
+
NormalizedEvent,
|
|
23
|
+
ParserState,
|
|
24
|
+
RawRunResult,
|
|
25
|
+
RunRequest,
|
|
26
|
+
RunStatus,
|
|
27
|
+
} from "./types.ts";
|
|
28
|
+
|
|
29
|
+
const optionsSchema = Type.Object(
|
|
30
|
+
{
|
|
31
|
+
patchFile: Type.Optional(
|
|
32
|
+
Type.String({
|
|
33
|
+
description:
|
|
34
|
+
"Absolute path of a dsh patch-list overlay (YAML) passed as `--patch` on every run, follow-ups included; " +
|
|
35
|
+
"it supplies the model route (`llm-pi-ai` providers and the `agent-default-model` row)",
|
|
36
|
+
}),
|
|
37
|
+
),
|
|
38
|
+
},
|
|
39
|
+
{ additionalProperties: false },
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
type ClassifiedStatus = Extract<RunStatus, "unavailable" | "auth_required" | "quota">;
|
|
43
|
+
|
|
44
|
+
/** LLM failure codes of `dsh-llm/src/error.ts` that a role's fallback chain reacts to. */
|
|
45
|
+
const FAILURE_CODES: Record<string, { status: ClassifiedStatus; code: string }> = {
|
|
46
|
+
MISSING_CREDENTIAL: { status: "auth_required", code: "auth_required" },
|
|
47
|
+
INVALID_CREDENTIAL: { status: "auth_required", code: "auth_required" },
|
|
48
|
+
AUTH: { status: "auth_required", code: "auth_required" },
|
|
49
|
+
ACCOUNT_SIGN_IN_REQUIRED: { status: "auth_required", code: "auth_required" },
|
|
50
|
+
ACCOUNT_TOKEN_INVALID: { status: "auth_required", code: "auth_required" },
|
|
51
|
+
NO_CREDENTIAL_STORE: { status: "auth_required", code: "auth_required" },
|
|
52
|
+
HTTP_401: { status: "auth_required", code: "auth_required" },
|
|
53
|
+
HTTP_403: { status: "auth_required", code: "auth_required" },
|
|
54
|
+
QUOTA: { status: "quota", code: "quota" },
|
|
55
|
+
ACCOUNT_QUOTA: { status: "quota", code: "quota" },
|
|
56
|
+
RATE_LIMIT: { status: "quota", code: "quota" },
|
|
57
|
+
HTTP_429: { status: "quota", code: "quota" },
|
|
58
|
+
UNKNOWN_MODEL: { status: "unavailable", code: "model_unavailable" },
|
|
59
|
+
MODEL_UNSUPPORTED: { status: "unavailable", code: "model_unavailable" },
|
|
60
|
+
UNSUPPORTED_REASONING_EFFORT: { status: "unavailable", code: "model_unavailable" },
|
|
61
|
+
INVALID_MODEL_CONTEXT: { status: "unavailable", code: "model_unavailable" },
|
|
62
|
+
INVALID_MODEL_INFO: { status: "unavailable", code: "model_unavailable" },
|
|
63
|
+
INVALID_MODEL_MAX_TOKENS: { status: "unavailable", code: "model_unavailable" },
|
|
64
|
+
INVALID_MODEL_REASONING: { status: "unavailable", code: "model_unavailable" },
|
|
65
|
+
NO_ADAPTER: { status: "unavailable", code: "config_invalid" },
|
|
66
|
+
INVALID_CONFIG: { status: "unavailable", code: "config_invalid" },
|
|
67
|
+
UNSUPPORTED_OPTION: { status: "unavailable", code: "config_invalid" },
|
|
68
|
+
CLIENT_UPGRADE_REQUIRED: { status: "unavailable", code: "client_upgrade_required" },
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** Tool error code of a call the auto-reviewer refused (AUTO_REVIEW_FAILED is a reviewer failure, not a denial). */
|
|
72
|
+
const DENIED_TOOL_CODES = new Set(["AUTO_REVIEW_DENIED"]);
|
|
73
|
+
const DENIED_RESULT = /\[sandbox:[^\]]*denied|no approval channel is available/i;
|
|
74
|
+
const HOME_NOT_WRITABLE = /\b(EACCES|EROFS|EPERM)\b/;
|
|
75
|
+
const RESUME_ERROR = /--session-id|session "[^"]*" does not exist|another (directory|folder)/i;
|
|
76
|
+
|
|
77
|
+
interface TurnEnd {
|
|
78
|
+
turn?: number;
|
|
79
|
+
kind?: string;
|
|
80
|
+
code?: string;
|
|
81
|
+
message?: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
interface DshExtra {
|
|
85
|
+
lastTurnStart?: number;
|
|
86
|
+
lastTurnEnd?: TurnEnd;
|
|
87
|
+
sawFinal?: boolean;
|
|
88
|
+
finalWithoutTurn?: boolean;
|
|
89
|
+
sawError?: boolean;
|
|
90
|
+
calls?: Record<string, string>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* DeepSeek Harness (`dsh --profile headless --json`). Event schema: `dsh-headless/src/json-stream.ts`
|
|
95
|
+
* (session, status turn/step phases, thinking, text, tool_call, tool_result, final, error).
|
|
96
|
+
* The headless profile has no model flag: the model comes from the composed `agent-default-model` row,
|
|
97
|
+
* which the profile option `patchFile` or a generated overlay replaces.
|
|
98
|
+
* SIGTERM ends the run with a partial `final` and no matching `turn_end` (exit 0); SIGINT exits 130.
|
|
99
|
+
*/
|
|
100
|
+
export const dshAdapter: AgentAdapter = {
|
|
101
|
+
id: "dsh",
|
|
102
|
+
displayName: "DeepSeek Harness",
|
|
103
|
+
binary: "dsh",
|
|
104
|
+
testedVersions: ">=0.2.1-rc.3 <0.3.0-0",
|
|
105
|
+
stability: "experimental",
|
|
106
|
+
liveChecked: "0.2.2-rc.3 on 2026-11-25: analyze, implement, follow-up, cancellation, timeout, auth failure (route from a patchFile)",
|
|
107
|
+
stopSignal: "SIGTERM",
|
|
108
|
+
optionsSchema,
|
|
109
|
+
capabilities: {
|
|
110
|
+
resume: true,
|
|
111
|
+
structuredOutput: false,
|
|
112
|
+
listModels: false,
|
|
113
|
+
reportsCost: false,
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
detect(env: HostEnv) {
|
|
117
|
+
return detectByVersion(env, this.testedVersions);
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
buildInvocation(req: RunRequest): Invocation {
|
|
121
|
+
const files = [];
|
|
122
|
+
const outputFiles: Record<string, string> = {};
|
|
123
|
+
// Launcher flags (`--profile`, `--patch`) come first; the first unknown token starts the app's arguments.
|
|
124
|
+
const args = ["--profile", "headless"];
|
|
125
|
+
const patchFile = typeof req.options?.patchFile === "string" ? req.options.patchFile : undefined;
|
|
126
|
+
if (patchFile) args.push("--patch", patchFile);
|
|
127
|
+
const overlay = modelOverlay(req.model, req.effort);
|
|
128
|
+
if (overlay !== undefined) {
|
|
129
|
+
// Patches apply in argv order, so the per-run model row replaces the one from `patchFile`.
|
|
130
|
+
const overlayPath = join(req.runDir, "dsh-model.yml");
|
|
131
|
+
files.push({ path: overlayPath, content: overlay });
|
|
132
|
+
args.push("--patch", overlayPath);
|
|
133
|
+
outputFiles.modelOverlay = overlayPath;
|
|
134
|
+
}
|
|
135
|
+
args.push("--json");
|
|
136
|
+
if (req.sessionId) args.push("--session-id", req.sessionId);
|
|
137
|
+
// `-` reads the task from stdin.
|
|
138
|
+
args.push(...(req.extraArgs ?? []), "-");
|
|
139
|
+
return {
|
|
140
|
+
args,
|
|
141
|
+
cwd: req.workspace,
|
|
142
|
+
// The user's DSH_HOME stays: it holds the sessions a follow-up resumes, the `.env` credential fallback,
|
|
143
|
+
// and the user's profile patches and skills. An unwritable home fails the boot and reads as `unavailable`.
|
|
144
|
+
env: { ...req.env, DSH_PERMISSION_MODE: "danger-full-access" },
|
|
145
|
+
stdin: req.prompt,
|
|
146
|
+
files,
|
|
147
|
+
outputFiles,
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
parseEvent(line: string, state: ParserState): NormalizedEvent[] {
|
|
152
|
+
const event = parseJsonLine(line);
|
|
153
|
+
if (!event) {
|
|
154
|
+
if (line.trim()) state.nonJsonLines++;
|
|
155
|
+
return [];
|
|
156
|
+
}
|
|
157
|
+
const extra = state.extra as DshExtra;
|
|
158
|
+
switch (getString(event, "type")) {
|
|
159
|
+
case "session":
|
|
160
|
+
return recordSession(state, getString(event, "sessionId"));
|
|
161
|
+
case "status":
|
|
162
|
+
return parseStatus(event, state, extra);
|
|
163
|
+
case "thinking": {
|
|
164
|
+
const text = getString(event, "text");
|
|
165
|
+
return text?.trim() ? [{ type: "activity", kind: "thinking", summary: clip(text) }] : [];
|
|
166
|
+
}
|
|
167
|
+
case "text":
|
|
168
|
+
return recordText(state, getString(event, "text"));
|
|
169
|
+
case "tool_call": {
|
|
170
|
+
const summary = toolSummary(getString(event, "tool") ?? "tool", event.input);
|
|
171
|
+
const callId = getString(event, "callId");
|
|
172
|
+
if (callId) extra.calls = { ...extra.calls, [callId]: summary };
|
|
173
|
+
return [{ type: "activity", kind: "tool", summary }];
|
|
174
|
+
}
|
|
175
|
+
case "tool_result":
|
|
176
|
+
return parseToolResult(event, state, extra);
|
|
177
|
+
case "final": {
|
|
178
|
+
extra.sawFinal = true;
|
|
179
|
+
state.finalText = getString(event, "text") ?? "";
|
|
180
|
+
// 0.2.2 adds `reason`; null means the run ended without a terminal turn (interrupted).
|
|
181
|
+
if ("reason" in event && event.reason === null) extra.finalWithoutTurn = true;
|
|
182
|
+
return [];
|
|
183
|
+
}
|
|
184
|
+
case "error": {
|
|
185
|
+
const message = getString(event, "message") ?? "error";
|
|
186
|
+
extra.sawError = true;
|
|
187
|
+
state.errors.push(message);
|
|
188
|
+
return [{ type: "warning", message: clip(message, 200) }];
|
|
189
|
+
}
|
|
190
|
+
default:
|
|
191
|
+
return [];
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
finalize(raw, state) {
|
|
196
|
+
const extra = state.extra as DshExtra;
|
|
197
|
+
const overlay = raw.outputFiles.modelOverlay;
|
|
198
|
+
if (raw.spawnError) return standardFinalize(raw, state);
|
|
199
|
+
|
|
200
|
+
// A boot failure (profile plugins, unwritable DSH_HOME) prints only to stderr and emits no JSON.
|
|
201
|
+
if (!state.sessionId && !extra.sawError && !extra.sawFinal && raw.exitCode !== null && raw.exitCode !== 0) {
|
|
202
|
+
return startupFailure(raw, state, overlay);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// A failure outside a turn: unknown option, empty task, unusable `--session-id`.
|
|
206
|
+
if (extra.sawError && !extra.sawFinal) {
|
|
207
|
+
const message = state.errors.join("\n");
|
|
208
|
+
if (!classifyFailureText(message) && RESUME_ERROR.test(message)) {
|
|
209
|
+
return { ...baseOutcome(state), status: "failed", reason: `resume_failed: ${clip(state.errors.at(-1) ?? message, 200)}` };
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const end = extra.lastTurnEnd;
|
|
214
|
+
const matched = end !== undefined && extra.lastTurnStart !== undefined && end.turn === extra.lastTurnStart;
|
|
215
|
+
if (extra.lastTurnStart !== undefined && (!matched || extra.finalWithoutTurn)) {
|
|
216
|
+
state.terminal = { ok: false, reason: "interrupted: the last turn has no turn_end" };
|
|
217
|
+
} else if (end) {
|
|
218
|
+
state.terminal = turnTerminal(end, state);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const outcome = standardFinalize(raw, state, { finalText: state.finalText });
|
|
222
|
+
const cls = end?.code ? FAILURE_CODES[end.code] : undefined;
|
|
223
|
+
if (cls && outcome.status !== "completed") {
|
|
224
|
+
outcome.status = cls.status;
|
|
225
|
+
outcome.reason = `${cls.code}: ${end?.code}: ${clip(end?.message ?? "", 160)}`;
|
|
226
|
+
if (cls.code === "model_unavailable" && overlay) outcome.reason += ` (${overlayHint(overlay)})`;
|
|
227
|
+
}
|
|
228
|
+
return outcome;
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
function parseStatus(event: Record<string, unknown>, state: ParserState, extra: DshExtra): NormalizedEvent[] {
|
|
233
|
+
const phase = getString(event, "phase");
|
|
234
|
+
const turn = getNumber(event, "turn");
|
|
235
|
+
if (phase === "turn_start") {
|
|
236
|
+
extra.lastTurnStart = turn;
|
|
237
|
+
return [];
|
|
238
|
+
}
|
|
239
|
+
if (phase === "step_end" && isObject(event.usage)) {
|
|
240
|
+
return [
|
|
241
|
+
recordUsage(state, {
|
|
242
|
+
inputTokens: getNumber(event, "usage", "inputTokens"),
|
|
243
|
+
outputTokens: getNumber(event, "usage", "outputTokens"),
|
|
244
|
+
}),
|
|
245
|
+
];
|
|
246
|
+
}
|
|
247
|
+
if (phase === "turn_end") {
|
|
248
|
+
state.usage.turns = (state.usage.turns ?? 0) + 1;
|
|
249
|
+
const end: TurnEnd = {
|
|
250
|
+
turn,
|
|
251
|
+
kind: getString(event, "reason", "kind"),
|
|
252
|
+
code: getString(event, "reason", "error", "code"),
|
|
253
|
+
message: getString(event, "reason", "error", "message"),
|
|
254
|
+
};
|
|
255
|
+
extra.lastTurnEnd = end;
|
|
256
|
+
if (end.kind === "blocked") {
|
|
257
|
+
return [recordDenied(state, `turn ${turn ?? "?"} blocked by a hook or policy before its next step`)];
|
|
258
|
+
}
|
|
259
|
+
if (end.kind === "error") {
|
|
260
|
+
const message = `${end.code ?? "UNKNOWN"}: ${end.message ?? "turn failed"}`;
|
|
261
|
+
state.errors.push(message);
|
|
262
|
+
return [{ type: "warning", message: clip(message, 200) }];
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return [];
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function parseToolResult(event: Record<string, unknown>, state: ParserState, extra: DshExtra): NormalizedEvent[] {
|
|
269
|
+
if (getString(event, "status") !== "error") return [];
|
|
270
|
+
const callId = getString(event, "callId");
|
|
271
|
+
const tool = (callId && extra.calls?.[callId]) || "tool";
|
|
272
|
+
const code = getString(event, "error", "code");
|
|
273
|
+
const result = getString(event, "result") ?? "";
|
|
274
|
+
if ((code && DENIED_TOOL_CODES.has(code)) || DENIED_RESULT.test(result)) {
|
|
275
|
+
const detail = getString(event, "error", "reason") ?? result;
|
|
276
|
+
return [recordDenied(state, `${clip(tool, 120)}: ${clip(detail, 120)}`)];
|
|
277
|
+
}
|
|
278
|
+
return [];
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function turnTerminal(end: TurnEnd, state: ParserState): ParserState["terminal"] {
|
|
282
|
+
switch (end.kind) {
|
|
283
|
+
case "completed":
|
|
284
|
+
return { ok: true };
|
|
285
|
+
case "blocked":
|
|
286
|
+
return { ok: false, reason: "blocked: a hook or policy rejected the next step" };
|
|
287
|
+
case "error":
|
|
288
|
+
return { ok: false, reason: state.errors.at(-1) ?? "error" };
|
|
289
|
+
case "max-tokens":
|
|
290
|
+
return { ok: false, reason: "max_tokens: the turn hit the output token limit" };
|
|
291
|
+
default:
|
|
292
|
+
return { ok: false, reason: `${end.kind ?? "unknown"}: the turn did not complete` };
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function startupFailure(raw: RawRunResult, state: ParserState, overlay: string | undefined): AdapterOutcome {
|
|
297
|
+
const base = baseOutcome(state);
|
|
298
|
+
const cls = classifyFailureText(raw.stderr);
|
|
299
|
+
if (cls) return { ...base, status: cls.status, reason: `${cls.code}: ${cls.line}` };
|
|
300
|
+
const lines = raw.stderr.split(/\r?\n/).filter((line) => line.trim());
|
|
301
|
+
const notWritable = lines.find((line) => HOME_NOT_WRITABLE.test(line));
|
|
302
|
+
if (notWritable) return { ...base, status: "unavailable", reason: `dsh_home_not_writable: ${clip(notWritable, 200)}` };
|
|
303
|
+
let reason = `startup_failed: ${clip(lines.at(-1) ?? `exited with ${raw.exitCode} before the session started`, 200)}`;
|
|
304
|
+
if (overlay) reason += ` (${overlayHint(overlay)})`;
|
|
305
|
+
return { ...base, status: "unavailable", reason };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function baseOutcome(state: ParserState) {
|
|
309
|
+
const usage = Object.keys(state.usage).length > 0 ? { ...state.usage } : undefined;
|
|
310
|
+
return { sessionId: state.sessionId, deniedActions: [...new Set(state.denied)], usage };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function overlayHint(overlay: string): string {
|
|
314
|
+
const provider = overlayValue(overlay, "provider");
|
|
315
|
+
const model = overlayValue(overlay, "model");
|
|
316
|
+
const route = provider && model ? `${provider}/${model}` : `model ${model ?? "?"} without a provider`;
|
|
317
|
+
return `model overlay selects ${route}; dsh takes the model as <provider>/<model> of a route declared in the patchFile`;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function overlayValue(overlay: string, key: string): string | undefined {
|
|
321
|
+
const raw = overlay.match(new RegExp(`^\\s+${key}: (.*)$`, "m"))?.[1];
|
|
322
|
+
if (raw === undefined) return undefined;
|
|
323
|
+
try {
|
|
324
|
+
const value = JSON.parse(raw) as unknown;
|
|
325
|
+
return typeof value === "string" ? value : raw;
|
|
326
|
+
} catch {
|
|
327
|
+
return raw;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* The `agent-default-model` row for `req.model` (`provider/model`, split at the first `/`) and `req.effort`.
|
|
333
|
+
* A user patch replaces the whole row, so the overlay restates every field. A model without a provider is
|
|
334
|
+
* written as given and fails the boot (`unavailable`); an effort without a model cannot be applied to the
|
|
335
|
+
* configured default and is left out.
|
|
336
|
+
*/
|
|
337
|
+
function modelOverlay(model: string | undefined, effort: string | undefined): string | undefined {
|
|
338
|
+
if (!model) return undefined;
|
|
339
|
+
const slash = model.indexOf("/");
|
|
340
|
+
const lines = ["- id: agent-default-model", " config:"];
|
|
341
|
+
if (slash > 0) {
|
|
342
|
+
lines.push(` provider: ${JSON.stringify(model.slice(0, slash))}`, ` model: ${JSON.stringify(model.slice(slash + 1))}`);
|
|
343
|
+
} else {
|
|
344
|
+
lines.push(` model: ${JSON.stringify(model)}`);
|
|
345
|
+
}
|
|
346
|
+
if (effort) lines.push(` reasoningEffort: ${JSON.stringify(effort)}`);
|
|
347
|
+
return `${lines.join("\n")}\n`;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function toolSummary(tool: string, input: unknown): string {
|
|
351
|
+
const command = getString(input, "command");
|
|
352
|
+
if (command) return `$ ${clip(command, 90)}`;
|
|
353
|
+
const path = getString(input, "path") ?? getString(input, "file_path") ?? getString(input, "filePath");
|
|
354
|
+
if (path) return `${tool} ${clip(path, 90)}`;
|
|
355
|
+
return tool;
|
|
356
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Scripted stand-in for an agent CLI, driven by a scenario file. Used by the engine tests through the fake adapter.
|
|
3
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
4
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { dirname, resolve } from "node:path";
|
|
6
|
+
|
|
7
|
+
const scenario = JSON.parse(readFileSync(process.argv[2], "utf8"));
|
|
8
|
+
const sleep = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
9
|
+
const line = (value) => process.stdout.write(`${JSON.stringify(value)}\n`);
|
|
10
|
+
|
|
11
|
+
let prompt = "";
|
|
12
|
+
if (!process.stdin.isTTY) {
|
|
13
|
+
for await (const chunk of process.stdin) prompt += chunk;
|
|
14
|
+
}
|
|
15
|
+
if (scenario.recordPrompt) writeFileSync(scenario.recordPrompt, prompt);
|
|
16
|
+
|
|
17
|
+
for (const step of scenario.steps ?? []) {
|
|
18
|
+
if (step.ignoreSignals) for (const name of step.ignoreSignals) process.on(name, () => {});
|
|
19
|
+
if (step.onSignal) {
|
|
20
|
+
process.on(step.onSignal.signal, () => {
|
|
21
|
+
line({ type: "result", text: step.onSignal.text ?? "interrupted", ok: false });
|
|
22
|
+
process.exit(step.onSignal.exit ?? 130);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
if (step.session) line({ type: "session", id: step.session });
|
|
26
|
+
if (step.text !== undefined) line({ type: "text", text: step.text });
|
|
27
|
+
if (step.tool) line({ type: "tool", name: step.tool });
|
|
28
|
+
if (step.denied) line({ type: "denied", action: step.denied });
|
|
29
|
+
if (step.usage) line({ type: "usage", ...step.usage });
|
|
30
|
+
if (step.error) line({ type: "error", message: step.error });
|
|
31
|
+
if (step.emit) line(step.emit);
|
|
32
|
+
if (step.raw !== undefined) process.stdout.write(step.raw);
|
|
33
|
+
if (step.stderr) process.stderr.write(step.stderr);
|
|
34
|
+
if (step.write) {
|
|
35
|
+
const target = resolve(process.cwd(), step.write.path);
|
|
36
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
37
|
+
writeFileSync(target, step.write.content ?? "");
|
|
38
|
+
}
|
|
39
|
+
if (step.spawnSleeper) {
|
|
40
|
+
const child = spawn("sleep", [String(step.spawnSleeper.seconds ?? 60)], { stdio: "ignore" });
|
|
41
|
+
if (step.spawnSleeper.pidFile) writeFileSync(step.spawnSleeper.pidFile, String(child.pid));
|
|
42
|
+
}
|
|
43
|
+
if (step.envToFile) writeFileSync(step.envToFile.path, process.env[step.envToFile.name] ?? "");
|
|
44
|
+
if (step.argsToFile) writeFileSync(step.argsToFile, JSON.stringify(process.argv.slice(3)));
|
|
45
|
+
if (step.run) spawnSync(step.run[0], step.run.slice(1), { stdio: "ignore" });
|
|
46
|
+
if (step.result !== undefined) line({ type: "result", text: step.result, ok: step.ok ?? true });
|
|
47
|
+
if (step.sleep) await sleep(step.sleep);
|
|
48
|
+
if (step.exit !== undefined) process.exit(step.exit);
|
|
49
|
+
}
|
|
50
|
+
process.exit(scenario.exitCode ?? 0);
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
import { clip, getNumber, getString, parseJsonLine, recordDenied, recordSession, recordText, recordUsage, standardFinalize } from "./common.ts";
|
|
4
|
+
import type { AgentAdapter, Invocation, NormalizedEvent, ParserState, RunRequest } from "./types.ts";
|
|
5
|
+
|
|
6
|
+
export const FAKE_AGENT_SCRIPT = join(import.meta.dirname, "fake-agent.mjs");
|
|
7
|
+
|
|
8
|
+
export interface FakeScenario {
|
|
9
|
+
steps?: Array<Record<string, unknown>>;
|
|
10
|
+
exitCode?: number;
|
|
11
|
+
recordPrompt?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface FakeAdapterOptions {
|
|
15
|
+
id?: string;
|
|
16
|
+
stopSignal?: "SIGINT" | "SIGTERM";
|
|
17
|
+
resume?: boolean;
|
|
18
|
+
/** Picks the scenario of a run; defaults to `options.scenario` of the profile. */
|
|
19
|
+
scenarioFor?: (req: RunRequest) => FakeScenario;
|
|
20
|
+
watchStderr?: AgentAdapter["watchStderr"];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Scripted adapter for tests: runs `fake-agent.mjs` under the current Node with a scenario file. */
|
|
24
|
+
export function createFakeAdapter(options: FakeAdapterOptions = {}): AgentAdapter {
|
|
25
|
+
return {
|
|
26
|
+
id: options.id ?? "fake",
|
|
27
|
+
displayName: "Fake agent",
|
|
28
|
+
binary: process.execPath,
|
|
29
|
+
testedVersions: ">=1.0.0",
|
|
30
|
+
stability: "stable",
|
|
31
|
+
stopSignal: options.stopSignal ?? "SIGTERM",
|
|
32
|
+
watchStderr: options.watchStderr,
|
|
33
|
+
optionsSchema: Type.Object({ scenario: Type.Optional(Type.Unknown()) }, { additionalProperties: false }),
|
|
34
|
+
capabilities: { resume: options.resume ?? true, structuredOutput: false, listModels: false, reportsCost: false },
|
|
35
|
+
|
|
36
|
+
async detect() {
|
|
37
|
+
return { available: true, path: process.execPath, version: "1.0.0", inTestedRange: true };
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
buildInvocation(req: RunRequest): Invocation {
|
|
41
|
+
const scenario = options.scenarioFor?.(req) ?? ((req.options?.scenario as FakeScenario | undefined) ?? { steps: [] });
|
|
42
|
+
const scenarioPath = join(req.runDir, "scenario.json");
|
|
43
|
+
return {
|
|
44
|
+
args: [FAKE_AGENT_SCRIPT, scenarioPath, ...(req.sessionId ? ["--resume", req.sessionId] : [])],
|
|
45
|
+
cwd: req.workspace,
|
|
46
|
+
stdin: req.prompt,
|
|
47
|
+
files: [{ path: scenarioPath, content: JSON.stringify(scenario) }],
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
parseEvent(line: string, state: ParserState): NormalizedEvent[] {
|
|
52
|
+
const event = parseJsonLine(line);
|
|
53
|
+
if (!event) {
|
|
54
|
+
if (line.trim()) state.nonJsonLines++;
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
switch (getString(event, "type")) {
|
|
58
|
+
case "session":
|
|
59
|
+
return recordSession(state, getString(event, "id"));
|
|
60
|
+
case "text":
|
|
61
|
+
return recordText(state, getString(event, "text"));
|
|
62
|
+
case "tool":
|
|
63
|
+
return [{ type: "activity", kind: "tool", summary: clip(getString(event, "name") ?? "tool") }];
|
|
64
|
+
case "denied":
|
|
65
|
+
return [recordDenied(state, getString(event, "action") ?? "action")];
|
|
66
|
+
case "usage":
|
|
67
|
+
return [recordUsage(state, { inputTokens: getNumber(event, "input"), outputTokens: getNumber(event, "output") })];
|
|
68
|
+
case "error":
|
|
69
|
+
state.errors.push(getString(event, "message") ?? "error");
|
|
70
|
+
return [{ type: "warning", message: getString(event, "message") ?? "error" }];
|
|
71
|
+
case "result": {
|
|
72
|
+
const ok = event.ok !== false;
|
|
73
|
+
state.terminal = { ok, reason: ok ? undefined : getString(event, "text") };
|
|
74
|
+
if (ok) state.finalText = getString(event, "text");
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
77
|
+
default:
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
finalize(raw, state) {
|
|
83
|
+
return standardFinalize(raw, state);
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const fakeAdapter = createFakeAdapter();
|