@speculoai/baton 0.4.0 → 0.5.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/LICENSE +21 -0
- package/README.md +103 -391
- package/README.zh-CN.md +103 -391
- package/dist/adapters/agy.js +8 -3
- package/dist/adapters/claude.js +11 -4
- package/dist/adapters/codex.js +11 -2
- package/dist/adapters/cursor.js +14 -6
- package/dist/cli.js +99 -37
- package/dist/contract.js +7 -7
- package/dist/git.js +22 -3
- package/dist/install-ui.js +74 -0
- package/dist/install.js +21 -31
- package/dist/mcp.js +21 -16
- package/dist/process.js +1 -1
- package/dist/rules.js +394 -0
- package/dist/scope.js +5 -5
- package/dist/store.js +2 -2
- package/dist/supervisor.js +38 -11
- package/dist/tail.js +5 -5
- package/dist/tasks.js +146 -34
- package/dist/worktree.js +104 -19
- package/package.json +5 -3
package/dist/supervisor.js
CHANGED
|
@@ -6,6 +6,7 @@ import { getAdapter } from "./adapters/index.js";
|
|
|
6
6
|
import { buildPrompt, interpret } from "./contract.js";
|
|
7
7
|
import { baselineToSnapshot, gitChanges, gitRepoRoot, gitSnapshot, pathsSinceBaseline } from "./git.js";
|
|
8
8
|
import { applyScopePolicy, findScopeViolations, isDirUnderCwd, relativizeToTaskCwd } from "./scope.js";
|
|
9
|
+
import { resolveRulesync, rulesGitRoot, rulesyncCheck } from "./rules.js";
|
|
9
10
|
import { appendEvent, readMeta, writeMeta, readTurn, writeTurn, taskDir, lockTask } from "./store.js";
|
|
10
11
|
import { redact } from "./redact.js";
|
|
11
12
|
import { killGroup } from "./process.js";
|
|
@@ -85,7 +86,7 @@ export async function supervise(id) {
|
|
|
85
86
|
const turn = readTurn(id, meta.turn);
|
|
86
87
|
const adapter = getAdapter(meta.target);
|
|
87
88
|
if (!adapter)
|
|
88
|
-
throw new Error("
|
|
89
|
+
throw new Error("Unknown target.");
|
|
89
90
|
const save = () => { meta.updated_at = new Date().toISOString(); writeMeta(meta); };
|
|
90
91
|
const emit = (data) => {
|
|
91
92
|
meta.last_event_at = new Date().toISOString();
|
|
@@ -94,6 +95,28 @@ export async function supervise(id) {
|
|
|
94
95
|
};
|
|
95
96
|
emit({ type: "turn_started", author: turn.author, prompt_preview: redact(turn.prompt).slice(0, 200) });
|
|
96
97
|
const heartbeat = setInterval(() => { meta.heartbeat_at = new Date().toISOString(); save(); }, 10_000);
|
|
98
|
+
// rulesync --check before the idle timer so its duration does not count against the worker.
|
|
99
|
+
if (meta.turn === 1 && meta.rules?.rulesync?.status === "pending") {
|
|
100
|
+
const repoRoot = rulesGitRoot(meta.cwd) ?? meta.cwd;
|
|
101
|
+
const rulesyncBin = resolveRulesync(repoRoot);
|
|
102
|
+
if (rulesyncBin) {
|
|
103
|
+
const checked = await rulesyncCheck(rulesyncBin, repoRoot);
|
|
104
|
+
meta.rules.rulesync = {
|
|
105
|
+
...meta.rules.rulesync,
|
|
106
|
+
status: checked.status,
|
|
107
|
+
detail: checked.detail,
|
|
108
|
+
};
|
|
109
|
+
save();
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
meta.rules.rulesync = {
|
|
113
|
+
...meta.rules.rulesync,
|
|
114
|
+
status: "skipped",
|
|
115
|
+
detail: "rulesync not found in node_modules/.bin or PATH",
|
|
116
|
+
};
|
|
117
|
+
save();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
97
120
|
const before = gitSnapshot(meta.cwd);
|
|
98
121
|
let stdout = "";
|
|
99
122
|
let stderr = "";
|
|
@@ -112,7 +135,7 @@ export async function supervise(id) {
|
|
|
112
135
|
try {
|
|
113
136
|
const bin = adapter.resolve(process.env);
|
|
114
137
|
if (!bin)
|
|
115
|
-
throw new Error(`${meta.target}
|
|
138
|
+
throw new Error(`${meta.target} is not on PATH.`);
|
|
116
139
|
spec = adapter.build({
|
|
117
140
|
bin,
|
|
118
141
|
cwd: meta.cwd,
|
|
@@ -123,6 +146,10 @@ export async function supervise(id) {
|
|
|
123
146
|
mode: meta.mode ?? null,
|
|
124
147
|
});
|
|
125
148
|
meta.warning = spec.warning;
|
|
149
|
+
if (meta.mode !== "readonly" && !gitRepoRoot(meta.cwd)) {
|
|
150
|
+
const note = "cwd is not a git repository; changes cannot be tracked or reverted.";
|
|
151
|
+
meta.warning = meta.warning ? `${meta.warning}; ${note}` : note;
|
|
152
|
+
}
|
|
126
153
|
if (spec.modelArg !== undefined)
|
|
127
154
|
meta.model_argv = spec.modelArg;
|
|
128
155
|
else
|
|
@@ -144,7 +171,7 @@ export async function supervise(id) {
|
|
|
144
171
|
output.on("data", (chunk) => {
|
|
145
172
|
lastByteAt = Date.now();
|
|
146
173
|
fs.appendFileSync(file, chunk);
|
|
147
|
-
// shortcut:
|
|
174
|
+
// shortcut: Only the most recent 2MB is kept for interpretation; full output always stays in raw; extend parsing if markers must span windows.
|
|
148
175
|
if (stream === "stdout")
|
|
149
176
|
stdout = (stdout + chunk.toString()).slice(-2_000_000);
|
|
150
177
|
else
|
|
@@ -170,7 +197,7 @@ export async function supervise(id) {
|
|
|
170
197
|
child.once("error", reject);
|
|
171
198
|
child.once("close", resolve);
|
|
172
199
|
});
|
|
173
|
-
child.stdin?.on("error", () => { }); //
|
|
200
|
+
child.stdin?.on("error", () => { }); // the child CLI may exit before reading stdin; close will deliver the final result.
|
|
174
201
|
child.stdin?.end(spec.stdin ?? undefined);
|
|
175
202
|
code = await exited;
|
|
176
203
|
if (spec.lastMessagePath) {
|
|
@@ -179,7 +206,7 @@ export async function supervise(id) {
|
|
|
179
206
|
if (last)
|
|
180
207
|
message = last;
|
|
181
208
|
}
|
|
182
|
-
catch { /*
|
|
209
|
+
catch { /* keep legacy behavior: use stream messages when the CLI wrote no file. */ }
|
|
183
210
|
}
|
|
184
211
|
}
|
|
185
212
|
catch (error) {
|
|
@@ -200,7 +227,7 @@ export async function supervise(id) {
|
|
|
200
227
|
let summary = failureSummary(stdout, stderr);
|
|
201
228
|
if (stopped) {
|
|
202
229
|
final = stopped === "cancelled" ? "cancelled" : "timed_out";
|
|
203
|
-
summary = stopped === "cancelled" ? "
|
|
230
|
+
summary = stopped === "cancelled" ? "Cancelled." : `Timed out: ${stopped}.`;
|
|
204
231
|
}
|
|
205
232
|
else if (code !== 0 && adapter.isAuthFailure(raw)) {
|
|
206
233
|
final = "not_authenticated";
|
|
@@ -225,13 +252,13 @@ export async function supervise(id) {
|
|
|
225
252
|
turn.git_changes = gitChanges(meta.cwd, before, after);
|
|
226
253
|
if (meta.mode === "readonly" && final === "success") {
|
|
227
254
|
if (!meta.readonly_baseline) {
|
|
228
|
-
const note = "readonly:
|
|
255
|
+
const note = "readonly: not a git directory; skipping post-run check";
|
|
229
256
|
meta.warning = meta.warning ? `${meta.warning}; ${note}` : note;
|
|
230
257
|
}
|
|
231
258
|
else if (!after) {
|
|
232
259
|
final = "readonly_violation";
|
|
233
260
|
meta.status = final;
|
|
234
|
-
meta.summary = `readonly_violation: git snapshot
|
|
261
|
+
meta.summary = `readonly_violation: git snapshot failed\n${meta.summary ?? ""}`;
|
|
235
262
|
}
|
|
236
263
|
else {
|
|
237
264
|
const dirty = pathsSinceBaseline(meta.cwd, baselineToSnapshot(meta.readonly_baseline), after);
|
|
@@ -244,13 +271,13 @@ export async function supervise(id) {
|
|
|
244
271
|
}
|
|
245
272
|
if (meta.scope?.length && meta.scope_baseline && final !== "readonly_violation") {
|
|
246
273
|
if (!after) {
|
|
247
|
-
const note = "scope_violation: git snapshot
|
|
274
|
+
const note = "scope_violation: git snapshot failed; cannot verify scope";
|
|
248
275
|
meta.warning = meta.warning ? `${meta.warning}; ${note}` : note;
|
|
249
276
|
meta.scope_violations = ["<snapshot-unavailable>"];
|
|
250
277
|
if ((meta.scope_on_violation ?? "warn") === "fail" && final === "success") {
|
|
251
278
|
final = "scope_violation";
|
|
252
279
|
meta.status = final;
|
|
253
|
-
meta.summary = `scope_violation:
|
|
280
|
+
meta.summary = `scope_violation: see scope_violations\n${meta.summary ?? ""}`;
|
|
254
281
|
}
|
|
255
282
|
}
|
|
256
283
|
else {
|
|
@@ -280,7 +307,7 @@ export async function supervise(id) {
|
|
|
280
307
|
if ((meta.scope_on_violation ?? "warn") === "fail" && final === "success") {
|
|
281
308
|
final = "scope_violation";
|
|
282
309
|
meta.status = final;
|
|
283
|
-
meta.summary = `scope_violation:
|
|
310
|
+
meta.summary = `scope_violation: see scope_violations\n${meta.summary ?? ""}`;
|
|
284
311
|
}
|
|
285
312
|
}
|
|
286
313
|
}
|
package/dist/tail.js
CHANGED
|
@@ -5,15 +5,15 @@ import { taskDir } from "./store.js";
|
|
|
5
5
|
import { status } from "./tasks.js";
|
|
6
6
|
import { StringDecoder } from "node:string_decoder";
|
|
7
7
|
export function formatEvent(event) {
|
|
8
|
-
const prefix = `[${event.ts}]
|
|
8
|
+
const prefix = `[${event.ts}] turn ${event.turn}`;
|
|
9
9
|
switch (event.type) {
|
|
10
10
|
case "turn_started": return `${prefix} ${event.author}: ${event.prompt_preview}`;
|
|
11
11
|
case "message": return `${prefix} ${event.text}`;
|
|
12
|
-
case "tool_call": return `${prefix}
|
|
13
|
-
case "tool_result": return `${prefix} ${event.name} ${event.ok ? "
|
|
14
|
-
case "error": return `${prefix}
|
|
12
|
+
case "tool_call": return `${prefix} call ${event.name}: ${event.detail}`;
|
|
13
|
+
case "tool_result": return `${prefix} ${event.name} ${event.ok ? "ok" : "failed"}: ${event.detail}`;
|
|
14
|
+
case "error": return `${prefix} error: ${event.text}`;
|
|
15
15
|
case "raw": return `${prefix} ${event.stream}: ${event.line}`;
|
|
16
|
-
case "turn_ended": return `${prefix}
|
|
16
|
+
case "turn_ended": return `${prefix} ended: ${event.status} (exit ${event.exit_code})`;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
export async function tail(id, options = {}) {
|
package/dist/tasks.js
CHANGED
|
@@ -9,28 +9,39 @@ import { readMeta, writeMeta, writeTurn, readTurn, appendEvent, taskDir, lockTas
|
|
|
9
9
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
10
10
|
import { redact } from "./redact.js";
|
|
11
11
|
import { isAlive, killGroup } from "./process.js";
|
|
12
|
-
import { gitSnapshot, snapshotToBaseline } from "./git.js";
|
|
12
|
+
import { dirtyPaths, gitRepoRoot, gitSnapshot, snapshotToBaseline } from "./git.js";
|
|
13
13
|
import { normalizeScopeInput, scopePromptPrefix } from "./scope.js";
|
|
14
|
+
import { buildRulesyncReport, discoverRules, hasRulesyncSource, readRulesyncConfig, resolveRulesync, rulesGitRoot, rulesPointer, unseenRules, } from "./rules.js";
|
|
15
|
+
import { addWorktree, assertNotNestedWorktree, batonWorktreeExemptPaths, resolveRepoRoot } from "./worktree.js";
|
|
16
|
+
const DIRTY_PATHS_CAP = 50;
|
|
17
|
+
/** Validate --isolate / --allow-dirty against mode. Throws on invalid combos. */
|
|
18
|
+
export function resolveWriteSafety(mode, isolate, allowDirty) {
|
|
19
|
+
if (mode === "readonly" && (isolate || allowDirty)) {
|
|
20
|
+
throw new Error("--isolate and --allow-dirty are only for --mode write.");
|
|
21
|
+
}
|
|
22
|
+
if (isolate && allowDirty)
|
|
23
|
+
throw new Error("--isolate and --allow-dirty cannot be combined.");
|
|
24
|
+
}
|
|
14
25
|
/** Resolve readonly enforcement; throws unless allowWeak when strength is none. */
|
|
15
26
|
export function resolveReadonlyEnforcement(adapter, mode, allowWeak) {
|
|
16
27
|
if (allowWeak && mode !== "readonly")
|
|
17
|
-
throw new Error("--allow-weak
|
|
28
|
+
throw new Error("--allow-weak is only for --mode readonly.");
|
|
18
29
|
if (!mode || mode === "write")
|
|
19
30
|
return {};
|
|
20
31
|
// Runtime guard — CLI casts via `as TaskMode`.
|
|
21
32
|
if (mode !== "readonly")
|
|
22
|
-
throw new Error("mode
|
|
33
|
+
throw new Error("mode must be readonly or write.");
|
|
23
34
|
const enforcement = adapter.readonlyEnforcement ?? "none";
|
|
24
35
|
if (enforcement === "none" && !allowWeak) {
|
|
25
|
-
throw new Error(`${adapter.id}
|
|
36
|
+
throw new Error(`${adapter.id} cannot enforce read-only; add --allow-weak to allow it.`);
|
|
26
37
|
}
|
|
27
38
|
if (allowWeak && enforcement !== "none")
|
|
28
|
-
throw new Error(`${adapter.id}
|
|
39
|
+
throw new Error(`${adapter.id} has a read-only mapping; --allow-weak is not needed.`);
|
|
29
40
|
return { mode: "readonly", enforcement };
|
|
30
41
|
}
|
|
31
42
|
function positive(value, name) {
|
|
32
43
|
if (!Number.isFinite(value) || value <= 0)
|
|
33
|
-
throw new Error(`${name}
|
|
44
|
+
throw new Error(`${name} must be a positive number.`);
|
|
34
45
|
return value;
|
|
35
46
|
}
|
|
36
47
|
async function launch(id) {
|
|
@@ -45,23 +56,23 @@ async function launch(id) {
|
|
|
45
56
|
}
|
|
46
57
|
child.kill("SIGKILL");
|
|
47
58
|
killGroup(readMeta(id)?.child_pid ?? null);
|
|
48
|
-
throw new Error("supervisor
|
|
59
|
+
throw new Error("supervisor not ready.");
|
|
49
60
|
}
|
|
50
61
|
export async function start(input) {
|
|
51
62
|
if (process.env.BATON_DELEGATION === "1")
|
|
52
|
-
return { status: "failed", summary: "BATON_DELEGATION=1
|
|
63
|
+
return { status: "failed", summary: "BATON_DELEGATION=1; refusing to delegate again." };
|
|
53
64
|
if (!input.prompt?.trim())
|
|
54
|
-
throw new Error("
|
|
65
|
+
throw new Error("prompt is missing.");
|
|
55
66
|
const adapter = getAdapter(input.target);
|
|
56
67
|
if (!adapter)
|
|
57
|
-
throw new Error(
|
|
68
|
+
throw new Error(`Unknown target: ${input.target}`);
|
|
58
69
|
const idle = positive(input.idleTimeout ?? 600, "idle_timeout");
|
|
59
70
|
const max = input.maxDuration == null ? null : positive(input.maxDuration, "max_duration");
|
|
60
71
|
if (input.reasoningEffort && !["low", "medium", "high", "ultra"].includes(input.reasoningEffort))
|
|
61
|
-
throw new Error("
|
|
72
|
+
throw new Error("Unknown effort.");
|
|
62
73
|
const model = input.model?.trim() || undefined;
|
|
63
74
|
if (model && adapter.id !== "cursor")
|
|
64
|
-
throw new Error(`${adapter.id}
|
|
75
|
+
throw new Error(`${adapter.id} does not support --model yet (cursor only).`);
|
|
65
76
|
let model_argv;
|
|
66
77
|
if (adapter.id === "cursor") {
|
|
67
78
|
const resolved = resolveCursorModelArg(model, input.reasoningEffort ?? null);
|
|
@@ -69,20 +80,99 @@ export async function start(input) {
|
|
|
69
80
|
model_argv = resolved;
|
|
70
81
|
}
|
|
71
82
|
const modeFields = resolveReadonlyEnforcement(adapter, input.mode, input.allowWeak);
|
|
83
|
+
resolveWriteSafety(input.mode, input.isolate, input.allowDirty);
|
|
72
84
|
const cwd = path.resolve(input.cwd || process.env.BATON_CWD || process.cwd());
|
|
73
85
|
if (!fs.statSync(cwd).isDirectory())
|
|
74
|
-
throw new Error("cwd
|
|
86
|
+
throw new Error("cwd must be a directory.");
|
|
75
87
|
if (!adapter.resolve(process.env))
|
|
76
|
-
return { status: "not_installed", summary: `${input.target}
|
|
88
|
+
return { status: "not_installed", summary: `${input.target} is not on PATH.` };
|
|
89
|
+
const rulesMode = input.rules ?? "check";
|
|
90
|
+
if (rulesMode !== "check" && rulesMode !== "pointer" && rulesMode !== "off") {
|
|
91
|
+
throw new Error("rules must be check, pointer, or off.");
|
|
92
|
+
}
|
|
77
93
|
const scopeFields = normalizeScopeInput(input.scope, input.scopeOnViolation);
|
|
94
|
+
const writeMode = !modeFields.mode || modeFields.mode === "write";
|
|
95
|
+
// Dirty-tree guard: in-place write only. --isolate skips this.
|
|
96
|
+
if (writeMode && !input.isolate && !input.allowDirty) {
|
|
97
|
+
const dirty = dirtyPaths(cwd);
|
|
98
|
+
if (dirty) {
|
|
99
|
+
const exempt = batonWorktreeExemptPaths(cwd);
|
|
100
|
+
const filtered = dirty.filter((p) => !exempt.has(p));
|
|
101
|
+
if (filtered.length) {
|
|
102
|
+
const truncated = filtered.length > DIRTY_PATHS_CAP;
|
|
103
|
+
return {
|
|
104
|
+
status: "dirty_worktree",
|
|
105
|
+
summary: "Working tree has uncommitted changes; refuse in-place write. Use --isolate (starts from HEAD, ignores uncommitted changes) or --allow-dirty to proceed.",
|
|
106
|
+
dirty_paths: truncated ? filtered.slice(0, DIRTY_PATHS_CAP) : filtered,
|
|
107
|
+
...(truncated ? { dirty_paths_truncated: true } : {}),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const task_id = randomUUID();
|
|
113
|
+
const source_cwd = cwd;
|
|
114
|
+
let taskCwd = cwd;
|
|
115
|
+
let worktree;
|
|
116
|
+
if (input.isolate) {
|
|
117
|
+
if (!gitRepoRoot(cwd))
|
|
118
|
+
throw new Error("--isolate requires a git repository.");
|
|
119
|
+
try {
|
|
120
|
+
assertNotNestedWorktree(cwd);
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
throw new Error(`${error.message} Drop --isolate or run from the main checkout.`);
|
|
124
|
+
}
|
|
125
|
+
const repo = resolveRepoRoot(cwd);
|
|
126
|
+
const short = task_id.replace(/-/g, "").slice(0, 8);
|
|
127
|
+
const name = `task-${short}`;
|
|
128
|
+
const added = addWorktree(cwd, { name, branch: `baton/${name}`, base: "HEAD" });
|
|
129
|
+
const realCwd = fs.realpathSync(cwd);
|
|
130
|
+
const realRepo = fs.realpathSync(repo);
|
|
131
|
+
const rel = path.relative(realRepo, realCwd);
|
|
132
|
+
if (rel.startsWith("..") || path.isAbsolute(rel)) {
|
|
133
|
+
throw new Error("--isolate cwd must be inside the repository.");
|
|
134
|
+
}
|
|
135
|
+
taskCwd = rel ? path.join(added.path, rel) : added.path;
|
|
136
|
+
if (!fs.existsSync(taskCwd) || !fs.statSync(taskCwd).isDirectory()) {
|
|
137
|
+
throw new Error(`--isolate could not map cwd into worktree: ${rel || "."}`);
|
|
138
|
+
}
|
|
139
|
+
worktree = {
|
|
140
|
+
path: added.path,
|
|
141
|
+
branch: added.branch,
|
|
142
|
+
source_cwd,
|
|
143
|
+
gitignore_updated: added.gitignore_updated,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
78
146
|
let scope_baseline;
|
|
79
147
|
let readonly_baseline;
|
|
80
148
|
let prompt = input.prompt;
|
|
81
149
|
const needSnap = Boolean(scopeFields.scope?.length) || modeFields.mode === "readonly";
|
|
82
|
-
const snap = needSnap ? gitSnapshot(
|
|
150
|
+
const snap = needSnap ? gitSnapshot(taskCwd) : null;
|
|
151
|
+
let rules;
|
|
152
|
+
let pointer = "";
|
|
153
|
+
if (rulesMode !== "off") {
|
|
154
|
+
const found = discoverRules(taskCwd);
|
|
155
|
+
const unseen = unseenRules(found, adapter.rules);
|
|
156
|
+
pointer = rulesMode === "pointer" ? rulesPointer(unseen) : "";
|
|
157
|
+
rules = {
|
|
158
|
+
mode: rulesMode,
|
|
159
|
+
found,
|
|
160
|
+
unseen,
|
|
161
|
+
pointer_injected: Boolean(pointer),
|
|
162
|
+
};
|
|
163
|
+
const repoRoot = rulesGitRoot(taskCwd) ?? taskCwd;
|
|
164
|
+
if (hasRulesyncSource(repoRoot)) {
|
|
165
|
+
const bin = resolveRulesync(repoRoot);
|
|
166
|
+
const config = readRulesyncConfig(repoRoot);
|
|
167
|
+
rules.rulesync = buildRulesyncReport(config, adapter.rules, bin);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
// Prefix order: scope, then rules pointer, then the user prompt (spec 3.5).
|
|
171
|
+
if (pointer)
|
|
172
|
+
prompt = `${pointer}${prompt}`;
|
|
83
173
|
if (scopeFields.scope?.length) {
|
|
84
174
|
if (!snap?.head)
|
|
85
|
-
throw new Error("
|
|
175
|
+
throw new Error("With --scope, cwd must be a git repository (HEAD baseline required).");
|
|
86
176
|
scope_baseline = snapshotToBaseline(snap);
|
|
87
177
|
prompt = `${scopePromptPrefix(scopeFields.scope)}${prompt}`;
|
|
88
178
|
}
|
|
@@ -90,7 +180,7 @@ export async function start(input) {
|
|
|
90
180
|
readonly_baseline = snapshotToBaseline(snap);
|
|
91
181
|
const now = new Date().toISOString();
|
|
92
182
|
const meta = {
|
|
93
|
-
task_id
|
|
183
|
+
task_id, target: adapter.id, cwd: taskCwd, created_at: now, updated_at: now,
|
|
94
184
|
status: "running", turn: 1, session_id: null, reasoning_effort: input.reasoningEffort,
|
|
95
185
|
model, model_argv,
|
|
96
186
|
...modeFields,
|
|
@@ -99,6 +189,9 @@ export async function start(input) {
|
|
|
99
189
|
heartbeat_at: null, last_event_at: null,
|
|
100
190
|
...scopeFields,
|
|
101
191
|
scope_baseline,
|
|
192
|
+
rules,
|
|
193
|
+
...(input.allowDirty ? { allow_dirty: true } : {}),
|
|
194
|
+
...(worktree ? { worktree } : {}),
|
|
102
195
|
};
|
|
103
196
|
writeMeta(meta);
|
|
104
197
|
writeTurn(meta.task_id, { turn: 1, author: "host", prompt, files: input.files, status: "running" });
|
|
@@ -109,7 +202,7 @@ async function launchTurn(meta) {
|
|
|
109
202
|
await launch(meta.task_id);
|
|
110
203
|
}
|
|
111
204
|
catch (error) {
|
|
112
|
-
const failed = { ...readMeta(meta.task_id) ?? meta, status: "failed", updated_at: new Date().toISOString(), summary: redact(`supervisor
|
|
205
|
+
const failed = { ...readMeta(meta.task_id) ?? meta, status: "failed", updated_at: new Date().toISOString(), summary: redact(`supervisor failed to start: ${error.message}`) };
|
|
113
206
|
writeTurn(meta.task_id, { ...readTurn(meta.task_id, meta.turn), status: "failed", summary: failed.summary });
|
|
114
207
|
appendEvent(meta.task_id, { ts: failed.updated_at, turn: meta.turn, type: "turn_ended", status: "failed", exit_code: null });
|
|
115
208
|
writeMeta(failed);
|
|
@@ -117,7 +210,7 @@ async function launchTurn(meta) {
|
|
|
117
210
|
}
|
|
118
211
|
return meta;
|
|
119
212
|
}
|
|
120
|
-
//
|
|
213
|
+
// Terminal view includes this turn's git_changes so the host need not read turns/<n>.json.
|
|
121
214
|
function finished(meta) {
|
|
122
215
|
try {
|
|
123
216
|
return { ...meta, git_changes: readTurn(meta.task_id, meta.turn).git_changes };
|
|
@@ -134,7 +227,7 @@ function orphaned(meta) {
|
|
|
134
227
|
export function status(id) {
|
|
135
228
|
let meta = readMeta(id);
|
|
136
229
|
if (!meta)
|
|
137
|
-
throw new Error("
|
|
230
|
+
throw new Error("No such task.");
|
|
138
231
|
if (meta.status !== "running")
|
|
139
232
|
return finished(meta);
|
|
140
233
|
const quiet = Math.max(0, (Date.now() - Date.parse(meta.last_event_at || meta.updated_at)) / 1000);
|
|
@@ -147,7 +240,7 @@ export function status(id) {
|
|
|
147
240
|
meta = readMeta(id);
|
|
148
241
|
if (meta.status !== "running" || !orphaned(meta))
|
|
149
242
|
return meta;
|
|
150
|
-
//
|
|
243
|
+
// The heartbeat may just be a slow write; check the pid again. If the heartbeat is stale and the process is still alive, stop the writer first, then take over.
|
|
151
244
|
if (isAlive(meta.supervisor_pid)) {
|
|
152
245
|
try {
|
|
153
246
|
process.kill(meta.supervisor_pid, "SIGKILL");
|
|
@@ -159,7 +252,7 @@ export function status(id) {
|
|
|
159
252
|
}
|
|
160
253
|
killGroup(meta.child_pid);
|
|
161
254
|
meta.status = "failed";
|
|
162
|
-
meta.summary = "supervisor
|
|
255
|
+
meta.summary = "supervisor exited unexpectedly";
|
|
163
256
|
meta.updated_at = new Date().toISOString();
|
|
164
257
|
try {
|
|
165
258
|
const turn = readTurn(id, meta.turn);
|
|
@@ -199,14 +292,14 @@ export async function cancel(id) {
|
|
|
199
292
|
}
|
|
200
293
|
await sleep(20);
|
|
201
294
|
}
|
|
202
|
-
throw new Error("
|
|
295
|
+
throw new Error("Cancellation not yet confirmed; retry status.");
|
|
203
296
|
}
|
|
204
297
|
export async function wait(ids, seconds = 50) {
|
|
205
298
|
if (!ids.length)
|
|
206
|
-
throw new Error("
|
|
299
|
+
throw new Error("task_ids is missing.");
|
|
207
300
|
ids.forEach(taskDir);
|
|
208
301
|
if (seconds !== Infinity && (!Number.isFinite(seconds) || seconds < 0))
|
|
209
|
-
throw new Error("for
|
|
302
|
+
throw new Error("for must be a non-negative number of seconds.");
|
|
210
303
|
const deadline = seconds === Infinity ? Infinity : Date.now() + seconds * 1000;
|
|
211
304
|
for (;;) {
|
|
212
305
|
const views = ids.map(status);
|
|
@@ -221,13 +314,13 @@ export async function wait(ids, seconds = 50) {
|
|
|
221
314
|
export async function reply(id, prompt, author = "host") {
|
|
222
315
|
taskDir(id);
|
|
223
316
|
if (process.env.BATON_DELEGATION === "1")
|
|
224
|
-
throw new Error("BATON_DELEGATION=1
|
|
317
|
+
throw new Error("BATON_DELEGATION=1; refusing to delegate again.");
|
|
225
318
|
if (!prompt.trim())
|
|
226
|
-
throw new Error("
|
|
319
|
+
throw new Error("prompt is missing.");
|
|
227
320
|
if (author !== "host" && author !== "human")
|
|
228
|
-
throw new Error("as
|
|
321
|
+
throw new Error("as must be host or human.");
|
|
229
322
|
const current = status(id);
|
|
230
|
-
const busy = { task_id: id, status: "busy", summary: "
|
|
323
|
+
const busy = { task_id: id, status: "busy", summary: "This task is running." };
|
|
231
324
|
if (current.status === "running")
|
|
232
325
|
return busy;
|
|
233
326
|
const release = lockTask(id);
|
|
@@ -239,11 +332,11 @@ export async function reply(id, prompt, author = "host") {
|
|
|
239
332
|
if (meta.status === "running")
|
|
240
333
|
return busy;
|
|
241
334
|
if (!["success", "needs_input", "scope_violation", "readonly_violation"].includes(meta.status))
|
|
242
|
-
throw new Error(
|
|
335
|
+
throw new Error(`Cannot continue a task in status ${meta.status}.`);
|
|
243
336
|
if (!meta.session_id)
|
|
244
|
-
throw new Error("
|
|
337
|
+
throw new Error("No session_id; cannot resume the session.");
|
|
245
338
|
if (!getAdapter(meta.target)?.resolve(process.env))
|
|
246
|
-
throw new Error(`${meta.target}
|
|
339
|
+
throw new Error(`${meta.target} is not on PATH.`);
|
|
247
340
|
next = { ...meta, status: "running", turn: meta.turn + 1, updated_at: new Date().toISOString(), supervisor_pid: null, child_pid: null, heartbeat_at: null, last_event_at: null };
|
|
248
341
|
delete next.decision;
|
|
249
342
|
delete next.summary;
|
|
@@ -258,13 +351,32 @@ export async function reply(id, prompt, author = "host") {
|
|
|
258
351
|
return launchTurn(next);
|
|
259
352
|
}
|
|
260
353
|
export function ls(all = false, cwd = process.cwd()) {
|
|
261
|
-
|
|
354
|
+
const here = path.resolve(cwd);
|
|
355
|
+
let hereReal = here;
|
|
356
|
+
try {
|
|
357
|
+
hereReal = fs.realpathSync(here);
|
|
358
|
+
}
|
|
359
|
+
catch { /* keep resolved */ }
|
|
360
|
+
return listTasks().filter((meta) => {
|
|
361
|
+
if (all)
|
|
362
|
+
return true;
|
|
363
|
+
if (meta.cwd === here || meta.worktree?.source_cwd === here)
|
|
364
|
+
return true;
|
|
365
|
+
try {
|
|
366
|
+
if (fs.realpathSync(meta.cwd) === hereReal)
|
|
367
|
+
return true;
|
|
368
|
+
if (meta.worktree?.source_cwd && fs.realpathSync(meta.worktree.source_cwd) === hereReal)
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
catch { /* missing path */ }
|
|
372
|
+
return false;
|
|
373
|
+
}).map((meta) => status(meta.task_id))
|
|
262
374
|
.sort((a, b) => Date.parse(b.updated_at) - Date.parse(a.updated_at));
|
|
263
375
|
}
|
|
264
376
|
export function prune(older) {
|
|
265
377
|
const cutoff = Date.now() - older * 86_400_000;
|
|
266
378
|
if (!Number.isFinite(older) || older < 0 || !Number.isFinite(cutoff))
|
|
267
|
-
throw new Error("older
|
|
379
|
+
throw new Error("older must be a non-negative number of days.");
|
|
268
380
|
const deleted = [];
|
|
269
381
|
for (const candidate of listTasks()) {
|
|
270
382
|
if (candidate.status === "running" || Date.parse(candidate.updated_at) >= cutoff)
|