dsh-tower 0.0.0-stage → 1.1.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/CHANGELOG.md +65 -0
- package/LICENSE +21 -0
- package/README.md +159 -2
- package/README.zh.md +160 -0
- package/cordis.patch.yml +23 -0
- package/icon.svg +7 -0
- package/index.js +296 -0
- package/lib/git.js +144 -0
- package/lib/modeStore.js +68 -0
- package/lib/store.js +123 -0
- package/lib/tools.js +801 -0
- package/lib/tower.js +1507 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +53 -4
- package/worker.js +41 -0
package/lib/tools.js
ADDED
|
@@ -0,0 +1,801 @@
|
|
|
1
|
+
// Tower tools: init, plan, mission, status, spawn, roster, adopt.
|
|
2
|
+
import { Tower } from './tower.js'
|
|
3
|
+
|
|
4
|
+
const OUTPUT = {
|
|
5
|
+
schema: { type: 'object' },
|
|
6
|
+
render: (_args, value) => [{ type: 'text', text: JSON.stringify(value, null, 2) }],
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const ROLE_ENUM = ['worker', 'reviewer']
|
|
10
|
+
|
|
11
|
+
function define(ctx, definition) {
|
|
12
|
+
ctx.effect(() => ctx.tools.register(definition))
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function fail(message) {
|
|
16
|
+
throw new Error(message)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function textOf(blocks) {
|
|
20
|
+
if (!Array.isArray(blocks)) return null
|
|
21
|
+
return blocks
|
|
22
|
+
.map((block) => (block && block.type === 'text' ? block.text : `[${block && block.type}]`))
|
|
23
|
+
.join('\n')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function observeActivity(ctx, tower) {
|
|
27
|
+
const view = await tower.rosterView()
|
|
28
|
+
const subagents = ctx.get('subagents')
|
|
29
|
+
const activity = {}
|
|
30
|
+
const parentStatus = {}
|
|
31
|
+
if (!subagents) return { activity, parentStatus }
|
|
32
|
+
const parents = [...new Set(view.roster.map((entry) => entry.parentSessionId).filter(Boolean))]
|
|
33
|
+
for (const parent of parents) {
|
|
34
|
+
try {
|
|
35
|
+
const rows = await subagents.listDescendants(parent)
|
|
36
|
+
for (const row of rows) activity[String(row.id)] = row.activity ?? 'unknown'
|
|
37
|
+
parentStatus[parent] = 'ok'
|
|
38
|
+
} catch (error) {
|
|
39
|
+
parentStatus[parent] = `unavailable: ${String(error?.message ?? error)}`
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { activity, parentStatus }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function workerBriefing({ repoRoot, base, mission, deps, worktree, instructions }) {
|
|
46
|
+
const lines = []
|
|
47
|
+
lines.push(`# Tower mission ${mission.id} — ${mission.title}`)
|
|
48
|
+
lines.push('')
|
|
49
|
+
lines.push(`You are the tower worker for mission ${mission.id} in repository ${repoRoot}.`)
|
|
50
|
+
lines.push('')
|
|
51
|
+
lines.push(`Work ONLY in this git worktree: ${worktree.path}`)
|
|
52
|
+
lines.push(`Branch: ${worktree.branch} (based on ${base} @ ${String(worktree.baseCommit).slice(0, 12)})`)
|
|
53
|
+
lines.push('')
|
|
54
|
+
lines.push(`Kind: ${mission.kind}`)
|
|
55
|
+
lines.push(
|
|
56
|
+
`Scope (only files you may touch): ${mission.scope.length > 0 ? mission.scope.join(', ') : '(none — survey is read-only)'}`,
|
|
57
|
+
)
|
|
58
|
+
if (mission.tasks.length > 0) {
|
|
59
|
+
lines.push('Tasks:')
|
|
60
|
+
for (const task of mission.tasks) lines.push(`- [${task.status}] ${task.text}`)
|
|
61
|
+
}
|
|
62
|
+
if (deps.length > 0) {
|
|
63
|
+
lines.push('Dependencies:')
|
|
64
|
+
for (const dep of deps) lines.push(`- ${dep.id} ${dep.title ?? ''} (${dep.status})`)
|
|
65
|
+
}
|
|
66
|
+
if (mission.context) {
|
|
67
|
+
lines.push('Context (verbatim from the user):')
|
|
68
|
+
lines.push(mission.context)
|
|
69
|
+
}
|
|
70
|
+
lines.push('')
|
|
71
|
+
lines.push('Rules:')
|
|
72
|
+
lines.push(`- Pass workdir="${worktree.path}" on every shell command; never edit the main checkout.`)
|
|
73
|
+
lines.push('- Stay inside the declared scope; out-of-scope changes are rejected later.')
|
|
74
|
+
lines.push('- Commit your work on the mission branch. Do not merge, push, or spawn other agents.')
|
|
75
|
+
lines.push(
|
|
76
|
+
`- Update progress: tower_mission({repoRoot:"${repoRoot}",id:"${mission.id}",task_done:"..."}) or blocker:"...".`,
|
|
77
|
+
)
|
|
78
|
+
lines.push(
|
|
79
|
+
`- When the work is finished and every task is closed, call tower_complete({repoRoot:"${repoRoot}",missionId:"${mission.id}",report:"<full handoff>"}). Unread inbox messages block completion, so read them with tower_inbox first.`,
|
|
80
|
+
)
|
|
81
|
+
lines.push('- Your final message is the full handoff: files changed, how you verified, what remains.')
|
|
82
|
+
if (instructions) {
|
|
83
|
+
lines.push('')
|
|
84
|
+
lines.push('Extra tower instructions:')
|
|
85
|
+
lines.push(instructions)
|
|
86
|
+
}
|
|
87
|
+
return lines.join('\n')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function reviewerBriefing({ repoRoot, base, mission, deps, worktree, instructions }) {
|
|
91
|
+
const lines = []
|
|
92
|
+
lines.push(`# Tower review — mission ${mission.id} — ${mission.title}`)
|
|
93
|
+
lines.push('')
|
|
94
|
+
lines.push(`You are the independent reviewer for mission ${mission.id} in repository ${repoRoot}.`)
|
|
95
|
+
lines.push('')
|
|
96
|
+
lines.push(`Review branch: ${worktree.branch}`)
|
|
97
|
+
lines.push(`Worktree: ${worktree.path}`)
|
|
98
|
+
lines.push(`Reviewed base: ${base} @ ${String(worktree.baseCommit).slice(0, 12)}`)
|
|
99
|
+
lines.push('')
|
|
100
|
+
lines.push(`Kind: ${mission.kind}`)
|
|
101
|
+
if (mission.tasks.length > 0) {
|
|
102
|
+
lines.push('The worker was asked to complete:')
|
|
103
|
+
for (const task of mission.tasks) lines.push(`- [${task.status}] ${task.text}`)
|
|
104
|
+
}
|
|
105
|
+
if (deps.length > 0) {
|
|
106
|
+
lines.push('Dependencies:')
|
|
107
|
+
for (const dep of deps) lines.push(`- ${dep.id} ${dep.title ?? ''} (${dep.status})`)
|
|
108
|
+
}
|
|
109
|
+
if (mission.context) {
|
|
110
|
+
lines.push('Context (verbatim from the user):')
|
|
111
|
+
lines.push(mission.context)
|
|
112
|
+
}
|
|
113
|
+
lines.push('')
|
|
114
|
+
lines.push('Rules:')
|
|
115
|
+
lines.push('- Read-only: do not modify files, commit, or merge.')
|
|
116
|
+
lines.push('- Inspect the diff from the reviewed base to the branch tip and map every task to the diff.')
|
|
117
|
+
lines.push('- Check scope, correctness, tests, and security. Report severity and concrete evidence.')
|
|
118
|
+
lines.push(
|
|
119
|
+
`- Submit the formal verdict with tower_review({repoRoot:"${repoRoot}",missionId:"${mission.id}",status:"clean",merge:"merge",findings:"none",checks:["..."],decision:"..."}). Use status "p1-Nitems"/"p2-Nitems" and merge "fix-then-merge"/"hold" for problems; a non-clean verdict returns the mission to active.`,
|
|
120
|
+
)
|
|
121
|
+
lines.push(
|
|
122
|
+
`- File out-of-scope issues with tower_finding({repoRoot:"${repoRoot}",type:"bug",title:"...",summary:"...",details:"...",suggested_fix:"..."}) instead of fixing them.`,
|
|
123
|
+
)
|
|
124
|
+
lines.push('- Your final message is the review report; the tool call above is the formal verdict.')
|
|
125
|
+
if (instructions) {
|
|
126
|
+
lines.push('')
|
|
127
|
+
lines.push('Extra tower instructions:')
|
|
128
|
+
lines.push(instructions)
|
|
129
|
+
}
|
|
130
|
+
return lines.join('\n')
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function registerTowerTools(ctx) {
|
|
134
|
+
define(ctx, {
|
|
135
|
+
name: 'tower_init',
|
|
136
|
+
description:
|
|
137
|
+
'Initialize Tower mission state for a repository (absolute repoRoot). Creates <git-common-dir>/dsh-tower/state.json and an activity log; idempotent on an existing workspace. Optionally records the local base branch missions will later fork from and merge back into.',
|
|
138
|
+
parameters: {
|
|
139
|
+
type: 'object',
|
|
140
|
+
properties: {
|
|
141
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
142
|
+
base: { type: 'string', description: 'Optional local base branch (defaults to the checked-out branch).' },
|
|
143
|
+
},
|
|
144
|
+
required: ['repoRoot'],
|
|
145
|
+
additionalProperties: false,
|
|
146
|
+
},
|
|
147
|
+
output: OUTPUT,
|
|
148
|
+
async execute(args) {
|
|
149
|
+
const tower = await Tower.open(args.repoRoot)
|
|
150
|
+
return tower.init({ base: args.base })
|
|
151
|
+
},
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
define(ctx, {
|
|
155
|
+
name: 'tower_plan',
|
|
156
|
+
description:
|
|
157
|
+
'Plan one or more missions in an initialized repository. Each mission has a printable-ASCII title, relative scope globs, optional task checklist, optional mission-id dependencies, and kind build|survey. Assigns M1.. ids and auto-adds a dependency when build scopes overlap (serializes merges); rejects dependency cycles.',
|
|
158
|
+
parameters: {
|
|
159
|
+
type: 'object',
|
|
160
|
+
properties: {
|
|
161
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
162
|
+
missions: {
|
|
163
|
+
type: 'array',
|
|
164
|
+
description: 'Missions to create, in intended order.',
|
|
165
|
+
items: {
|
|
166
|
+
type: 'object',
|
|
167
|
+
properties: {
|
|
168
|
+
title: { type: 'string', description: 'Printable ASCII title; used as the mission label.' },
|
|
169
|
+
scope: { type: 'array', items: { type: 'string' }, description: 'Relative file globs this mission may touch; empty only for survey.' },
|
|
170
|
+
tasks: { type: 'array', items: { type: 'string' }, description: 'Verifiable task items the worker ticks off.' },
|
|
171
|
+
deps: { type: 'array', items: { type: 'string' }, description: 'Mission ids that must merge before this one.' },
|
|
172
|
+
kind: { type: 'string', enum: ['build', 'survey'], description: 'survey = read-only investigation that reserves no scope.' },
|
|
173
|
+
context: { type: 'string', description: "The user's own key sentences, verbatim (max 4096 chars)." },
|
|
174
|
+
},
|
|
175
|
+
required: ['title'],
|
|
176
|
+
additionalProperties: false,
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
required: ['repoRoot', 'missions'],
|
|
181
|
+
additionalProperties: false,
|
|
182
|
+
},
|
|
183
|
+
output: OUTPUT,
|
|
184
|
+
async execute(args) {
|
|
185
|
+
const tower = await Tower.open(args.repoRoot)
|
|
186
|
+
return tower.plan({ missions: args.missions })
|
|
187
|
+
},
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
define(ctx, {
|
|
191
|
+
name: 'tower_mission',
|
|
192
|
+
description:
|
|
193
|
+
'Update one mission: lifecycle status, decision note, blocker, clear blockers, mark a task done, drop a task with a reason, or widen the scope (tower only). "merged" is never set here; only a future tower_merge records it. Merged and abandoned are terminal.',
|
|
194
|
+
parameters: {
|
|
195
|
+
type: 'object',
|
|
196
|
+
properties: {
|
|
197
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
198
|
+
id: { type: 'string', description: 'Mission id, e.g. M1.' },
|
|
199
|
+
status: {
|
|
200
|
+
type: 'string',
|
|
201
|
+
enum: ['planned', 'active', 'completed', 'blocked', 'paused', 'abandoned'],
|
|
202
|
+
description: 'New lifecycle status.',
|
|
203
|
+
},
|
|
204
|
+
note: { type: 'string', description: 'Append a decision-log note.' },
|
|
205
|
+
blocker: { type: 'string', description: 'Report a blocker (also sets status to blocked).' },
|
|
206
|
+
clear_blockers: { type: 'boolean', description: 'Clear all recorded blockers.' },
|
|
207
|
+
task_done: { type: 'string', description: 'Mark the first open task containing this text as done.' },
|
|
208
|
+
task_drop: { type: 'string', description: 'Mark the first open task containing this text as dropped.' },
|
|
209
|
+
task_drop_reason: { type: 'string', description: 'Required with task_drop: why the task is legitimately descoped.' },
|
|
210
|
+
scope: { type: 'array', items: { type: 'string' }, description: 'Tower only: replacement scope globs; bumps scopeGeneration.' },
|
|
211
|
+
},
|
|
212
|
+
required: ['repoRoot', 'id'],
|
|
213
|
+
additionalProperties: false,
|
|
214
|
+
},
|
|
215
|
+
output: OUTPUT,
|
|
216
|
+
async execute(args) {
|
|
217
|
+
const tower = await Tower.open(args.repoRoot)
|
|
218
|
+
return tower.mission({
|
|
219
|
+
id: args.id,
|
|
220
|
+
status: args.status,
|
|
221
|
+
note: args.note,
|
|
222
|
+
blocker: args.blocker,
|
|
223
|
+
clear_blockers: args.clear_blockers,
|
|
224
|
+
task_done: args.task_done,
|
|
225
|
+
task_drop: args.task_drop,
|
|
226
|
+
task_drop_reason: args.task_drop_reason,
|
|
227
|
+
scope: args.scope,
|
|
228
|
+
})
|
|
229
|
+
},
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
define(ctx, {
|
|
233
|
+
name: 'tower_status',
|
|
234
|
+
description:
|
|
235
|
+
'Read the Tower dashboard for a repository: state path, base branch, revision, mission status counts, per-mission summary (scope/deps/tasks/worktree/owner), roster size, and the activity tail. Read-only.',
|
|
236
|
+
parameters: {
|
|
237
|
+
type: 'object',
|
|
238
|
+
properties: {
|
|
239
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
240
|
+
},
|
|
241
|
+
required: ['repoRoot'],
|
|
242
|
+
additionalProperties: false,
|
|
243
|
+
},
|
|
244
|
+
output: OUTPUT,
|
|
245
|
+
async execute(args) {
|
|
246
|
+
const tower = await Tower.open(args.repoRoot)
|
|
247
|
+
return tower.status()
|
|
248
|
+
},
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
define(ctx, {
|
|
252
|
+
name: 'tower_spawn',
|
|
253
|
+
description:
|
|
254
|
+
'Spawn a worker or reviewer for a mission. Provisions (or reuses) the mission git worktree and branch, generates a self-contained briefing (mission tasks, scope, deps, context, rules), and starts a background tower-worker subagent in that worktree. Records the agent in the roster and marks the mission active. Returns immediately unless wait=true.',
|
|
255
|
+
parameters: {
|
|
256
|
+
type: 'object',
|
|
257
|
+
properties: {
|
|
258
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
259
|
+
missionId: { type: 'string', description: 'Mission id, e.g. M1.' },
|
|
260
|
+
role: { type: 'string', enum: ROLE_ENUM, description: 'worker executes; reviewer audits the mission branch.' },
|
|
261
|
+
name: { type: 'string', description: 'Unique roster name; defaults to <role>-<missionId>.' },
|
|
262
|
+
instructions: { type: 'string', description: 'Extra tower instructions appended to the briefing.' },
|
|
263
|
+
worktreeRoot: { type: 'string', description: 'Optional absolute parent directory for the worktree.' },
|
|
264
|
+
wait: { type: 'boolean', description: 'Wait for the child result instead of returning immediately (testing/debug).' },
|
|
265
|
+
waitMs: { type: 'number', description: 'Max wait with wait=true, default 180000.' },
|
|
266
|
+
},
|
|
267
|
+
required: ['repoRoot', 'missionId'],
|
|
268
|
+
additionalProperties: false,
|
|
269
|
+
},
|
|
270
|
+
output: OUTPUT,
|
|
271
|
+
async execute(args, exec) {
|
|
272
|
+
const tower = await Tower.open(args.repoRoot)
|
|
273
|
+
const role = args.role ?? 'worker'
|
|
274
|
+
if (!ROLE_ENUM.includes(role)) fail(`role must be one of ${ROLE_ENUM.join('|')}`)
|
|
275
|
+
const context = await tower.missionContext(args.missionId)
|
|
276
|
+
if (role === 'reviewer' && !context.mission.worktree) {
|
|
277
|
+
fail('reviewer needs an existing mission worktree; spawn a worker first')
|
|
278
|
+
}
|
|
279
|
+
const provisioned = await tower.provision({
|
|
280
|
+
missionId: args.missionId,
|
|
281
|
+
worktreeRoot: args.worktreeRoot,
|
|
282
|
+
})
|
|
283
|
+
const { mission, worktree } = provisioned
|
|
284
|
+
const contextWithWorktree = {
|
|
285
|
+
...context,
|
|
286
|
+
mission: {
|
|
287
|
+
...context.mission,
|
|
288
|
+
worktree: { path: worktree.path, branch: worktree.branch, baseCommit: worktree.baseCommit },
|
|
289
|
+
},
|
|
290
|
+
}
|
|
291
|
+
const name = (args.name ?? `${role}-${mission.id.toLowerCase()}`).trim()
|
|
292
|
+
const briefing =
|
|
293
|
+
role === 'worker'
|
|
294
|
+
? workerBriefing({
|
|
295
|
+
repoRoot: tower.root,
|
|
296
|
+
base: context.base,
|
|
297
|
+
mission: contextWithWorktree.mission,
|
|
298
|
+
deps: context.deps,
|
|
299
|
+
worktree,
|
|
300
|
+
instructions: args.instructions,
|
|
301
|
+
})
|
|
302
|
+
: reviewerBriefing({
|
|
303
|
+
repoRoot: tower.root,
|
|
304
|
+
base: context.base,
|
|
305
|
+
mission: contextWithWorktree.mission,
|
|
306
|
+
deps: context.deps,
|
|
307
|
+
worktree,
|
|
308
|
+
instructions: args.instructions,
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
const subagents = ctx.get('subagents')
|
|
312
|
+
if (!subagents) fail('subagents service is unavailable; cannot spawn')
|
|
313
|
+
const providers = subagents.list()
|
|
314
|
+
const provider = providers.includes('spawn') ? 'spawn' : providers[0]
|
|
315
|
+
if (!provider) fail('no subagent provider is registered')
|
|
316
|
+
|
|
317
|
+
const activation = await subagents.startActivation({
|
|
318
|
+
provider,
|
|
319
|
+
label: name,
|
|
320
|
+
request: {
|
|
321
|
+
cwd: worktree.path,
|
|
322
|
+
agentPreset: 'tower-worker',
|
|
323
|
+
prompt: [{ type: 'text', text: briefing }],
|
|
324
|
+
parent: exec.agent,
|
|
325
|
+
maxDepth: 1,
|
|
326
|
+
},
|
|
327
|
+
signal: exec.signal,
|
|
328
|
+
delivery: 'parent',
|
|
329
|
+
})
|
|
330
|
+
if (activation.result && typeof activation.result.catch === 'function') {
|
|
331
|
+
activation.result.catch(() => {})
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const agentId = String(activation.childId)
|
|
335
|
+
const entry = await tower.recordSpawn({
|
|
336
|
+
missionId: mission.id,
|
|
337
|
+
name,
|
|
338
|
+
role,
|
|
339
|
+
agentId,
|
|
340
|
+
parentSessionId: String(exec.agent.id),
|
|
341
|
+
cwd: worktree.path,
|
|
342
|
+
branch: worktree.branch,
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
const report = {
|
|
346
|
+
name,
|
|
347
|
+
agentId,
|
|
348
|
+
role,
|
|
349
|
+
missionId: mission.id,
|
|
350
|
+
worktree: { path: worktree.path, branch: worktree.branch, baseCommit: worktree.baseCommit },
|
|
351
|
+
created: provisioned.created,
|
|
352
|
+
briefingChars: briefing.length,
|
|
353
|
+
rosterEntry: entry,
|
|
354
|
+
provider,
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (args.wait === true) {
|
|
358
|
+
const waitMs = Math.max(1000, Math.min(600000, args.waitMs ?? 180000))
|
|
359
|
+
let timer
|
|
360
|
+
const timeout = new Promise((resolve) => {
|
|
361
|
+
timer = setTimeout(() => resolve({ __timeout: true }), waitMs)
|
|
362
|
+
})
|
|
363
|
+
let result
|
|
364
|
+
try {
|
|
365
|
+
result = await Promise.race([activation.result, timeout])
|
|
366
|
+
} finally {
|
|
367
|
+
clearTimeout(timer)
|
|
368
|
+
}
|
|
369
|
+
if (result && result.__timeout) {
|
|
370
|
+
report.timedOut = true
|
|
371
|
+
} else {
|
|
372
|
+
report.stopReason = result?.stopReason ?? null
|
|
373
|
+
report.output = textOf(result?.output)
|
|
374
|
+
report.diagnostic = result?.diagnostic ?? null
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return report
|
|
378
|
+
},
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
define(ctx, {
|
|
382
|
+
name: 'tower_roster',
|
|
383
|
+
description:
|
|
384
|
+
'List tower roster agents with authoritative live activity from the subagent catalog (running/inactive/missing/unknown). Read-only; never resumes children. Use before completion or merge decisions.',
|
|
385
|
+
parameters: {
|
|
386
|
+
type: 'object',
|
|
387
|
+
properties: {
|
|
388
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
389
|
+
},
|
|
390
|
+
required: ['repoRoot'],
|
|
391
|
+
additionalProperties: false,
|
|
392
|
+
},
|
|
393
|
+
output: OUTPUT,
|
|
394
|
+
async execute(args) {
|
|
395
|
+
const tower = await Tower.open(args.repoRoot)
|
|
396
|
+
const view = await tower.rosterView()
|
|
397
|
+
const subagents = ctx.get('subagents')
|
|
398
|
+
if (!subagents) fail('subagents service is unavailable; cannot observe roster')
|
|
399
|
+
|
|
400
|
+
const parents = [...new Set(view.roster.map((entry) => entry.parentSessionId).filter(Boolean))]
|
|
401
|
+
const activity = new Map()
|
|
402
|
+
const parentStatus = {}
|
|
403
|
+
for (const parent of parents) {
|
|
404
|
+
try {
|
|
405
|
+
const rows = await subagents.listDescendants(parent)
|
|
406
|
+
for (const row of rows) activity.set(String(row.id), row.activity ?? 'unknown')
|
|
407
|
+
parentStatus[parent] = 'ok'
|
|
408
|
+
} catch (error) {
|
|
409
|
+
parentStatus[parent] = `unavailable: ${String(error?.message ?? error)}`
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const roster = view.roster.map((entry) => {
|
|
414
|
+
const observed = activity.get(entry.agentId)
|
|
415
|
+
const known = parentStatus[entry.parentSessionId] === 'ok'
|
|
416
|
+
return {
|
|
417
|
+
...entry,
|
|
418
|
+
activity: observed ?? (known ? 'missing' : 'unknown'),
|
|
419
|
+
observed: observed !== undefined,
|
|
420
|
+
}
|
|
421
|
+
})
|
|
422
|
+
return { revision: view.revision, roster, parentStatus, missions: view.missions }
|
|
423
|
+
},
|
|
424
|
+
})
|
|
425
|
+
|
|
426
|
+
define(ctx, {
|
|
427
|
+
name: 'tower_adopt',
|
|
428
|
+
description:
|
|
429
|
+
'Reconcile the roster with the durable subagent catalog after a restart or new session. Agents that are provably gone are marked retired (records are kept); agents whose parent catalog cannot be read stay unknown and are never retired. Does not resume, reassign, or delete any mission or worktree.',
|
|
430
|
+
parameters: {
|
|
431
|
+
type: 'object',
|
|
432
|
+
properties: {
|
|
433
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
434
|
+
},
|
|
435
|
+
required: ['repoRoot'],
|
|
436
|
+
additionalProperties: false,
|
|
437
|
+
},
|
|
438
|
+
output: OUTPUT,
|
|
439
|
+
async execute(args) {
|
|
440
|
+
const tower = await Tower.open(args.repoRoot)
|
|
441
|
+
const view = await tower.rosterView()
|
|
442
|
+
const subagents = ctx.get('subagents')
|
|
443
|
+
if (!subagents) fail('subagents service is unavailable; cannot adopt')
|
|
444
|
+
|
|
445
|
+
const parents = [...new Set(view.roster.map((entry) => entry.parentSessionId).filter(Boolean))]
|
|
446
|
+
const activity = new Map()
|
|
447
|
+
const parentStatus = {}
|
|
448
|
+
for (const parent of parents) {
|
|
449
|
+
try {
|
|
450
|
+
const rows = await subagents.listDescendants(parent)
|
|
451
|
+
for (const row of rows) activity.set(String(row.id), row.activity ?? 'unknown')
|
|
452
|
+
parentStatus[parent] = 'ok'
|
|
453
|
+
} catch (error) {
|
|
454
|
+
parentStatus[parent] = `unavailable: ${String(error?.message ?? error)}`
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const retiredCandidates = []
|
|
459
|
+
for (const entry of view.roster) {
|
|
460
|
+
if (entry.state === 'retired') continue
|
|
461
|
+
if (parentStatus[entry.parentSessionId] !== 'ok') continue
|
|
462
|
+
if (!activity.has(entry.agentId)) retiredCandidates.push(entry.agentId)
|
|
463
|
+
}
|
|
464
|
+
const { retired } = await tower.markRetired(retiredCandidates)
|
|
465
|
+
const after = await tower.rosterView()
|
|
466
|
+
return {
|
|
467
|
+
retired,
|
|
468
|
+
skippedUnknownParents: Object.entries(parentStatus)
|
|
469
|
+
.filter(([, status]) => status !== 'ok')
|
|
470
|
+
.map(([parent]) => parent),
|
|
471
|
+
roster: after.roster,
|
|
472
|
+
parentStatus,
|
|
473
|
+
}
|
|
474
|
+
},
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
define(ctx, {
|
|
478
|
+
name: 'tower_send',
|
|
479
|
+
description:
|
|
480
|
+
'Send a durable inbox message to a tower participant: a roster agent name, "tower", or "all" (broadcast). The message is persisted first; when the recipient is a currently running roster agent the tower also steers it into that child\'s current turn. Returns delivery results.',
|
|
481
|
+
parameters: {
|
|
482
|
+
type: 'object',
|
|
483
|
+
properties: {
|
|
484
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
485
|
+
to: { type: 'string', description: 'Recipient: a roster agent name, "tower", or "all".' },
|
|
486
|
+
subject: { type: 'string', description: 'One-line subject.' },
|
|
487
|
+
body: { type: 'string', description: 'Full message body (markdown).' },
|
|
488
|
+
scope: { type: 'string', description: 'Optional scope tag, e.g. the mission id.' },
|
|
489
|
+
},
|
|
490
|
+
required: ['repoRoot', 'to', 'subject', 'body'],
|
|
491
|
+
additionalProperties: false,
|
|
492
|
+
},
|
|
493
|
+
output: OUTPUT,
|
|
494
|
+
async execute(args, exec) {
|
|
495
|
+
const tower = await Tower.open(args.repoRoot)
|
|
496
|
+
const view = await tower.rosterView()
|
|
497
|
+
const self = view.roster.find((entry) => entry.agentId === String(exec.agent.id))
|
|
498
|
+
const from = self ? self.name : 'tower'
|
|
499
|
+
if (typeof args.subject !== 'string' || args.subject.trim().length === 0) {
|
|
500
|
+
fail('tower_send requires a subject')
|
|
501
|
+
}
|
|
502
|
+
if (typeof args.body !== 'string' || args.body.trim().length === 0) {
|
|
503
|
+
fail('tower_send requires a body')
|
|
504
|
+
}
|
|
505
|
+
const message = await tower.recordMessage({
|
|
506
|
+
from,
|
|
507
|
+
to: args.to,
|
|
508
|
+
subject: args.subject.trim(),
|
|
509
|
+
body: args.body,
|
|
510
|
+
scope: args.scope,
|
|
511
|
+
})
|
|
512
|
+
|
|
513
|
+
const subagents = ctx.get('subagents')
|
|
514
|
+
const results = []
|
|
515
|
+
if (!subagents) {
|
|
516
|
+
results.push({ target: '*', pushed: false, reason: 'subagents service unavailable; persisted only' })
|
|
517
|
+
} else {
|
|
518
|
+
const roster = view.roster
|
|
519
|
+
const parents = [...new Set(roster.map((entry) => entry.parentSessionId).filter(Boolean))]
|
|
520
|
+
const activity = new Map()
|
|
521
|
+
for (const parent of parents) {
|
|
522
|
+
try {
|
|
523
|
+
const rows = await subagents.listDescendants(parent)
|
|
524
|
+
for (const row of rows) activity.set(String(row.id), row.activity ?? 'unknown')
|
|
525
|
+
} catch {
|
|
526
|
+
// an unreadable parent catalog only means "do not wake it"
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
const targets =
|
|
530
|
+
args.to === 'all' ? roster.map((entry) => entry.name) : [args.to]
|
|
531
|
+
for (const targetName of targets) {
|
|
532
|
+
if (targetName === 'tower') {
|
|
533
|
+
results.push({ target: 'tower', pushed: false, reason: 'tower reads its inbox' })
|
|
534
|
+
continue
|
|
535
|
+
}
|
|
536
|
+
const entry = roster.find((candidate) => candidate.name === targetName)
|
|
537
|
+
if (!entry) {
|
|
538
|
+
results.push({ target: targetName, pushed: false, reason: 'not in roster' })
|
|
539
|
+
continue
|
|
540
|
+
}
|
|
541
|
+
if (activity.get(entry.agentId) !== 'running') {
|
|
542
|
+
results.push({ target: targetName, pushed: false, reason: 'not running; queued in inbox' })
|
|
543
|
+
continue
|
|
544
|
+
}
|
|
545
|
+
try {
|
|
546
|
+
await subagents.sendMessage(
|
|
547
|
+
exec.agent,
|
|
548
|
+
entry.agentId,
|
|
549
|
+
[
|
|
550
|
+
{
|
|
551
|
+
type: 'text',
|
|
552
|
+
text: `[tower inbox ${message.id}] ${message.subject}\n\n${message.body}`,
|
|
553
|
+
},
|
|
554
|
+
],
|
|
555
|
+
{ signal: exec.signal },
|
|
556
|
+
)
|
|
557
|
+
results.push({ target: targetName, pushed: true })
|
|
558
|
+
} catch (error) {
|
|
559
|
+
results.push({ target: targetName, pushed: false, reason: String(error?.message ?? error) })
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
if (results.length > 0) await tower.recordDelivery(message.id, results)
|
|
564
|
+
return {
|
|
565
|
+
message: { id: message.id, from, to: message.to, subject: message.subject },
|
|
566
|
+
deliveries: results,
|
|
567
|
+
}
|
|
568
|
+
},
|
|
569
|
+
})
|
|
570
|
+
|
|
571
|
+
define(ctx, {
|
|
572
|
+
name: 'tower_inbox',
|
|
573
|
+
description:
|
|
574
|
+
'Read your tower inbox: messages addressed to you plus broadcasts, newest first. Only returned messages are acknowledged; the result reports unreadRemaining, so keep reading until it reaches 0. Set include_read for history. Unread messages block tower_complete.',
|
|
575
|
+
parameters: {
|
|
576
|
+
type: 'object',
|
|
577
|
+
properties: {
|
|
578
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
579
|
+
limit: { type: 'number', description: 'Max messages to return (default 20), newest first.' },
|
|
580
|
+
include_read: { type: 'boolean', description: 'Include previously acknowledged messages for history lookup.' },
|
|
581
|
+
},
|
|
582
|
+
required: ['repoRoot'],
|
|
583
|
+
additionalProperties: false,
|
|
584
|
+
},
|
|
585
|
+
output: OUTPUT,
|
|
586
|
+
async execute(args, exec) {
|
|
587
|
+
const tower = await Tower.open(args.repoRoot)
|
|
588
|
+
const view = await tower.rosterView()
|
|
589
|
+
const self = view.roster.find((entry) => entry.agentId === String(exec.agent.id))
|
|
590
|
+
const reader = self ? self.name : 'tower'
|
|
591
|
+
return tower.inbox({ reader, limit: args.limit, includeRead: args.include_read === true })
|
|
592
|
+
},
|
|
593
|
+
})
|
|
594
|
+
|
|
595
|
+
define(ctx, {
|
|
596
|
+
name: 'tower_complete',
|
|
597
|
+
description:
|
|
598
|
+
'Worker-only: report mission completion. Enforced by the store: every task done or dropped, no blockers, the mission branch has at least one commit since its spawn base, and no unread inbox messages. Stores the full report; completion means awaiting review, not merged.',
|
|
599
|
+
parameters: {
|
|
600
|
+
type: 'object',
|
|
601
|
+
properties: {
|
|
602
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
603
|
+
missionId: { type: 'string', description: 'Mission id, e.g. M1.' },
|
|
604
|
+
report: { type: 'string', description: 'Full completion report for the tower and the next review round.' },
|
|
605
|
+
},
|
|
606
|
+
required: ['repoRoot', 'missionId', 'report'],
|
|
607
|
+
additionalProperties: false,
|
|
608
|
+
},
|
|
609
|
+
output: OUTPUT,
|
|
610
|
+
async execute(args, exec) {
|
|
611
|
+
const tower = await Tower.open(args.repoRoot)
|
|
612
|
+
return tower.complete({
|
|
613
|
+
missionId: args.missionId,
|
|
614
|
+
agentId: String(exec.agent.id),
|
|
615
|
+
report: args.report,
|
|
616
|
+
})
|
|
617
|
+
},
|
|
618
|
+
})
|
|
619
|
+
|
|
620
|
+
define(ctx, {
|
|
621
|
+
name: 'tower_finding',
|
|
622
|
+
description:
|
|
623
|
+
'File a structured finding (bug / improve / vuln / idea) for the tower to route. Use this for anything notable OUTSIDE your mission scope instead of editing out-of-scope files. Severity is one of low|medium|high|critical.',
|
|
624
|
+
parameters: {
|
|
625
|
+
type: 'object',
|
|
626
|
+
properties: {
|
|
627
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
628
|
+
type: { type: 'string', enum: ['bug', 'improve', 'vuln', 'idea'], description: 'Finding category.' },
|
|
629
|
+
title: { type: 'string', description: 'Short finding title.' },
|
|
630
|
+
severity: { type: 'string', enum: ['low', 'medium', 'high', 'critical'], description: 'Optional severity.' },
|
|
631
|
+
summary: { type: 'string', description: 'What was found, in a sentence or two.' },
|
|
632
|
+
location: { type: 'string', description: 'File/symbol the finding concerns.' },
|
|
633
|
+
details: { type: 'string', description: 'Full details: evidence, reproduction, impact.' },
|
|
634
|
+
suggested_fix: { type: 'string', description: 'What you would do about it.' },
|
|
635
|
+
missionId: { type: 'string', description: 'Optional mission this finding relates to.' },
|
|
636
|
+
},
|
|
637
|
+
required: ['repoRoot', 'type', 'title', 'summary', 'details', 'suggested_fix'],
|
|
638
|
+
additionalProperties: false,
|
|
639
|
+
},
|
|
640
|
+
output: OUTPUT,
|
|
641
|
+
async execute(args, exec) {
|
|
642
|
+
const tower = await Tower.open(args.repoRoot)
|
|
643
|
+
const view = await tower.rosterView()
|
|
644
|
+
const self = view.roster.find((entry) => entry.agentId === String(exec.agent.id))
|
|
645
|
+
return tower.addFinding({
|
|
646
|
+
type: args.type,
|
|
647
|
+
title: args.title,
|
|
648
|
+
severity: args.severity,
|
|
649
|
+
summary: args.summary,
|
|
650
|
+
location: args.location,
|
|
651
|
+
details: args.details,
|
|
652
|
+
suggested_fix: args.suggested_fix,
|
|
653
|
+
missionId: args.missionId,
|
|
654
|
+
reportedBy: self ? self.name : 'tower',
|
|
655
|
+
})
|
|
656
|
+
},
|
|
657
|
+
})
|
|
658
|
+
|
|
659
|
+
define(ctx, {
|
|
660
|
+
name: 'tower_review',
|
|
661
|
+
description:
|
|
662
|
+
'Submit an independent review verdict for a completed mission, bound to the current branch tip. status is "clean", "p1-Nitems", or "p2-Nitems"; merge is "merge", "fix-then-merge", or "hold" (only clean + merge is later approval). Non-clean returns the mission to active. Reviewers are capped at 5 rounds per branch; a tower-submitted review is break-glass and requires breakGlassReason.',
|
|
663
|
+
parameters: {
|
|
664
|
+
type: 'object',
|
|
665
|
+
properties: {
|
|
666
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
667
|
+
missionId: { type: 'string', description: 'Mission id, e.g. M1.' },
|
|
668
|
+
status: { type: 'string', description: 'Verdict: "clean", or "p1-Nitems" / "p2-Nitems" with the number of findings at that priority.' },
|
|
669
|
+
merge: { type: 'string', enum: ['merge', 'fix-then-merge', 'hold'], description: 'Merge recommendation for the tower.' },
|
|
670
|
+
findings: { type: 'string', description: 'Full findings text (markdown); write "none" when clean.' },
|
|
671
|
+
checks: { type: 'array', items: { type: 'string' }, description: 'Checklist items you verified.' },
|
|
672
|
+
decision: { type: 'string', description: 'The reasoning behind your verdict.' },
|
|
673
|
+
breakGlassReason: { type: 'string', description: 'Tower-only exceptional self-review: why no independent reviewer was used.' },
|
|
674
|
+
},
|
|
675
|
+
required: ['repoRoot', 'missionId', 'status', 'merge', 'findings', 'decision'],
|
|
676
|
+
additionalProperties: false,
|
|
677
|
+
},
|
|
678
|
+
output: OUTPUT,
|
|
679
|
+
async execute(args, exec) {
|
|
680
|
+
const tower = await Tower.open(args.repoRoot)
|
|
681
|
+
return tower.submitReview({
|
|
682
|
+
missionId: args.missionId,
|
|
683
|
+
agentId: String(exec.agent.id),
|
|
684
|
+
status: args.status,
|
|
685
|
+
merge: args.merge,
|
|
686
|
+
findings: args.findings,
|
|
687
|
+
checks: args.checks,
|
|
688
|
+
decision: args.decision,
|
|
689
|
+
breakGlassReason: args.breakGlassReason,
|
|
690
|
+
})
|
|
691
|
+
},
|
|
692
|
+
})
|
|
693
|
+
|
|
694
|
+
define(ctx, {
|
|
695
|
+
name: 'tower_preflight',
|
|
696
|
+
description:
|
|
697
|
+
'Read-only merge gate for one mission: returns ok plus stable blocker codes (branch-missing, deps-unmerged, read-only-survey, stale-base, no-review, review-hold, not-clean, review-not-approved, tip-moved, review-mission-mismatch, out-of-scope, base-dirty, branch-owned-by-closed-missions, and mission/worker guards). Never changes anything.',
|
|
698
|
+
parameters: {
|
|
699
|
+
type: 'object',
|
|
700
|
+
properties: {
|
|
701
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
702
|
+
missionId: { type: 'string', description: 'Mission id, e.g. M1.' },
|
|
703
|
+
},
|
|
704
|
+
required: ['repoRoot', 'missionId'],
|
|
705
|
+
additionalProperties: false,
|
|
706
|
+
},
|
|
707
|
+
output: OUTPUT,
|
|
708
|
+
async execute(args) {
|
|
709
|
+
const tower = await Tower.open(args.repoRoot)
|
|
710
|
+
const { activity } = await observeActivity(ctx, tower)
|
|
711
|
+
const gate = await tower.evaluateGate({ missionId: args.missionId, activity })
|
|
712
|
+
return {
|
|
713
|
+
ok: gate.ok,
|
|
714
|
+
blockers: gate.blockers,
|
|
715
|
+
missionId: args.missionId,
|
|
716
|
+
branchTip: gate.branchTip,
|
|
717
|
+
baseTip: gate.baseTip,
|
|
718
|
+
changedFiles: gate.changedFiles,
|
|
719
|
+
review: gate.review
|
|
720
|
+
? {
|
|
721
|
+
round: gate.review.round,
|
|
722
|
+
status: gate.review.status,
|
|
723
|
+
mergeVerdict: gate.review.mergeVerdict,
|
|
724
|
+
reviewer: gate.review.reviewer,
|
|
725
|
+
reviewedCommit: gate.review.reviewedCommit,
|
|
726
|
+
}
|
|
727
|
+
: null,
|
|
728
|
+
}
|
|
729
|
+
},
|
|
730
|
+
})
|
|
731
|
+
|
|
732
|
+
define(ctx, {
|
|
733
|
+
name: 'tower_merge',
|
|
734
|
+
description:
|
|
735
|
+
'Merge a mission branch into the recorded base branch with --no-ff, after the hard gate passes. Verifies the two merge parents and records the mission as merged. A failed gate returns stable blocker codes instead of merging; a Git conflict aborts the merge and marks the mission blocked.',
|
|
736
|
+
parameters: {
|
|
737
|
+
type: 'object',
|
|
738
|
+
properties: {
|
|
739
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
740
|
+
missionId: { type: 'string', description: 'Mission id, e.g. M1.' },
|
|
741
|
+
},
|
|
742
|
+
required: ['repoRoot', 'missionId'],
|
|
743
|
+
additionalProperties: false,
|
|
744
|
+
},
|
|
745
|
+
output: OUTPUT,
|
|
746
|
+
async execute(args) {
|
|
747
|
+
const tower = await Tower.open(args.repoRoot)
|
|
748
|
+
const { activity } = await observeActivity(ctx, tower)
|
|
749
|
+
return tower.merge({ missionId: args.missionId, activity })
|
|
750
|
+
},
|
|
751
|
+
})
|
|
752
|
+
|
|
753
|
+
define(ctx, {
|
|
754
|
+
name: 'tower_rebase',
|
|
755
|
+
description:
|
|
756
|
+
'Rebase a mission branch onto the current base tip inside its own worktree. Refuses while its worker is running or the worktree is dirty. A clean conflict-free rebase waives re-review for the old review; a conflict is aborted and the mission is marked blocked.',
|
|
757
|
+
parameters: {
|
|
758
|
+
type: 'object',
|
|
759
|
+
properties: {
|
|
760
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
761
|
+
missionId: { type: 'string', description: 'Mission id, e.g. M1.' },
|
|
762
|
+
},
|
|
763
|
+
required: ['repoRoot', 'missionId'],
|
|
764
|
+
additionalProperties: false,
|
|
765
|
+
},
|
|
766
|
+
output: OUTPUT,
|
|
767
|
+
async execute(args) {
|
|
768
|
+
const tower = await Tower.open(args.repoRoot)
|
|
769
|
+
const { activity } = await observeActivity(ctx, tower)
|
|
770
|
+
return tower.rebase({ missionId: args.missionId, activity })
|
|
771
|
+
},
|
|
772
|
+
})
|
|
773
|
+
|
|
774
|
+
define(ctx, {
|
|
775
|
+
name: 'tower_teardown',
|
|
776
|
+
description:
|
|
777
|
+
'Tear down eligible mission worktrees after their missions are merged or abandoned. A worktree whose roster agent is running is always kept (force cannot override); dirty worktrees need force; exclude keeps named missions. dry_run previews the remove/keep decisions without changing anything.',
|
|
778
|
+
parameters: {
|
|
779
|
+
type: 'object',
|
|
780
|
+
properties: {
|
|
781
|
+
repoRoot: { type: 'string', description: 'Absolute path inside the target Git repository.' },
|
|
782
|
+
force: { type: 'boolean', description: 'Remove worktrees that contain uncommitted changes (never a live agent).' },
|
|
783
|
+
exclude: { type: 'array', items: { type: 'string' }, description: 'Mission ids to keep.' },
|
|
784
|
+
dry_run: { type: 'boolean', description: 'Preview remove/keep decisions without changing disk or state.' },
|
|
785
|
+
},
|
|
786
|
+
required: ['repoRoot'],
|
|
787
|
+
additionalProperties: false,
|
|
788
|
+
},
|
|
789
|
+
output: OUTPUT,
|
|
790
|
+
async execute(args) {
|
|
791
|
+
const tower = await Tower.open(args.repoRoot)
|
|
792
|
+
const { activity } = await observeActivity(ctx, tower)
|
|
793
|
+
return tower.teardown({
|
|
794
|
+
force: args.force === true,
|
|
795
|
+
exclude: args.exclude,
|
|
796
|
+
dryRun: args.dry_run === true,
|
|
797
|
+
activity,
|
|
798
|
+
})
|
|
799
|
+
},
|
|
800
|
+
})
|
|
801
|
+
}
|