@sshirkit/dsh-orchestrator 0.9.2 → 1.0.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/README.md +142 -93
- package/README.zh.md +144 -95
- package/docs/images/dispatch-panel.png +0 -0
- package/docs/images/hero-banner.png +0 -0
- package/lib/api.js +1 -0
- package/lib/catalog.js +2 -1
- package/lib/client.js +131 -44
- package/lib/delegate-status.js +125 -0
- package/lib/delegate.js +299 -59
- package/lib/gate.js +57 -15
- package/lib/git-audit.js +383 -0
- package/lib/index.js +1185 -173
- package/lib/orchestrate.js +313 -89
- package/lib/reconcile.js +590 -0
- package/lib/retry.js +68 -5
- package/lib/rules.js +15 -2
- package/lib/state-store.js +455 -0
- package/lib/supervise.js +593 -87
- package/lib/timeline.js +464 -0
- package/lib/tool-surface.js +69 -21
- package/lib/worktree-manage.js +244 -0
- package/lib/worktree-tool.js +38 -0
- package/lib/worktree.js +687 -0
- package/lib/write-audit.js +333 -0
- package/lib/write-lease.js +161 -7
- package/lib/write-scope-tool.js +29 -0
- package/lib/write-scope.js +14 -18
- package/package.json +3 -3
package/lib/client.js
CHANGED
|
@@ -14,7 +14,6 @@ window.__ModuleLoader__.load({
|
|
|
14
14
|
ReactDOM = null
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
/** Dictionary namespace owned by this plugin. */
|
|
18
17
|
const NS = 'orchestrator'
|
|
19
18
|
|
|
20
19
|
const zh = {
|
|
@@ -37,11 +36,15 @@ window.__ModuleLoader__.load({
|
|
|
37
36
|
'route.enable': '启用调度模式',
|
|
38
37
|
'route.enableNote': '开启后主代理只做规划、由子代理执行;已打开的会话也会立即生效。',
|
|
39
38
|
'route.enableFailed': '切换失败',
|
|
39
|
+
'worktreeIsolation.label': '默认启用 Git Worktree 隔离',
|
|
40
|
+
'worktreeIsolation.hint': 'Git 仓库中的写任务默认使用独立 Worktree,并用 git diff / git log 审计。delegate 按任务、orchestrate 按条目隔离;非 Git 目录自动使用逻辑快照审计。',
|
|
40
41
|
'allowed.title': 'Allowed Tools',
|
|
41
42
|
'allowed.search': '搜索工具…',
|
|
42
43
|
'allowed.empty': '没有匹配的工具',
|
|
43
44
|
'allowed.reset': '重置',
|
|
44
45
|
'allowed.hint': '白名单式驱动:勾选 = 允许主代理使用;未勾选(含新插件工具)= 自动禁用。',
|
|
46
|
+
'allowed.nativeBlocked': '按 ADR-0004 默认禁用;建议改用 delegate / orchestrate。显式勾选可覆盖。',
|
|
47
|
+
'allowed.teamDisabled': '当前配置已关闭原生 Team 共享任务板(allowAgentTeam: false)。',
|
|
45
48
|
'supervise.label': '子代理巡检间隔(秒)',
|
|
46
49
|
'supervise.hint': '默认 600 秒(10 分钟)。主代理会按此间隔检查运行中的子代理,运行中修改立即生效。范围 10–86400。',
|
|
47
50
|
'supervise.invalid': '请输入 10–86400 之间的整数',
|
|
@@ -72,11 +75,15 @@ window.__ModuleLoader__.load({
|
|
|
72
75
|
'route.enable': 'Enable dispatch mode',
|
|
73
76
|
'route.enableNote': 'The main agent only plans and delegates the work; an open conversation follows the switch immediately.',
|
|
74
77
|
'route.enableFailed': 'Could not switch',
|
|
78
|
+
'worktreeIsolation.label': 'Enable Git Worktree isolation by default',
|
|
79
|
+
'worktreeIsolation.hint': 'Write tasks in Git repositories use separate worktrees with git diff / git log audits: one per delegate task or orchestrate item. Non-Git directories fall back to snapshot audits.',
|
|
75
80
|
'allowed.title': 'Allowed Tools',
|
|
76
81
|
'allowed.search': 'Filter tools…',
|
|
77
82
|
'allowed.empty': 'No matching tools',
|
|
78
83
|
'allowed.reset': 'Reset',
|
|
79
84
|
'allowed.hint': 'Whitelist-driven: checked = allowed for the main agent; unchecked (including new plugin tools) = blocked automatically.',
|
|
85
|
+
'allowed.nativeBlocked': 'Disabled by default under ADR-0004; prefer delegate / orchestrate. Check explicitly to override.',
|
|
86
|
+
'allowed.teamDisabled': 'The native Team task board is disabled by configuration (allowAgentTeam: false).',
|
|
80
87
|
'supervise.label': 'Subagent check interval (seconds)',
|
|
81
88
|
'supervise.hint': 'Default 600 seconds (10 minutes). The main agent checks running subagents at this interval; changes apply immediately. Range 10–86400.',
|
|
82
89
|
'supervise.invalid': 'Enter an integer between 10 and 86400',
|
|
@@ -121,7 +128,7 @@ window.__ModuleLoader__.load({
|
|
|
121
128
|
fields: { provider: 'provider', model: 'model', effort: 'reasoningEffort', clear: 'unset' },
|
|
122
129
|
}
|
|
123
130
|
const WORKER = {
|
|
124
|
-
ns:
|
|
131
|
+
ns: NS,
|
|
125
132
|
fields: { provider: 'workerProvider', model: 'workerModel', effort: 'workerReasoningEffort', clear: 'empty' },
|
|
126
133
|
}
|
|
127
134
|
|
|
@@ -281,8 +288,8 @@ window.__ModuleLoader__.load({
|
|
|
281
288
|
}
|
|
282
289
|
|
|
283
290
|
// ---- Allowed tools --------------------------------------------------------------------
|
|
284
|
-
//
|
|
285
|
-
//
|
|
291
|
+
// The base whitelist mirrors DEFAULT_ALLOWED_TOOLS in lib/tool-surface.js. ADR-0004's
|
|
292
|
+
// optional Team tools and default-blocked native tools mirror lib/gate.js.
|
|
286
293
|
// A web client plugin has no API that enumerates host tools — the session remote offers
|
|
287
294
|
// session/model/skill methods only, and configForms exposes this plugin's own config — so
|
|
288
295
|
// the plugin's server half collects the live catalog and publishes it as the volatile
|
|
@@ -293,21 +300,24 @@ window.__ModuleLoader__.load({
|
|
|
293
300
|
const DEFAULT_ALLOWED_TOOLS = [
|
|
294
301
|
'delegate',
|
|
295
302
|
'orchestrate',
|
|
303
|
+
'delegate_status',
|
|
304
|
+
'worktree_manage',
|
|
296
305
|
'send_message',
|
|
297
306
|
'interrupt_agent',
|
|
298
307
|
'todo_write',
|
|
299
308
|
'ask_user_question',
|
|
300
309
|
'compress',
|
|
301
|
-
'session_search',
|
|
302
|
-
'session_trace',
|
|
303
|
-
'session_event_search',
|
|
304
|
-
'session_event_read',
|
|
305
|
-
'session_event_trace',
|
|
306
310
|
]
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
311
|
+
const NATIVE_TEAM_TOOLS = [
|
|
312
|
+
'team_task_create',
|
|
313
|
+
'team_task_list',
|
|
314
|
+
'team_task_get',
|
|
315
|
+
'team_task_update',
|
|
316
|
+
]
|
|
317
|
+
const DEFAULT_BLOCKED_NATIVE_TOOLS = ['subagent_fork', 'workflow']
|
|
318
|
+
/** The whitelist key the panel reads and writes, and the Team compatibility switch it reads. */
|
|
310
319
|
const ALLOW_KEY = 'allowedTools'
|
|
320
|
+
const ALLOW_AGENT_TEAM_KEY = 'allowAgentTeam'
|
|
311
321
|
/** The read-only config field the host publishes the live catalog into. */
|
|
312
322
|
const CATALOG_KEY = 'toolCatalog'
|
|
313
323
|
|
|
@@ -338,18 +348,20 @@ window.__ModuleLoader__.load({
|
|
|
338
348
|
return live.length > 0 ? live : remembered ?? []
|
|
339
349
|
}
|
|
340
350
|
|
|
351
|
+
function shippedToolsOf(catalog, allowAgentTeam = true) {
|
|
352
|
+
const team = allowAgentTeam === false ? [] : NATIVE_TEAM_TOOLS.filter((name) => catalog.includes(name))
|
|
353
|
+
return [...DEFAULT_ALLOWED_TOOLS, ...team]
|
|
354
|
+
}
|
|
355
|
+
|
|
341
356
|
/**
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
* when the shipped list does not know it, and starts unchecked — a tool the user never
|
|
345
|
-
* allowed, including one a plugin or MCP server registered later, stays blocked for the
|
|
346
|
-
* main agent until it is checked here.
|
|
357
|
+
* The base whitelist and registered Team support come first. Other catalog or profile
|
|
358
|
+
* names are listed alphabetically, including native tools a profile can opt in to.
|
|
347
359
|
*/
|
|
348
|
-
function toolListOf(value, catalog) {
|
|
349
|
-
const listed =
|
|
360
|
+
function toolListOf(value, catalog = catalogOf(value)) {
|
|
361
|
+
const listed = shippedToolsOf(catalog)
|
|
350
362
|
const seen = new Set(listed)
|
|
351
363
|
const extras = new Set()
|
|
352
|
-
for (const name of catalog
|
|
364
|
+
for (const name of catalog) {
|
|
353
365
|
if (seen.has(name)) continue
|
|
354
366
|
extras.add(name)
|
|
355
367
|
}
|
|
@@ -361,13 +373,15 @@ window.__ModuleLoader__.load({
|
|
|
361
373
|
}
|
|
362
374
|
|
|
363
375
|
/**
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
367
|
-
* stay distinct: `[]` means "allow nothing", `undefined` means "ship defaults".
|
|
376
|
+
* Mirrors the host's resolveEffectiveAllowedTools() for the settings catalog. An
|
|
377
|
+
* explicit array replaces the defaults (`[]` included); the Team switch can suppress
|
|
378
|
+
* Team names even when they were explicitly stored.
|
|
368
379
|
*/
|
|
369
|
-
function allowedSetOf(value) {
|
|
370
|
-
|
|
380
|
+
function allowedSetOf(value, catalog = catalogOf(value)) {
|
|
381
|
+
const allowAgentTeam = value?.[ALLOW_AGENT_TEAM_KEY]
|
|
382
|
+
if (!Array.isArray(value?.[ALLOW_KEY])) return new Set(shippedToolsOf(catalog, allowAgentTeam))
|
|
383
|
+
const names = namesOf(value, ALLOW_KEY)
|
|
384
|
+
return new Set(allowAgentTeam === false ? names.filter((name) => !NATIVE_TEAM_TOOLS.includes(name)) : names)
|
|
371
385
|
}
|
|
372
386
|
|
|
373
387
|
/** Case-insensitive substring match on the tool name; an empty query keeps every row. */
|
|
@@ -378,15 +392,20 @@ window.__ModuleLoader__.load({
|
|
|
378
392
|
}
|
|
379
393
|
|
|
380
394
|
/**
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
* schema default instead of pinning a copy of this build's list; anything else — extra
|
|
384
|
-
* names, a removed name, or an emptied whitelist — is stored verbatim.
|
|
395
|
+
* A set equal to the current defaults clears the override. While Team is switched
|
|
396
|
+
* off, changing another checkbox must not discard a stored Team preference.
|
|
385
397
|
*/
|
|
386
|
-
function allowOps(checked) {
|
|
387
|
-
const
|
|
388
|
-
const
|
|
389
|
-
|
|
398
|
+
function allowOps(checked, value, catalog = catalogOf(value)) {
|
|
399
|
+
const allowAgentTeam = value?.[ALLOW_AGENT_TEAM_KEY]
|
|
400
|
+
const next = new Set(checked)
|
|
401
|
+
if (allowAgentTeam === false) {
|
|
402
|
+
for (const name of namesOf(value, ALLOW_KEY)) {
|
|
403
|
+
if (NATIVE_TEAM_TOOLS.includes(name)) next.add(name)
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const list = [...next]
|
|
407
|
+
const defaults = shippedToolsOf(catalog, allowAgentTeam)
|
|
408
|
+
const isDefault = list.length === defaults.length && defaults.every((name) => next.has(name))
|
|
390
409
|
return [isDefault ? { op: 'unset', path: [ALLOW_KEY] } : { op: 'set', path: [ALLOW_KEY], value: list }]
|
|
391
410
|
}
|
|
392
411
|
|
|
@@ -1096,6 +1115,64 @@ window.__ModuleLoader__.load({
|
|
|
1096
1115
|
)
|
|
1097
1116
|
}
|
|
1098
1117
|
|
|
1118
|
+
function worktreeIsolationOf(config) {
|
|
1119
|
+
return config?.worktreeIsolationEnabled ?? true
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
/** Toggle the default for future write dispatches without pinning any tool preferences. */
|
|
1123
|
+
function WorktreeIsolationField({ scope, t }) {
|
|
1124
|
+
const snapshot = useScope(scope)
|
|
1125
|
+
const checked = worktreeIsolationOf(snapshot.value)
|
|
1126
|
+
const editable = snapshot.status === 'ready' && snapshot.writable === true
|
|
1127
|
+
const [busy, setBusy] = React.useState(false)
|
|
1128
|
+
const [problem, setProblem] = React.useState(null)
|
|
1129
|
+
const alive = React.useRef(true)
|
|
1130
|
+
React.useEffect(() => {
|
|
1131
|
+
alive.current = true
|
|
1132
|
+
return () => {
|
|
1133
|
+
alive.current = false
|
|
1134
|
+
}
|
|
1135
|
+
}, [])
|
|
1136
|
+
|
|
1137
|
+
const toggle = async (on) => {
|
|
1138
|
+
if (busy) return
|
|
1139
|
+
const live = scope.getSnapshot()
|
|
1140
|
+
if (live.status !== 'ready' || live.writable !== true) {
|
|
1141
|
+
setProblem(t('route.readonly'))
|
|
1142
|
+
return
|
|
1143
|
+
}
|
|
1144
|
+
setBusy(true)
|
|
1145
|
+
setProblem(null)
|
|
1146
|
+
try {
|
|
1147
|
+
await scope.mutate([{ op: 'set', path: ['worktreeIsolationEnabled'], value: on }], live.revision)
|
|
1148
|
+
if (alive.current && worktreeIsolationOf(scope.getSnapshot().value) !== on) {
|
|
1149
|
+
setProblem(t('route.enableFailed'))
|
|
1150
|
+
}
|
|
1151
|
+
} catch {
|
|
1152
|
+
if (alive.current) setProblem(t('route.enableFailed'))
|
|
1153
|
+
} finally {
|
|
1154
|
+
if (alive.current) setBusy(false)
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
return h(
|
|
1159
|
+
'div',
|
|
1160
|
+
{ style: { display: 'grid', gap: '4px' } },
|
|
1161
|
+
h(
|
|
1162
|
+
'div',
|
|
1163
|
+
{ style: { display: 'flex', alignItems: 'center', gap: '10px' } },
|
|
1164
|
+
h(
|
|
1165
|
+
'div',
|
|
1166
|
+
{ style: { display: 'grid', gap: '2px', flex: '1 1 auto', minWidth: 0 } },
|
|
1167
|
+
h('span', { style: { fontWeight: 600 } }, t('worktreeIsolation.label')),
|
|
1168
|
+
h('span', { style: { color: 'var(--dsw-alias-label-tertiary, inherit)', lineHeight: 1.3 } }, t('worktreeIsolation.hint')),
|
|
1169
|
+
),
|
|
1170
|
+
h(EnableSwitch, { checked, disabled: busy || !editable, onChange: toggle, label: t('worktreeIsolation.label') }),
|
|
1171
|
+
),
|
|
1172
|
+
problem === null ? null : h('div', { role: 'alert', style: { color: 'var(--dsw-alias-label-error, #ff7b72)' } }, problem),
|
|
1173
|
+
)
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1099
1176
|
/**
|
|
1100
1177
|
* The right-hand column: one checkbox per tool the host offers, searched by name.
|
|
1101
1178
|
* Whitelist-driven — checked means the main agent may use the tool, and everything
|
|
@@ -1123,7 +1200,8 @@ window.__ModuleLoader__.load({
|
|
|
1123
1200
|
const live = catalogFor(snapshot.value, remembered.current)
|
|
1124
1201
|
if (live.length > 0) remembered.current = live
|
|
1125
1202
|
const names = toolListOf(snapshot.value, live)
|
|
1126
|
-
const allowed = allowedSetOf(snapshot.value)
|
|
1203
|
+
const allowed = allowedSetOf(snapshot.value, live)
|
|
1204
|
+
const teamEnabled = snapshot.value?.[ALLOW_AGENT_TEAM_KEY] !== false
|
|
1127
1205
|
const visible = filterTools(names, query)
|
|
1128
1206
|
const editable = snapshot.status === 'ready' && snapshot.writable === true
|
|
1129
1207
|
|
|
@@ -1151,7 +1229,7 @@ window.__ModuleLoader__.load({
|
|
|
1151
1229
|
const next = new Set(allowed)
|
|
1152
1230
|
if (on) next.add(name)
|
|
1153
1231
|
else next.delete(name)
|
|
1154
|
-
return write(allowOps(next))
|
|
1232
|
+
return write(allowOps(next, snapshot.value, live))
|
|
1155
1233
|
}
|
|
1156
1234
|
|
|
1157
1235
|
return h(
|
|
@@ -1195,21 +1273,26 @@ window.__ModuleLoader__.load({
|
|
|
1195
1273
|
{ style: { padding: '6px 8px', color: 'var(--dsw-alias-label-tertiary, inherit)', lineHeight: 1.4 } },
|
|
1196
1274
|
t('allowed.empty'),
|
|
1197
1275
|
)
|
|
1198
|
-
: visible.map((name) =>
|
|
1199
|
-
|
|
1276
|
+
: visible.map((name) => {
|
|
1277
|
+
const teamDisabled = !teamEnabled && NATIVE_TEAM_TOOLS.includes(name)
|
|
1278
|
+
const disabled = saving || !editable || teamDisabled
|
|
1279
|
+
const hint = teamDisabled
|
|
1280
|
+
? t('allowed.teamDisabled')
|
|
1281
|
+
: DEFAULT_BLOCKED_NATIVE_TOOLS.includes(name) ? t('allowed.nativeBlocked') : undefined
|
|
1282
|
+
return h(
|
|
1200
1283
|
'label',
|
|
1201
1284
|
{ key: name, style: allowedRowStyle(allowed.has(name)), 'data-tool': name },
|
|
1202
1285
|
h('input', {
|
|
1203
1286
|
type: 'checkbox',
|
|
1204
1287
|
checked: allowed.has(name),
|
|
1205
|
-
disabled
|
|
1288
|
+
disabled,
|
|
1206
1289
|
'aria-label': name,
|
|
1207
|
-
style: allowedCheckboxStyle(!
|
|
1290
|
+
style: allowedCheckboxStyle(!disabled),
|
|
1208
1291
|
onChange: (event) => toggle(name, event.target.checked),
|
|
1209
1292
|
}),
|
|
1210
|
-
h('span', { style: allowedNameStyle, title: name }, name),
|
|
1211
|
-
)
|
|
1212
|
-
),
|
|
1293
|
+
h('span', { style: allowedNameStyle, title: hint === undefined ? name : `${name}: ${hint}` }, name),
|
|
1294
|
+
)
|
|
1295
|
+
}),
|
|
1213
1296
|
),
|
|
1214
1297
|
),
|
|
1215
1298
|
problem === null ? null : h('div', { role: 'alert', style: { color: 'var(--dsw-alias-label-error, #ff7b72)' } }, problem),
|
|
@@ -1621,6 +1704,7 @@ window.__ModuleLoader__.load({
|
|
|
1621
1704
|
label: t('route.enable'),
|
|
1622
1705
|
}),
|
|
1623
1706
|
),
|
|
1707
|
+
h(WorktreeIsolationField, { scope: scopes.orch, t }),
|
|
1624
1708
|
showLoading
|
|
1625
1709
|
? h('div', null, t('route.loading'))
|
|
1626
1710
|
: groups === undefined
|
|
@@ -1664,7 +1748,6 @@ window.__ModuleLoader__.load({
|
|
|
1664
1748
|
|
|
1665
1749
|
function apply(ctx) {
|
|
1666
1750
|
ctx.effect(() => ctx.locale.register(NS, { zh, en }), 'orchestrator: dictionaries')
|
|
1667
|
-
|
|
1668
1751
|
const scopes = { main: ctx.configForms.get(MAIN.ns), worker: ctx.configForms.get(WORKER.ns), orch: ctx.configForms.get(NS) }
|
|
1669
1752
|
ctx.slots.inject('conversation.input.right', () =>
|
|
1670
1753
|
ctx.slots.register(
|
|
@@ -1695,7 +1778,10 @@ window.__ModuleLoader__.load({
|
|
|
1695
1778
|
WORKER,
|
|
1696
1779
|
INTERVAL,
|
|
1697
1780
|
DEFAULT_ALLOWED_TOOLS,
|
|
1781
|
+
NATIVE_TEAM_TOOLS,
|
|
1782
|
+
DEFAULT_BLOCKED_NATIVE_TOOLS,
|
|
1698
1783
|
ALLOW_KEY,
|
|
1784
|
+
ALLOW_AGENT_TEAM_KEY,
|
|
1699
1785
|
CATALOG_KEY,
|
|
1700
1786
|
namesOf,
|
|
1701
1787
|
catalogOf,
|
|
@@ -1717,6 +1803,7 @@ window.__ModuleLoader__.load({
|
|
|
1717
1803
|
allowOps,
|
|
1718
1804
|
resetAllowOps,
|
|
1719
1805
|
EnableSwitch,
|
|
1806
|
+
WorktreeIsolationField,
|
|
1720
1807
|
AllowedToolsPanel,
|
|
1721
1808
|
RoutePicker,
|
|
1722
1809
|
routeKey,
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { formatInlineAuditNotice } from './timeline.js'
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_DELEGATE_STATUS_TOOL_NAME = 'delegate_status'
|
|
4
|
+
|
|
5
|
+
const DEFAULT_LIMIT = 20
|
|
6
|
+
const MAX_LIMIT = 64
|
|
7
|
+
|
|
8
|
+
function renderStatus({ total, counts, items }) {
|
|
9
|
+
const lines = [
|
|
10
|
+
`委派验收状态:共 ${total} 条,本次显示 ${items.length} 条。`,
|
|
11
|
+
`运行中 ${counts.running};已结束待审计 ${counts.endedPendingAudit};审计通过 ${counts.auditPassed};` +
|
|
12
|
+
`审计失败 ${counts.auditFailed};未纳入审计 ${counts.auditUnmanaged}。`,
|
|
13
|
+
`工作树待评审 ${counts.worktreePendingReview}。`,
|
|
14
|
+
]
|
|
15
|
+
if (items.length === 0) {
|
|
16
|
+
lines.push('当前会话没有符合条件的委派记录;缺少记录不代表验收通过。')
|
|
17
|
+
}
|
|
18
|
+
for (const entry of items) {
|
|
19
|
+
const { childId, agentName, label, dispatchState, auditState, leaseState, baselineId, worktree } = entry
|
|
20
|
+
const delivery = worktree ? ` [wt:${worktree.id} ${worktree.state}]` : ''
|
|
21
|
+
const address = agentName ? `(${agentName})` : ''
|
|
22
|
+
lines.push(`【${childId}】${address}${label ?? '未命名任务'}:执行 ${dispatchState};审计 ${auditState};` +
|
|
23
|
+
`租约 ${leaseState};基线 ${baselineId ?? '无'}。${delivery}`)
|
|
24
|
+
if (auditState === 'failed') {
|
|
25
|
+
lines.push(formatInlineAuditNotice([{ childId, entry }]))
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return [{ type: 'text', text: lines.join('\n') }]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function summarizeWorktree(worktree) {
|
|
32
|
+
if (worktree == null) return null
|
|
33
|
+
const { id, branch, state, commits, dirty, locked, path } = worktree
|
|
34
|
+
return { id, branch, state, commits, dirty, locked, path }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function createDelegateStatusTool({
|
|
38
|
+
timeline,
|
|
39
|
+
toolName = DEFAULT_DELEGATE_STATUS_TOOL_NAME,
|
|
40
|
+
isRootAgent,
|
|
41
|
+
defineTool = (definition) => definition,
|
|
42
|
+
} = {}) {
|
|
43
|
+
return defineTool({
|
|
44
|
+
name: toolName,
|
|
45
|
+
description: '只读查询当前根代理会话直接委派的子代理执行、写作用域审计、租约及工作树交付状态;验收前用于核验终态。',
|
|
46
|
+
parameters: {
|
|
47
|
+
childId: { type: 'string', description: '可选:只查询当前会话下此 ID 对应的子代理。' },
|
|
48
|
+
includeCompleted: { type: 'boolean', default: true, description: '是否包含执行已结束的子代理。' },
|
|
49
|
+
limit: {
|
|
50
|
+
type: 'integer',
|
|
51
|
+
// The host author schema supports enum, not minimum/maximum.
|
|
52
|
+
enum: Array.from({ length: MAX_LIMIT }, (_, index) => index + 1),
|
|
53
|
+
default: DEFAULT_LIMIT,
|
|
54
|
+
description: `最多返回条数(1..${MAX_LIMIT},默认 ${DEFAULT_LIMIT})。`,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
output: {
|
|
58
|
+
schema: {
|
|
59
|
+
type: 'object',
|
|
60
|
+
additionalProperties: false,
|
|
61
|
+
properties: {
|
|
62
|
+
content: {
|
|
63
|
+
type: 'array',
|
|
64
|
+
items: {
|
|
65
|
+
type: 'object',
|
|
66
|
+
additionalProperties: false,
|
|
67
|
+
properties: { type: { type: 'string', const: 'text' }, text: { type: 'string' } },
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
details: {
|
|
71
|
+
type: 'object',
|
|
72
|
+
additionalProperties: false,
|
|
73
|
+
properties: {
|
|
74
|
+
parentId: { type: 'string' },
|
|
75
|
+
total: { type: 'integer' },
|
|
76
|
+
counts: {
|
|
77
|
+
type: 'object',
|
|
78
|
+
additionalProperties: false,
|
|
79
|
+
properties: {
|
|
80
|
+
running: { type: 'integer' },
|
|
81
|
+
endedPendingAudit: { type: 'integer' },
|
|
82
|
+
auditPassed: { type: 'integer' },
|
|
83
|
+
auditFailed: { type: 'integer' },
|
|
84
|
+
auditUnmanaged: { type: 'integer' },
|
|
85
|
+
worktreePendingReview: { type: 'integer' },
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
items: { type: 'array', items: { type: 'object', additionalProperties: true } },
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
render: (_args, value) => value.content,
|
|
94
|
+
},
|
|
95
|
+
isConcurrencySafe: () => true,
|
|
96
|
+
async execute(args, exec) {
|
|
97
|
+
const parent = exec?.agent
|
|
98
|
+
// The delegate path keys its records by exec.agent.id, not by a caller-supplied id.
|
|
99
|
+
const parentId = parent?.id
|
|
100
|
+
if (typeof parentId !== 'string' || parentId.trim() === '') {
|
|
101
|
+
throw new Error(`${toolName} 无法识别调用代理的会话 ID,拒绝查询。`)
|
|
102
|
+
}
|
|
103
|
+
if (isRootAgent !== undefined && (typeof isRootAgent !== 'function' || !isRootAgent(parent))) {
|
|
104
|
+
throw new Error(`${toolName} 仅限根代理调用,子代理不能查询委派验收状态。`)
|
|
105
|
+
}
|
|
106
|
+
if (Object.hasOwn(args ?? {}, 'parentId')) {
|
|
107
|
+
throw new Error(`${toolName} 不接受 parentId 参数,只能查询调用方自己的委派记录。`)
|
|
108
|
+
}
|
|
109
|
+
const { childId, includeCompleted = true, limit = DEFAULT_LIMIT } = args ?? {}
|
|
110
|
+
if (childId !== undefined && (typeof childId !== 'string' || childId.trim() === '')) {
|
|
111
|
+
throw new Error('childId 必须是非空字符串。')
|
|
112
|
+
}
|
|
113
|
+
if (typeof includeCompleted !== 'boolean') throw new Error('includeCompleted 必须是布尔值。')
|
|
114
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > MAX_LIMIT) {
|
|
115
|
+
throw new Error(`limit 必须是 1..${MAX_LIMIT} 之间的整数。`)
|
|
116
|
+
}
|
|
117
|
+
const report = timeline.queryParent(parentId, { childId, includeCompleted, limit })
|
|
118
|
+
const details = {
|
|
119
|
+
...report,
|
|
120
|
+
items: report.items.map((entry) => ({ ...entry, worktree: summarizeWorktree(entry.worktree) })),
|
|
121
|
+
}
|
|
122
|
+
return { content: renderStatus(report), details }
|
|
123
|
+
},
|
|
124
|
+
})
|
|
125
|
+
}
|