opencode-subagent-tools 0.1.0 → 0.2.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/CHANGELOG.md +36 -0
- package/README.md +23 -18
- package/SECURITY.md +1 -1
- package/SKILL.md +60 -20
- package/docs/development.md +14 -10
- package/docs/installation.md +1 -7
- package/docs/limitations.md +45 -9
- package/docs/reference.md +83 -32
- package/package.json +5 -5
- package/src/access.ts +79 -79
- package/src/background.ts +602 -0
- package/src/config.ts +47 -47
- package/src/cursor.ts +41 -39
- package/src/discovery.ts +77 -69
- package/src/errors.ts +7 -7
- package/src/events.ts +141 -95
- package/src/format.ts +20 -20
- package/src/index.ts +49 -36
- package/src/interrupt.ts +4 -4
- package/src/live-state.ts +64 -41
- package/src/messages.ts +134 -84
- package/src/permissions.ts +18 -18
- package/src/session-row.ts +82 -21
- package/src/session.ts +69 -69
- package/src/skill.ts +24 -24
- package/src/status.ts +64 -64
- package/src/text.ts +16 -16
- package/src/tokens.ts +51 -51
- package/src/tools/cancel-descendants.ts +303 -170
- package/src/tools/cancel.ts +139 -73
- package/src/tools/index.ts +32 -30
- package/src/tools/input.ts +46 -44
- package/src/tools/list.ts +281 -175
- package/src/tools/names.ts +2 -2
- package/src/tools/output.ts +51 -51
- package/src/tools/send.ts +130 -96
- package/src/tools/usage.ts +181 -143
|
@@ -1,49 +1,73 @@
|
|
|
1
|
-
import type { Plugin } from "@opencode/plugin"
|
|
2
|
-
import type { ToolEditor } from "@opencode/plugin/promise/tool"
|
|
3
|
-
import type { Access } from "../access"
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import type {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
import type { Plugin } from "@opencode/plugin";
|
|
2
|
+
import type { ToolEditor } from "@opencode/plugin/promise/tool";
|
|
3
|
+
import type { Access } from "../access";
|
|
4
|
+
import {
|
|
5
|
+
BACKGROUND_JOB_SCHEMA,
|
|
6
|
+
backgroundJobLabel,
|
|
7
|
+
type BackgroundJobInfo,
|
|
8
|
+
} from "../background";
|
|
9
|
+
import type { Config } from "../config";
|
|
10
|
+
import { encodeCursor, isAfterCursor, readCursor } from "../cursor";
|
|
11
|
+
import type { Discovery } from "../discovery";
|
|
12
|
+
import { errorMessage, toolError } from "../errors";
|
|
13
|
+
import { INTERRUPT_OUTCOME, interruptOutcome } from "../interrupt";
|
|
14
|
+
import type { LiveStore } from "../live-state";
|
|
15
|
+
import { guardTarget, type Guard } from "../permissions";
|
|
16
|
+
import type { SessionRow } from "../session-row";
|
|
17
|
+
import {
|
|
18
|
+
RUN_STATUS,
|
|
19
|
+
RUN_STATUSES,
|
|
20
|
+
TERMINAL_RUN_STATUSES,
|
|
21
|
+
type RunStatus,
|
|
22
|
+
} from "../status";
|
|
23
|
+
import { MAX_REASON_LENGTH, readTextField, readToolInput } from "./input";
|
|
24
|
+
import { TOOL } from "./names";
|
|
25
|
+
import { formatPropertyDescription, resolveToolOutputStrategy } from "./output";
|
|
26
|
+
|
|
27
|
+
export const CANCEL_INTERRUPT = INTERRUPT_OUTCOME;
|
|
28
|
+
|
|
29
|
+
export type CancelInterrupt = (typeof CANCEL_INTERRUPT)[keyof typeof CANCEL_INTERRUPT];
|
|
19
30
|
|
|
20
31
|
export const CANCEL_READ_BACK = {
|
|
21
32
|
ok: "ok",
|
|
22
33
|
failed: "failed",
|
|
23
34
|
notRead: "not-read",
|
|
24
|
-
} as const
|
|
35
|
+
} as const;
|
|
25
36
|
|
|
26
|
-
export type CancelReadBack = (typeof CANCEL_READ_BACK)[keyof typeof CANCEL_READ_BACK]
|
|
37
|
+
export type CancelReadBack = (typeof CANCEL_READ_BACK)[keyof typeof CANCEL_READ_BACK];
|
|
27
38
|
|
|
28
39
|
export interface CancelDescendantsTarget {
|
|
29
|
-
sessionID: string
|
|
30
|
-
interrupt: CancelInterrupt
|
|
31
|
-
readBack: CancelReadBack
|
|
32
|
-
status?:
|
|
33
|
-
|
|
40
|
+
sessionID: string;
|
|
41
|
+
interrupt: CancelInterrupt;
|
|
42
|
+
readBack: CancelReadBack;
|
|
43
|
+
status?: RunStatus;
|
|
44
|
+
backgroundJob?: BackgroundJobInfo;
|
|
45
|
+
error?: string;
|
|
34
46
|
}
|
|
35
47
|
|
|
36
48
|
export interface CancelDescendantsOutput {
|
|
37
|
-
known: number
|
|
38
|
-
considered: number
|
|
39
|
-
running: number
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
known: number;
|
|
50
|
+
considered: number;
|
|
51
|
+
running: number;
|
|
52
|
+
activeCount: number;
|
|
53
|
+
subtreeActiveCount: number;
|
|
54
|
+
backgroundJobCount: number;
|
|
55
|
+
backgroundJobMayRemain?: boolean;
|
|
56
|
+
beforeCursorRunning?: number;
|
|
57
|
+
targets: CancelDescendantsTarget[];
|
|
58
|
+
remaining: number;
|
|
59
|
+
aborted: boolean;
|
|
60
|
+
reason?: string;
|
|
61
|
+
cursor?: string;
|
|
62
|
+
warning?: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface CancelDescendantsSummary {
|
|
66
|
+
activeCount: number;
|
|
67
|
+
subtreeActiveCount: number;
|
|
68
|
+
backgroundJobCount: number;
|
|
69
|
+
backgroundJobMayRemain: boolean;
|
|
70
|
+
idleParentsWithActiveDescendants: number;
|
|
47
71
|
}
|
|
48
72
|
|
|
49
73
|
const CANCEL_DESCENDANTS_TARGET_SCHEMA = {
|
|
@@ -62,12 +86,13 @@ const CANCEL_DESCENDANTS_TARGET_SCHEMA = {
|
|
|
62
86
|
description:
|
|
63
87
|
"ok: status check succeeded and status is set; failed: status check failed; not-read: not attempted or aborted.",
|
|
64
88
|
},
|
|
65
|
-
status: { type: "string", enum: [...
|
|
89
|
+
status: { type: "string", enum: [...RUN_STATUSES] },
|
|
90
|
+
backgroundJob: BACKGROUND_JOB_SCHEMA,
|
|
66
91
|
error: { type: "string" },
|
|
67
92
|
},
|
|
68
93
|
required: ["sessionID", "interrupt", "readBack"],
|
|
69
94
|
additionalProperties: false,
|
|
70
|
-
} as const
|
|
95
|
+
} as const;
|
|
71
96
|
|
|
72
97
|
const CANCEL_DESCENDANTS_OUTPUT_SCHEMA = {
|
|
73
98
|
type: "object",
|
|
@@ -85,6 +110,24 @@ const CANCEL_DESCENDANTS_OUTPUT_SCHEMA = {
|
|
|
85
110
|
type: "integer",
|
|
86
111
|
description: "Running descendants among considered.",
|
|
87
112
|
},
|
|
113
|
+
activeCount: {
|
|
114
|
+
type: "integer",
|
|
115
|
+
description:
|
|
116
|
+
"Considered descendants with their own active execution or background job.",
|
|
117
|
+
},
|
|
118
|
+
subtreeActiveCount: {
|
|
119
|
+
type: "integer",
|
|
120
|
+
description: "Considered descendants that are active or have an active descendant.",
|
|
121
|
+
},
|
|
122
|
+
backgroundJobCount: {
|
|
123
|
+
type: "integer",
|
|
124
|
+
description:
|
|
125
|
+
"Considered descendants that are not running but own a background job.",
|
|
126
|
+
},
|
|
127
|
+
backgroundJobMayRemain: {
|
|
128
|
+
type: "boolean",
|
|
129
|
+
description: "True when an observed background job may still run after this call.",
|
|
130
|
+
},
|
|
88
131
|
beforeCursorRunning: {
|
|
89
132
|
type: "integer",
|
|
90
133
|
description:
|
|
@@ -108,62 +151,72 @@ const CANCEL_DESCENDANTS_OUTPUT_SCHEMA = {
|
|
|
108
151
|
cursor: { type: "string" },
|
|
109
152
|
warning: { type: "string" },
|
|
110
153
|
},
|
|
111
|
-
required: [
|
|
154
|
+
required: [
|
|
155
|
+
"known",
|
|
156
|
+
"considered",
|
|
157
|
+
"running",
|
|
158
|
+
"activeCount",
|
|
159
|
+
"subtreeActiveCount",
|
|
160
|
+
"backgroundJobCount",
|
|
161
|
+
"targets",
|
|
162
|
+
"remaining",
|
|
163
|
+
"aborted",
|
|
164
|
+
],
|
|
112
165
|
additionalProperties: false,
|
|
113
|
-
} as const
|
|
166
|
+
} as const;
|
|
114
167
|
|
|
115
168
|
const isTerminalTarget = (target: CancelDescendantsTarget): boolean =>
|
|
116
169
|
target.readBack === CANCEL_READ_BACK.ok &&
|
|
117
170
|
target.status !== undefined &&
|
|
118
|
-
|
|
171
|
+
TERMINAL_RUN_STATUSES.has(target.status);
|
|
119
172
|
|
|
120
173
|
const INTERRUPT_SUMMARY_LABEL: Readonly<Record<CancelInterrupt, string>> = {
|
|
121
174
|
[CANCEL_INTERRUPT.accepted]: "interrupt accepted",
|
|
122
175
|
[CANCEL_INTERRUPT.rejected]: "interrupt rejected",
|
|
123
176
|
[CANCEL_INTERRUPT.failed]: "interrupt failed",
|
|
124
177
|
[CANCEL_INTERRUPT.unknown]: "interrupt outcome unknown",
|
|
125
|
-
}
|
|
178
|
+
};
|
|
126
179
|
|
|
127
180
|
const INTERRUPT_SUMMARY_ORDER: readonly CancelInterrupt[] = Object.values(
|
|
128
181
|
CANCEL_INTERRUPT,
|
|
129
|
-
).filter((outcome) => outcome !== CANCEL_INTERRUPT.accepted)
|
|
182
|
+
).filter((outcome) => outcome !== CANCEL_INTERRUPT.accepted);
|
|
130
183
|
|
|
131
184
|
const READ_BACK_SUMMARY_LABEL: Readonly<Record<CancelReadBack, string>> = {
|
|
132
185
|
[CANCEL_READ_BACK.ok]: "read-back ok",
|
|
133
186
|
[CANCEL_READ_BACK.failed]: "read-back failed",
|
|
134
187
|
[CANCEL_READ_BACK.notRead]: "read-back not completed",
|
|
135
|
-
}
|
|
188
|
+
};
|
|
136
189
|
|
|
137
190
|
const countInterrupts = (
|
|
138
191
|
targets: readonly CancelDescendantsTarget[],
|
|
139
192
|
): ReadonlyMap<string, number> => {
|
|
140
193
|
const counts = new Map<string, number>(
|
|
141
194
|
Object.values(CANCEL_INTERRUPT).map((outcome) => [outcome, 0]),
|
|
142
|
-
)
|
|
195
|
+
);
|
|
143
196
|
for (const target of targets) {
|
|
144
|
-
counts.set(target.interrupt, (counts.get(target.interrupt) ?? 0) + 1)
|
|
197
|
+
counts.set(target.interrupt, (counts.get(target.interrupt) ?? 0) + 1);
|
|
145
198
|
}
|
|
146
|
-
return counts
|
|
147
|
-
}
|
|
199
|
+
return counts;
|
|
200
|
+
};
|
|
148
201
|
|
|
149
202
|
const countReadBacks = (
|
|
150
203
|
targets: readonly CancelDescendantsTarget[],
|
|
151
204
|
): ReadonlyMap<string, number> => {
|
|
152
205
|
const counts = new Map<string, number>(
|
|
153
206
|
Object.values(CANCEL_READ_BACK).map((outcome) => [outcome, 0]),
|
|
154
|
-
)
|
|
207
|
+
);
|
|
155
208
|
for (const target of targets) {
|
|
156
|
-
counts.set(target.readBack, (counts.get(target.readBack) ?? 0) + 1)
|
|
209
|
+
counts.set(target.readBack, (counts.get(target.readBack) ?? 0) + 1);
|
|
157
210
|
}
|
|
158
|
-
return counts
|
|
159
|
-
}
|
|
211
|
+
return counts;
|
|
212
|
+
};
|
|
160
213
|
|
|
161
214
|
const READ_BACK_DETAIL: ReadonlyMap<CancelReadBack, string> = new Map([
|
|
162
215
|
[CANCEL_READ_BACK.failed, READ_BACK_SUMMARY_LABEL[CANCEL_READ_BACK.failed]],
|
|
163
216
|
[CANCEL_READ_BACK.notRead, READ_BACK_SUMMARY_LABEL[CANCEL_READ_BACK.notRead]],
|
|
164
|
-
])
|
|
217
|
+
]);
|
|
165
218
|
|
|
166
|
-
type TargetDetail = (target: CancelDescendantsTarget) => string | undefined
|
|
219
|
+
type TargetDetail = (target: CancelDescendantsTarget) => string | undefined;
|
|
167
220
|
|
|
168
221
|
const INTERRUPT_DETAIL: ReadonlyMap<CancelInterrupt, TargetDetail> = new Map([
|
|
169
222
|
[
|
|
@@ -174,7 +227,7 @@ const INTERRUPT_DETAIL: ReadonlyMap<CancelInterrupt, TargetDetail> = new Map([
|
|
|
174
227
|
CANCEL_INTERRUPT.rejected,
|
|
175
228
|
(target: CancelDescendantsTarget) =>
|
|
176
229
|
READ_BACK_DETAIL.get(target.readBack) ??
|
|
177
|
-
(target.status ===
|
|
230
|
+
(target.status === RUN_STATUS.running
|
|
178
231
|
? "interrupt rejected; state still says running"
|
|
179
232
|
: undefined),
|
|
180
233
|
],
|
|
@@ -184,109 +237,163 @@ const INTERRUPT_DETAIL: ReadonlyMap<CancelInterrupt, TargetDetail> = new Map([
|
|
|
184
237
|
`interrupt failed: ${target.error ?? "unknown error"}`,
|
|
185
238
|
],
|
|
186
239
|
[CANCEL_INTERRUPT.unknown, () => "interrupt outcome unknown"],
|
|
187
|
-
])
|
|
240
|
+
]);
|
|
188
241
|
|
|
189
242
|
const targetDetail = (target: CancelDescendantsTarget): string => {
|
|
190
|
-
const detail = INTERRUPT_DETAIL.get(target.interrupt)?.(target)
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
243
|
+
const detail = INTERRUPT_DETAIL.get(target.interrupt)?.(target);
|
|
244
|
+
const background = target.backgroundJob
|
|
245
|
+
? `background=${backgroundJobLabel(target.backgroundJob)} cannot be stopped`
|
|
246
|
+
: undefined;
|
|
247
|
+
if (detail && background) return `${detail}; ${background}`;
|
|
248
|
+
if (detail) return detail;
|
|
249
|
+
if (background) return background;
|
|
250
|
+
if (target.status === RUN_STATUS.running) return "still running";
|
|
251
|
+
if (target.status === RUN_STATUS.unknown) return "status unknown";
|
|
252
|
+
return target.status ?? "status unavailable";
|
|
253
|
+
};
|
|
196
254
|
|
|
197
255
|
export const summarizeCancelDescendants = (
|
|
198
256
|
targets: readonly CancelDescendantsTarget[],
|
|
199
257
|
remaining: number,
|
|
200
258
|
cursor?: string,
|
|
259
|
+
summary?: CancelDescendantsSummary,
|
|
201
260
|
): string => {
|
|
202
261
|
if (targets.length === 0) {
|
|
203
|
-
if (remaining
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
262
|
+
if (remaining > 0) {
|
|
263
|
+
return `No interrupts were attempted; ${remaining} running subagent(s) left${
|
|
264
|
+
cursor ? `; use cursor ${cursor} to continue` : "; retry with the same cursor"
|
|
265
|
+
}.`;
|
|
266
|
+
}
|
|
267
|
+
const parts: string[] = [];
|
|
268
|
+
if (summary?.backgroundJobCount) {
|
|
269
|
+
parts.push(
|
|
270
|
+
`${summary.backgroundJobCount} idle subagent(s) own background jobs that this tool cannot stop`,
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
if (summary?.idleParentsWithActiveDescendants) {
|
|
274
|
+
parts.push(
|
|
275
|
+
`${summary.idleParentsWithActiveDescendants} idle parent(s) have active descendants and may wake`,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
if (parts.length === 0 && summary?.subtreeActiveCount) {
|
|
279
|
+
parts.push(
|
|
280
|
+
`${summary.subtreeActiveCount} considered subagent(s) have active work in their subtree`,
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
if (parts.length === 0) return "No running subagents needed an interrupt.";
|
|
284
|
+
let text = `No running executions were interrupted: ${parts.join("; ")}.`;
|
|
285
|
+
if (summary?.backgroundJobMayRemain) text += " Background work may remain.";
|
|
286
|
+
return text;
|
|
207
287
|
}
|
|
208
288
|
|
|
209
|
-
const interruptCounts = countInterrupts(targets)
|
|
210
|
-
const readBackCounts = countReadBacks(targets)
|
|
211
|
-
const terminal = targets.filter(isTerminalTarget).length
|
|
289
|
+
const interruptCounts = countInterrupts(targets);
|
|
290
|
+
const readBackCounts = countReadBacks(targets);
|
|
291
|
+
const terminal = targets.filter(isTerminalTarget).length;
|
|
212
292
|
const stillRunning = targets.filter(
|
|
213
293
|
(target) =>
|
|
214
|
-
target.readBack === CANCEL_READ_BACK.ok && target.status ===
|
|
215
|
-
).length
|
|
294
|
+
target.readBack === CANCEL_READ_BACK.ok && target.status === RUN_STATUS.running,
|
|
295
|
+
).length;
|
|
216
296
|
const readBackNotRead = targets.filter(
|
|
217
297
|
(target) =>
|
|
218
298
|
target.readBack === CANCEL_READ_BACK.notRead &&
|
|
219
299
|
(target.interrupt === CANCEL_INTERRUPT.accepted ||
|
|
220
300
|
target.interrupt === CANCEL_INTERRUPT.rejected),
|
|
221
|
-
).length
|
|
301
|
+
).length;
|
|
222
302
|
const statusUnknown = targets.filter(
|
|
223
303
|
(target) =>
|
|
224
|
-
target.readBack === CANCEL_READ_BACK.ok && target.status ===
|
|
225
|
-
).length
|
|
304
|
+
target.readBack === CANCEL_READ_BACK.ok && target.status === RUN_STATUS.unknown,
|
|
305
|
+
).length;
|
|
226
306
|
|
|
227
|
-
const accepted = interruptCounts.get(CANCEL_INTERRUPT.accepted) ?? 0
|
|
307
|
+
const accepted = interruptCounts.get(CANCEL_INTERRUPT.accepted) ?? 0;
|
|
228
308
|
const parts: string[] = [
|
|
229
309
|
`Interrupt accepted for ${accepted} of ${targets.length} target(s)`,
|
|
230
|
-
]
|
|
310
|
+
];
|
|
231
311
|
for (const outcome of INTERRUPT_SUMMARY_ORDER) {
|
|
232
|
-
const count = interruptCounts.get(outcome) ?? 0
|
|
233
|
-
if (count > 0) parts.push(`${count} ${INTERRUPT_SUMMARY_LABEL[outcome]}`)
|
|
312
|
+
const count = interruptCounts.get(outcome) ?? 0;
|
|
313
|
+
if (count > 0) parts.push(`${count} ${INTERRUPT_SUMMARY_LABEL[outcome]}`);
|
|
234
314
|
}
|
|
235
|
-
const knownInterrupts = new Set<string>(Object.values(CANCEL_INTERRUPT))
|
|
315
|
+
const knownInterrupts = new Set<string>(Object.values(CANCEL_INTERRUPT));
|
|
236
316
|
for (const [outcome, count] of interruptCounts) {
|
|
237
317
|
if (!knownInterrupts.has(outcome) && count > 0) {
|
|
238
|
-
parts.push(`${count} interrupt ${outcome}`)
|
|
318
|
+
parts.push(`${count} interrupt ${outcome}`);
|
|
239
319
|
}
|
|
240
320
|
}
|
|
241
|
-
if (terminal > 0) parts.push(`${terminal} terminal`)
|
|
242
|
-
if (stillRunning > 0) parts.push(`${stillRunning} still running`)
|
|
243
|
-
const readBackFailed = readBackCounts.get(CANCEL_READ_BACK.failed) ?? 0
|
|
321
|
+
if (terminal > 0) parts.push(`${terminal} terminal`);
|
|
322
|
+
if (stillRunning > 0) parts.push(`${stillRunning} still running`);
|
|
323
|
+
const readBackFailed = readBackCounts.get(CANCEL_READ_BACK.failed) ?? 0;
|
|
244
324
|
if (readBackFailed > 0) {
|
|
245
|
-
parts.push(`${readBackFailed} ${READ_BACK_SUMMARY_LABEL[CANCEL_READ_BACK.failed]}`)
|
|
325
|
+
parts.push(`${readBackFailed} ${READ_BACK_SUMMARY_LABEL[CANCEL_READ_BACK.failed]}`);
|
|
246
326
|
}
|
|
247
327
|
if (readBackNotRead > 0) {
|
|
248
|
-
parts.push(`${readBackNotRead} ${READ_BACK_SUMMARY_LABEL[CANCEL_READ_BACK.notRead]}`)
|
|
328
|
+
parts.push(`${readBackNotRead} ${READ_BACK_SUMMARY_LABEL[CANCEL_READ_BACK.notRead]}`);
|
|
249
329
|
}
|
|
250
|
-
const knownReadBacks = new Set<string>(Object.values(CANCEL_READ_BACK))
|
|
330
|
+
const knownReadBacks = new Set<string>(Object.values(CANCEL_READ_BACK));
|
|
251
331
|
for (const [outcome, count] of readBackCounts) {
|
|
252
332
|
if (!knownReadBacks.has(outcome) && count > 0) {
|
|
253
|
-
parts.push(`${count} read-back ${outcome}`)
|
|
333
|
+
parts.push(`${count} read-back ${outcome}`);
|
|
254
334
|
}
|
|
255
335
|
}
|
|
256
|
-
if (statusUnknown > 0) parts.push(`${statusUnknown} status unknown`)
|
|
336
|
+
if (statusUnknown > 0) parts.push(`${statusUnknown} status unknown`);
|
|
337
|
+
if (summary?.backgroundJobCount) {
|
|
338
|
+
parts.push(
|
|
339
|
+
`${summary.backgroundJobCount} idle subagent(s) own background jobs that cannot be stopped`,
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
if (summary?.idleParentsWithActiveDescendants) {
|
|
343
|
+
parts.push(
|
|
344
|
+
`${summary.idleParentsWithActiveDescendants} idle parent(s) have active descendants and may wake`,
|
|
345
|
+
);
|
|
346
|
+
}
|
|
257
347
|
|
|
258
|
-
let text = `${parts[0]}${parts.length > 1 ? `: ${parts.slice(1).join(", ")}` : ""}
|
|
348
|
+
let text = `${parts[0]}${parts.length > 1 ? `: ${parts.slice(1).join(", ")}` : ""}.`;
|
|
259
349
|
if (remaining > 0) {
|
|
260
350
|
text += ` ${remaining} running subagent(s) left${
|
|
261
351
|
cursor ? `; use cursor ${cursor} to continue` : "; retry with the same cursor"
|
|
262
|
-
}
|
|
352
|
+
}.`;
|
|
353
|
+
}
|
|
354
|
+
if (summary?.backgroundJobMayRemain) {
|
|
355
|
+
text += " Background jobs may remain; this tool cannot stop them.";
|
|
263
356
|
}
|
|
264
357
|
|
|
265
358
|
const details = targets
|
|
266
|
-
.filter((target) => !isTerminalTarget(target))
|
|
267
|
-
.map((target) => `${target.sessionID} (${targetDetail(target)})`)
|
|
268
|
-
if (details.length > 0) text += `\n${details.join("\n")}
|
|
269
|
-
return text
|
|
270
|
-
}
|
|
359
|
+
.filter((target) => !isTerminalTarget(target) || target.backgroundJob !== undefined)
|
|
360
|
+
.map((target) => `${target.sessionID} (${targetDetail(target)})`);
|
|
361
|
+
if (details.length > 0) text += `\n${details.join("\n")}`;
|
|
362
|
+
return text;
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const countActivity = (rows: readonly SessionRow[]) => ({
|
|
366
|
+
activeCount: rows.filter((row) => row.active).length,
|
|
367
|
+
subtreeActiveCount: rows.filter((row) => row.subtreeActive).length,
|
|
368
|
+
backgroundJobCount: rows.filter(
|
|
369
|
+
(row) => row.status !== RUN_STATUS.running && row.backgroundJob !== undefined,
|
|
370
|
+
).length,
|
|
371
|
+
idleParentsWithActiveDescendants: rows.filter(
|
|
372
|
+
(row) => row.status !== RUN_STATUS.running && row.activeDescendants === true,
|
|
373
|
+
).length,
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
const backgroundFor = (row: SessionRow): { backgroundJob?: BackgroundJobInfo } =>
|
|
377
|
+
row.backgroundJob ? { backgroundJob: { ...row.backgroundJob, mayRemain: true } } : {};
|
|
271
378
|
|
|
272
379
|
export interface CancelDescendantsDeps {
|
|
273
|
-
config: Config
|
|
274
|
-
access: Access
|
|
275
|
-
discovery: Discovery
|
|
276
|
-
guard: Guard
|
|
277
|
-
live: LiveStore
|
|
278
|
-
session: Plugin.Context["session"]
|
|
380
|
+
config: Config;
|
|
381
|
+
access: Access;
|
|
382
|
+
discovery: Discovery;
|
|
383
|
+
guard: Guard;
|
|
384
|
+
live: LiveStore;
|
|
385
|
+
session: Plugin.Context["session"];
|
|
279
386
|
}
|
|
280
387
|
|
|
281
388
|
export const registerCancelDescendantsTool = (
|
|
282
389
|
editor: ToolEditor,
|
|
283
390
|
{ config, access, discovery, guard, live, session }: CancelDescendantsDeps,
|
|
284
391
|
) => {
|
|
285
|
-
const { allowedOutputFormats, limits } = config
|
|
392
|
+
const { allowedOutputFormats, limits } = config;
|
|
286
393
|
const outputStrategy = resolveToolOutputStrategy(
|
|
287
394
|
allowedOutputFormats,
|
|
288
395
|
TOOL.cancelDescendants,
|
|
289
|
-
)
|
|
396
|
+
);
|
|
290
397
|
|
|
291
398
|
editor.add({
|
|
292
399
|
name: TOOL.cancelDescendants,
|
|
@@ -295,6 +402,7 @@ export const registerCancelDescendantsTool = (
|
|
|
295
402
|
`up to ${limits.maxCancelBatch} at a time. ` +
|
|
296
403
|
"There is no sessionID argument; it always stays in the caller's subtree. " +
|
|
297
404
|
"The batch is checked in one permission request and reports each target in targets. " +
|
|
405
|
+
"Only running executions are targets; idle sessions that own background jobs cannot be stopped by this tool and are reported in backgroundJobCount. " +
|
|
298
406
|
"Use cursor while remaining is above 0. Cursor retries skip failed, unknown, rejected, and not-read targets. " +
|
|
299
407
|
"Nested cancellation can wake an intermediate parent; re-run list and cancel it again if it resumed. " +
|
|
300
408
|
"Discovery can miss running descendants. Use cancel for one target; this is a destructive batch and a soft interrupt, not a hard abort.",
|
|
@@ -319,146 +427,157 @@ export const registerCancelDescendantsTool = (
|
|
|
319
427
|
output: outputStrategy.schema(CANCEL_DESCENDANTS_OUTPUT_SCHEMA),
|
|
320
428
|
options: { namespace: config.namespace },
|
|
321
429
|
execute: async (rawInput, context) => {
|
|
322
|
-
const { input, format } = readToolInput(rawInput, outputStrategy)
|
|
323
|
-
const reasonField = readTextField(input?.reason, MAX_REASON_LENGTH, "reason")
|
|
324
|
-
if (reasonField.error) return toolError(reasonField.error)
|
|
325
|
-
const reason = reasonField.text
|
|
326
|
-
const reasonText = reason ? ` (${reason})` : ""
|
|
327
|
-
const caller = context?.sessionID
|
|
430
|
+
const { input, format } = readToolInput(rawInput, outputStrategy);
|
|
431
|
+
const reasonField = readTextField(input?.reason, MAX_REASON_LENGTH, "reason");
|
|
432
|
+
if (reasonField.error) return toolError(reasonField.error);
|
|
433
|
+
const reason = reasonField.text;
|
|
434
|
+
const reasonText = reason ? ` (${reason})` : "";
|
|
435
|
+
const caller = context?.sessionID;
|
|
328
436
|
if (!caller) {
|
|
329
437
|
return toolError(
|
|
330
438
|
"Refusing: the calling session is unknown, so the subtree to cancel cannot be determined.",
|
|
331
|
-
)
|
|
439
|
+
);
|
|
332
440
|
}
|
|
333
441
|
const cursorResult = readCursor(input?.cursor, {
|
|
334
442
|
tool: TOOL.cancelDescendants,
|
|
335
443
|
root: caller,
|
|
336
|
-
})
|
|
444
|
+
});
|
|
337
445
|
if (cursorResult.error) {
|
|
338
|
-
return toolError(cursorResult.error)
|
|
446
|
+
return toolError(cursorResult.error);
|
|
339
447
|
}
|
|
340
|
-
const cursor = cursorResult.cursor
|
|
448
|
+
const cursor = cursorResult.cursor;
|
|
341
449
|
if (context?.signal?.aborted) {
|
|
342
|
-
return toolError("Aborted before discovery.")
|
|
450
|
+
return toolError("Aborted before discovery.");
|
|
343
451
|
}
|
|
344
452
|
|
|
345
453
|
const respond = (content: string, output: CancelDescendantsOutput) =>
|
|
346
|
-
outputStrategy.respond(content, output, format)
|
|
454
|
+
outputStrategy.respond(content, output, format);
|
|
347
455
|
|
|
348
|
-
const found = await discovery.discoverRows(caller, context?.signal)
|
|
456
|
+
const found = await discovery.discoverRows(caller, context?.signal);
|
|
349
457
|
const warningMessage =
|
|
350
|
-
"discovery was incomplete; some running subagents may not have been stopped."
|
|
351
|
-
const warning = found.complete ? "" : `\nWarning: ${warningMessage}
|
|
352
|
-
const known = found.rows
|
|
458
|
+
"discovery was incomplete; some running subagents may not have been stopped.";
|
|
459
|
+
const warning = found.complete ? "" : `\nWarning: ${warningMessage}`;
|
|
460
|
+
const known = found.rows;
|
|
353
461
|
const afterCursor = cursor
|
|
354
462
|
? known.filter((row) => isAfterCursor(row, cursor))
|
|
355
|
-
: known
|
|
356
|
-
const running = afterCursor.filter((row) => row.status ===
|
|
463
|
+
: known;
|
|
464
|
+
const running = afterCursor.filter((row) => row.status === RUN_STATUS.running);
|
|
357
465
|
const beforeCursorRunning = cursor
|
|
358
466
|
? known.filter(
|
|
359
467
|
(row) =>
|
|
360
468
|
!(row.launchedAt === cursor.launchedAt && row.id === cursor.id) &&
|
|
361
469
|
!isAfterCursor(row, cursor) &&
|
|
362
|
-
row.status ===
|
|
470
|
+
row.status === RUN_STATUS.running,
|
|
363
471
|
).length
|
|
364
|
-
: 0
|
|
472
|
+
: 0;
|
|
365
473
|
const cursorNote =
|
|
366
474
|
beforeCursorRunning > 0
|
|
367
475
|
? `\nNote: ${beforeCursorRunning} running subagent(s) are before this cursor window; re-run without a cursor to include them.`
|
|
368
|
-
: ""
|
|
476
|
+
: "";
|
|
369
477
|
|
|
478
|
+
const activity = countActivity(afterCursor);
|
|
370
479
|
const emptyOutput = (aborted = false): CancelDescendantsOutput => ({
|
|
371
480
|
known: known.length,
|
|
372
481
|
considered: afterCursor.length,
|
|
373
482
|
running: running.length,
|
|
483
|
+
activeCount: activity.activeCount,
|
|
484
|
+
subtreeActiveCount: activity.subtreeActiveCount,
|
|
485
|
+
backgroundJobCount: activity.backgroundJobCount,
|
|
486
|
+
...(activity.backgroundJobCount > 0 ? { backgroundJobMayRemain: true } : {}),
|
|
374
487
|
...(beforeCursorRunning > 0 ? { beforeCursorRunning } : {}),
|
|
375
488
|
targets: [],
|
|
376
489
|
remaining: running.length,
|
|
377
490
|
aborted,
|
|
378
491
|
...(reason ? { reason } : {}),
|
|
379
|
-
})
|
|
492
|
+
});
|
|
380
493
|
const finish = (content: string, output: CancelDescendantsOutput) =>
|
|
381
494
|
found.complete
|
|
382
495
|
? respond(content + cursorNote, output)
|
|
383
496
|
: respond(content + cursorNote + warning, {
|
|
384
497
|
...output,
|
|
385
498
|
warning: warningMessage,
|
|
386
|
-
})
|
|
499
|
+
});
|
|
387
500
|
const abortBeforeInterrupts = () =>
|
|
388
501
|
finish(
|
|
389
502
|
`Aborted before any interrupt was requested.${reasonText}`,
|
|
390
503
|
emptyOutput(true),
|
|
391
|
-
)
|
|
504
|
+
);
|
|
392
505
|
|
|
393
|
-
if (context?.signal?.aborted) return abortBeforeInterrupts()
|
|
506
|
+
if (context?.signal?.aborted) return abortBeforeInterrupts();
|
|
394
507
|
|
|
395
508
|
if (known.length === 0) {
|
|
396
509
|
return finish(
|
|
397
510
|
`Nothing to stop for ${caller}: no subagents discovered. ` +
|
|
398
511
|
`Run ${config.namespace}_${TOOL.list} to see discovered sessions.${reasonText}`,
|
|
399
512
|
emptyOutput(),
|
|
400
|
-
)
|
|
513
|
+
);
|
|
401
514
|
}
|
|
402
515
|
|
|
403
516
|
if (afterCursor.length === 0) {
|
|
404
517
|
return finish(
|
|
405
518
|
`Cursor is past the end: ${known.length} known subagent(s).${reasonText}`,
|
|
406
519
|
emptyOutput(),
|
|
407
|
-
)
|
|
520
|
+
);
|
|
408
521
|
}
|
|
409
522
|
|
|
410
523
|
if (running.length === 0) {
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
524
|
+
const inactive = activity.subtreeActiveCount === 0;
|
|
525
|
+
const text = inactive
|
|
526
|
+
? `No running subagents among ${afterCursor.length} known${cursor ? " after the cursor" : ""}.`
|
|
527
|
+
: summarizeCancelDescendants([], 0, undefined, {
|
|
528
|
+
...activity,
|
|
529
|
+
backgroundJobMayRemain: activity.backgroundJobCount > 0,
|
|
530
|
+
});
|
|
531
|
+
return finish(text + reasonText, emptyOutput());
|
|
415
532
|
}
|
|
416
533
|
|
|
417
|
-
const targets = running.slice(0, limits.maxCancelBatch)
|
|
418
|
-
if (context?.signal?.aborted) return abortBeforeInterrupts()
|
|
534
|
+
const targets = running.slice(0, limits.maxCancelBatch);
|
|
535
|
+
if (context?.signal?.aborted) return abortBeforeInterrupts();
|
|
419
536
|
|
|
420
537
|
// Evaluate the whole batch before interrupting anything: a denial
|
|
421
538
|
// must surface as a tool error without leaving a partial batch behind.
|
|
422
539
|
await guard(
|
|
423
540
|
targets.map((target) => guardTarget(target, target.id)),
|
|
424
541
|
context,
|
|
425
|
-
)
|
|
426
|
-
if (context?.signal?.aborted) return abortBeforeInterrupts()
|
|
542
|
+
);
|
|
543
|
+
if (context?.signal?.aborted) return abortBeforeInterrupts();
|
|
427
544
|
|
|
428
|
-
const results: CancelDescendantsTarget[] = []
|
|
429
|
-
let aborted = false
|
|
545
|
+
const results: CancelDescendantsTarget[] = [];
|
|
546
|
+
let aborted = false;
|
|
430
547
|
|
|
431
548
|
for (const target of targets) {
|
|
432
549
|
if (context?.signal?.aborted) {
|
|
433
|
-
aborted = true
|
|
434
|
-
break
|
|
550
|
+
aborted = true;
|
|
551
|
+
break;
|
|
435
552
|
}
|
|
436
553
|
|
|
437
|
-
let interrupt: CancelInterrupt
|
|
554
|
+
let interrupt: CancelInterrupt;
|
|
438
555
|
try {
|
|
439
556
|
const result = (await session.interrupt(
|
|
440
557
|
{ sessionID: target.id, resume: false },
|
|
441
558
|
{ signal: context?.signal },
|
|
442
|
-
)) as { interrupted?: boolean } | undefined
|
|
443
|
-
interrupt = interruptOutcome(result)
|
|
559
|
+
)) as { interrupted?: boolean } | undefined;
|
|
560
|
+
interrupt = interruptOutcome(result);
|
|
444
561
|
} catch (error) {
|
|
445
562
|
if (context?.signal?.aborted) {
|
|
446
563
|
results.push({
|
|
447
564
|
sessionID: target.id,
|
|
448
565
|
interrupt: CANCEL_INTERRUPT.unknown,
|
|
449
566
|
readBack: CANCEL_READ_BACK.notRead,
|
|
567
|
+
...backgroundFor(target),
|
|
450
568
|
error: "aborted during interrupt; outcome unknown",
|
|
451
|
-
})
|
|
452
|
-
aborted = true
|
|
453
|
-
break
|
|
569
|
+
});
|
|
570
|
+
aborted = true;
|
|
571
|
+
break;
|
|
454
572
|
}
|
|
455
573
|
results.push({
|
|
456
574
|
sessionID: target.id,
|
|
457
575
|
interrupt: CANCEL_INTERRUPT.failed,
|
|
458
576
|
readBack: CANCEL_READ_BACK.notRead,
|
|
577
|
+
...backgroundFor(target),
|
|
459
578
|
error: errorMessage(error),
|
|
460
|
-
})
|
|
461
|
-
continue
|
|
579
|
+
});
|
|
580
|
+
continue;
|
|
462
581
|
}
|
|
463
582
|
|
|
464
583
|
if (context?.signal?.aborted) {
|
|
@@ -466,62 +585,76 @@ export const registerCancelDescendantsTool = (
|
|
|
466
585
|
sessionID: target.id,
|
|
467
586
|
interrupt,
|
|
468
587
|
readBack: CANCEL_READ_BACK.notRead,
|
|
588
|
+
...backgroundFor(target),
|
|
469
589
|
error: "aborted before read-back; outcome unknown",
|
|
470
|
-
})
|
|
471
|
-
aborted = true
|
|
472
|
-
break
|
|
590
|
+
});
|
|
591
|
+
aborted = true;
|
|
592
|
+
break;
|
|
473
593
|
}
|
|
474
|
-
const { info: after, ok } = await access.getSession(target.id, context?.signal)
|
|
594
|
+
const { info: after, ok } = await access.getSession(target.id, context?.signal);
|
|
475
595
|
if (!ok || !after) {
|
|
476
596
|
if (context?.signal?.aborted) {
|
|
477
597
|
results.push({
|
|
478
598
|
sessionID: target.id,
|
|
479
599
|
interrupt,
|
|
480
600
|
readBack: CANCEL_READ_BACK.notRead,
|
|
601
|
+
...backgroundFor(target),
|
|
481
602
|
error: "aborted during read-back; outcome unknown",
|
|
482
|
-
})
|
|
483
|
-
aborted = true
|
|
484
|
-
break
|
|
603
|
+
});
|
|
604
|
+
aborted = true;
|
|
605
|
+
break;
|
|
485
606
|
}
|
|
486
607
|
results.push({
|
|
487
608
|
sessionID: target.id,
|
|
488
609
|
interrupt,
|
|
489
610
|
readBack: CANCEL_READ_BACK.failed,
|
|
611
|
+
...backgroundFor(target),
|
|
490
612
|
error: "read-back failed",
|
|
491
|
-
})
|
|
492
|
-
continue
|
|
613
|
+
});
|
|
614
|
+
continue;
|
|
493
615
|
}
|
|
494
616
|
results.push({
|
|
495
617
|
sessionID: target.id,
|
|
496
618
|
interrupt,
|
|
497
619
|
readBack: CANCEL_READ_BACK.ok,
|
|
498
620
|
status: live.statusFor(after, target.id),
|
|
499
|
-
|
|
621
|
+
...backgroundFor(target),
|
|
622
|
+
});
|
|
500
623
|
}
|
|
501
624
|
|
|
502
|
-
const remaining = Math.max(0, running.length - results.length)
|
|
625
|
+
const remaining = Math.max(0, running.length - results.length);
|
|
503
626
|
const nextCursor =
|
|
504
627
|
remaining > 0 && results.length > 0
|
|
505
628
|
? encodeCursor(targets[results.length - 1], {
|
|
506
629
|
tool: TOOL.cancelDescendants,
|
|
507
630
|
root: caller,
|
|
508
631
|
})
|
|
509
|
-
: undefined
|
|
632
|
+
: undefined;
|
|
633
|
+
const backgroundJobMayRemain =
|
|
634
|
+
activity.backgroundJobCount > 0 ||
|
|
635
|
+
results.some((target) => target.backgroundJob?.mayRemain === true);
|
|
510
636
|
const output: CancelDescendantsOutput = {
|
|
511
637
|
known: known.length,
|
|
512
638
|
considered: afterCursor.length,
|
|
513
639
|
running: running.length,
|
|
640
|
+
activeCount: activity.activeCount,
|
|
641
|
+
subtreeActiveCount: activity.subtreeActiveCount,
|
|
642
|
+
backgroundJobCount: activity.backgroundJobCount,
|
|
643
|
+
...(backgroundJobMayRemain ? { backgroundJobMayRemain: true } : {}),
|
|
514
644
|
...(beforeCursorRunning > 0 ? { beforeCursorRunning } : {}),
|
|
515
645
|
targets: results,
|
|
516
646
|
remaining,
|
|
517
647
|
aborted,
|
|
518
|
-
}
|
|
519
|
-
if (reason) output.reason = reason
|
|
520
|
-
if (nextCursor) output.cursor = nextCursor
|
|
521
|
-
|
|
522
|
-
const summary = summarizeCancelDescendants(results, remaining, nextCursor
|
|
523
|
-
|
|
524
|
-
|
|
648
|
+
};
|
|
649
|
+
if (reason) output.reason = reason;
|
|
650
|
+
if (nextCursor) output.cursor = nextCursor;
|
|
651
|
+
|
|
652
|
+
const summary = summarizeCancelDescendants(results, remaining, nextCursor, {
|
|
653
|
+
...activity,
|
|
654
|
+
backgroundJobMayRemain,
|
|
655
|
+
});
|
|
656
|
+
const abortText = aborted ? " Aborted before the batch completed." : "";
|
|
657
|
+
return finish(summary + reasonText + abortText, output);
|
|
525
658
|
},
|
|
526
|
-
})
|
|
527
|
-
}
|
|
659
|
+
});
|
|
660
|
+
};
|