brl-subagent 2.6.0 → 2.8.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/AGENT.md +3 -2
- package/README.md +31 -10
- package/package.json +8 -5
- package/src/concurrency.ts +25 -2
- package/src/dispatch-line.ts +144 -0
- package/src/engineering-bar.ts +69 -0
- package/src/event-bus.ts +10 -0
- package/src/git.ts +58 -1
- package/src/headstate.ts +471 -0
- package/src/history.ts +13 -1
- package/src/index.ts +1328 -947
- package/src/kill-escalation.ts +87 -7
- package/src/loader.ts +224 -0
- package/src/mode-prologue.ts +169 -0
- package/src/model-availability.ts +13 -1
- package/src/notify-completion.ts +146 -5
- package/src/params.ts +356 -66
- package/src/prelude.ts +356 -4
- package/src/presets.ts +127 -150
- package/src/project-flags.ts +58 -0
- package/src/recovery-engine.ts +10 -0
- package/src/recovery.ts +155 -19
- package/src/run-registry.ts +24 -1
- package/src/runner.ts +34 -4
- package/src/schema.ts +101 -55
- package/src/session-manager.ts +1549 -264
- package/src/state.ts +118 -16
- package/src/store-permissions.ts +98 -0
- package/src/templates.ts +92 -121
- package/src/transcript.ts +5 -2
- package/src/transport-retry.ts +90 -0
- package/src/tui-format.ts +174 -6
- package/src/tui.ts +284 -377
- package/src/types.ts +219 -25
- package/src/unit-run.ts +10 -1
- package/src/validate.ts +120 -0
package/src/notify-completion.ts
CHANGED
|
@@ -17,15 +17,17 @@
|
|
|
17
17
|
* controls the WAKE (D2). D1: failed/stopped → "steer", completed → "followUp".
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
20
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
21
21
|
import type {
|
|
22
22
|
BackgroundAgent,
|
|
23
|
+
BackgroundAgentIdentity,
|
|
23
24
|
SubagentRun,
|
|
24
25
|
CompletionNotifyMode,
|
|
25
26
|
AgentStatus,
|
|
26
27
|
} from "./types";
|
|
27
28
|
import { truncateTail } from "./transcript-tail";
|
|
28
29
|
import { formatRunDuration } from "./history";
|
|
30
|
+
import { formatBudgetDuration } from "./tui-format";
|
|
29
31
|
import { classifyTerminalOutcome } from "./types";
|
|
30
32
|
import { resolveTerminalRunEntry } from "./state";
|
|
31
33
|
|
|
@@ -59,7 +61,11 @@ export interface DeliveryResolution {
|
|
|
59
61
|
triggerTurn: boolean;
|
|
60
62
|
}
|
|
61
63
|
|
|
62
|
-
/**
|
|
64
|
+
/**
|
|
65
|
+
* Default dedupe cap. The set stays bounded by evicting its oldest entry when it
|
|
66
|
+
* exceeds this — never the whole set (issue #440: a clear-all forgot the id it had
|
|
67
|
+
* just claimed, so that id could be delivered twice).
|
|
68
|
+
*/
|
|
63
69
|
const DEDUPE_CAP = 200;
|
|
64
70
|
|
|
65
71
|
/**
|
|
@@ -114,6 +120,49 @@ function buildSummaryLine(
|
|
|
114
120
|
return line;
|
|
115
121
|
}
|
|
116
122
|
|
|
123
|
+
/**
|
|
124
|
+
* The ONE timeout predicate (issue #423): a run hit its deadline when the
|
|
125
|
+
* classified category says so or the agent's error is the deadline stamp. The
|
|
126
|
+
* wake, the result renderer and the budget line all read it, so the two
|
|
127
|
+
* exhaustion surfaces cannot disagree.
|
|
128
|
+
*/
|
|
129
|
+
export function isTimeoutEvidence(agent: BackgroundAgent, run?: SubagentRun): boolean {
|
|
130
|
+
const category = run?.errorCategory ?? run?.originalParams?.errorCategory;
|
|
131
|
+
return category === "timeout" || /^Timed out/.test(agent.error ?? "");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* One line stating a run's budget (issue #423). PURE. Returns undefined when the
|
|
136
|
+
* record carries no budget — an old record written before `timeoutMs` existed
|
|
137
|
+
* has nothing to state, so no line is better than an invented 30 m.
|
|
138
|
+
*
|
|
139
|
+
* Shapes:
|
|
140
|
+
* - timeout: `Budget: 30m (exhausted)`
|
|
141
|
+
* - otherwise `Budget: 30m — 4m 12s used, 25m 48s left`
|
|
142
|
+
*
|
|
143
|
+
* `status` guards the exhausted form: a run classified `completed` never claims
|
|
144
|
+
* to have exhausted its budget (the evidence predicate alone would otherwise be
|
|
145
|
+
* enough to downgrade a late-but-successful settle). `run` is optional because
|
|
146
|
+
* the result path (`get_subagent_result`) has only the agent record; both read
|
|
147
|
+
* the SAME predicate.
|
|
148
|
+
*/
|
|
149
|
+
export function formatBudgetLine(
|
|
150
|
+
agent: BackgroundAgent,
|
|
151
|
+
status: CompletionStatus | undefined,
|
|
152
|
+
run: SubagentRun | undefined,
|
|
153
|
+
now: number = Date.now(),
|
|
154
|
+
): string | undefined {
|
|
155
|
+
if (agent.timeoutMs === undefined) return undefined;
|
|
156
|
+
const budget = formatBudgetDuration(agent.timeoutMs);
|
|
157
|
+
if (status !== "completed" && isTimeoutEvidence(agent, run)) {
|
|
158
|
+
return `Budget: ${budget} (exhausted)`;
|
|
159
|
+
}
|
|
160
|
+
const endMs = agent.completedAt ?? now;
|
|
161
|
+
const usedMs = Math.max(0, endMs - agent.startedAt);
|
|
162
|
+
const remainingMs = Math.max(0, agent.startedAt + agent.timeoutMs - endMs);
|
|
163
|
+
return `Budget: ${budget} — ${formatBudgetDuration(usedMs)} used, ${formatBudgetDuration(remainingMs)} left`;
|
|
164
|
+
}
|
|
165
|
+
|
|
117
166
|
/**
|
|
118
167
|
* PURE builder — no pi SDK, no side effects, never throws. Returns the
|
|
119
168
|
* sendMessage payload shape per D3.
|
|
@@ -169,6 +218,13 @@ export function buildCompletionMessage(
|
|
|
169
218
|
: stored;
|
|
170
219
|
const tail = truncateTail(body, 15);
|
|
171
220
|
const parts = [summary];
|
|
221
|
+
// Issue #423: the budget rides its own part, and ONLY on a failed/stopped
|
|
222
|
+
// wake — a completed run's message is unchanged (the human reads the budget
|
|
223
|
+
// when something went wrong, not on every success).
|
|
224
|
+
if (status !== "completed") {
|
|
225
|
+
const budgetLine = formatBudgetLine(agent, status, run);
|
|
226
|
+
if (budgetLine) parts.push(budgetLine);
|
|
227
|
+
}
|
|
172
228
|
if (headline) parts.push(headline);
|
|
173
229
|
if (tail) parts.push(tail);
|
|
174
230
|
parts.push(COMPLETION_DIRECTIVE);
|
|
@@ -209,13 +265,20 @@ export function resolveDelivery(
|
|
|
209
265
|
|
|
210
266
|
/**
|
|
211
267
|
* Dedupe guard — first terminal event per id wins. Returns true when the event
|
|
212
|
-
* should be delivered, false when it is a duplicate.
|
|
213
|
-
* set is
|
|
268
|
+
* should be delivered, false when it is a duplicate. Retention is bounded (W1):
|
|
269
|
+
* once the set exceeds the cap its OLDEST entry is evicted (a `Set` iterates in
|
|
270
|
+
* insertion order, so that is exact). The id just claimed is the newest entry, so
|
|
271
|
+
* its own call evicts an EARLIER id — not the set, and not that claim. A clear-all
|
|
272
|
+
* forgot the claim entirely, so the very next duplicate was delivered twice (#440).
|
|
214
273
|
*/
|
|
215
274
|
export function markTerminalSeen(seen: Set<string>, id: string, cap = DEDUPE_CAP): boolean {
|
|
275
|
+
cap = Math.max(cap, 1); // a cap below 1 evicts the claim just made — retention must keep it
|
|
216
276
|
if (seen.has(id)) return false;
|
|
217
277
|
seen.add(id);
|
|
218
|
-
if (seen.size > cap)
|
|
278
|
+
if (seen.size > cap) {
|
|
279
|
+
const oldest = seen.values().next().value;
|
|
280
|
+
if (oldest !== undefined) seen.delete(oldest);
|
|
281
|
+
}
|
|
219
282
|
return true;
|
|
220
283
|
}
|
|
221
284
|
|
|
@@ -254,3 +317,81 @@ export function sendCompletionNotification(
|
|
|
254
317
|
): void {
|
|
255
318
|
pi.sendMessage(message, { deliverAs: delivery.deliverAs, triggerTurn: delivery.triggerTurn });
|
|
256
319
|
}
|
|
320
|
+
|
|
321
|
+
// ---------------------------------------------------------------------------
|
|
322
|
+
// Started-but-degraded monitoring advisory (issue #439)
|
|
323
|
+
//
|
|
324
|
+
// A background spawn whose tail faulted is a STARTED run whose monitoring is
|
|
325
|
+
// degraded — never a failed spawn. The conductor learns that from the spawn
|
|
326
|
+
// result's paragraph; the user learns it from this advisory. The advisory is
|
|
327
|
+
// deliberately NOT a terminal notice and never takes the terminal claim: the
|
|
328
|
+
// run is live, and its genuine completion notice must still be delivered later
|
|
329
|
+
// (the resolved contract on the issue). A run that emits the advisory and then
|
|
330
|
+
// never delivers a terminal notice (no listener at settle time) degrades to
|
|
331
|
+
// "advisory only" — a stated limitation, not a silent one.
|
|
332
|
+
// ---------------------------------------------------------------------------
|
|
333
|
+
|
|
334
|
+
/** The advisory's custom message type. NOT a terminal notice type. */
|
|
335
|
+
export const MONITORING_DEGRADED_CUSTOM_TYPE = "subagent-monitoring-degraded";
|
|
336
|
+
|
|
337
|
+
/** The user-facing half of the advisory (the footer/toast line). */
|
|
338
|
+
export const MONITORING_DEGRADED_TOAST = "Background agent started — monitoring degraded";
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* The conductor-facing paragraph a STARTED-but-degraded spawn result carries.
|
|
342
|
+
* Built from the identity record only — never from a property read on the live
|
|
343
|
+
* agent (see the boundary statement in `startBackgroundAgent`). EMPTY when the
|
|
344
|
+
* tail attached fully: callers append it unconditionally.
|
|
345
|
+
*/
|
|
346
|
+
export function buildMonitoringDegradedParagraph(
|
|
347
|
+
unit: Pick<BackgroundAgentIdentity, "id" | "label">,
|
|
348
|
+
tailFailures: string[],
|
|
349
|
+
): string {
|
|
350
|
+
if (tailFailures.length === 0) return "";
|
|
351
|
+
const name = unit.label ? `"${unit.label}" ` : "";
|
|
352
|
+
return (
|
|
353
|
+
`\n\nMonitoring degraded: ${name}started, but its live monitoring could not be attached (${tailFailures.join("; ")}). ` +
|
|
354
|
+
`Do not re-dispatch — the run is live; retrieve with get_subagent_result({ agent_id: "${unit.id}" }).`
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** The advisory's message body — the same facts, phrased for the user's screen. */
|
|
359
|
+
export function buildMonitoringDegradedAdvisory(
|
|
360
|
+
unit: Pick<BackgroundAgentIdentity, "id" | "label">,
|
|
361
|
+
tailFailures: string[],
|
|
362
|
+
): string {
|
|
363
|
+
if (tailFailures.length === 0) return "";
|
|
364
|
+
const name = unit.label ? ` "${unit.label}"` : "";
|
|
365
|
+
return (
|
|
366
|
+
`Background agent${name} started, but its monitoring is degraded (${tailFailures.join("; ")}). ` +
|
|
367
|
+
`The run is live — do not re-dispatch it; retrieve with get_subagent_result({ agent_id: "${unit.id}" }).`
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Emit the advisory on both channels, each in its own try: a throwing
|
|
373
|
+
* `sendMessage` must not swallow the footer line, and a missing UI must not
|
|
374
|
+
* swallow the message. Best-effort by design — this is the report of a
|
|
375
|
+
* degraded path and cannot itself fail the spawn.
|
|
376
|
+
*/
|
|
377
|
+
export function sendMonitoringDegradedAdvisory(
|
|
378
|
+
pi: ExtensionAPI,
|
|
379
|
+
ctx: Pick<ExtensionContext, "ui">,
|
|
380
|
+
identity: BackgroundAgentIdentity,
|
|
381
|
+
tailFailures: string[],
|
|
382
|
+
): void {
|
|
383
|
+
try {
|
|
384
|
+
pi.sendMessage(
|
|
385
|
+
{
|
|
386
|
+
customType: MONITORING_DEGRADED_CUSTOM_TYPE,
|
|
387
|
+
content: buildMonitoringDegradedAdvisory(identity, tailFailures),
|
|
388
|
+
display: true,
|
|
389
|
+
details: { agentId: identity.id, tailFailures },
|
|
390
|
+
},
|
|
391
|
+
{ deliverAs: "followUp" },
|
|
392
|
+
);
|
|
393
|
+
} catch { /* best-effort */ }
|
|
394
|
+
try {
|
|
395
|
+
ctx.ui.notify(MONITORING_DEGRADED_TOAST, "warning");
|
|
396
|
+
} catch { /* best-effort */ }
|
|
397
|
+
}
|
package/src/params.ts
CHANGED
|
@@ -21,8 +21,10 @@ import type {
|
|
|
21
21
|
ApprovalMode,
|
|
22
22
|
SubagentToolOptions,
|
|
23
23
|
ResolvedParams,
|
|
24
|
+
SnapshotParams,
|
|
25
|
+
DelegateTaskParams,
|
|
24
26
|
} from "./types";
|
|
25
|
-
import { resolveThinkingLevel } from "./types";
|
|
27
|
+
import { approvalBlocksBackground, declaredThinkingLevel, resolveThinkingLevel } from "./types";
|
|
26
28
|
import { getAllPresets, getPreset } from "./presets";
|
|
27
29
|
import { classifyTask } from "./router";
|
|
28
30
|
import { normalizeTimeout } from "./validate";
|
|
@@ -50,7 +52,7 @@ export const KNOWN_DELEGATE_KEYS = new Set([
|
|
|
50
52
|
"outputFile", "label", "model", "timeout", "cwd", "tools",
|
|
51
53
|
"excludeTools", "noBuiltinTools", "preset", "template", "params",
|
|
52
54
|
"retryRunId", "gitMode", "retryOnTimeout", "approvalMode", "background",
|
|
53
|
-
"priority", "chain", "tasks", "graph", "force",
|
|
55
|
+
"priority", "chain", "tasks", "graph", "force", "noJev",
|
|
54
56
|
] as const);
|
|
55
57
|
|
|
56
58
|
/**
|
|
@@ -82,25 +84,12 @@ export function findUnknownParams(
|
|
|
82
84
|
* resolveRetryParams rebuilds a fresh literal and a key missing here is both
|
|
83
85
|
* unrestored AND discarded when passed explicitly on a retry call. Records
|
|
84
86
|
* written before this change simply lack the keys (additive, undefined-safe).
|
|
87
|
+
*
|
|
88
|
+
* Issue #381 (audit D5): the 16-field shape is declared once, as
|
|
89
|
+
* `SnapshotParams` in types.ts, and shared with `SubagentRun["originalParams"]`;
|
|
90
|
+
* this signature previously carried its own copy of the literal.
|
|
85
91
|
*/
|
|
86
|
-
export function snapshotOriginalParams(params: {
|
|
87
|
-
systemPrompt?: string;
|
|
88
|
-
inheritSystemPrompt?: boolean;
|
|
89
|
-
model?: string;
|
|
90
|
-
thinkingLevel?: string;
|
|
91
|
-
priority?: string;
|
|
92
|
-
outputFile?: string;
|
|
93
|
-
timeout?: number;
|
|
94
|
-
cwd?: string;
|
|
95
|
-
tools?: string[];
|
|
96
|
-
excludeTools?: string[];
|
|
97
|
-
noBuiltinTools?: boolean;
|
|
98
|
-
preset?: string;
|
|
99
|
-
background?: boolean;
|
|
100
|
-
gitMode?: string;
|
|
101
|
-
approvalMode?: string;
|
|
102
|
-
force?: boolean;
|
|
103
|
-
}): Record<string, unknown> {
|
|
92
|
+
export function snapshotOriginalParams(params: SnapshotParams): Record<string, unknown> {
|
|
104
93
|
return {
|
|
105
94
|
systemPrompt: params.systemPrompt,
|
|
106
95
|
inheritSystemPrompt: params.inheritSystemPrompt,
|
|
@@ -192,30 +181,333 @@ export function defaultBackgroundFor(
|
|
|
192
181
|
return effectiveApprovalMode === "auto";
|
|
193
182
|
}
|
|
194
183
|
|
|
184
|
+
// ---------------------------------------------------------------------------
|
|
185
|
+
// The thinking-level dispatch-shape pre-flight (issue #412, rule 2)
|
|
186
|
+
// ---------------------------------------------------------------------------
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The notice the dispatch emits when it auto-switches the shape. The text
|
|
190
|
+
* states the TRUE tool fact (why the shape matters), because that is the only
|
|
191
|
+
* thing that makes the switch acceptable to the conductor that asked for
|
|
192
|
+
* foreground: a foreground run has no transcript to resume, so a timeout there
|
|
193
|
+
* loses the work, while a background run keeps its session and is retried on a
|
|
194
|
+
* transport error.
|
|
195
|
+
*/
|
|
196
|
+
export const THINKING_SWITCH_NOTICE =
|
|
197
|
+
"Dispatch pre-flight (no deadline means the durable path): a high/xhigh-thinking run was dispatched FOREGROUND with an explicit timeout. " +
|
|
198
|
+
"The explicit timeout was dropped — foreground runs persist nothing, so a timeout there loses the run's whole context, while the 30-minute background deadline applies instead. " +
|
|
199
|
+
"The run is now background; the conductor is woken on terminal state. Pass force: true to keep it foreground (state the reason in the task text).";
|
|
200
|
+
|
|
201
|
+
/** The pre-flight's verdict for one dispatch. */
|
|
202
|
+
export type ThinkingDispatchSwitch = "switch" | "drop-timeout" | "forced" | "none";
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* The notice the dispatch emits on the SECOND arm (issue #431): the run was
|
|
206
|
+
* going to be killed by its own explicit foreground timeout, but its approval
|
|
207
|
+
* mode makes a background run impossible (or it never asked for one), so the
|
|
208
|
+
* deadline is dropped and the shape stays foreground.
|
|
209
|
+
*
|
|
210
|
+
* Deliberately NOT `THINKING_SWITCH_NOTICE`: that text says "the run is now
|
|
211
|
+
* background", which is a lie on this arm. The trade-off (no clock on a hung
|
|
212
|
+
* child) is stated rather than hidden — a dropped deadline removes the only
|
|
213
|
+
* bound the runner had.
|
|
214
|
+
*/
|
|
215
|
+
export const THINKING_TIMEOUT_DROP_NOTICE =
|
|
216
|
+
"Dispatch pre-flight (no deadline means the durable path): a high/xhigh-thinking run carried an explicit timeout, but its effective approval mode needs the interactive approval dialog — a background run would be rejected ('always') or would silently auto-approve ('writes'), so the shape cannot move. " +
|
|
217
|
+
"The explicit timeout was dropped and the run stays FOREGROUND: foreground runs have no default deadline, so no clock kills it, and the conductor's call blocks until the child settles. " +
|
|
218
|
+
"The trade-off is explicit — a dropped deadline also removes the only clock on a child that hangs. Pass force: true to keep the explicit timeout.";
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Issue #412 (rule 2: "no deadline ⇒ durable path") — what should the
|
|
222
|
+
* pre-flight do with this dispatch?
|
|
223
|
+
*
|
|
224
|
+
* The 2026-10-31 incident: a plan critique ran foreground under a 15-minute
|
|
225
|
+
* timeout, was killed at ~14 minutes ("almost done"), and lost everything —
|
|
226
|
+
* the background re-run then cost another 17 minutes. The decision was
|
|
227
|
+
* conductor discretion; this predicate makes the durable shape the only one
|
|
228
|
+
* for the runs where losing the context is expensive.
|
|
229
|
+
*
|
|
230
|
+
* One rule, two arms — the run must not be killable by its own deadline:
|
|
231
|
+
*
|
|
232
|
+
* processChecks (the project opted into OUR process — issue #415)
|
|
233
|
+
* + thinkingLevel high|xhigh
|
|
234
|
+
* + an explicit timeout (the deadline that would kill it)
|
|
235
|
+
* + an effective FOREGROUND shape
|
|
236
|
+
*
|
|
237
|
+
* → "switch": move to background and drop the timeout (the 30-minute
|
|
238
|
+
* background default applies) — used when a background run is possible.
|
|
239
|
+
* → "drop-timeout": keep foreground and drop the timeout (a foreground
|
|
240
|
+
* run has no default deadline since #423) — used when it is not:
|
|
241
|
+
* · `background: false` + effective `always`: switching there would be
|
|
242
|
+
* REJECTED (no dialog in background), and a pre-flight must never
|
|
243
|
+
* turn a valid dispatch into an error (#433);
|
|
244
|
+
* · `background` omitted where the #361 default resolved FOREGROUND
|
|
245
|
+
* (effective `writes`/`always`): the default already judged the
|
|
246
|
+
* approval mode, so the deadline is all that is left to remove.
|
|
247
|
+
*
|
|
248
|
+
* Deliberately exempt:
|
|
249
|
+
* - `processChecks: false` — this is a CONDUCTOR decision rule, not product
|
|
250
|
+
* behavior. A user's explicit foreground timeout is usually a budget cap,
|
|
251
|
+
* so overriding it could triple time and cost against their instruction.
|
|
252
|
+
* The umbrella flag (ADR 0019) is what makes the pre-flight opt-in; the
|
|
253
|
+
* predicate has no global mode.
|
|
254
|
+
* - `chain`/`graph` — an uncapped batch multiplies a hang across N units and
|
|
255
|
+
* has no per-unit pre-flight (#434); the exemption is about the BATCH now,
|
|
256
|
+
* not about `background: true` being rejected (which motivated it for the
|
|
257
|
+
* switch arm only).
|
|
258
|
+
* - an explicit `background: true` — the caller asked for background; there
|
|
259
|
+
* is no foreground deadline to lose.
|
|
260
|
+
* - an omitted `background` whose effective shape is background (`auto`) —
|
|
261
|
+
* the #361 default already chose the durable shape.
|
|
262
|
+
* - no explicit timeout — there is no deadline to drop.
|
|
263
|
+
* - `force: true` — the override, reported as `forced` (not `none`) so the
|
|
264
|
+
* caller can tell "kept by request" from "never applied". `force` carries
|
|
265
|
+
* no reason field (ADR 0007): the dispatch text is the reason.
|
|
266
|
+
* PRECEDENCE: force maps `switch`/`drop-timeout` → `forced`; an arm that
|
|
267
|
+
* computes `none` stays `none` REGARDLESS of force — there is nothing to
|
|
268
|
+
* override, and the call site's "foreground forced" log line must never
|
|
269
|
+
* print for a run that is already background.
|
|
270
|
+
*
|
|
271
|
+
* The omitted-background arm keys on the EFFECTIVE SHAPE via
|
|
272
|
+
* `defaultBackgroundFor`, never on a re-spelled mode list — the same helper the
|
|
273
|
+
* #361 default uses, so the two cannot disagree. `effectiveApprovalMode` is
|
|
274
|
+
* resolved by `resolveEffectiveApprovalMode(params.approvalMode, state)` at the
|
|
275
|
+
* call site, never read from the raw call param: a config-tier mode must reach
|
|
276
|
+
* this predicate, and the `always` guard goes through `approvalBlocksBackground`
|
|
277
|
+
* — the same helper the background gates reject with.
|
|
278
|
+
*
|
|
279
|
+
* INVARIANT (MAJ-7): "drop the timeout" is `params.timeout = undefined`, which
|
|
280
|
+
* `resolveSubagentParams` merges as `params.timeout ?? preset?.timeout`. The
|
|
281
|
+
* drop arm's no-deadline guarantee therefore holds ONLY because no preset can
|
|
282
|
+
* carry a `timeout` at runtime (`presetFromFrontmatter` maps none, so
|
|
283
|
+
* `SubagentPreset.timeout` is dead). If preset timeouts ever map, the drop
|
|
284
|
+
* silently re-merges one, no test fails, and the notice becomes a lie — merge
|
|
285
|
+
* or skip the preset value here first.
|
|
286
|
+
*
|
|
287
|
+
* `thinkingLevel` is the DECLARED level (per-call, else the named preset's),
|
|
288
|
+
* not the capped one: `maxThinkingLevel` defaults to `off` for an unconfigured
|
|
289
|
+
* session, so applying the cap here would disable the pre-flight exactly where
|
|
290
|
+
* nobody configured anything — the drift this rule exists to remove.
|
|
291
|
+
*/
|
|
292
|
+
/**
|
|
293
|
+
* #434 review (DRY): the two levels the pre-flight treats as "high thinking".
|
|
294
|
+
* ONE member test, used by the dispatch predicate and by its unit projection —
|
|
295
|
+
* the literal pair is shared core, not a documented divergence, and U10 pins
|
|
296
|
+
* that the two predicates agree on the same declared pair.
|
|
297
|
+
*/
|
|
298
|
+
export function isHighDeclaredLevel(level: ThinkingLevel | string | undefined): boolean {
|
|
299
|
+
return level === "high" || level === "xhigh";
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function resolveThinkingDispatchSwitch(input: {
|
|
303
|
+
processChecks: boolean;
|
|
304
|
+
thinkingLevel: ThinkingLevel | undefined;
|
|
305
|
+
explicitBackground: boolean | undefined;
|
|
306
|
+
timeout: number | undefined;
|
|
307
|
+
force: boolean | undefined;
|
|
308
|
+
shape: DispatchShape;
|
|
309
|
+
/** The mode the child will actually run with (call param, else config). */
|
|
310
|
+
effectiveApprovalMode: ApprovalMode;
|
|
311
|
+
}): ThinkingDispatchSwitch {
|
|
312
|
+
if (!input.processChecks) return "none";
|
|
313
|
+
if (input.shape === "chain" || input.shape === "graph") return "none";
|
|
314
|
+
if (!isHighDeclaredLevel(input.thinkingLevel)) return "none";
|
|
315
|
+
if (input.timeout === undefined) return "none";
|
|
316
|
+
const base: ThinkingDispatchSwitch =
|
|
317
|
+
input.explicitBackground === true
|
|
318
|
+
? "none"
|
|
319
|
+
: input.explicitBackground === false
|
|
320
|
+
? approvalBlocksBackground(input.effectiveApprovalMode)
|
|
321
|
+
? "drop-timeout"
|
|
322
|
+
: "switch"
|
|
323
|
+
: defaultBackgroundFor(input.shape, input.effectiveApprovalMode)
|
|
324
|
+
? "none"
|
|
325
|
+
: "drop-timeout";
|
|
326
|
+
if (base === "none") return "none";
|
|
327
|
+
return input.force === true ? "forced" : base;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* The notice a batch dispatch emits when the UNIT projection of the same rule
|
|
332
|
+
* (issue #434) removes a unit's own declared deadline.
|
|
333
|
+
*
|
|
334
|
+
* Emitted at most once per dispatch, at the first affected unit's run — so the
|
|
335
|
+
* wording deliberately claims NO completeness: it names that one unit and says
|
|
336
|
+
* when the others are judged. A unit cancelled before its run is never named,
|
|
337
|
+
* and a unit whose own declaration is not eligible gets no line at all.
|
|
338
|
+
*
|
|
339
|
+
* Deliberately NOT `THINKING_TIMEOUT_DROP_NOTICE`: that text reasons about the
|
|
340
|
+
* DISPATCH's effective approval mode, which says nothing about a unit that
|
|
341
|
+
* declared its own pair inside a batch the caller already shaped.
|
|
342
|
+
*/
|
|
343
|
+
export const THINKING_UNIT_TIMEOUT_DROP_NOTICE =
|
|
344
|
+
"Dispatch pre-flight (no deadline means the durable path): a batch unit declares its own high/xhigh thinking level with an explicit timeout, so that unit's timeout is dropped and it runs FOREGROUND — a foreground run has no default deadline, so no clock kills it. " +
|
|
345
|
+
"Only the unit this notice is attached to is reported here; the other units are judged when each reaches its run, so a unit that has not started yet is not covered. " +
|
|
346
|
+
"Pass force: true to keep the units' declared timeouts.";
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Whether a UNIT runs foreground or background — the unit's own shape, which
|
|
350
|
+
* decides whether its declared deadline is a foreground hazard at all (issue
|
|
351
|
+
* #434).
|
|
352
|
+
*
|
|
353
|
+
* Background units are excluded by construction: a `tasks` fan-out spawns its
|
|
354
|
+
* units as detached background agents, where the deadline is the legitimate
|
|
355
|
+
* 30-minute budget and dropping it would REMOVE a bound instead of adding
|
|
356
|
+
* durability. Chain/graph units are always foreground (an explicit
|
|
357
|
+
* `background: true` batch is rejected at dispatch).
|
|
358
|
+
*/
|
|
359
|
+
export type UnitShape = "foreground" | "background";
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Issue #434 — the UNIT projection of `resolveThinkingDispatchSwitch`.
|
|
363
|
+
*
|
|
364
|
+
* The dispatch rule only judges the DISPATCH-level pair (`params.thinkingLevel`
|
|
365
|
+
* + `params.timeout`). Batch units carry their own (`schema.ts`
|
|
366
|
+
* `STEP_HEAD_FIELDS`/`STEP_THINKING_LEVEL_FIELD`/`STEP_TAIL_FIELDS`) and
|
|
367
|
+
* `mergeSubTaskParams` lets the unit win per field, so a unit declaring
|
|
368
|
+
* `thinkingLevel: "high"` + `timeout: 600000` under no top-level pair
|
|
369
|
+
* reproduced the 2026-11-06 incident for that unit alone: the dispatch
|
|
370
|
+
* predicate exits early on `params.timeout === undefined`.
|
|
371
|
+
*
|
|
372
|
+
* Only ONE arm is available per unit — a unit cannot become its own background
|
|
373
|
+
* dispatch (it is one spawn inside a batch the caller already shaped), so the
|
|
374
|
+
* projection answers a boolean and never re-states the switch/drop-timeout
|
|
375
|
+
* approval arms that decide a DISPATCH's shape.
|
|
376
|
+
*
|
|
377
|
+
* Eligible when ALL hold:
|
|
378
|
+
*
|
|
379
|
+
* 1. `processChecks` — threaded from `execute` so a unit is judged by the
|
|
380
|
+
* same project opt-in as its dispatch. NEVER re-read at the unit site: a
|
|
381
|
+
* relative `params.cwd` re-read (against the process cwd instead of
|
|
382
|
+
* `ctx.cwd`) measured `false` where the dispatch resolved `true`.
|
|
383
|
+
* 2. `force !== true` — the override reaches units too; it is the caller's
|
|
384
|
+
* statement that the declared pair is intentional.
|
|
385
|
+
* 3. the unit's EFFECTIVE DECLARED level is high/xhigh: its own declaration,
|
|
386
|
+
* else the dispatch's declared level, which the unit inherits. NOT
|
|
387
|
+
* `merged.thinkingLevel`: that value is CAPPED, and the default cap is
|
|
388
|
+
* `off`, so judging it would silently disable this guard exactly where
|
|
389
|
+
* nobody configured anything — the same drift #443 removed one level up.
|
|
390
|
+
* 4. the MERGED timeout is defined and `> 0`. Only a positive timeout arms a
|
|
391
|
+
* foreground timer (`runner.ts`: `if (timeout && timeout > 0)`), and
|
|
392
|
+
* `mergeSubTaskParams` merges with `??`, so a declared `timeout: 0`
|
|
393
|
+
* OVERRIDES an inherited timeout and arms nothing — never eligible.
|
|
394
|
+
* 5. the unit DECLARES the pair — a non-empty `thinkingLevel`, or `timeout`
|
|
395
|
+
* present (a `0` included: it is a declaration, it just never arms a
|
|
396
|
+
* timer). This is the #434/#431 boundary: a unit that declares NEITHER
|
|
397
|
+
* and inherits the dispatch's pair was already judged at dispatch level,
|
|
398
|
+
* including the chain/graph exemption — so inherited pairs stay exempt
|
|
399
|
+
* (pinned, not accidental) while unit-declared ones do not.
|
|
400
|
+
* 6. the unit's shape is FOREGROUND (`unitShape`, passed explicitly so the
|
|
401
|
+
* exclusion is visible at the call site).
|
|
402
|
+
*
|
|
403
|
+
* The caller applies the effect — `merged.timeout = undefined` — before the
|
|
404
|
+
* unit's run record is created, so the retry snapshot records an absent
|
|
405
|
+
* deadline instead of restoring the cap (ADR 0009).
|
|
406
|
+
*/
|
|
407
|
+
export function shouldDropUnitTimeout(input: {
|
|
408
|
+
processChecks: boolean;
|
|
409
|
+
force: boolean | undefined;
|
|
410
|
+
/** The DISPATCH's declared level, threaded from `execute` (clause 3). */
|
|
411
|
+
dispatchDeclaredLevel: ThinkingLevel | undefined;
|
|
412
|
+
unitShape: UnitShape;
|
|
413
|
+
/** The unit's OWN declaration: `""`/`undefined` both mean "inherit". */
|
|
414
|
+
declaredLevel: ThinkingLevel | "" | undefined;
|
|
415
|
+
/** The unit's OWN timeout declaration (`undefined` = inherit). */
|
|
416
|
+
declaredTimeout: number | undefined;
|
|
417
|
+
/** `mergeSubTaskParams`' result: the deadline the unit would actually run with. */
|
|
418
|
+
mergedTimeout: number | undefined;
|
|
419
|
+
}): boolean {
|
|
420
|
+
if (!input.processChecks) return false;
|
|
421
|
+
if (input.force === true) return false;
|
|
422
|
+
if (input.unitShape !== "foreground") return false;
|
|
423
|
+
const effectiveDeclaredLevel = input.declaredLevel || input.dispatchDeclaredLevel;
|
|
424
|
+
if (!isHighDeclaredLevel(effectiveDeclaredLevel)) return false;
|
|
425
|
+
if (input.mergedTimeout === undefined || input.mergedTimeout <= 0) return false;
|
|
426
|
+
return Boolean(input.declaredLevel) || input.declaredTimeout !== undefined;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* The resolver's input: the dispatch-param fields resolution actually reads,
|
|
431
|
+
* derived from the registered schema (issue #379) instead of re-spelled here.
|
|
432
|
+
*
|
|
433
|
+
* `task` is OPTIONAL because the schema says so (`chain`/`tasks`/`graph`
|
|
434
|
+
* dispatches carry no mode-level task) — batch call sites therefore pass the
|
|
435
|
+
* registered params object through unchanged. The four `params as {…}`
|
|
436
|
+
* assertions this replaces existed only to claim a `task` the schema never
|
|
437
|
+
* required. `resolveSubagentParams` defaults it with `?? ""`, which every
|
|
438
|
+
* reader of the field treats like `undefined` (see the `task` line below).
|
|
439
|
+
*
|
|
440
|
+
* Compile-time coverage of this key list is ratcheted in index.ts
|
|
441
|
+
* (`_ResolveInputCoverage`) — a schema field the resolver forgets is silent at
|
|
442
|
+
* runtime, so only the compiler can catch it.
|
|
443
|
+
*/
|
|
444
|
+
export type ResolveInputParams = Pick<
|
|
445
|
+
DelegateTaskParams,
|
|
446
|
+
| "task"
|
|
447
|
+
| "label"
|
|
448
|
+
| "preset"
|
|
449
|
+
| "systemPrompt"
|
|
450
|
+
| "inheritSystemPrompt"
|
|
451
|
+
| "thinkingLevel"
|
|
452
|
+
| "outputFile"
|
|
453
|
+
| "timeout"
|
|
454
|
+
| "cwd"
|
|
455
|
+
| "tools"
|
|
456
|
+
| "excludeTools"
|
|
457
|
+
| "noBuiltinTools"
|
|
458
|
+
| "gitMode"
|
|
459
|
+
| "approvalMode"
|
|
460
|
+
| "template"
|
|
461
|
+
// Capability-block override: carried here so the validation call sites
|
|
462
|
+
// (which receive the resolved params) can forward it to validatePreTask.
|
|
463
|
+
// Resolution itself ignores it.
|
|
464
|
+
| "force"
|
|
465
|
+
>;
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Issue #412: resolve the preset a dispatch will actually run with, INCLUDING
|
|
469
|
+
* the auto-route decision. Extracted from `resolveSubagentParams` so the
|
|
470
|
+
* dispatch pre-flight can ask "what thinking level will this run have?"
|
|
471
|
+
* without re-spelling the routing condition — a second copy of that condition
|
|
472
|
+
* is exactly the silent drift `_ResolveInputCoverage` and the KNOWN_DELEGATE_KEYS
|
|
473
|
+
* ratchet exist to prevent.
|
|
474
|
+
*
|
|
475
|
+
* `name` is what the caller asked for (or the routed match); `preset` is the
|
|
476
|
+
* resolved record, `undefined` when neither exists. `autoRouted` distinguishes
|
|
477
|
+
* a routed match from an explicit request (the caller logs the evidence).
|
|
478
|
+
*/
|
|
479
|
+
export function resolveDispatchPreset(
|
|
480
|
+
params: Pick<
|
|
481
|
+
ResolveInputParams,
|
|
482
|
+
"preset" | "template" | "tools" | "excludeTools" | "noBuiltinTools" | "task"
|
|
483
|
+
>,
|
|
484
|
+
state: Pick<SessionState, "builtinPresets" | "customPresets">,
|
|
485
|
+
): { name?: string; preset?: SubagentPreset; autoRouted: boolean; keyword?: string } {
|
|
486
|
+
if (params.preset) {
|
|
487
|
+
return { name: params.preset, preset: getPreset(params.preset, state.builtinPresets, state.customPresets), autoRouted: false };
|
|
488
|
+
}
|
|
489
|
+
// E2: an explicit template or tool preference is intent that must win over
|
|
490
|
+
// keyword routing (issue #57) — unchanged from the resolver's rule.
|
|
491
|
+
const hasExplicitToolPreference =
|
|
492
|
+
params.tools !== undefined ||
|
|
493
|
+
params.excludeTools !== undefined ||
|
|
494
|
+
params.noBuiltinTools !== undefined;
|
|
495
|
+
if (params.template || hasExplicitToolPreference) return { autoRouted: false };
|
|
496
|
+
const matched = classifyTask(
|
|
497
|
+
params.task ?? "",
|
|
498
|
+
getAllPresets(state.builtinPresets, state.customPresets),
|
|
499
|
+
);
|
|
500
|
+
if (!matched) return { autoRouted: false };
|
|
501
|
+
return {
|
|
502
|
+
name: matched.preset,
|
|
503
|
+
preset: getPreset(matched.preset, state.builtinPresets, state.customPresets),
|
|
504
|
+
autoRouted: true,
|
|
505
|
+
keyword: matched.keyword,
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
|
|
195
509
|
export function resolveSubagentParams(
|
|
196
|
-
params:
|
|
197
|
-
task: string;
|
|
198
|
-
label?: string;
|
|
199
|
-
preset?: string;
|
|
200
|
-
systemPrompt?: string;
|
|
201
|
-
inheritSystemPrompt?: boolean;
|
|
202
|
-
thinkingLevel?: string;
|
|
203
|
-
outputFile?: string;
|
|
204
|
-
timeout?: number;
|
|
205
|
-
cwd?: string;
|
|
206
|
-
tools?: string[];
|
|
207
|
-
excludeTools?: string[];
|
|
208
|
-
noBuiltinTools?: boolean;
|
|
209
|
-
gitMode?: string;
|
|
210
|
-
approvalMode?: string;
|
|
211
|
-
template?: string;
|
|
212
|
-
/**
|
|
213
|
-
* Capability-block override: carried here so the validation call sites
|
|
214
|
-
* (which receive the resolved params) can forward it to validatePreTask.
|
|
215
|
-
* Resolution itself ignores it.
|
|
216
|
-
*/
|
|
217
|
-
force?: boolean;
|
|
218
|
-
},
|
|
510
|
+
params: ResolveInputParams,
|
|
219
511
|
state: SessionState,
|
|
220
512
|
ctx: ExtensionContext,
|
|
221
513
|
log: Logger,
|
|
@@ -229,25 +521,16 @@ export function resolveSubagentParams(
|
|
|
229
521
|
autoRouteKeyword?: string;
|
|
230
522
|
} {
|
|
231
523
|
// E2: Auto-route to best preset only when the conductor expressed NO
|
|
232
|
-
// explicit preference — an explicit preset, template, or tool
|
|
233
|
-
//
|
|
234
|
-
//
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
if (!resolvedPreset && !params.template && !hasExplicitToolPreference) {
|
|
243
|
-
const allPresets = getAllPresets(state.builtinPresets, state.customPresets);
|
|
244
|
-
const matched = classifyTask(params.task, allPresets);
|
|
245
|
-
if (matched) {
|
|
246
|
-
resolvedPreset = matched.preset;
|
|
247
|
-
wasAutoRouted = true;
|
|
248
|
-
routeKeyword = matched.keyword;
|
|
249
|
-
log.info("Auto-routed task to preset", { preset: matched.preset, keyword: matched.keyword });
|
|
250
|
-
}
|
|
524
|
+
// explicit preference — an explicit preset, template, or tool parameters
|
|
525
|
+
// (tools/excludeTools/noBuiltinTools) all count as intent that must win over
|
|
526
|
+
// keyword-based routing (issue #57). The rule lives in `resolveDispatchPreset`
|
|
527
|
+
// so the dispatch pre-flight (#412) resolves the SAME preset this merge does.
|
|
528
|
+
const routed = resolveDispatchPreset(params, state);
|
|
529
|
+
const resolvedPreset = routed.name;
|
|
530
|
+
const wasAutoRouted = routed.autoRouted;
|
|
531
|
+
const routeKeyword = routed.keyword;
|
|
532
|
+
if (wasAutoRouted) {
|
|
533
|
+
log.info("Auto-routed task to preset", { preset: resolvedPreset, keyword: routeKeyword });
|
|
251
534
|
}
|
|
252
535
|
|
|
253
536
|
const preset = resolvedPreset
|
|
@@ -255,9 +538,13 @@ export function resolveSubagentParams(
|
|
|
255
538
|
: undefined;
|
|
256
539
|
const autoRoutedPreset = wasAutoRouted ? preset : undefined;
|
|
257
540
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
541
|
+
// The DECLARED level (per-call, else the selected preset's) — read through the
|
|
542
|
+
// shared helper so both dispatch guards (issue #443) judge exactly the value
|
|
543
|
+
// this merge resolves, not a second interpretation of it.
|
|
544
|
+
const mergedThinkingLevel = declaredThinkingLevel(
|
|
545
|
+
params.thinkingLevel,
|
|
546
|
+
preset?.thinkingLevel,
|
|
547
|
+
) as ThinkingLevel | undefined;
|
|
261
548
|
const mergedSystemPrompt = params.systemPrompt ?? preset?.systemPrompt;
|
|
262
549
|
const mergedInheritSP = params.inheritSystemPrompt ?? preset?.inheritSystemPrompt;
|
|
263
550
|
const mergedOutputFile = params.outputFile ?? preset?.outputFile;
|
|
@@ -301,7 +588,10 @@ export function resolveSubagentParams(
|
|
|
301
588
|
: undefined;
|
|
302
589
|
|
|
303
590
|
return {
|
|
304
|
-
|
|
591
|
+
// "" ≡ undefined at all three readers of the field: mergeSubTaskParams
|
|
592
|
+
// (`subTask.task || globalParams.task`), validatePreTask
|
|
593
|
+
// (`config.task || ''`) and classifyTask (`if (!task …)`).
|
|
594
|
+
task: params.task ?? "",
|
|
305
595
|
label: params.label?.trim() || undefined,
|
|
306
596
|
inheritSP: mergedInheritSP !== false,
|
|
307
597
|
customSP: mergedSystemPrompt,
|