@speculoai/baton 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/store.js CHANGED
@@ -42,6 +42,25 @@ export function readMeta(id) {
42
42
  return null;
43
43
  }
44
44
  }
45
+ export function hasTurnEnded(id, turn) {
46
+ let text;
47
+ try {
48
+ text = fs.readFileSync(path.join(taskDir(id), "events.jsonl"), "utf8");
49
+ }
50
+ catch (error) {
51
+ if (error.code === "ENOENT")
52
+ return false;
53
+ throw error;
54
+ }
55
+ for (const line of text.split("\n")) {
56
+ if (!line)
57
+ continue;
58
+ const event = JSON.parse(line);
59
+ if (event.type === "turn_ended" && event.turn === turn)
60
+ return true;
61
+ }
62
+ return false;
63
+ }
45
64
  export function appendEvent(id, event) {
46
65
  const safe = Object.fromEntries(Object.entries(event).map(([key, value]) => [key, typeof value === "string" ? redact(value) : value]));
47
66
  fs.appendFileSync(path.join(ensureTask(id), "events.jsonl"), `${JSON.stringify(safe)}\n`, { mode: 0o600 });
@@ -7,7 +7,7 @@ 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
9
  import { resolveRulesync, rulesGitRoot, rulesyncCheck } from "./rules.js";
10
- import { appendEvent, readMeta, writeMeta, readTurn, writeTurn, taskDir, lockTask } from "./store.js";
10
+ import { appendEvent, hasTurnEnded, readMeta, writeMeta, readTurn, writeTurn, taskDir, lockTask } from "./store.js";
11
11
  import { redact } from "./redact.js";
12
12
  import { killGroup } from "./process.js";
13
13
  const QUOTA_PHRASES = ["usage limit", "quota exceeded", "rate limit"];
@@ -69,9 +69,18 @@ export async function supervise(id) {
69
69
  let childPid = null;
70
70
  let stopped = null;
71
71
  const stop = (reason) => {
72
- if (!stopped)
72
+ if (!stopped) {
73
73
  stopped = reason;
74
- killGroup(childPid);
74
+ meta.stop_reason = reason;
75
+ meta.updated_at = new Date().toISOString();
76
+ writeMeta(meta);
77
+ }
78
+ try {
79
+ killGroup(childPid);
80
+ }
81
+ catch { /* 决定已落盘;杀进程失败留待孤儿收尾 */ }
82
+ if (process.env.BATON_CRASH_AFTER_STOP_PERSIST === "1")
83
+ process.exit(1);
75
84
  };
76
85
  const cancel = () => stop("cancelled");
77
86
  process.on("SIGTERM", cancel);
@@ -312,9 +321,13 @@ export async function supervise(id) {
312
321
  }
313
322
  }
314
323
  }
324
+ delete meta.stop_reason;
315
325
  turn.status = final;
316
326
  turn.summary = meta.summary;
317
327
  writeTurn(id, turn);
318
- emit({ type: "turn_ended", status: final, exit_code: code });
328
+ if (hasTurnEnded(id, meta.turn))
329
+ save();
330
+ else
331
+ emit({ type: "turn_ended", status: final, exit_code: code });
319
332
  process.off("SIGTERM", cancel);
320
333
  }
package/dist/tasks.js CHANGED
@@ -5,7 +5,7 @@ import { spawn, spawnSync } from "node:child_process";
5
5
  import { once } from "node:events";
6
6
  import { adapters, getAdapter } from "./adapters/index.js";
7
7
  import { resolveCursorModelArg } from "./adapters/cursor.js";
8
- import { readMeta, writeMeta, writeTurn, readTurn, appendEvent, taskDir, lockTask, listTasks } from "./store.js";
8
+ import { readMeta, writeMeta, writeTurn, readTurn, appendEvent, hasTurnEnded, taskDir, lockTask, listTasks } from "./store.js";
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";
@@ -251,19 +251,25 @@ export function status(id) {
251
251
  }
252
252
  }
253
253
  killGroup(meta.child_pid);
254
- meta.status = "failed";
255
- meta.summary = "supervisor exited unexpectedly";
254
+ const reason = meta.stop_reason;
255
+ const decided = reason === "idle" || reason === "max_duration" || reason === "cancelled";
256
+ meta.status = decided ? (reason === "cancelled" ? "cancelled" : "timed_out") : "failed";
257
+ meta.summary = decided ? (reason === "cancelled" ? "Cancelled." : `Timed out: ${reason}.`) : "supervisor exited unexpectedly";
258
+ delete meta.stop_reason;
256
259
  meta.updated_at = new Date().toISOString();
257
260
  try {
258
261
  const turn = readTurn(id, meta.turn);
259
- writeTurn(id, { ...turn, status: "failed", summary: meta.summary });
262
+ writeTurn(id, { ...turn, status: meta.status, summary: meta.summary });
260
263
  }
261
264
  catch (error) {
262
265
  if (error.code !== "ENOENT")
263
266
  throw error;
264
267
  }
265
- appendEvent(id, { ts: meta.updated_at, turn: meta.turn, type: "turn_ended", status: "failed", exit_code: null });
268
+ if (!hasTurnEnded(id, meta.turn))
269
+ appendEvent(id, { ts: meta.updated_at, turn: meta.turn, type: "turn_ended", status: meta.status, exit_code: null });
266
270
  writeMeta(meta);
271
+ if (decided)
272
+ return finished(meta);
267
273
  return { ...meta, liveness: "orphaned", quiet_for_s: quiet };
268
274
  }
269
275
  finally {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@speculoai/baton",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Hand a bounded coding task to another local agent CLI, then continue the same session when it needs a decision.",
5
5
  "license": "MIT",
6
6
  "type": "module",