@sshirkit/dsh-orchestrator 1.0.2 → 1.0.3
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 +44 -23
- package/README.zh.md +42 -22
- package/lib/client.js +657 -384
- package/lib/delegate-status.js +42 -67
- package/lib/delegate.js +69 -127
- package/lib/gate.js +9 -3
- package/lib/index.js +500 -432
- package/lib/orchestrate.js +91 -109
- package/lib/read-index.js +186 -0
- package/lib/reconcile.js +2 -3
- package/lib/rules.js +11 -10
- package/lib/session-tools.js +194 -22
- package/lib/state-store.js +14 -9
- package/lib/supervise.js +69 -85
- package/lib/switch-notice.js +2 -2
- package/lib/timeline.js +23 -181
- package/lib/tool-surface.js +33 -7
- package/lib/worker.js +18 -7
- package/lib/worktree-manage.js +3 -10
- package/lib/worktree.js +3 -12
- package/lib/write-lease.js +57 -12
- package/package.json +1 -1
- package/lib/git-audit.js +0 -383
- package/lib/write-audit.js +0 -333
package/lib/timeline.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
export const INLINE_AUDIT_SOURCE_KIND = 'orchestrator-write-audit-inline'
|
|
2
|
-
|
|
3
|
-
const DEFAULT_ISOLATION = 'logical-post-audit'
|
|
4
|
-
const WORKTREE_ISOLATION = 'git-worktree'
|
|
5
1
|
const WORKTREE_STATES = new Set([
|
|
6
2
|
'active', 'ready_for_review', 'merged', 'discarded', 'merge_conflict', 'cleanup_failed',
|
|
7
3
|
])
|
|
@@ -12,32 +8,10 @@ const idOrNull = (value) => isId(value) ? value : null
|
|
|
12
8
|
const textOrNull = (value) => typeof value === 'string' ? value : null
|
|
13
9
|
const snapshot = (entry) => structuredClone(entry)
|
|
14
10
|
|
|
15
|
-
function copyMetadata(value) {
|
|
16
|
-
if (typeof value === 'string') return value
|
|
17
|
-
if (value == null || typeof value !== 'object') return null
|
|
18
|
-
try {
|
|
19
|
-
return JSON.parse(JSON.stringify(value))
|
|
20
|
-
} catch {
|
|
21
|
-
return null
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
11
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (typeof value?.[key] === 'string' || value?.[key] === null) result[key] = value[key]
|
|
29
|
-
}
|
|
30
|
-
if (value?.detail !== undefined) result.detail = copyMetadata(value.detail)
|
|
31
|
-
return result
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const isCompleted = (entry) => entry.dispatchState === 'ended' && entry.auditState !== 'pending'
|
|
35
|
-
const isEvictable = (entry) => isCompleted(entry) &&
|
|
36
|
-
(entry.auditState !== 'failed' || Boolean(entry.auditDeliveredVia)) &&
|
|
37
|
-
!OPEN_WORKTREE_STATES.has(entry.worktree?.state)
|
|
38
|
-
const needsAttention = (entry) => entry.dispatchState === 'ended' &&
|
|
39
|
-
(entry.auditState === 'pending' || (entry.auditState === 'failed' && !entry.auditDeliveredVia))
|
|
40
|
-
const queryPriority = (entry) => isCompleted(entry) && entry.auditState !== 'failed' ? 1 : 0
|
|
12
|
+
const isCompleted = (entry) => entry.dispatchState === 'ended'
|
|
13
|
+
const isEvictable = (entry) => isCompleted(entry) && !entry.pendingNextTurn && !OPEN_WORKTREE_STATES.has(entry.worktree?.state)
|
|
14
|
+
const queryPriority = (entry) => isCompleted(entry) ? 1 : 0
|
|
41
15
|
const newestFirst = (left, right) => right.updatedAt - left.updatedAt
|
|
42
16
|
|
|
43
17
|
function initialWorktree(worktree) {
|
|
@@ -46,7 +20,7 @@ function initialWorktree(worktree) {
|
|
|
46
20
|
return {
|
|
47
21
|
id: idOrNull(id), path: textOrNull(path ?? worktree.worktreePath), execCwd: textOrNull(execCwd),
|
|
48
22
|
branch: textOrNull(branch), baseCommit: idOrNull(baseCommit),
|
|
49
|
-
state: 'active',
|
|
23
|
+
state: 'active', mergeCommit: null, conflictFiles: [], locked: false,
|
|
50
24
|
}
|
|
51
25
|
}
|
|
52
26
|
|
|
@@ -73,28 +47,19 @@ const countOr = (value, fallback) => Number.isSafeInteger(value) && value >= 0 ?
|
|
|
73
47
|
const timeOrNull = (value) => Number.isFinite(value) ? value : null
|
|
74
48
|
const textList = (value) => Array.isArray(value) ? value.filter((item) => typeof item === 'string') : []
|
|
75
49
|
|
|
76
|
-
function initialAttempt({ readOnly,
|
|
50
|
+
function initialAttempt({ readOnly, leaseId, startedAt }) {
|
|
77
51
|
return {
|
|
78
52
|
readOnly: readOnly === true,
|
|
79
53
|
runId: null,
|
|
80
|
-
baselineId: idOrNull(baselineId),
|
|
81
54
|
dispatchState: 'running',
|
|
82
|
-
auditState: isId(baselineId) ? 'pending' : 'unmanaged',
|
|
83
55
|
leaseState: readOnly !== true && isId(leaseId) ? 'held' : 'none',
|
|
84
56
|
leaseId: idOrNull(leaseId),
|
|
85
57
|
startedAt,
|
|
86
58
|
endedAt: null,
|
|
87
59
|
disposedAt: null,
|
|
88
|
-
auditedAt: null,
|
|
89
60
|
releasedAt: null,
|
|
90
61
|
updatedAt: startedAt,
|
|
91
62
|
stopReason: null,
|
|
92
|
-
violations: [],
|
|
93
|
-
truncatedViolations: 0,
|
|
94
|
-
incomplete: false,
|
|
95
|
-
isolation: worktree ? WORKTREE_ISOLATION : isId(baselineId) ? DEFAULT_ISOLATION : null,
|
|
96
|
-
auditSummary: null,
|
|
97
|
-
auditDeliveredVia: null,
|
|
98
63
|
}
|
|
99
64
|
}
|
|
100
65
|
|
|
@@ -102,13 +67,11 @@ function initialAttempt({ readOnly, baselineId, leaseId, startedAt, worktree })
|
|
|
102
67
|
export function createDispatchTimeline({
|
|
103
68
|
maxPerParent = 64,
|
|
104
69
|
maxParents = 64,
|
|
105
|
-
maxViolations = 32,
|
|
106
70
|
now = () => Date.now(),
|
|
107
71
|
} = {}) {
|
|
108
72
|
for (const [name, value, minimum] of [
|
|
109
73
|
['maxPerParent', maxPerParent, 1],
|
|
110
74
|
['maxParents', maxParents, 1],
|
|
111
|
-
['maxViolations', maxViolations, 0],
|
|
112
75
|
]) {
|
|
113
76
|
if (!Number.isSafeInteger(value) || value < minimum) {
|
|
114
77
|
throw new RangeError(`${name} 必须是大于或等于 ${minimum} 的安全整数。`)
|
|
@@ -193,29 +156,24 @@ export function createDispatchTimeline({
|
|
|
193
156
|
continue
|
|
194
157
|
}
|
|
195
158
|
const worktree = persistedWorktree(row)
|
|
196
|
-
const entry = { childId, parentId, worktree, ...initialAttempt({ ...row, startedAt
|
|
159
|
+
const entry = { childId, parentId, worktree, ...initialAttempt({ ...row, startedAt }) }
|
|
197
160
|
const dispatchState = row.dispatchState
|
|
198
161
|
entry.dispatchState = TERMINAL_DISPATCH_STATES.has(dispatchState) ? 'ended'
|
|
199
162
|
: idOrNull(dispatchState) ?? (Number.isFinite(row.endedAt) ? 'ended' : entry.dispatchState)
|
|
200
163
|
entry.attemptSeq = countOr(row.attemptSeq, 1)
|
|
201
|
-
for (const key of ['runId', '
|
|
164
|
+
for (const key of ['runId', 'leaseState']) {
|
|
202
165
|
if (isId(row[key])) entry[key] = row[key]
|
|
203
166
|
}
|
|
204
|
-
for (const key of ['label', 'cwd', 'stopReason'
|
|
205
|
-
for (const key of ['endedAt', 'disposedAt', '
|
|
167
|
+
for (const key of ['label', 'cwd', 'stopReason']) entry[key] = textOrNull(row[key])
|
|
168
|
+
for (const key of ['endedAt', 'disposedAt', 'releasedAt']) entry[key] = timeOrNull(row[key])
|
|
206
169
|
entry.writeScope = textList(row.writeScope)
|
|
207
|
-
entry.
|
|
208
|
-
if (
|
|
209
|
-
entry.auditSummary = copyMetadata(row.auditSummary)
|
|
210
|
-
const violations = Array.isArray(row.violations) ? row.violations : []
|
|
211
|
-
entry.violations = violations.slice(0, maxViolations).map(copyViolation)
|
|
212
|
-
entry.truncatedViolations = countOr(row.truncatedViolations, 0) + Math.max(0, violations.length - maxViolations)
|
|
170
|
+
entry.pendingNextTurn = row.pendingNextTurn ? snapshot(row.pendingNextTurn) : null
|
|
171
|
+
if (isId(row.allowNewReason)) entry.allowNewReason = row.allowNewReason
|
|
213
172
|
for (const key of ['agentName', 'rootSessionId', 'batchId']) if (isId(row[key])) entry[key] = row[key]
|
|
214
173
|
for (const key of ['itemIndex', 'stageIndex']) {
|
|
215
174
|
const index = countOr(row[key], null)
|
|
216
175
|
if (index !== null) entry[key] = index
|
|
217
176
|
}
|
|
218
|
-
if (Array.isArray(row.expectedTargets)) entry.expectedTargets = textList(row.expectedTargets)
|
|
219
177
|
if (!parents.has(parentId)) parents.set(parentId, new Map())
|
|
220
178
|
entry.updatedAt = updatedAt
|
|
221
179
|
touch(entry, updatedAt)
|
|
@@ -228,7 +186,7 @@ export function createDispatchTimeline({
|
|
|
228
186
|
return result
|
|
229
187
|
}
|
|
230
188
|
|
|
231
|
-
function recordDispatched({ parentId, childId, agentName, label, readOnly, writeScope, cwd,
|
|
189
|
+
function recordDispatched({ parentId, childId, agentName, label, readOnly, writeScope, cwd, leaseId, allowNewReason,
|
|
232
190
|
startedAt, worktree }) {
|
|
233
191
|
if (!isId(parentId) || !isId(childId)) return false
|
|
234
192
|
if (!parents.has(parentId)) parents.set(parentId, new Map())
|
|
@@ -239,11 +197,13 @@ export function createDispatchTimeline({
|
|
|
239
197
|
parentId,
|
|
240
198
|
...(isId(agentName) && { agentName }),
|
|
241
199
|
label: textOrNull(label),
|
|
200
|
+
...(isId(allowNewReason) && { allowNewReason }),
|
|
201
|
+
pendingNextTurn: null,
|
|
242
202
|
writeScope: Array.isArray(writeScope) ? writeScope.filter((item) => typeof item === 'string') : [],
|
|
243
203
|
cwd: textOrNull(cwd),
|
|
244
204
|
attemptSeq: records.get(childId)?.attemptSeq ?? 1,
|
|
245
205
|
worktree: isolatedTree,
|
|
246
|
-
...initialAttempt({ readOnly,
|
|
206
|
+
...initialAttempt({ readOnly, leaseId, startedAt: timestamp(startedAt) }),
|
|
247
207
|
}
|
|
248
208
|
touch(entry, entry.startedAt)
|
|
249
209
|
if (records.size > maxPerParent) evictOldest(records)
|
|
@@ -258,17 +218,17 @@ export function createDispatchTimeline({
|
|
|
258
218
|
return touch(entry, timestamp())
|
|
259
219
|
}
|
|
260
220
|
|
|
261
|
-
function recordRetry({ childId,
|
|
221
|
+
function recordRetry({ childId, leaseId, readOnly, writeScope, pendingNextTurn, startedAt, parentId }) {
|
|
262
222
|
const entry = lookup({ childId, parentId })
|
|
263
223
|
if (!entry) return false
|
|
264
224
|
const at = timestamp(startedAt)
|
|
265
225
|
Object.assign(entry, initialAttempt({
|
|
266
226
|
readOnly: readOnly ?? entry.readOnly,
|
|
267
|
-
baselineId,
|
|
268
227
|
leaseId,
|
|
269
228
|
startedAt: at,
|
|
270
|
-
worktree: entry.worktree,
|
|
271
229
|
}), { attemptSeq: entry.attemptSeq + 1 })
|
|
230
|
+
if (writeScope !== undefined) entry.writeScope = textList(writeScope)
|
|
231
|
+
if (pendingNextTurn !== undefined) entry.pendingNextTurn = snapshot(pendingNextTurn)
|
|
272
232
|
return touch(entry, at)
|
|
273
233
|
}
|
|
274
234
|
|
|
@@ -295,8 +255,6 @@ export function createDispatchTimeline({
|
|
|
295
255
|
const worktree = entry?.worktree
|
|
296
256
|
if (!worktree || (patch.id !== undefined && patch.id !== worktree.id)) return false
|
|
297
257
|
worktree.state = normalizeWorktreeState(patch.state) ?? worktree.state
|
|
298
|
-
if (Number.isSafeInteger(patch.commits) && patch.commits >= 0) worktree.commits = patch.commits
|
|
299
|
-
if (typeof patch.dirty === 'boolean') worktree.dirty = patch.dirty
|
|
300
258
|
if (patch.mergeCommit === null || isId(patch.mergeCommit)) worktree.mergeCommit = patch.mergeCommit
|
|
301
259
|
if (Array.isArray(patch.conflictFiles)) {
|
|
302
260
|
worktree.conflictFiles = patch.conflictFiles.filter((file) => typeof file === 'string')
|
|
@@ -305,39 +263,6 @@ export function createDispatchTimeline({
|
|
|
305
263
|
return touch(entry, timestamp(patch.at))
|
|
306
264
|
}
|
|
307
265
|
|
|
308
|
-
function recordAudit(event, report, metadata) {
|
|
309
|
-
// Legacy callers supply flat evidence; Git settlement may also supply the raw audit.
|
|
310
|
-
const evidence = typeof event === 'string' ? { ...metadata, childId: event } : event
|
|
311
|
-
const audit = report ?? evidence.audit
|
|
312
|
-
const { childId, runId, baselineId, passed, incomplete, unmanaged, violations, summary,
|
|
313
|
-
isolation, git, at, parentId } = { ...audit, ...evidence }
|
|
314
|
-
const entry = lookup({ childId, parentId })
|
|
315
|
-
if (!entry || (isId(runId) && entry.runId && entry.runId !== runId) ||
|
|
316
|
-
(isId(baselineId) && entry.baselineId && entry.baselineId !== baselineId)) return false
|
|
317
|
-
const rows = Array.isArray(violations) ? violations : []
|
|
318
|
-
entry.baselineId ??= idOrNull(baselineId)
|
|
319
|
-
entry.incomplete = incomplete === true || (incomplete === undefined && git != null &&
|
|
320
|
-
(summary?.truncated === true || rows.some((row) => row?.code === 'audit-incomplete')))
|
|
321
|
-
if (unmanaged === true) {
|
|
322
|
-
entry.auditState = 'unmanaged'
|
|
323
|
-
} else {
|
|
324
|
-
entry.auditState = !entry.incomplete && isId(baselineId) && passed === true ? 'passed' : 'failed'
|
|
325
|
-
}
|
|
326
|
-
entry.violations = rows.slice(0, maxViolations).map(copyViolation)
|
|
327
|
-
entry.truncatedViolations = Math.max(0, rows.length - maxViolations)
|
|
328
|
-
entry.auditedAt = timestamp(at)
|
|
329
|
-
entry.auditSummary = textOrNull(summary)
|
|
330
|
-
if (git != null && entry.worktree) {
|
|
331
|
-
entry.worktree.commits = git.commits?.length ?? 0
|
|
332
|
-
entry.worktree.dirty = (git.dirtyFiles?.length ?? 0) > 0
|
|
333
|
-
}
|
|
334
|
-
const explicitIsolation = copyMetadata(audit?.isolationLevel || evidence.isolationLevel || isolation)
|
|
335
|
-
entry.isolation = explicitIsolation ?? (git != null || entry.worktree
|
|
336
|
-
? entry.isolation
|
|
337
|
-
: entry.auditState === 'unmanaged' ? null : DEFAULT_ISOLATION)
|
|
338
|
-
return touch(entry, entry.auditedAt)
|
|
339
|
-
}
|
|
340
|
-
|
|
341
266
|
function recordLeaseReleased({ childId, leaseId, at, parentId }) {
|
|
342
267
|
const entry = lookup({ childId, parentId })
|
|
343
268
|
if (!entry || (leaseId != null && leaseId !== entry.leaseId)) return false
|
|
@@ -346,13 +271,6 @@ export function createDispatchTimeline({
|
|
|
346
271
|
return touch(entry, entry.releasedAt)
|
|
347
272
|
}
|
|
348
273
|
|
|
349
|
-
function markAuditDelivered({ childId, via, parentId }) {
|
|
350
|
-
const entry = lookup({ childId, parentId })
|
|
351
|
-
if (!entry || via !== 'inline-pre-step') return false
|
|
352
|
-
entry.auditDeliveredVia = via
|
|
353
|
-
return touch(entry, timestamp())
|
|
354
|
-
}
|
|
355
|
-
|
|
356
274
|
function get(parentId, childId) {
|
|
357
275
|
const entry = parents.get(parentId)?.get(childId)
|
|
358
276
|
return entry ? snapshot(entry) : null
|
|
@@ -364,16 +282,13 @@ export function createDispatchTimeline({
|
|
|
364
282
|
const entries = childId === undefined ? [...(records?.values() ?? [])] : entry ? [entry] : []
|
|
365
283
|
const filtered = includeCompleted ? entries : entries.filter((item) => item.dispatchState !== 'ended')
|
|
366
284
|
const counts = {
|
|
367
|
-
running: 0,
|
|
285
|
+
running: 0, ended: 0, worktreePendingReview: 0,
|
|
368
286
|
}
|
|
369
287
|
for (const item of filtered) {
|
|
370
288
|
const state = item.worktree?.state
|
|
371
289
|
if (state === 'ready_for_review' || state === 'merge_conflict') counts.worktreePendingReview += 1
|
|
372
290
|
if (item.dispatchState !== 'ended') counts.running += 1
|
|
373
|
-
else
|
|
374
|
-
else if (item.auditState === 'passed') counts.auditPassed += 1
|
|
375
|
-
else if (item.auditState === 'failed') counts.auditFailed += 1
|
|
376
|
-
else counts.auditUnmanaged += 1
|
|
291
|
+
else counts.ended += 1
|
|
377
292
|
}
|
|
378
293
|
const items = filtered.reverse()
|
|
379
294
|
.sort((left, right) => queryPriority(left) - queryPriority(right) || newestFirst(left, right))
|
|
@@ -382,83 +297,10 @@ export function createDispatchTimeline({
|
|
|
382
297
|
return { parentId, total: filtered.length, counts, items }
|
|
383
298
|
}
|
|
384
299
|
|
|
385
|
-
function listAttentionItems(parentId) {
|
|
386
|
-
return [...(parents.get(parentId)?.values() ?? [])].reverse()
|
|
387
|
-
.filter(needsAttention)
|
|
388
|
-
.sort(newestFirst)
|
|
389
|
-
.map(snapshot)
|
|
390
|
-
}
|
|
391
300
|
|
|
392
301
|
return {
|
|
393
|
-
recordDispatched, recordRunStarted, recordRetry, recordDisposed, recordEnded,
|
|
394
|
-
recordLeaseReleased,
|
|
395
|
-
get, queryParent,
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
function worktreeDeliveryNotice(worktree, passed) {
|
|
400
|
-
const id = worktree?.id ?? '未知'
|
|
401
|
-
if (worktree?.state === 'discarded') {
|
|
402
|
-
return worktree.mergeCommit ? `工作树 ${id} 已清理,其交付物此前已合回。`
|
|
403
|
-
: `工作树 ${id} 的交付物已丢弃,未合回主仓。`
|
|
404
|
-
}
|
|
405
|
-
if (worktree?.state === 'merged' || (worktree?.state === 'cleanup_failed' && worktree.mergeCommit)) {
|
|
406
|
-
return `工作树 ${id} 的交付物已合回。`
|
|
407
|
-
}
|
|
408
|
-
return passed
|
|
409
|
-
? `交付物保留在工作树 ${id},尚未合回主仓,请调用 worktree_manage 执行 inspect / merge / discard。`
|
|
410
|
-
: `违规现场保留在工作树 ${id},尚未合回主仓;请先调用 worktree_manage 执行 inspect 核查。`
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
/** Render same-frame evidence without implying that a logical audit is a sandbox. */
|
|
414
|
-
export function formatInlineAuditNotice(records) {
|
|
415
|
-
const entries = Array.isArray(records) ? records
|
|
416
|
-
: records == null ? [] : [{ childId: records.childId, entry: records.entry ?? records }]
|
|
417
|
-
const lines = []
|
|
418
|
-
for (const { childId, entry } of entries) {
|
|
419
|
-
const prefix = `【${childId}】` + (entry?.agentName ? `(${entry.agentName})` : '')
|
|
420
|
-
const baseline = entry?.baselineId ?? '无'
|
|
421
|
-
const worktree = entry?.worktree
|
|
422
|
-
const isolated = entry?.isolation === WORKTREE_ISOLATION || worktree != null
|
|
423
|
-
const branch = worktree?.branch ?? '未知'
|
|
424
|
-
switch (entry?.auditState) {
|
|
425
|
-
case 'passed':
|
|
426
|
-
if (isolated) {
|
|
427
|
-
const changes = `新增提交 ${worktree?.commits ?? 0} 个${worktree?.dirty ? ',含未提交改动' : ''}`
|
|
428
|
-
lines.push(`${prefix}写作用域审计已通过(基线 ${baseline};git-worktree 独立检出,分支 ${branch},${changes})。` +
|
|
429
|
-
worktreeDeliveryNotice(worktree, true))
|
|
430
|
-
} else {
|
|
431
|
-
lines.push(`${prefix}写作用域审计已通过(基线 ${baseline};${entry.readOnly ? '只读逻辑审计' : '作用域内变更'})。`)
|
|
432
|
-
}
|
|
433
|
-
break
|
|
434
|
-
case 'failed': {
|
|
435
|
-
const violations = entry.violations ?? []
|
|
436
|
-
const omitted = entry.truncatedViolations
|
|
437
|
-
const count = `${violations.length + (Number.isInteger(omitted) ? omitted : 0)}${omitted === true ? '+' : ''}`
|
|
438
|
-
const isolation = isolated ? `;git-worktree 工作树 ${worktree?.id ?? '未知'},分支 ${branch}` : ''
|
|
439
|
-
lines.push(`${prefix}不得验收为通过。写作用域审计未通过(基线 ${baseline},${count} 项${isolation}):`)
|
|
440
|
-
for (const row of violations) {
|
|
441
|
-
const kind = row.kind ?? row.code ?? '违规'
|
|
442
|
-
const detail = row.detail ?? row.reason
|
|
443
|
-
const path = row.path ? `:${JSON.stringify(row.path)}` : ''
|
|
444
|
-
lines.push(`- ${kind}${path}${detail ? `(${JSON.stringify(detail)})` : ''}`)
|
|
445
|
-
}
|
|
446
|
-
if (omitted) lines.push(Number.isInteger(omitted) ? `另有 ${omitted} 项未列出。` : '其余违规未列出。')
|
|
447
|
-
if (entry.incomplete) lines.push('审计采样不完整或超限。')
|
|
448
|
-
lines.push(isolated ? worktreeDeliveryNotice(worktree)
|
|
449
|
-
: 'logical-post-audit:只做工作区逻辑写后审计,不是物理隔离;不能阻止写入、覆盖工作区外或区分并发写入者。')
|
|
450
|
-
break
|
|
451
|
-
}
|
|
452
|
-
case 'pending':
|
|
453
|
-
lines.push(isolated
|
|
454
|
-
? `${prefix}执行已结束,但 git-worktree 工作树 ${worktree?.id ?? '未知'}(分支 ${branch})的写作用域审计仍为 pending(尚未结算),不得直接验收通过;验收前请调用 delegate_status 核验终态。`
|
|
455
|
-
: `${prefix}执行已结束,但写作用域审计仍为 pending(尚未结算),不得直接验收通过;验收前请调用 delegate_status 核验终态。`)
|
|
456
|
-
break
|
|
457
|
-
default:
|
|
458
|
-
lines.push(isolated
|
|
459
|
-
? `${prefix}git-worktree 工作树 ${worktree?.id ?? '未知'}(分支 ${branch})未纳入本插件写作用域审计基线(unmanaged),请调用 worktree_manage 执行 inspect 核查。`
|
|
460
|
-
: `${prefix}未纳入本插件写作用域审计基线(unmanaged),请按需核验工作区变更。`)
|
|
461
|
-
}
|
|
302
|
+
recordDispatched, recordRunStarted, recordRetry, recordDisposed, recordEnded,
|
|
303
|
+
recordLeaseReleased, recordWorktreeState,
|
|
304
|
+
get, queryParent, exportEntries, hydrateEntries,
|
|
462
305
|
}
|
|
463
|
-
return lines.join('\n')
|
|
464
306
|
}
|
package/lib/tool-surface.js
CHANGED
|
@@ -25,8 +25,9 @@
|
|
|
25
25
|
|
|
26
26
|
import { DELEGATE_STATUS_TOOL_NAME, DELEGATE_TOOL, ORCHESTRATE_TOOL } from './api.js'
|
|
27
27
|
import { viewNames } from './catalog.js'
|
|
28
|
-
import { isDelegatedChild, resolveEffectiveAllowedTools } from './gate.js'
|
|
29
|
-
import {
|
|
28
|
+
import { DISPATCH_MODE_TOOLS, isDelegatedChild, resolveEffectiveAllowedTools } from './gate.js'
|
|
29
|
+
import { dispatchWriteParameters } from './delegate.js'
|
|
30
|
+
import { TEAM_SHARED_GOAL_RULE, mainAgentRule } from './rules.js'
|
|
30
31
|
import { RUN_CODE_TOOL } from './run-code-guard.js'
|
|
31
32
|
|
|
32
33
|
export { DEFAULT_BLOCKED_NATIVE_TOOLS, NATIVE_TEAM_TOOLS, resolveEffectiveAllowedTools } from './gate.js'
|
|
@@ -66,6 +67,18 @@ const SURFACE = Symbol.for('dsh-orchestrator.toolSurface')
|
|
|
66
67
|
const CORDIS_ORIGINAL = Symbol.for('cordis.original')
|
|
67
68
|
const ROOT_ONLY_TOOLS = new Set([DELEGATE_STATUS_TOOL_NAME, 'worktree_manage'])
|
|
68
69
|
|
|
70
|
+
/** Keep both author specs and the host's compiled JSON Schema consistent. */
|
|
71
|
+
function hideReadOnly(parameters, batch) {
|
|
72
|
+
const properties = parameters?.properties ?? parameters
|
|
73
|
+
if (!properties || !Object.hasOwn(properties, 'readOnly')) return parameters
|
|
74
|
+
const { readOnly: _hidden, ...visible } = properties
|
|
75
|
+
const schema = dispatchWriteParameters({ batch, readOnlyEnabled: false })
|
|
76
|
+
for (const name of ['writeScope', 'isolation']) {
|
|
77
|
+
if (visible[name]) visible[name] = { ...visible[name], description: schema[name].description }
|
|
78
|
+
}
|
|
79
|
+
return parameters.properties ? { ...parameters, properties: visible } : visible
|
|
80
|
+
}
|
|
81
|
+
|
|
69
82
|
/** A name a definition carries, or `''` for a shape this wrapper does not understand. */
|
|
70
83
|
function nameOf(definition) {
|
|
71
84
|
const name = definition?.name
|
|
@@ -88,11 +101,13 @@ export function installToolSurface({
|
|
|
88
101
|
tools,
|
|
89
102
|
allowed,
|
|
90
103
|
describeDelegate,
|
|
104
|
+
describeOrchestrate,
|
|
105
|
+
readOnlyEnabled,
|
|
91
106
|
allowAgentTeam = true,
|
|
92
107
|
isNativeTeamEnabled = () => false,
|
|
93
108
|
enabled = () => true,
|
|
94
109
|
workerDeny = [DELEGATE_TOOL, ORCHESTRATE_TOOL],
|
|
95
|
-
disabledDeny =
|
|
110
|
+
disabledDeny = DISPATCH_MODE_TOOLS,
|
|
96
111
|
shadow = OFFICIAL_SHADOW,
|
|
97
112
|
} = {}) {
|
|
98
113
|
// Cordis returns a new method proxy on every ordinary get. Patch the actual service so
|
|
@@ -160,15 +175,26 @@ export function installToolSurface({
|
|
|
160
175
|
const teamGuideTool = visibleNames.has('team_task_create') && isNativeTeamEnabled(scope)
|
|
161
176
|
? (visibleNames.has(DELEGATE_TOOL) ? DELEGATE_TOOL : ORCHESTRATE_TOOL)
|
|
162
177
|
: undefined
|
|
178
|
+
const allowReadOnly = readOnlyEnabled?.(scope)
|
|
163
179
|
return visible.map((definition) => {
|
|
164
180
|
const name = nameOf(definition)
|
|
165
|
-
const
|
|
166
|
-
if (!
|
|
167
|
-
|
|
181
|
+
const batch = name === ORCHESTRATE_TOOL
|
|
182
|
+
if (!batch && name !== DELEGATE_TOOL) return definition
|
|
183
|
+
const describe = batch ? describeOrchestrate : describeDelegate
|
|
184
|
+
if (!describe && name !== teamGuideTool && allowReadOnly !== false) return definition
|
|
185
|
+
const options = { enabled: true, readOnlyEnabled: allowReadOnly }
|
|
186
|
+
let description = describe?.(allowedForScope, options) ?? definition.description
|
|
187
|
+
if (batch && describe && !visibleNames.has(DELEGATE_TOOL)) {
|
|
188
|
+
description = mainAgentRule(allowedForScope, options) + description
|
|
189
|
+
}
|
|
168
190
|
if (name === teamGuideTool) description = `${description} ${TEAM_SHARED_GOAL_RULE}`
|
|
191
|
+
const parameters = allowReadOnly === false ? hideReadOnly(definition.parameters, batch) : definition.parameters
|
|
169
192
|
return Object.defineProperties(Object.create(Object.getPrototypeOf(definition)), {
|
|
170
193
|
...Object.getOwnPropertyDescriptors(definition),
|
|
171
194
|
description: { value: description, enumerable: true, configurable: true },
|
|
195
|
+
...(parameters !== definition.parameters && {
|
|
196
|
+
parameters: { value: parameters, enumerable: true, configurable: true },
|
|
197
|
+
}),
|
|
172
198
|
})
|
|
173
199
|
})
|
|
174
200
|
}
|
|
@@ -192,7 +218,7 @@ export function installToolSurface({
|
|
|
192
218
|
// The context-global view feeds the Allowed Tools panel: it must stay complete.
|
|
193
219
|
if (scope === undefined || scope === null) return list
|
|
194
220
|
const delegated = isDelegatedChild(scope)
|
|
195
|
-
if (!enabled()) {
|
|
221
|
+
if (!enabled(scope)) {
|
|
196
222
|
if (!delegated && offDeny.size === 0) return list
|
|
197
223
|
return list.filter((definition) => {
|
|
198
224
|
const name = nameOf(definition)
|
package/lib/worker.js
CHANGED
|
@@ -13,23 +13,34 @@
|
|
|
13
13
|
* dispatch mode active unless explicitly turned off.
|
|
14
14
|
*/
|
|
15
15
|
export function isDispatchEnabled(config) {
|
|
16
|
+
return configValue(config, 'enabled', true) !== false
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Read a volatile config ref or a plain value. */
|
|
20
|
+
export function configValue(config, key, fallback) {
|
|
16
21
|
try {
|
|
17
|
-
const field = config?.
|
|
22
|
+
const field = config?.[key]
|
|
18
23
|
const value = typeof field?.get === 'function' ? field.get() : field
|
|
19
|
-
return value
|
|
24
|
+
return value ?? fallback
|
|
20
25
|
} catch {
|
|
21
|
-
return
|
|
26
|
+
return fallback
|
|
22
27
|
}
|
|
23
28
|
}
|
|
24
29
|
|
|
25
30
|
/** The worker route from config, in the `AgentOptions` shape `startActivation` takes. */
|
|
26
31
|
export function readRoute(config) {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
if (provider === '' || model === '') {
|
|
32
|
+
const route = workerRouteFromConfig(config)
|
|
33
|
+
if (route === null) {
|
|
30
34
|
throw new Error('orchestrator: the worker route is not configured (set workerProvider and workerModel)')
|
|
31
35
|
}
|
|
32
|
-
|
|
36
|
+
return route
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function workerRouteFromConfig(config) {
|
|
40
|
+
const provider = String(configValue(config, 'workerProvider', '')).trim()
|
|
41
|
+
const model = String(configValue(config, 'workerModel', '')).trim()
|
|
42
|
+
if (provider === '' || model === '') return null
|
|
43
|
+
const effort = String(configValue(config, 'workerReasoningEffort', '')).trim()
|
|
33
44
|
// Changing the route without naming an effort lets the model resolve its own default,
|
|
34
45
|
// so an empty effort is simply left out rather than sent as an empty string.
|
|
35
46
|
return { provider, model, ...(effort === '' ? {} : { reasoningEffort: effort }) }
|
package/lib/worktree-manage.js
CHANGED
|
@@ -83,12 +83,6 @@ function toolResult(action, text, payload) {
|
|
|
83
83
|
return toLosslessJsonValue({ content: [{ type: 'text', text: String(text) }], details })
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
function auditReviewText({ auditSummary } = {}) {
|
|
87
|
-
if (typeof auditSummary?.auditPassed !== 'boolean') return ''
|
|
88
|
-
const { commits, dirty, auditPassed } = auditSummary
|
|
89
|
-
return ` 最近审计${auditPassed ? '通过' : '未通过'}(${commits} 个提交${dirty ? ',有未提交改动' : ''})。`
|
|
90
|
-
}
|
|
91
|
-
|
|
92
86
|
function nextWorktreeState(record, action, result) {
|
|
93
87
|
if (result.worktree?.state) return result.worktree.state
|
|
94
88
|
if (result.state) return result.state
|
|
@@ -148,7 +142,7 @@ export function createWorktreeManageTool({
|
|
|
148
142
|
} = {}) {
|
|
149
143
|
return defineTool({
|
|
150
144
|
name: toolName,
|
|
151
|
-
description: '仅根代理可用:列出、检查、合回或丢弃当前会话的 Git
|
|
145
|
+
description: '仅根代理可用:列出、检查、合回或丢弃当前会话的 Git 工作树交付,或清理当前仓库的孤立工作树。',
|
|
152
146
|
parameters: {
|
|
153
147
|
action: { type: 'string', enum: ACTIONS, required: true, description: '要执行的工作树操作。' },
|
|
154
148
|
worktreeId: { type: 'string', description: '工作树 ID;inspect、merge、discard 需要此项或 childId。' },
|
|
@@ -208,7 +202,7 @@ export function createWorktreeManageTool({
|
|
|
208
202
|
const owned = rows.filter((row) => row?.parentId === parentId &&
|
|
209
203
|
(options.includeClosed || !CLOSED_STATES.has(row.state)))
|
|
210
204
|
const text = [`当前会话有 ${owned.length} 棵工作树。`,
|
|
211
|
-
...owned.map((row) => `- ${row.id}:${row.state}(分支 ${row.branch ?? '未知'}
|
|
205
|
+
...owned.map((row) => `- ${row.id}:${row.state}(分支 ${row.branch ?? '未知'})`)].join('\n')
|
|
212
206
|
return toolResult(action, text, { ok: true, worktrees: owned, total: owned.length })
|
|
213
207
|
}
|
|
214
208
|
if (action === 'prune') {
|
|
@@ -231,8 +225,7 @@ export function createWorktreeManageTool({
|
|
|
231
225
|
if (action === 'inspect') {
|
|
232
226
|
const text = result.ok
|
|
233
227
|
? `工作树 ${record.id}(分支 ${record.branch ?? '未知'}):${result.exists === false ? '目录已不存在。'
|
|
234
|
-
: `新增提交 ${result.aheadCount ?? result.commits?.length ?? 0} 个${result.dirty ? ',有未提交改动' : ''}${result.diffTruncated ? ',差异已截断' : ''}。`}`
|
|
235
|
-
auditReviewText(result.worktree ?? record)
|
|
228
|
+
: `新增提交 ${result.aheadCount ?? result.commits?.length ?? 0} 个${result.dirty ? ',有未提交改动' : ''}${result.diffTruncated ? ',差异已截断' : ''}。`}`
|
|
236
229
|
: `检查工作树 ${record.id} 失败:${result.message ?? formatWorktreeError(result.code)}`
|
|
237
230
|
return toolResult(action, text, result)
|
|
238
231
|
}
|
package/lib/worktree.js
CHANGED
|
@@ -34,14 +34,6 @@ function childIdsOf(record) {
|
|
|
34
34
|
return [...new Set([record.childId, ...ids].filter(isId))]
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
function copyAuditSummary(value) {
|
|
38
|
-
try {
|
|
39
|
-
return JSON.parse(JSON.stringify(value ?? null))
|
|
40
|
-
} catch {
|
|
41
|
-
return null
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
37
|
function persistedRecord(record) {
|
|
46
38
|
return {
|
|
47
39
|
id: record.id, parentId: textOrNull(record.parentId), childId: textOrNull(record.childId),
|
|
@@ -54,7 +46,6 @@ function persistedRecord(record) {
|
|
|
54
46
|
label: textOrNull(record.label), state: record.state, mergeCommit: textOrNull(record.mergeCommit),
|
|
55
47
|
cleaned: record.cleaned === true, locked: isWorktreeLockedRecord(record),
|
|
56
48
|
createdAt: timeOrNull(record.createdAt), updatedAt: timeOrNull(record.updatedAt),
|
|
57
|
-
auditSummary: copyAuditSummary(record.auditSummary),
|
|
58
49
|
}
|
|
59
50
|
}
|
|
60
51
|
|
|
@@ -380,7 +371,7 @@ export function createWorktreeManager({ now = () => Date.now(), newId = randomUU
|
|
|
380
371
|
const worktree = save({
|
|
381
372
|
id, parentId, childId, batchId, itemIndex, label, repoRoot, sourceCwd: path.resolve(sourceCwd),
|
|
382
373
|
subPath, worktreePath, execCwd, branch, baseCommit, baseBranch, state: 'active',
|
|
383
|
-
createdAt: time, updatedAt: time, settledAt: null, closedAt: null,
|
|
374
|
+
createdAt: time, updatedAt: time, settledAt: null, closedAt: null,
|
|
384
375
|
mergeCommit: null, conflictFiles: [], error: null,
|
|
385
376
|
})
|
|
386
377
|
if (childId !== null) children.set(childId, id)
|
|
@@ -434,11 +425,11 @@ export function createWorktreeManager({ now = () => Date.now(), newId = randomUU
|
|
|
434
425
|
return update(record, { state: 'active', conflictFiles: [], error: null })
|
|
435
426
|
}
|
|
436
427
|
|
|
437
|
-
function markReadyForReview(id
|
|
428
|
+
function markReadyForReview(id) {
|
|
438
429
|
const record = get(id)
|
|
439
430
|
if (!record || isWorktreeLockedRecord(record) ||
|
|
440
431
|
!['active', 'ready_for_review'].includes(record.state)) return record
|
|
441
|
-
return update(record, { state: 'ready_for_review', settledAt: now()
|
|
432
|
+
return update(record, { state: 'ready_for_review', settledAt: now() })
|
|
442
433
|
}
|
|
443
434
|
|
|
444
435
|
function inspect(idOrChildId, { maxDiffChars = 16000, maxCommits = 30 } = {}) {
|