@mjasnikovs/pi-task 0.18.27 → 0.18.28

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.
@@ -43,16 +43,20 @@ export interface PiTaskConfig {
43
43
  */
44
44
  extensionWhitelist: string[];
45
45
  /**
46
- * Wall-clock ceiling (ms) on a SINGLE tool execution in the MAIN session
47
- * before the command watchdog cancels it and reminds the model to set its
48
- * own timeout. Local models routinely run a command that never returns
46
+ * Wall-clock ceiling (ms) on a SINGLE tool execution before the command
47
+ * watchdog steps in. Local models routinely run a command that never returns
49
48
  * (e.g. `godot --headless` with no timeout, a dev server, a hung test) and
50
49
  * the run wedges until the user manually aborts. pi's bash tool has an
51
50
  * OPTIONAL timeout with NO default (bash.js), so a command the model didn't
52
- * bound runs forever; this is the missing default, enforced from the host
53
- * side via ctx.abort() (which kills the tool's whole process tree) plus an
54
- * auto-reminder turn. Tool-agnostic: it arms on any tool execution, though
55
- * in practice only bash runs long enough to trip it. 0 = off.
51
+ * bound runs forever; this is the missing default.
52
+ *
53
+ * ONE knob, TWO surfaces (shared/command-watchdog.ts): in the MAIN session
54
+ * the overrun call is cancelled via ctx.abort() (kills the tool's whole
55
+ * process tree) plus an auto-reminder turn; in the verify/fix GATE children
56
+ * (gate-deps.ts) the child is killed and re-spawned with a hint, the ceiling
57
+ * halving on repeat hangs. 0 = off — which unguards BOTH surfaces, gates
58
+ * included. Tool-agnostic: it arms on any tool execution, though in practice
59
+ * only bash runs long enough to trip it.
56
60
  * DEFAULT 15 min: long enough for a real build/test suite, short enough that
57
61
  * a true hang doesn't cost half an hour of dead time.
58
62
  */
@@ -102,7 +102,9 @@ const ITEMS = [
102
102
  description: 'Cancel a single command that runs longer than this and remind the model to set '
103
103
  + 'its own timeout. Catches a local model that runs a command which never returns '
104
104
  + '(hung build, dev server, no-timeout check) so the run stops itself instead of '
105
- + 'waiting for a manual abort. off disables it',
105
+ + 'waiting for a manual abort. One knob for both surfaces: the main session AND '
106
+ + 'the verify/fix gate children. off disables it everywhere — gates can then hang '
107
+ + 'unbounded',
106
108
  // Display human labels; the stored config value stays the ms number.
107
109
  values: COMMAND_TIMEOUT_OPTIONS.map(o => o.label)
108
110
  }
@@ -58,6 +58,13 @@ export interface ChildResult {
58
58
  export interface ToolCall {
59
59
  name: string;
60
60
  args: unknown;
61
+ /**
62
+ * pi's id for this tool call, carried on both `tool_execution_start` and
63
+ * `tool_execution_end` so a caller can pair them (the command watchdog arms
64
+ * on start and disarms on the matching end). Optional because the loop
65
+ * detector — the other consumer — keys on name+args and never needs it.
66
+ */
67
+ toolCallId?: string;
61
68
  }
62
69
  export interface LoopHit {
63
70
  call: ToolCall;
@@ -100,6 +107,7 @@ export interface RunChildJsonEventsOptions {
100
107
  name: string;
101
108
  isError: boolean;
102
109
  text: string;
110
+ toolCallId?: string;
103
111
  }) => void;
104
112
  onFirstByte?: () => void;
105
113
  /**
@@ -150,12 +150,13 @@ export class JsonEventSink {
150
150
  }
151
151
  if (t === 'tool_execution_start') {
152
152
  const tn = typeof evt.toolName === 'string' ? evt.toolName : 'tool';
153
+ const id = typeof evt.toolCallId === 'string' ? evt.toolCallId : undefined;
153
154
  if (opts.onLine) {
154
155
  const detail = summarizeToolArgs(tn, evt.args);
155
156
  opts.onLine(detail ? `${tn}: ${detail}` : tn);
156
157
  }
157
158
  if (opts.onToolCall) {
158
- const hit = opts.onToolCall({ name: tn, args: evt.args });
159
+ const hit = opts.onToolCall({ name: tn, args: evt.args, toolCallId: id });
159
160
  if (hit)
160
161
  this.onLoopKill();
161
162
  }
@@ -163,9 +164,10 @@ export class JsonEventSink {
163
164
  }
164
165
  if (t === 'tool_execution_end' && opts.onToolResult) {
165
166
  const tn = typeof evt.toolName === 'string' ? evt.toolName : 'tool';
167
+ const id = typeof evt.toolCallId === 'string' ? evt.toolCallId : undefined;
166
168
  const res = evt.result;
167
169
  const text = toolResultText(res?.content);
168
- opts.onToolResult({ name: tn, isError: evt.isError === true, text });
170
+ opts.onToolResult({ name: tn, isError: evt.isError === true, text, toolCallId: id });
169
171
  }
170
172
  }
171
173
  }
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Command watchdog — the per-tool-call wall-clock machine, shared by both
3
+ * surfaces that can run a command which never returns.
4
+ *
5
+ * WHY THIS LIVES IN shared/: pi's bash tool takes an OPTIONAL `timeout` with NO
6
+ * default (pi-coding-agent core/tools/bash.js), so ANY command the model didn't
7
+ * bound runs forever. That is true in two places, and they are disjoint:
8
+ *
9
+ * MAIN SESSION — the implementation turn, handed off via sendUserMessage
10
+ * (task/orchestrator.ts). Guarded by registerCommandWatchdog
11
+ * in task/command-watchdog.ts.
12
+ * CHILD pi — the verify / lint-fix / recommend / final-fix gate children
13
+ * (task/gate-deps.ts). Children are spawned `--no-extensions`
14
+ * (CHILD_BASE_ARGS), so the host's extension-event watchdog
15
+ * cannot see them at all. Guarded inside runWorker.
16
+ *
17
+ * Neither registration can cover the other's surface, so both exist — but the
18
+ * TIMER STATE MACHINE is identical, and lives here once. What differs is only
19
+ * the `onFire` side effect, which each adapter supplies:
20
+ *
21
+ * main session — ctx.abort() cancels just that tool call and the session
22
+ * survives to receive a follow-up reminder turn.
23
+ * child — there is no per-tool cancellation channel into a child, so
24
+ * the whole child is killed and re-spawned with
25
+ * {@link commandTimeoutHint} prepended. Coarser by necessity:
26
+ * the child's accumulated context is lost.
27
+ */
28
+ /** Opaque timer handle — a real `setTimeout` return in production, anything the
29
+ * test's fake scheduler hands back under test. */
30
+ export type TimerHandle = unknown;
31
+ export interface WatchdogDeps {
32
+ /**
33
+ * The ceiling in ms, read PER command-start. How live that read is depends
34
+ * on the adapter: the main session passes a config read, so a /task-config
35
+ * change takes effect on the next command with no reload; the child adapter
36
+ * passes a constant frozen per attempt (the halved ceiling), so there a
37
+ * config change lands at the next attempt/gate, not the next command.
38
+ * 0 (or any non-positive value) means the watchdog is off and never arms.
39
+ */
40
+ getTimeoutMs: () => number;
41
+ schedule: (fn: () => void, ms: number) => TimerHandle;
42
+ cancel: (handle: TimerHandle) => void;
43
+ /** Invoked when a command overruns: each adapter aborts/kills here. */
44
+ onFire: (toolCallId: string, toolName: string, timeoutMs: number) => void;
45
+ }
46
+ /**
47
+ * MAIN-SESSION reminder, delivered as a follow-up turn after ctx.abort() has
48
+ * cancelled the offending tool call. The session is still alive and remembers
49
+ * the call, so this addresses it in the second person, present tense.
50
+ */
51
+ export declare function reminderMessage(toolName: string, timeoutMs: number): string;
52
+ /**
53
+ * CHILD restart hint, prepended to the prompt of a re-spawned gate child. The
54
+ * killed child is GONE — this one never saw the command — so it is framed as
55
+ * "your previous attempt" and names the command, which the fresh child would
56
+ * otherwise have no way to know it must avoid repeating unbounded.
57
+ *
58
+ * `editsMayPersist` — set for a write-capable child (edit/write tools, or bash,
59
+ * whose commands have side effects). Nothing reverts the working tree between
60
+ * attempts, so telling such a child its previous attempt was "discarded" is
61
+ * false: partial edits and command side effects survive the kill, and a fresh
62
+ * child that believes it starts clean may re-apply them or misread the tree.
63
+ * Only the CONVERSATION is gone; the hint must say so precisely.
64
+ *
65
+ * Replaces the generic worker-timeout hint for this case: that one blames
66
+ * "exploring too long", which is the wrong diagnosis for a hung command and
67
+ * never mentions the timeout parameter.
68
+ */
69
+ export declare function commandTimeoutHint(toolName: string, timeoutMs: number, opts?: {
70
+ commandDetail?: string;
71
+ editsMayPersist?: boolean;
72
+ }): string;
73
+ export declare class CommandWatchdog {
74
+ private readonly deps;
75
+ /** Armed timers, keyed by the tool call they guard. Tool executions are
76
+ * sequential, so this holds at most one entry in normal operation, but the
77
+ * map keeps it correct even if pi ever overlaps two calls. */
78
+ private readonly active;
79
+ constructor(deps: WatchdogDeps);
80
+ /** Arm a timer for a starting tool. No-op when the watchdog is off. */
81
+ onStart(toolCallId: string, toolName: string): void;
82
+ /** Disarm the timer for a finished tool. */
83
+ onEnd(toolCallId: string): void;
84
+ /** Cancel every armed timer — a turn-end / session-shutdown / child-exit
85
+ * safety net so no stray timer can fire into a later, unrelated command. */
86
+ clearAll(): void;
87
+ private disarm;
88
+ private fire;
89
+ }
90
+ /**
91
+ * Real-clock schedule/cancel, unref'd so a pending watchdog timer can never
92
+ * itself keep the process alive on exit. Shared by both adapters; tests
93
+ * substitute a fake scheduler instead.
94
+ */
95
+ export declare const realTimerDeps: Pick<WatchdogDeps, 'schedule' | 'cancel'>;
@@ -0,0 +1,150 @@
1
+ /**
2
+ * Command watchdog — the per-tool-call wall-clock machine, shared by both
3
+ * surfaces that can run a command which never returns.
4
+ *
5
+ * WHY THIS LIVES IN shared/: pi's bash tool takes an OPTIONAL `timeout` with NO
6
+ * default (pi-coding-agent core/tools/bash.js), so ANY command the model didn't
7
+ * bound runs forever. That is true in two places, and they are disjoint:
8
+ *
9
+ * MAIN SESSION — the implementation turn, handed off via sendUserMessage
10
+ * (task/orchestrator.ts). Guarded by registerCommandWatchdog
11
+ * in task/command-watchdog.ts.
12
+ * CHILD pi — the verify / lint-fix / recommend / final-fix gate children
13
+ * (task/gate-deps.ts). Children are spawned `--no-extensions`
14
+ * (CHILD_BASE_ARGS), so the host's extension-event watchdog
15
+ * cannot see them at all. Guarded inside runWorker.
16
+ *
17
+ * Neither registration can cover the other's surface, so both exist — but the
18
+ * TIMER STATE MACHINE is identical, and lives here once. What differs is only
19
+ * the `onFire` side effect, which each adapter supplies:
20
+ *
21
+ * main session — ctx.abort() cancels just that tool call and the session
22
+ * survives to receive a follow-up reminder turn.
23
+ * child — there is no per-tool cancellation channel into a child, so
24
+ * the whole child is killed and re-spawned with
25
+ * {@link commandTimeoutHint} prepended. Coarser by necessity:
26
+ * the child's accumulated context is lost.
27
+ */
28
+ /** Whole minutes, floored at 1, for the human-facing ceiling in both messages. */
29
+ function minutes(timeoutMs) {
30
+ const mins = Math.max(1, Math.round(timeoutMs / 60_000));
31
+ return `${mins} minute${mins === 1 ? '' : 's'}`;
32
+ }
33
+ /**
34
+ * The core correction, shared by both adapters' messages. Two jobs, both
35
+ * learned from live runs: stop the model reporting a killed command as a
36
+ * success, and name the ONE mechanism that prevents a repeat (the bash tool's
37
+ * own `timeout` parameter) rather than leaving "be faster" as the takeaway.
38
+ */
39
+ function correction() {
40
+ return (`The command was killed before it finished and produced NO result, so do not `
41
+ + `report it as completed or successful, and do not claim that anything it would `
42
+ + `have started (a server, build, or process) is now running. `
43
+ + `If it was a genuinely long-running command, you MUST re-run it with an explicit `
44
+ + `timeout — set the bash tool's \`timeout\` parameter (in seconds) so it cannot hang `
45
+ + `again — or break it into smaller steps. Do NOT simply retry the same unbounded command.`);
46
+ }
47
+ /**
48
+ * MAIN-SESSION reminder, delivered as a follow-up turn after ctx.abort() has
49
+ * cancelled the offending tool call. The session is still alive and remembers
50
+ * the call, so this addresses it in the second person, present tense.
51
+ */
52
+ export function reminderMessage(toolName, timeoutMs) {
53
+ return (`[SYSTEM] Your \`${toolName}\` call ran longer than ${minutes(timeoutMs)} `
54
+ + `and was automatically cancelled — it looked stuck. `
55
+ + correction());
56
+ }
57
+ /**
58
+ * CHILD restart hint, prepended to the prompt of a re-spawned gate child. The
59
+ * killed child is GONE — this one never saw the command — so it is framed as
60
+ * "your previous attempt" and names the command, which the fresh child would
61
+ * otherwise have no way to know it must avoid repeating unbounded.
62
+ *
63
+ * `editsMayPersist` — set for a write-capable child (edit/write tools, or bash,
64
+ * whose commands have side effects). Nothing reverts the working tree between
65
+ * attempts, so telling such a child its previous attempt was "discarded" is
66
+ * false: partial edits and command side effects survive the kill, and a fresh
67
+ * child that believes it starts clean may re-apply them or misread the tree.
68
+ * Only the CONVERSATION is gone; the hint must say so precisely.
69
+ *
70
+ * Replaces the generic worker-timeout hint for this case: that one blames
71
+ * "exploring too long", which is the wrong diagnosis for a hung command and
72
+ * never mentions the timeout parameter.
73
+ */
74
+ export function commandTimeoutHint(toolName, timeoutMs, opts) {
75
+ const what = opts?.commandDetail ? ` (${opts.commandDetail})` : '';
76
+ const aftermath = opts?.editsMayPersist ?
77
+ `killed and that attempt's conversation was discarded — you are starting over, `
78
+ + `BUT any file edits or command side effects it made before the kill are still `
79
+ + `in the working tree. Check the current state of files before assuming they `
80
+ + `are untouched or re-applying changes. `
81
+ : `killed and that attempt was discarded — you are seeing this task again from `
82
+ + `the start. `;
83
+ return (`[SYSTEM NOTE: Your previous attempt ran a \`${toolName}\` command${what} that had not `
84
+ + `returned after ${minutes(timeoutMs)}, so it was `
85
+ + aftermath
86
+ + correction()
87
+ + `]`);
88
+ }
89
+ export class CommandWatchdog {
90
+ deps;
91
+ /** Armed timers, keyed by the tool call they guard. Tool executions are
92
+ * sequential, so this holds at most one entry in normal operation, but the
93
+ * map keeps it correct even if pi ever overlaps two calls. */
94
+ active = new Map();
95
+ constructor(deps) {
96
+ this.deps = deps;
97
+ }
98
+ /** Arm a timer for a starting tool. No-op when the watchdog is off. */
99
+ onStart(toolCallId, toolName) {
100
+ const ms = this.deps.getTimeoutMs();
101
+ if (!(ms > 0))
102
+ return;
103
+ // A duplicate start for the same id must not leak the previous timer.
104
+ this.disarm(toolCallId);
105
+ const handle = this.deps.schedule(() => this.fire(toolCallId, toolName, ms), ms);
106
+ this.active.set(toolCallId, handle);
107
+ }
108
+ /** Disarm the timer for a finished tool. */
109
+ onEnd(toolCallId) {
110
+ this.disarm(toolCallId);
111
+ }
112
+ /** Cancel every armed timer — a turn-end / session-shutdown / child-exit
113
+ * safety net so no stray timer can fire into a later, unrelated command. */
114
+ clearAll() {
115
+ for (const handle of this.active.values())
116
+ this.deps.cancel(handle);
117
+ this.active.clear();
118
+ }
119
+ disarm(toolCallId) {
120
+ const handle = this.active.get(toolCallId);
121
+ if (handle !== undefined) {
122
+ this.deps.cancel(handle);
123
+ this.active.delete(toolCallId);
124
+ }
125
+ }
126
+ fire(toolCallId, toolName, ms) {
127
+ // If the tool ended in the same tick the timer fired, its entry is gone
128
+ // already — never abort a command that has just finished cleanly.
129
+ if (!this.active.has(toolCallId))
130
+ return;
131
+ this.active.delete(toolCallId);
132
+ this.deps.onFire(toolCallId, toolName, ms);
133
+ }
134
+ }
135
+ /**
136
+ * Real-clock schedule/cancel, unref'd so a pending watchdog timer can never
137
+ * itself keep the process alive on exit. Shared by both adapters; tests
138
+ * substitute a fake scheduler instead.
139
+ */
140
+ export const realTimerDeps = {
141
+ schedule: (fn, ms) => {
142
+ const handle = setTimeout(fn, ms);
143
+ if (typeof handle.unref === 'function') {
144
+ ;
145
+ handle.unref();
146
+ }
147
+ return handle;
148
+ },
149
+ cancel: handle => clearTimeout(handle)
150
+ };
@@ -1,7 +1,6 @@
1
1
  import type { ExtensionAPI } from '@earendil-works/pi-coding-agent';
2
2
  /**
3
- * Command watchdog — cancels a single tool execution that overruns the
4
- * configured ceiling and reminds the model to bound its own commands.
3
+ * MAIN-SESSION adapter for the command watchdog.
5
4
  *
6
5
  * WHY: a local model in the MAIN session routinely runs a command that never
7
6
  * returns — `godot --headless --check-only` with no timeout, a dev server, a
@@ -17,48 +16,15 @@ import type { ExtensionAPI } from '@earendil-works/pi-coding-agent';
17
16
  * what happened so it retries with a timeout instead of hanging again.
18
17
  *
19
18
  * Tool-agnostic: it arms on ANY tool, honouring "any command can run forever",
20
- * though in practice only bash runs long enough to trip it. The pure timer
21
- * state lives in {@link CommandWatchdog}; all side effects (abort, reminder,
22
- * per-call ctx lookup) live in the registration's `onFire`, so the machine is
23
- * unit-testable without a real pi session.
24
- */
25
- /** Opaque timer handle — a real `setTimeout` return in production, anything the
26
- * test's fake scheduler hands back under test. */
27
- export type TimerHandle = unknown;
28
- export interface WatchdogDeps {
29
- /**
30
- * The ceiling in ms, read PER command-start so a /task-config change takes
31
- * effect on the next command with no reload. 0 (or any non-positive value)
32
- * means the watchdog is off and never arms.
33
- */
34
- getTimeoutMs: () => number;
35
- schedule: (fn: () => void, ms: number) => TimerHandle;
36
- cancel: (handle: TimerHandle) => void;
37
- /** Invoked when a command overruns: the registration aborts + reminds here. */
38
- onFire: (toolCallId: string, toolName: string, timeoutMs: number) => void;
39
- }
40
- /**
41
- * The reminder delivered to the model after its command is cancelled. Kept pure
42
- * and exported so a test can assert its shape without driving the whole session.
19
+ * though in practice only bash runs long enough to trip it.
20
+ *
21
+ * SCOPE — this covers the main session ONLY, which is where the implementation
22
+ * turn runs (orchestrator hands the spec off via sendUserMessage). Gate
23
+ * children are spawned `--no-extensions`, so no host extension exists inside
24
+ * them; their equivalent guard lives in runWorker (workers/pi-worker-core.ts)
25
+ * and shares the same machine from shared/command-watchdog.ts.
43
26
  */
44
- export declare function reminderMessage(toolName: string, timeoutMs: number): string;
45
- export declare class CommandWatchdog {
46
- private readonly deps;
47
- /** Armed timers, keyed by the tool call they guard. Tool executions are
48
- * sequential, so this holds at most one entry in normal operation, but the
49
- * map keeps it correct even if pi ever overlaps two calls. */
50
- private readonly active;
51
- constructor(deps: WatchdogDeps);
52
- /** Arm a timer for a starting tool. No-op when the watchdog is off. */
53
- onStart(toolCallId: string, toolName: string): void;
54
- /** Disarm the timer for a finished tool. */
55
- onEnd(toolCallId: string): void;
56
- /** Cancel every armed timer — a turn-end / session-shutdown safety net so no
57
- * stray timer can fire into a later, unrelated command. */
58
- clearAll(): void;
59
- private disarm;
60
- private fire;
61
- }
27
+ export { CommandWatchdog, commandTimeoutHint, realTimerDeps, reminderMessage, type TimerHandle, type WatchdogDeps } from '../shared/command-watchdog.js';
62
28
  /**
63
29
  * Wire the watchdog into the main session. Only ever active in the host session
64
30
  * (children run `--no-extensions`), which is exactly where the observed hangs
@@ -1,68 +1,33 @@
1
1
  import { getConfig } from '../config/config.js';
2
+ import { CommandWatchdog, realTimerDeps, reminderMessage } from '../shared/command-watchdog.js';
2
3
  /**
3
- * The reminder delivered to the model after its command is cancelled. Kept pure
4
- * and exported so a test can assert its shape without driving the whole session.
4
+ * MAIN-SESSION adapter for the command watchdog.
5
+ *
6
+ * WHY: a local model in the MAIN session routinely runs a command that never
7
+ * returns — `godot --headless --check-only` with no timeout, a dev server, a
8
+ * hung test — and the run wedges until the user manually aborts and tells the
9
+ * model to add a timeout. pi's bash tool takes an OPTIONAL `timeout` with NO
10
+ * default (see pi-coding-agent tools/bash.js), so any command the model didn't
11
+ * bound runs forever. This supplies the missing default from the host side.
12
+ *
13
+ * HOW: arm a wall-clock timer on `tool_execution_start`, disarm it on
14
+ * `tool_execution_end`. If it elapses, `ctx.abort()` cancels the in-flight
15
+ * operation — which fires the tool's AbortSignal, and pi's bash executor kills
16
+ * the whole process tree on abort — then a follow-up user turn tells the model
17
+ * what happened so it retries with a timeout instead of hanging again.
18
+ *
19
+ * Tool-agnostic: it arms on ANY tool, honouring "any command can run forever",
20
+ * though in practice only bash runs long enough to trip it.
21
+ *
22
+ * SCOPE — this covers the main session ONLY, which is where the implementation
23
+ * turn runs (orchestrator hands the spec off via sendUserMessage). Gate
24
+ * children are spawned `--no-extensions`, so no host extension exists inside
25
+ * them; their equivalent guard lives in runWorker (workers/pi-worker-core.ts)
26
+ * and shares the same machine from shared/command-watchdog.ts.
5
27
  */
6
- export function reminderMessage(toolName, timeoutMs) {
7
- const mins = Math.max(1, Math.round(timeoutMs / 60_000));
8
- return (`[SYSTEM] Your \`${toolName}\` call ran longer than ${mins} minute`
9
- + `${mins === 1 ? '' : 's'} and was automatically cancelled — it looked stuck. `
10
- // Anti-fabrication: a live run showed the model react to the cancel by
11
- // reporting the killed command as succeeded ("the server is now running").
12
- // State plainly that it produced nothing so the model can't claim success.
13
- + `The command was killed before it finished and produced NO result, so do not `
14
- + `report it as completed or successful, and do not claim that anything it would `
15
- + `have started (a server, build, or process) is now running. `
16
- + `If it was a genuinely long-running command, you MUST re-run it with an explicit `
17
- + `timeout — set the bash tool's \`timeout\` parameter (in seconds) so it cannot hang `
18
- + `again — or break it into smaller steps. Do NOT simply retry the same unbounded command.`);
19
- }
20
- export class CommandWatchdog {
21
- deps;
22
- /** Armed timers, keyed by the tool call they guard. Tool executions are
23
- * sequential, so this holds at most one entry in normal operation, but the
24
- * map keeps it correct even if pi ever overlaps two calls. */
25
- active = new Map();
26
- constructor(deps) {
27
- this.deps = deps;
28
- }
29
- /** Arm a timer for a starting tool. No-op when the watchdog is off. */
30
- onStart(toolCallId, toolName) {
31
- const ms = this.deps.getTimeoutMs();
32
- if (!(ms > 0))
33
- return;
34
- // A duplicate start for the same id must not leak the previous timer.
35
- this.disarm(toolCallId);
36
- const handle = this.deps.schedule(() => this.fire(toolCallId, toolName, ms), ms);
37
- this.active.set(toolCallId, handle);
38
- }
39
- /** Disarm the timer for a finished tool. */
40
- onEnd(toolCallId) {
41
- this.disarm(toolCallId);
42
- }
43
- /** Cancel every armed timer — a turn-end / session-shutdown safety net so no
44
- * stray timer can fire into a later, unrelated command. */
45
- clearAll() {
46
- for (const handle of this.active.values())
47
- this.deps.cancel(handle);
48
- this.active.clear();
49
- }
50
- disarm(toolCallId) {
51
- const handle = this.active.get(toolCallId);
52
- if (handle !== undefined) {
53
- this.deps.cancel(handle);
54
- this.active.delete(toolCallId);
55
- }
56
- }
57
- fire(toolCallId, toolName, ms) {
58
- // If the tool ended in the same tick the timer fired, its entry is gone
59
- // already — never abort a command that has just finished cleanly.
60
- if (!this.active.has(toolCallId))
61
- return;
62
- this.active.delete(toolCallId);
63
- this.deps.onFire(toolCallId, toolName, ms);
64
- }
65
- }
28
+ // Re-exported so existing importers (and the machine's own tests) keep their
29
+ // entry point while the implementation lives in shared/.
30
+ export { CommandWatchdog, commandTimeoutHint, realTimerDeps, reminderMessage } from '../shared/command-watchdog.js';
66
31
  /**
67
32
  * Wire the watchdog into the main session. Only ever active in the host session
68
33
  * (children run `--no-extensions`), which is exactly where the observed hangs
@@ -75,16 +40,7 @@ export function registerCommandWatchdog(pi) {
75
40
  const ctxByCall = new Map();
76
41
  const watchdog = new CommandWatchdog({
77
42
  getTimeoutMs: () => getConfig().requestTimeoutMs,
78
- schedule: (fn, ms) => {
79
- const handle = setTimeout(fn, ms);
80
- // Don't let a pending watchdog timer keep the process alive on exit.
81
- if (typeof handle.unref === 'function') {
82
- ;
83
- handle.unref();
84
- }
85
- return handle;
86
- },
87
- cancel: handle => clearTimeout(handle),
43
+ ...realTimerDeps,
88
44
  onFire: (toolCallId, toolName, timeoutMs) => {
89
45
  const ctx = ctxByCall.get(toolCallId);
90
46
  ctxByCall.delete(toolCallId);
@@ -90,6 +90,10 @@ export interface EnforceChildResult {
90
90
  loopHit?: unknown;
91
91
  leakedToolCall?: unknown;
92
92
  stalled?: boolean;
93
+ commandTimedOut?: {
94
+ toolName: string;
95
+ timeoutMs: number;
96
+ };
93
97
  }
94
98
  /**
95
99
  * Map the enforcement child's runWorker result to a fatal error message, or null
@@ -224,6 +224,15 @@ export function classifyEnforceChildFailure(r) {
224
224
  if (r.stalled) {
225
225
  return 'model server unreachable — the child produced no output and the model endpoint did not respond';
226
226
  }
227
+ // Same rule, same reason: the command watchdog's kill sets `aborted` too, so
228
+ // a child killed for a command that never returned would otherwise report as
229
+ // a user cancel. Its text is truncated mid-run — the verdict in it is partial
230
+ // and must never be parsed as a real one.
231
+ if (r.commandTimedOut) {
232
+ const mins = Math.max(1, Math.round(r.commandTimedOut.timeoutMs / 60_000));
233
+ return (`child ran a \`${r.commandTimedOut.toolName}\` command that had not returned after `
234
+ + `${mins} minute${mins === 1 ? '' : 's'} and was killed — it never bounded the command`);
235
+ }
227
236
  if (r.timedOut)
228
237
  return 'enforcement child timed out';
229
238
  if (r.loopHit)
@@ -263,6 +263,15 @@ export function buildGateDeps(params) {
263
263
  signal: sig,
264
264
  tools,
265
265
  timeoutMs: 0,
266
+ // The gate child runs to completion (timeoutMs 0), but a
267
+ // single command inside it must still be bounded: pi's bash
268
+ // tool has no default timeout, so a `bun run dev` / hung
269
+ // check the model forgot to bound wedges the gate forever.
270
+ // The stall guard cannot see it — a reachable model endpoint
271
+ // reads as proof of life while the command blocks. Same
272
+ // ceiling the main session uses, so one /task-config knob
273
+ // covers implementation and gates alike.
274
+ commandTimeoutMs: getConfig().requestTimeoutMs,
266
275
  loop: { pathThreshold: Number.POSITIVE_INFINITY },
267
276
  onLine: line => {
268
277
  lastLine = line;
@@ -404,6 +413,11 @@ export function buildGateDeps(params) {
404
413
  signal: sig,
405
414
  tools,
406
415
  timeoutMs: 0, // no wall-clock timeout — run to completion
416
+ // …but still bound any SINGLE command (see makeGateChild).
417
+ // enforce is read,edit today, so nothing here can hang on
418
+ // bash — wired anyway so a future tool grant can't quietly
419
+ // re-open the hole.
420
+ commandTimeoutMs: getConfig().requestTimeoutMs,
407
421
  // Exact-match loop guard only: pathThreshold Infinity
408
422
  // disables the path-revisit heuristic, so revisiting one
409
423
  // file (which IS this pass's job) never trips — only a
@@ -17,6 +17,7 @@ export interface RunWorkerInput {
17
17
  name: string;
18
18
  isError: boolean;
19
19
  text: string;
20
+ toolCallId?: string;
20
21
  }) => void;
21
22
  /**
22
23
  * Called for each context_usage snapshot the child emits (same `--mode json`
@@ -31,6 +32,26 @@ export interface RunWorkerInput {
31
32
  * own) — for a pass that must be allowed to finish however long it takes.
32
33
  */
33
34
  timeoutMs?: number;
35
+ /**
36
+ * PER-TOOL-CALL wall-clock ceiling in ms — the child-side half of the command
37
+ * watchdog (see shared/command-watchdog.ts). Arms on each tool_execution_start
38
+ * and disarms on the matching end; on overrun the child is killed and, within
39
+ * the shared restart budget, re-spawned with commandTimeoutHint.
40
+ *
41
+ * WHY SEPARATE FROM `timeoutMs`: that one bounds the whole worker and is
42
+ * deliberately 0 (unbounded) for gate children, which must run to completion.
43
+ * Neither it nor the stall guard can catch a hung command — the stall guard
44
+ * treats a reachable model endpoint as proof of life, which it is, even while
45
+ * a `bun run dev` the model forgot to bound blocks the child forever.
46
+ *
47
+ * This is the ceiling for the FIRST attempt; each HANG-caused restart halves
48
+ * it (see commandCeilingForAttempt — loop-caused restarts don't count), so a
49
+ * model that ignores the hint cannot spend the full ceiling again on every
50
+ * retry.
51
+ *
52
+ * 0 / omitted = off, so every existing caller is unchanged.
53
+ */
54
+ commandTimeoutMs?: number;
34
55
  /**
35
56
  * Per-worker loop-detector tuning. Defaults to the read-only research/impl
36
57
  * guard (LOOP_WINDOW / LOOP_THRESHOLD, path threshold = exact threshold). An
@@ -96,5 +117,39 @@ export interface RunWorkerResult {
96
117
  * aborted too, and mislabeling this as a user cancel hides a dead backend.
97
118
  */
98
119
  stalled?: boolean;
120
+ /**
121
+ * Set when the command watchdog killed the worker's FINAL attempt: one tool
122
+ * call outran `commandTimeoutMs` (a command the model never bounded). Like
123
+ * loopHit/timedOut the text is partial — treat as a failure. Names the tool
124
+ * so the caller's trail says which call hung rather than just "aborted".
125
+ *
126
+ * Check BEFORE `aborted`, same reasoning as `stalled`: the kill aborts too.
127
+ */
128
+ commandTimedOut?: {
129
+ toolName: string;
130
+ timeoutMs: number;
131
+ };
99
132
  }
133
+ /**
134
+ * The per-command ceiling for attempt N, halving each time a hang recurs.
135
+ *
136
+ * The first attempt gets the full configured ceiling — a genuinely slow build or
137
+ * test suite deserves it. But every hang-caused restart carries
138
+ * commandTimeoutHint, which tells the model in as many words to bound its
139
+ * command; a SECOND hang means it ignored an explicit instruction, and a third
140
+ * means it ignored it twice. Giving a non-complying child the full ceiling again
141
+ * would put the worst case at 3 × 15 min = 45 minutes of dead time, resting
142
+ * entirely on the model obeying prose. Halving bounds it at ~26 min while
143
+ * costing a complying child nothing.
144
+ *
145
+ * `priorHangs` counts watchdog kills specifically, NOT total restarts — the
146
+ * restart budget is shared with loop kills, and a child restarted for LOOPING
147
+ * never received the bound-your-command hint, so its first hang still deserves
148
+ * the full ceiling. Only a hang after a hang is defiance.
149
+ *
150
+ * Floored at 30s so repeated halving cannot shrink the ceiling to something no
151
+ * real command could finish inside — but never ABOVE the configured ceiling
152
+ * itself, or a caller asking for 10s would silently get 30.
153
+ */
154
+ export declare function commandCeilingForAttempt(baseMs: number, priorHangs: number): number;
100
155
  export declare function runWorker(input: RunWorkerInput): Promise<RunWorkerResult>;
@@ -1,5 +1,6 @@
1
1
  import { getPiInvocation } from '../shared/pi-invocation.js';
2
2
  import { runChildDefault } from '../shared/child-process.js';
3
+ import { CommandWatchdog, commandTimeoutHint, realTimerDeps } from '../shared/command-watchdog.js';
3
4
  import { childBaseArgs } from '../shared/child-extensions.js';
4
5
  import { LoopDetector } from '../task/loop-detector.js';
5
6
  import { LOOP_WINDOW, LOOP_THRESHOLD, MAX_LOOP_RESTARTS, formatLoopHint } from '../task/child-runner.js';
@@ -32,7 +33,12 @@ const RESEARCH_WORKER_TIMEOUT_MS = 240_000;
32
33
  * alive) just gets probed and waits on.
33
34
  */
34
35
  const STALL_AFTER_MS = 180_000;
35
- /** Restart hint after a wall-clock timeout — distinct from the loop hint. */
36
+ /**
37
+ * Restart hint after a WHOLE-WORKER wall-clock timeout — distinct from both the
38
+ * loop hint and the per-command hint. This one diagnoses over-exploration, which
39
+ * is what the whole-worker cap actually catches. A single hung COMMAND is a
40
+ * different fault with a different fix, and gets commandTimeoutHint instead.
41
+ */
36
42
  const WORKER_TIMEOUT_HINT = '[SYSTEM NOTE: Your previous attempt ran out of time before answering — you '
37
43
  + 'were exploring too long. Be decisive: do the minimum reads/greps needed, '
38
44
  + 'then write your answer now. Do not re-explore ground you have already covered.]';
@@ -69,6 +75,85 @@ function workerTimeout(external, ms) {
69
75
  }
70
76
  };
71
77
  }
78
+ /**
79
+ * The per-command ceiling for attempt N, halving each time a hang recurs.
80
+ *
81
+ * The first attempt gets the full configured ceiling — a genuinely slow build or
82
+ * test suite deserves it. But every hang-caused restart carries
83
+ * commandTimeoutHint, which tells the model in as many words to bound its
84
+ * command; a SECOND hang means it ignored an explicit instruction, and a third
85
+ * means it ignored it twice. Giving a non-complying child the full ceiling again
86
+ * would put the worst case at 3 × 15 min = 45 minutes of dead time, resting
87
+ * entirely on the model obeying prose. Halving bounds it at ~26 min while
88
+ * costing a complying child nothing.
89
+ *
90
+ * `priorHangs` counts watchdog kills specifically, NOT total restarts — the
91
+ * restart budget is shared with loop kills, and a child restarted for LOOPING
92
+ * never received the bound-your-command hint, so its first hang still deserves
93
+ * the full ceiling. Only a hang after a hang is defiance.
94
+ *
95
+ * Floored at 30s so repeated halving cannot shrink the ceiling to something no
96
+ * real command could finish inside — but never ABOVE the configured ceiling
97
+ * itself, or a caller asking for 10s would silently get 30.
98
+ */
99
+ export function commandCeilingForAttempt(baseMs, priorHangs) {
100
+ if (!(baseMs > 0))
101
+ return 0;
102
+ const floor = Math.min(baseMs, 30_000);
103
+ return Math.max(floor, Math.round(baseMs / 2 ** priorHangs));
104
+ }
105
+ /**
106
+ * Build the child-side command watchdog for ONE attempt: a per-tool-call timer
107
+ * machine (shared with the main session) whose `onFire` aborts `signal`, which
108
+ * runChild turns into a process-GROUP kill — reaping the hung command itself,
109
+ * not just the pi child holding it.
110
+ *
111
+ * LIMIT: the group kill only reaches processes still IN the group. A hung
112
+ * command that detached a daemon (setsid/nohup dev server) leaves it running —
113
+ * the fresh attempt can then hit a port the dead attempt's escapee still holds
114
+ * (the run-9 orphan-dev-server → false-EADDRINUSE shape). No cheap fix from
115
+ * here; the restart hint's "check current state" line is the mitigation.
116
+ *
117
+ * Returns null when the watchdog is off, so the caller keeps the plain timeout
118
+ * signal and no per-call bookkeeping happens at all.
119
+ */
120
+ function commandWatch(timeoutMs) {
121
+ if (!(timeoutMs > 0))
122
+ return null;
123
+ const ctrl = new AbortController();
124
+ // pi's toolCallId pairs start↔end. When it is absent (a fake stream in a
125
+ // test, an older pi), fall back to one shared slot: tool executions in a
126
+ // child are sequential, so a single slot is still correctly paired.
127
+ const key = (id) => id ?? 'anon';
128
+ const details = new Map();
129
+ let killed;
130
+ const watchdog = new CommandWatchdog({
131
+ getTimeoutMs: () => timeoutMs,
132
+ ...realTimerDeps,
133
+ onFire: (toolCallId, toolName, ms) => {
134
+ killed = {
135
+ toolName,
136
+ timeoutMs: ms,
137
+ ...(details.has(toolCallId) ? { detail: details.get(toolCallId) } : {})
138
+ };
139
+ ctrl.abort();
140
+ }
141
+ });
142
+ return {
143
+ onStart: call => {
144
+ const id = key(call.toolCallId);
145
+ const args = call.args;
146
+ if (typeof args?.command === 'string') {
147
+ details.set(id, args.command.slice(0, 120));
148
+ }
149
+ watchdog.onStart(id, call.name);
150
+ },
151
+ onEnd: id => watchdog.onEnd(key(id)),
152
+ killed: () => killed,
153
+ signal: ctrl.signal,
154
+ clear: () => watchdog.clearAll()
155
+ };
156
+ }
72
157
  export async function runWorker(input) {
73
158
  const tools = input.tools ?? DEFAULT_TOOLS;
74
159
  const baseArgs = [...childBaseArgs(input.extensions ?? []), '--mode', 'json', '--tools', tools];
@@ -79,6 +164,10 @@ export async function runWorker(input) {
79
164
  // hint up to MAX_LOOP_RESTARTS times before we give up. Leaked tool calls
80
165
  // keep their own MAX_LEAK_RETRIES budget below — a different failure mode.
81
166
  let restarts = 0;
167
+ // Watchdog kills specifically — drives the ceiling halving. Kept apart from
168
+ // `restarts` (the shared budget) so a loop-caused restart doesn't shorten
169
+ // the rope of a child that has never hung (see commandCeilingForAttempt).
170
+ let hangKills = 0;
82
171
  let leakRetries = 0;
83
172
  for (;;) {
84
173
  const prompt = hint === null ? input.prompt : `${hint}\n\n${input.prompt}`;
@@ -101,9 +190,13 @@ export async function runWorker(input) {
101
190
  // caller couldn't distinguish from a crash.
102
191
  let loopHit;
103
192
  const timeout = workerTimeout(input.signal, timeoutMs);
193
+ // Per-tool-call watchdog for this attempt (null when off). Its abort is
194
+ // OR'd with the worker timeout / external cancel into the child's signal.
195
+ const cmdWatch = commandWatch(commandCeilingForAttempt(input.commandTimeoutMs ?? 0, hangKills));
196
+ const childSignal = cmdWatch ? AbortSignal.any([timeout.signal, cmdWatch.signal]) : timeout.signal;
104
197
  let result;
105
198
  try {
106
- result = await runChildDefault(invocation, input.cwd, timeout.signal, {
199
+ result = await runChildDefault(invocation, input.cwd, childSignal, {
107
200
  mode: 'json-events',
108
201
  ...(input.stall === false ?
109
202
  {}
@@ -116,6 +209,7 @@ export async function runWorker(input) {
116
209
  }),
117
210
  onFirstByte: () => (tFirstByte = Date.now()),
118
211
  onToolCall: call => {
212
+ cmdWatch?.onStart(call);
119
213
  if (!loopDetector)
120
214
  return null;
121
215
  const hit = loopDetector.record(call);
@@ -124,18 +218,28 @@ export async function runWorker(input) {
124
218
  return hit;
125
219
  },
126
220
  onLine: input.onLine,
127
- onToolResult: input.onToolResult,
221
+ // Always wired when the watchdog is on — the sink only emits
222
+ // tool_execution_end if a handler exists, and without it every
223
+ // timer would stay armed and fire on a finished command.
224
+ onToolResult: cmdWatch ?
225
+ r => {
226
+ cmdWatch.onEnd(r.toolCallId);
227
+ input.onToolResult?.(r);
228
+ }
229
+ : input.onToolResult,
128
230
  onContextUsage: input.onContextUsage
129
231
  }, input.spawn);
130
232
  }
131
233
  finally {
132
234
  timeout.cleanup();
235
+ cmdWatch?.clear();
133
236
  }
134
237
  const tEnd = Date.now();
135
238
  const waitMs = tFirstByte === null ? tEnd - tStart : tFirstByte - tStart;
136
239
  const workMs = tFirstByte === null ? 0 : tEnd - tFirstByte;
137
240
  const text = result.text ?? '';
138
241
  const timedOut = timeout.timedOut();
242
+ const commandKill = cmdWatch?.killed();
139
243
  // A loop-kill gets the same restart-with-hint treatment every other phase
140
244
  // already gets (runPhaseWithLoopGuard) — name the offending call so the
141
245
  // re-spawn avoids it. Bounded by the shared restart budget.
@@ -144,6 +248,23 @@ export async function runWorker(input) {
144
248
  restarts++;
145
249
  continue;
146
250
  }
251
+ // A hung COMMAND is restartable too, on the same budget, but checked
252
+ // before the whole-worker timeout because its hint is the specific one:
253
+ // bound the command. (The two can't be confused — a watchdog kill leaves
254
+ // timeout.timedOut() false, since that flag tracks only its own timer.)
255
+ if (commandKill && !loopHit && restarts < MAX_LOOP_RESTARTS) {
256
+ hint = commandTimeoutHint(commandKill.toolName, commandKill.timeoutMs, {
257
+ commandDetail: commandKill.detail,
258
+ // Nothing reverts the tree between attempts, so a child that can
259
+ // mutate it (edit/write, or bash side effects) must not be told
260
+ // its previous attempt left no trace. Same capability test the
261
+ // gate logger uses — decided by tools, not by phase.
262
+ editsMayPersist: /\b(?:edit|bash|write)\b/.test(tools)
263
+ });
264
+ restarts++;
265
+ hangKills++;
266
+ continue;
267
+ }
147
268
  // A wall-clock timeout (the backstop for varied thrash the exact-match
148
269
  // detector misses) is also restartable, sharing the same budget. Skip when
149
270
  // a loop also tripped — the loop hint above is more specific.
@@ -171,7 +292,15 @@ export async function runWorker(input) {
171
292
  ...(leaked ? { leakedToolCall: leaked } : {}),
172
293
  ...(loopHit ? { loopHit } : {}),
173
294
  ...(timedOut ? { timedOut: true } : {}),
174
- ...(result.stalled ? { stalled: true } : {})
295
+ ...(result.stalled ? { stalled: true } : {}),
296
+ ...(commandKill ?
297
+ {
298
+ commandTimedOut: {
299
+ toolName: commandKill.toolName,
300
+ timeoutMs: commandKill.timeoutMs
301
+ }
302
+ }
303
+ : {})
175
304
  };
176
305
  }
177
306
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mjasnikovs/pi-task",
3
- "version": "0.18.27",
3
+ "version": "0.18.28",
4
4
  "description": "Deterministic task planning and spec-orchestration for local models — crash-safe /task pipelines with verify/enforce gates, a real-time remote web view, and web/docs/fetch/worker subagent tools.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",