@temmo/cli 0.0.0 → 0.19.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 (445) hide show
  1. package/.claude-plugin/marketplace.json +14 -0
  2. package/.claude-plugin/plugin.json +16 -0
  3. package/.codex-plugin/plugin.json +30 -0
  4. package/.cursor-plugin/plugin.json +22 -0
  5. package/.mcp.json +18 -0
  6. package/LICENSE +202 -0
  7. package/README.md +82 -2
  8. package/THIRD_PARTY_NOTICES +32 -0
  9. package/commands/afk.md +17 -0
  10. package/deploy/build-ui.mjs +65 -0
  11. package/deploy/claude/README.md +16 -0
  12. package/deploy/claude/auto-mode.json +31 -0
  13. package/deploy/postgres/compose.yaml +26 -0
  14. package/deploy/temmo-runner.service +24 -0
  15. package/deploy/verify-ui.mjs +15 -0
  16. package/harness-core/README.md +27 -0
  17. package/harness-core/bin/harness.mjs +17 -0
  18. package/harness-core/package.json +10 -0
  19. package/harness-core/stage.mjs +108 -0
  20. package/harness-core/stages/commit.md +7 -0
  21. package/harness-core/stages/pr.md +7 -0
  22. package/harness-core/stages/push.md +7 -0
  23. package/hooks/codex-hooks.json +14 -0
  24. package/hooks/cursor-hooks.json +17 -0
  25. package/hooks/hooks.json +27 -0
  26. package/lib/acp/permission.js +72 -0
  27. package/lib/acp/sessionConfig.js +109 -0
  28. package/lib/activity.js +244 -0
  29. package/lib/agent-guard.js +72 -0
  30. package/lib/agents-route.js +55 -0
  31. package/lib/agents.js +213 -0
  32. package/lib/agui.js +255 -0
  33. package/lib/alerts.js +36 -0
  34. package/lib/analyze-otlp.js +46 -0
  35. package/lib/analyze-run-code.js +397 -0
  36. package/lib/analyze-sources.js +372 -0
  37. package/lib/approval-card.js +54 -0
  38. package/lib/approval-cli.js +53 -0
  39. package/lib/approval-request.js +81 -0
  40. package/lib/approval-signer.js +48 -0
  41. package/lib/approvals.js +295 -0
  42. package/lib/attachment-cache.js +236 -0
  43. package/lib/attachment.js +63 -0
  44. package/lib/blocked.js +20 -0
  45. package/lib/build-review.js +90 -0
  46. package/lib/builder/cli.js +90 -0
  47. package/lib/builder/policy.js +176 -0
  48. package/lib/builder/recover.js +189 -0
  49. package/lib/builder/run.js +240 -0
  50. package/lib/builder/settle.js +198 -0
  51. package/lib/builder/stage.js +240 -0
  52. package/lib/builder/state.js +73 -0
  53. package/lib/builder/supervisor.js +289 -0
  54. package/lib/builder-clean.js +223 -0
  55. package/lib/builder-code.cjs +67 -0
  56. package/lib/builder.js +9 -0
  57. package/lib/bus-postgres.js +281 -0
  58. package/lib/bus.js +349 -0
  59. package/lib/catalog/author.js +278 -0
  60. package/lib/catalog/dot.js +426 -0
  61. package/lib/catalog/pattern-model.js +193 -0
  62. package/lib/catalog/patterns.js +324 -0
  63. package/lib/catalog/projects.js +83 -0
  64. package/lib/catalog/records.js +2 -0
  65. package/lib/catalog/roster.js +70 -0
  66. package/lib/catalog/team-files.js +49 -0
  67. package/lib/catalog/teams.js +109 -0
  68. package/lib/claude-socket.js +333 -0
  69. package/lib/cli.js +741 -0
  70. package/lib/code-mode.js +45 -0
  71. package/lib/code-read-policy.yaml +9 -0
  72. package/lib/code-run.js +461 -0
  73. package/lib/codex-compact.js +141 -0
  74. package/lib/compaction.js +21 -0
  75. package/lib/config.js +424 -0
  76. package/lib/cursor-trace.js +248 -0
  77. package/lib/deep-link.js +20 -0
  78. package/lib/deliver-push.js +120 -0
  79. package/lib/delivery.js +28 -0
  80. package/lib/deploy-tree.js +227 -0
  81. package/lib/durable.js +404 -0
  82. package/lib/factory-tick.js +32 -0
  83. package/lib/family.js +63 -0
  84. package/lib/fetch-origin.js +53 -0
  85. package/lib/format.js +133 -0
  86. package/lib/harness-surface-cli.js +153 -0
  87. package/lib/harness-surface.js +486 -0
  88. package/lib/harness.js +271 -0
  89. package/lib/hook.mjs +291 -0
  90. package/lib/idle.js +40 -0
  91. package/lib/install-cursor.js +115 -0
  92. package/lib/install-opencode.js +105 -0
  93. package/lib/job.js +82 -0
  94. package/lib/land-cli.js +319 -0
  95. package/lib/land-member.js +64 -0
  96. package/lib/land.js +320 -0
  97. package/lib/lessons.js +223 -0
  98. package/lib/limit-alert.js +423 -0
  99. package/lib/limits.js +284 -0
  100. package/lib/machine-prompt.js +19 -0
  101. package/lib/merge-sweep.js +149 -0
  102. package/lib/messaging.js +1002 -0
  103. package/lib/mission-cli.js +103 -0
  104. package/lib/mission.js +241 -0
  105. package/lib/mode.js +215 -0
  106. package/lib/models.js +114 -0
  107. package/lib/names.js +86 -0
  108. package/lib/opencode-api.js +57 -0
  109. package/lib/opencode-plugin.mjs +313 -0
  110. package/lib/operator-token.js +78 -0
  111. package/lib/operator.js +187 -0
  112. package/lib/pad-activity.js +143 -0
  113. package/lib/pad-fresh.js +44 -0
  114. package/lib/pad.js +198 -0
  115. package/lib/pattern-cli.js +120 -0
  116. package/lib/pattern-compiler.js +68 -0
  117. package/lib/pattern-runs.js +795 -0
  118. package/lib/pattern-stall.js +58 -0
  119. package/lib/pattern-tick.js +45 -0
  120. package/lib/pattern-workflow.js +69 -0
  121. package/lib/pi-extension.mjs +98 -0
  122. package/lib/pi.js +57 -0
  123. package/lib/plugin-copy.js +76 -0
  124. package/lib/pm-review.js +32 -0
  125. package/lib/pm-workspace.js +87 -0
  126. package/lib/postgres.js +115 -0
  127. package/lib/pr-card.js +34 -0
  128. package/lib/probe.js +295 -0
  129. package/lib/process.js +67 -0
  130. package/lib/project-pause.js +23 -0
  131. package/lib/prune.js +793 -0
  132. package/lib/questions.js +250 -0
  133. package/lib/receipts.js +63 -0
  134. package/lib/reflect.js +962 -0
  135. package/lib/rename-migration.js +118 -0
  136. package/lib/run-terminal.js +437 -0
  137. package/lib/runner-request.js +33 -0
  138. package/lib/sandbox-app.js +120 -0
  139. package/lib/sandbox-claims.js +258 -0
  140. package/lib/sandbox-cli.js +188 -0
  141. package/lib/sandbox-hosts-cli.js +68 -0
  142. package/lib/sandbox-hosts.js +94 -0
  143. package/lib/sandbox-pr.js +102 -0
  144. package/lib/sandbox-proposal.js +367 -0
  145. package/lib/sandbox-tools.js +143 -0
  146. package/lib/sandbox.js +501 -0
  147. package/lib/schedule-session.js +68 -0
  148. package/lib/session-activity.js +67 -0
  149. package/lib/session-events.js +94 -0
  150. package/lib/sessions/control.js +244 -0
  151. package/lib/sessions/git-refs.js +265 -0
  152. package/lib/sessions/launch.js +359 -0
  153. package/lib/sessions/registry.js +450 -0
  154. package/lib/sessions/scope.js +111 -0
  155. package/lib/setup.js +377 -0
  156. package/lib/signed-approval.js +225 -0
  157. package/lib/slash.js +167 -0
  158. package/lib/spawn-cli.js +45 -0
  159. package/lib/spawn.js +107 -0
  160. package/lib/start-limit.js +58 -0
  161. package/lib/state.js +24 -0
  162. package/lib/stop.js +308 -0
  163. package/lib/store-read.js +62 -0
  164. package/lib/store.js +252 -0
  165. package/lib/subagent-guard.js +240 -0
  166. package/lib/surfaces/channel-post.js +43 -0
  167. package/lib/surfaces/instructions.js +15 -0
  168. package/lib/surfaces/mcp-reload.js +144 -0
  169. package/lib/surfaces/ops.js +629 -0
  170. package/lib/surfaces/render-mcp.js +33 -0
  171. package/lib/surfaces/run-terminal-ops.js +40 -0
  172. package/lib/surfaces/sandbox-ops.js +36 -0
  173. package/lib/surfaces/schema.js +7 -0
  174. package/lib/surfaces/task-view.js +90 -0
  175. package/lib/surfaces/types.js +4 -0
  176. package/lib/task-cli.js +274 -0
  177. package/lib/task-order.js +34 -0
  178. package/lib/task-order.mjs +2 -0
  179. package/lib/task-route.js +125 -0
  180. package/lib/tasks.js +631 -0
  181. package/lib/team-permission.js +34 -0
  182. package/lib/team-templates/blitz/team.dot +9 -0
  183. package/lib/team-templates/factory/team.dot +26 -0
  184. package/lib/team-templates/feature/team.dot +14 -0
  185. package/lib/team-templates/fix-and-review/team.dot +12 -0
  186. package/lib/team-templates/product/team.dot +16 -0
  187. package/lib/team-templates/release/team.dot +7 -0
  188. package/lib/team-templates/review/team.dot +6 -0
  189. package/lib/terminal-draft.js +42 -0
  190. package/lib/thread-branch.js +82 -0
  191. package/lib/thread-channel-store.js +181 -0
  192. package/lib/thread-channels.js +116 -0
  193. package/lib/thread-listening.js +20 -0
  194. package/lib/thread-routes.js +91 -0
  195. package/lib/threads.js +441 -0
  196. package/lib/tool-api.js +33 -0
  197. package/lib/transcript.js +98 -0
  198. package/lib/ui-build-output.js +19 -0
  199. package/lib/ui-capabilities.js +90 -0
  200. package/lib/usage.js +182 -0
  201. package/lib/vendor/mcp-sdk.d.mts +4 -0
  202. package/lib/vendor/mcp-sdk.mjs +17046 -0
  203. package/lib/waiter.js +127 -0
  204. package/lib/workflows.js +546 -0
  205. package/lib/worktree-setup.js +86 -0
  206. package/package.json +112 -1
  207. package/runner/adapters/claude.js +465 -0
  208. package/runner/adapters/cli-sandbox.js +277 -0
  209. package/runner/adapters/codex.js +675 -0
  210. package/runner/adapters/cursor.mjs +937 -0
  211. package/runner/adapters/fake.js +119 -0
  212. package/runner/adapters/opencode-sandbox.js +370 -0
  213. package/runner/adapters/opencode.js +486 -0
  214. package/runner/adapters/pi-sandbox.js +134 -0
  215. package/runner/adapters/pi.js +364 -0
  216. package/runner/client.js +40 -0
  217. package/runner/contract.js +129 -0
  218. package/runner/control.js +178 -0
  219. package/runner/delivery.js +184 -0
  220. package/runner/factory-tick.mjs +10 -0
  221. package/runner/lock.js +41 -0
  222. package/runner/main.mjs +118 -0
  223. package/runner/pattern-tick.mjs +16 -0
  224. package/runner/publish.js +113 -0
  225. package/runner/runner.js +2612 -0
  226. package/runner/sandbox-run.mjs +239 -0
  227. package/runner/schedules.js +294 -0
  228. package/runner/task-wake.js +59 -0
  229. package/scripts/ai-collab +3 -0
  230. package/scripts/ai-collab-bus.mjs +4 -0
  231. package/scripts/ai-collab-code-mcp.mjs +3 -0
  232. package/scripts/ai-collab-hook +3 -0
  233. package/scripts/ai-collab-hook.mjs +3 -0
  234. package/scripts/ai-collab-mcp-tools.mjs +2 -0
  235. package/scripts/ai-collab-mcp.mjs +3 -0
  236. package/scripts/ai-collab.mjs +3 -0
  237. package/scripts/install-cursor.mjs +11 -0
  238. package/scripts/install-opencode.mjs +10 -0
  239. package/scripts/temmo +3 -0
  240. package/scripts/temmo-bus.mjs +151 -0
  241. package/scripts/temmo-code-mcp.mjs +63 -0
  242. package/scripts/temmo-hook +3 -0
  243. package/scripts/temmo-hook.mjs +201 -0
  244. package/scripts/temmo-mcp-tools.mjs +22 -0
  245. package/scripts/temmo-mcp.mjs +27 -0
  246. package/scripts/temmo.mjs +4 -0
  247. package/skills/antislop/SKILL.md +701 -0
  248. package/skills/antislop/VERSION +1 -0
  249. package/skills/antislop-code/SKILL.md +128 -0
  250. package/skills/antislop-human/LICENSE-web-quality-skills +21 -0
  251. package/skills/antislop-human/SKILL.md +204 -0
  252. package/skills/antislop-human/contrast-check.py +168 -0
  253. package/skills/antislop-human/contrast-mcp.py +125 -0
  254. package/skills/antislop-human/references/A11Y-PATTERNS.md +249 -0
  255. package/skills/antislop-human/references/WCAG.md +127 -0
  256. package/skills/antislop-layoutmobile/SKILL.md +170 -0
  257. package/skills/antislop-ui/LICENSE-web-quality-skills +21 -0
  258. package/skills/antislop-ui/SKILL.md +351 -0
  259. package/skills/antislop-ui/references/CORE-WEB-VITALS.md +181 -0
  260. package/skills/antislop-ui/references/WEB-BASELINE.md +28 -0
  261. package/skills/harness-surface/SKILL.md +42 -0
  262. package/skills/land/SKILL.md +32 -0
  263. package/skills/memory-file/SKILL.md +39 -0
  264. package/skills/temmo/SKILL.md +101 -0
  265. package/skills/temmo/references/role-programs.md +47 -0
  266. package/skills/ui-sdk/SKILL.md +56 -0
  267. package/skills/ui-sdk/references/agent-ready.md +136 -0
  268. package/skills/ui-sdk/references/build-interface.md +187 -0
  269. package/skills/ui-sdk/references/create-component.md +88 -0
  270. package/skills/ui-sdk/references/patterns.md +246 -0
  271. package/skills/ui-sdk/references/styles.md +66 -0
  272. package/skills/ui-sdk/references/testing.md +172 -0
  273. package/skills/ui-sdk/references/web-components.md +78 -0
  274. package/tools/screen-state.mjs +119 -0
  275. package/tools/smoke-ui.mjs +540 -0
  276. package/web/app/activity.jsx +251 -0
  277. package/web/app/agent-mark.tsx +43 -0
  278. package/web/app/alert-level.jsx +16 -0
  279. package/web/app/api/[action]/route.js +8 -0
  280. package/web/app/api/auth/[step]/route.js +12 -0
  281. package/web/app/api/events/route.js +8 -0
  282. package/web/app/api/file/[name]/route.js +8 -0
  283. package/web/app/api/land/route.js +12 -0
  284. package/web/app/api/lessons/route.js +8 -0
  285. package/web/app/api/push/key/route.js +8 -0
  286. package/web/app/api/push/subscribe/route.js +12 -0
  287. package/web/app/api/push/test/route.js +8 -0
  288. package/web/app/api/refs/route.js +8 -0
  289. package/web/app/api/review-diff/route.js +8 -0
  290. package/web/app/api/schedules/route.js +8 -0
  291. package/web/app/api/thread-diff/route.js +8 -0
  292. package/web/app/api/version/route.js +6 -0
  293. package/web/app/cards.jsx +189 -0
  294. package/web/app/chat-api.js +82 -0
  295. package/web/app/chat-composer.jsx +313 -0
  296. package/web/app/chat-console.jsx +691 -0
  297. package/web/app/chat-image.tsx +61 -0
  298. package/web/app/chat-message.jsx +122 -0
  299. package/web/app/chat-shared.jsx +59 -0
  300. package/web/app/chat-sidebar.jsx +25 -0
  301. package/web/app/chat-stream.js +127 -0
  302. package/web/app/chat.jsx +31 -0
  303. package/web/app/compactions.jsx +57 -0
  304. package/web/app/composer-actions.tsx +87 -0
  305. package/web/app/decisions.jsx +53 -0
  306. package/web/app/fonts/OFL-hanken-grotesk.txt +93 -0
  307. package/web/app/fonts/OFL-ibm-plex-mono.txt +93 -0
  308. package/web/app/fonts/hanken-grotesk-latin-wght-normal.woff2 +0 -0
  309. package/web/app/fonts/ibm-plex-mono-latin-400-normal.woff2 +0 -0
  310. package/web/app/fonts/ibm-plex-mono-latin-500-normal.woff2 +0 -0
  311. package/web/app/globals.css +199 -0
  312. package/web/app/home.jsx +246 -0
  313. package/web/app/icon.svg +1 -0
  314. package/web/app/land-panel.jsx +63 -0
  315. package/web/app/land-reviews.jsx +20 -0
  316. package/web/app/layout.jsx +17 -0
  317. package/web/app/lessons.tsx +76 -0
  318. package/web/app/logo.tsx +13 -0
  319. package/web/app/logos.jsx +16 -0
  320. package/web/app/manifest.js +8 -0
  321. package/web/app/mode-menu.tsx +46 -0
  322. package/web/app/new-session.jsx +275 -0
  323. package/web/app/notify.jsx +180 -0
  324. package/web/app/outcomes.tsx +94 -0
  325. package/web/app/page.jsx +9 -0
  326. package/web/app/pattern-graph.jsx +75 -0
  327. package/web/app/patterns-page.jsx +293 -0
  328. package/web/app/phone-nav.tsx +36 -0
  329. package/web/app/progress-feed.tsx +33 -0
  330. package/web/app/providers.tsx +27 -0
  331. package/web/app/quote.jsx +15 -0
  332. package/web/app/run-card.tsx +200 -0
  333. package/web/app/run-requests.tsx +148 -0
  334. package/web/app/scheduled.jsx +105 -0
  335. package/web/app/session-panel.tsx +174 -0
  336. package/web/app/session-toolbar.tsx +180 -0
  337. package/web/app/shadcn.css +638 -0
  338. package/web/app/shipped.jsx +49 -0
  339. package/web/app/subagent-runs.jsx +31 -0
  340. package/web/app/task-graph.jsx +67 -0
  341. package/web/app/tasks-strip.tsx +36 -0
  342. package/web/app/tasks.jsx +375 -0
  343. package/web/app/team-author.jsx +145 -0
  344. package/web/app/team-editor.jsx +88 -0
  345. package/web/app/thread-agents.tsx +32 -0
  346. package/web/app/thread-place.tsx +9 -0
  347. package/web/app/thread-sessions.tsx +149 -0
  348. package/web/app/thread-sidebar.tsx +418 -0
  349. package/web/app/thread-switcher.tsx +27 -0
  350. package/web/app/tool-diff.jsx +94 -0
  351. package/web/components/a2ui/controls.jsx +36 -0
  352. package/web/components/a2ui/option-picker.jsx +23 -0
  353. package/web/components/a2ui/script.tsx +121 -0
  354. package/web/components/a2ui/surface.jsx +81 -0
  355. package/web/components/chat/thread.jsx +50 -0
  356. package/web/components/script-text.tsx +42 -0
  357. package/web/components/ui/avatar.jsx +115 -0
  358. package/web/components/ui/badge.jsx +47 -0
  359. package/web/components/ui/button.jsx +63 -0
  360. package/web/components/ui/chat-container.jsx +52 -0
  361. package/web/components/ui/choice-list.tsx +72 -0
  362. package/web/components/ui/code-block.jsx +60 -0
  363. package/web/components/ui/copy-button.tsx +41 -0
  364. package/web/components/ui/dropdown-menu.jsx +257 -0
  365. package/web/components/ui/markdown.jsx +84 -0
  366. package/web/components/ui/message.jsx +28 -0
  367. package/web/components/ui/popover.jsx +33 -0
  368. package/web/components/ui/scroll-button.jsx +34 -0
  369. package/web/components/ui/select.jsx +189 -0
  370. package/web/components/ui/sheet.jsx +152 -0
  371. package/web/components/ui/sonner.jsx +10 -0
  372. package/web/components/ui/tabs.jsx +88 -0
  373. package/web/components/ui/textarea.jsx +20 -0
  374. package/web/components/ui/tooltip.jsx +56 -0
  375. package/web/components.json +24 -0
  376. package/web/instrumentation.js +8 -0
  377. package/web/lib/a2ui-catalog.js +34 -0
  378. package/web/lib/a2ui-pr.js +76 -0
  379. package/web/lib/a2ui-run-catalog.js +18 -0
  380. package/web/lib/a2ui-run.js +93 -0
  381. package/web/lib/a2ui.js +262 -0
  382. package/web/lib/activity-rows.js +275 -0
  383. package/web/lib/agent-detail.js +5 -0
  384. package/web/lib/alert-level.js +44 -0
  385. package/web/lib/approval-mode.js +22 -0
  386. package/web/lib/chat-turns.js +174 -0
  387. package/web/lib/child-subagents.js +16 -0
  388. package/web/lib/compaction-view.js +48 -0
  389. package/web/lib/composer-actions.js +40 -0
  390. package/web/lib/context-view.js +57 -0
  391. package/web/lib/fuzzy.js +26 -0
  392. package/web/lib/highlight.js +44 -0
  393. package/web/lib/lessons-view.js +23 -0
  394. package/web/lib/mentions.js +16 -0
  395. package/web/lib/message-routing.js +105 -0
  396. package/web/lib/new-session.js +85 -0
  397. package/web/lib/operator-messages.js +28 -0
  398. package/web/lib/pattern-graph.js +165 -0
  399. package/web/lib/pattern-view.js +33 -0
  400. package/web/lib/pending-summary.js +18 -0
  401. package/web/lib/permission-modes.js +30 -0
  402. package/web/lib/project-feed.js +107 -0
  403. package/web/lib/quote.js +7 -0
  404. package/web/lib/reply-quote.js +31 -0
  405. package/web/lib/scheduled.js +79 -0
  406. package/web/lib/selection.js +110 -0
  407. package/web/lib/send-confirmation.js +22 -0
  408. package/web/lib/session-dead.js +10 -0
  409. package/web/lib/session-state.js +132 -0
  410. package/web/lib/sidebar.js +15 -0
  411. package/web/lib/slash-commands.js +164 -0
  412. package/web/lib/start-default.js +35 -0
  413. package/web/lib/start-mode.js +35 -0
  414. package/web/lib/stream-views.js +49 -0
  415. package/web/lib/subagent-state.js +20 -0
  416. package/web/lib/task-board.js +54 -0
  417. package/web/lib/task-graph.js +183 -0
  418. package/web/lib/task-tags.js +41 -0
  419. package/web/lib/team-editor.js +10 -0
  420. package/web/lib/thread-groups.js +86 -0
  421. package/web/lib/threads.js +28 -0
  422. package/web/lib/tool-diff.js +273 -0
  423. package/web/lib/ui-events.js +58 -0
  424. package/web/lib/utils.js +5 -0
  425. package/web/lib/view-memory.js +25 -0
  426. package/web/next.config.mjs +37 -0
  427. package/web/postcss.config.mjs +1 -0
  428. package/web/proxy.js +25 -0
  429. package/web/public/sw.js +39 -0
  430. package/web/server/auth.js +178 -0
  431. package/web/server/dispatch.js +293 -0
  432. package/web/server/history.js +56 -0
  433. package/web/server/hosted.js +58 -0
  434. package/web/server/hub.js +126 -0
  435. package/web/server/instance.js +5 -0
  436. package/web/server/land-reviews.js +64 -0
  437. package/web/server/push.js +148 -0
  438. package/web/server/run-terminal.js +79 -0
  439. package/web/server/schedules.js +38 -0
  440. package/web/server/shipped.js +51 -0
  441. package/web/server/task-board.js +71 -0
  442. package/web/server/threads.js +209 -0
  443. package/web/server/ui.js +2937 -0
  444. package/web/tsconfig.base.json +16 -0
  445. package/web/tsconfig.json +9 -0
@@ -0,0 +1,426 @@
1
+ /** team.dot: parses the DOT subset temmo reads and serializes the canonical form. Attribute rules: docs/teams.md. */
2
+ import { claudePermissionError, HARNESSES, isHarness, parseHarnessModel } from "../harness.js";
3
+ const DOT_DONE = "done";
4
+ export const DOT_KINDS = ["broadcast", "pair", "fan-in"];
5
+ export const DOT_LANDINGS = ["pr", "packet", "branch"];
6
+ const DOT_COUNT_MAX = 8;
7
+ const DOT_CAP_MAX = 10;
8
+ export const DOT_EVENT_MAX = 60;
9
+ const DOT_VISITS_MAX = 50;
10
+ // One limit for a role prompt in team.dot and in validatePattern (RESPONSIBILITY_MAX in pattern-model.ts).
11
+ export const DOT_PROMPT_MAX = 1000;
12
+ // Same shape as ROLE_ID in pattern-model.ts.
13
+ const NODE_ID = /^[a-z][a-z0-9_-]{0,23}$/;
14
+ const SKILL = /^[a-z0-9][a-z0-9-]{0,63}$/;
15
+ const NODE_ATTRS = ["agent", "count", "skill", "prompt", "fallback", "code_mode", "pattern"];
16
+ // A call node names the pattern the runner starts as a child run; it leaves on "done" or "failed".
17
+ export const CALL_EVENTS = ["done", "failed"];
18
+ const PATTERN_NAME = /^[a-z0-9-]{1,24}$/;
19
+ // The harness a call node records; no session of it starts.
20
+ export const CALL_HARNESS = "claude";
21
+ const EDGE_ATTRS = ["on", "kind", "required", "cap", "overflow"];
22
+ // wteam display or session attributes: read, ignored, and reported as warnings.
23
+ const COSMETIC_ATTRS = ["shape", "label", "handoff_tokens", "continue"];
24
+ const GRAPH_ATTRS = ["description", "max_visits", "permission", "landing", "fallbacks"];
25
+ const PUNCT = "{}[]=,;";
26
+ const WORD = /[A-Za-z_][A-Za-z0-9_]*|-?\d+(?:\.\d+)?/y;
27
+ export class DotError extends Error {
28
+ line;
29
+ constructor(message, line) {
30
+ super(line ? `line ${line}: ${message}` : message);
31
+ this.name = "DotError";
32
+ this.line = line;
33
+ }
34
+ }
35
+ const isWord = (token) => token?.type === "string" || token?.type === "id";
36
+ const isPunct = (token, value) => token?.type === "punct" && token.value === value;
37
+ const countLines = (text) => text.split("\n").length - 1;
38
+ function tokenize(text) {
39
+ const tokens = [];
40
+ let line = 1;
41
+ let i = 0;
42
+ while (i < text.length) {
43
+ const c = text[i];
44
+ if (c === "\n") {
45
+ line += 1;
46
+ i += 1;
47
+ continue;
48
+ }
49
+ if (/\s/.test(c)) {
50
+ i += 1;
51
+ continue;
52
+ }
53
+ if (c === "/" && text[i + 1] === "/") {
54
+ while (i < text.length && text[i] !== "\n")
55
+ i += 1;
56
+ continue;
57
+ }
58
+ if (c === "/" && text[i + 1] === "*") {
59
+ const end = text.indexOf("*/", i + 2);
60
+ if (end < 0)
61
+ throw new DotError("unclosed comment", line);
62
+ line += countLines(text.slice(i, end));
63
+ i = end + 2;
64
+ continue;
65
+ }
66
+ if (c === '"') {
67
+ const start = line;
68
+ let value = "";
69
+ i += 1;
70
+ for (;;) {
71
+ if (i >= text.length)
72
+ throw new DotError("unclosed string", start);
73
+ const d = text[i];
74
+ if (d === '"') {
75
+ i += 1;
76
+ break;
77
+ }
78
+ if (d === "\\" && i + 1 < text.length) {
79
+ const e = text[i + 1];
80
+ if (e === '"' || e === "\\")
81
+ value += e;
82
+ else if (e === "n")
83
+ value += "\n";
84
+ else
85
+ value += `\\${e}`;
86
+ if (e === "\n")
87
+ line += 1;
88
+ i += 2;
89
+ continue;
90
+ }
91
+ if (d === "\n")
92
+ line += 1;
93
+ value += d;
94
+ i += 1;
95
+ }
96
+ tokens.push({ type: "string", value, line: start });
97
+ continue;
98
+ }
99
+ if (c === "-" && text[i + 1] === ">") {
100
+ tokens.push({ type: "punct", value: "->", line });
101
+ i += 2;
102
+ continue;
103
+ }
104
+ if (PUNCT.includes(c)) {
105
+ tokens.push({ type: "punct", value: c, line });
106
+ i += 1;
107
+ continue;
108
+ }
109
+ WORD.lastIndex = i;
110
+ const match = WORD.exec(text);
111
+ if (!match && c === "-")
112
+ throw new DotError("quote ids containing -", line);
113
+ if (!match)
114
+ throw new DotError(`unexpected character "${c}"`, line);
115
+ tokens.push({ type: "id", value: match[0], line });
116
+ i += match[0].length;
117
+ }
118
+ return tokens;
119
+ }
120
+ const intIn = (value, lo, hi) => {
121
+ if (!/^\d+$/.test(value))
122
+ return null;
123
+ const n = Number(value);
124
+ return n >= lo && n <= hi ? n : null;
125
+ };
126
+ function checkAgent(value, line) {
127
+ if (parseHarnessModel(String(value)))
128
+ return String(value);
129
+ const [harness, ...rest] = String(value).split(":");
130
+ if (!isHarness(harness)) {
131
+ throw new DotError(`agent harness must be one of ${HARNESSES.join(", ")}; got "${harness}"`, line);
132
+ }
133
+ throw new DotError(`invalid model "${rest.join(":")}" for ${harness}`, line);
134
+ }
135
+ /** Attributes by key, each checked against the allowed list and seen at most once. */
136
+ function attrMap(attrs, allowed, what, warnings) {
137
+ const seen = new Map();
138
+ for (const attr of attrs) {
139
+ if (!allowed.includes(attr.key) && COSMETIC_ATTRS.includes(attr.key)) {
140
+ warnings.push({ line: attr.line, message: `ignored ${what} attribute "${attr.key}" (wteam display attribute)` });
141
+ continue;
142
+ }
143
+ if (!allowed.includes(attr.key))
144
+ throw new DotError(`unknown ${what} attribute "${attr.key}"`, attr.line);
145
+ if (seen.has(attr.key))
146
+ throw new DotError(`duplicate ${what} attribute "${attr.key}"`, attr.line);
147
+ seen.set(attr.key, attr);
148
+ }
149
+ return seen;
150
+ }
151
+ function nodeFrom(id, attrs, line, warnings) {
152
+ if (!NODE_ID.test(id) || id === DOT_DONE)
153
+ throw new DotError(`invalid node id "${id}"`, line);
154
+ const seen = attrMap(attrs, NODE_ATTRS, "node", warnings);
155
+ const call = seen.get("pattern");
156
+ if (call) {
157
+ if (!PATTERN_NAME.test(call.value))
158
+ throw new DotError(`invalid pattern "${call.value}"`, call.line);
159
+ for (const attr of ["agent", "skill", "fallback", "code_mode"])
160
+ if (seen.has(attr))
161
+ throw new DotError(`call node "${id}" takes no ${attr}`, seen.get(attr)?.line ?? line);
162
+ }
163
+ else if (!seen.has("agent"))
164
+ throw new DotError(`node "${id}" needs agent="harness" or agent="harness:model"`, line);
165
+ const node = { id, agent: call ? CALL_HARNESS : checkAgent(seen.get("agent")?.value ?? "", seen.get("agent")?.line ?? line), count: 1 };
166
+ if (call)
167
+ node.pattern = call.value;
168
+ if (seen.has("count")) {
169
+ const { value, line: at } = seen.get("count") ?? { value: "", line };
170
+ const count = intIn(value, 1, DOT_COUNT_MAX);
171
+ if (count === null)
172
+ throw new DotError(`count must be a whole number 1-${DOT_COUNT_MAX}; got "${value}"`, at);
173
+ node.count = count;
174
+ }
175
+ if (seen.has("skill")) {
176
+ const { value, line: at } = seen.get("skill") ?? { value: "", line };
177
+ if (!SKILL.test(value))
178
+ throw new DotError(`invalid skill "${value}"`, at);
179
+ node.skill = value;
180
+ }
181
+ if (seen.has("prompt")) {
182
+ const { value, line: at } = seen.get("prompt") ?? { value: "", line };
183
+ if (value.length > DOT_PROMPT_MAX)
184
+ throw new DotError(`prompt is longer than ${DOT_PROMPT_MAX} characters`, at);
185
+ if (value.trim())
186
+ node.prompt = value.trim();
187
+ }
188
+ if (seen.has("fallback")) {
189
+ const { value, line: at } = seen.get("fallback") ?? { value: "", line };
190
+ node.fallback = checkAgent(value, at);
191
+ }
192
+ if (seen.has("code_mode")) {
193
+ const { value, line: at } = seen.get("code_mode") ?? { value: "", line };
194
+ if (value !== "on" && value !== "off")
195
+ throw new DotError(`code_mode must be on or off; got "${value}"`, at);
196
+ node.codeMode = value === "on";
197
+ }
198
+ Object.defineProperty(node, "line", { value: line, enumerable: false });
199
+ return node;
200
+ }
201
+ function edgeFrom(attrs, line, warnings) {
202
+ const seen = attrMap(attrs, EDGE_ATTRS, "edge", warnings);
203
+ if (!seen.has("on"))
204
+ throw new DotError('edge needs on="event"', line);
205
+ const on = (seen.get("on")?.value ?? "").trim();
206
+ if (!on || on.length > DOT_EVENT_MAX)
207
+ throw new DotError(`edge event must be 1-${DOT_EVENT_MAX} characters`, seen.get("on")?.line ?? line);
208
+ const kind = seen.get("kind")?.value ?? "pair";
209
+ if (!DOT_KINDS.includes(kind))
210
+ throw new DotError(`kind must be one of ${DOT_KINDS.join(", ")}; got "${kind}"`, seen.get("kind")?.line ?? line);
211
+ let required = false;
212
+ if (seen.has("required")) {
213
+ const { value, line: at } = seen.get("required") ?? { value: "", line };
214
+ if (value !== "true" && value !== "false")
215
+ throw new DotError(`required must be true or false; got "${value}"`, at);
216
+ required = value === "true";
217
+ }
218
+ const edge = { on, kind, required };
219
+ if (seen.has("cap")) {
220
+ const { value, line: at } = seen.get("cap") ?? { value: "", line };
221
+ const cap = intIn(value, 1, DOT_CAP_MAX);
222
+ if (cap === null)
223
+ throw new DotError(`cap must be a whole number 1-${DOT_CAP_MAX}; got "${value}"`, at);
224
+ edge.cap = cap;
225
+ }
226
+ if (seen.has("overflow")) {
227
+ const { value, line: at } = seen.get("overflow") ?? { value: "", line };
228
+ if (edge.cap === undefined)
229
+ throw new DotError("overflow needs a cap", at);
230
+ edge.overflow = value.trim();
231
+ }
232
+ return edge;
233
+ }
234
+ /** Parses team.dot text; throws DotError with the line of the first problem. */
235
+ export function parseDot(text) {
236
+ const tokens = tokenize(String(text));
237
+ const lastLine = tokens.at(-1)?.line ?? 1;
238
+ let at = 0;
239
+ const peek = () => tokens[at];
240
+ const take = () => tokens[at++];
241
+ const expect = (value) => {
242
+ const token = peek();
243
+ if (!isPunct(token, value))
244
+ throw new DotError(`expected "${value}"${token ? ` before "${token.value}"` : " at end of file"}`, token?.line ?? lastLine);
245
+ at += 1;
246
+ };
247
+ const attrList = () => {
248
+ const attrs = [];
249
+ expect("[");
250
+ while (!isPunct(peek(), "]")) {
251
+ const key = take();
252
+ if (!key)
253
+ throw new DotError('missing "]" at end of file', lastLine);
254
+ if (isPunct(key, "}"))
255
+ throw new DotError('missing "]" before "}"', key.line);
256
+ if (!isWord(key))
257
+ throw new DotError(`expected an attribute name, found "${key.value}"`, key.line);
258
+ expect("=");
259
+ const value = take();
260
+ if (!isWord(value))
261
+ throw new DotError(`expected a value for "${key.value}"`, key.line);
262
+ attrs.push({ key: key.value, value: value.value, line: key.line });
263
+ if (isPunct(peek(), ",") || isPunct(peek(), ";"))
264
+ at += 1;
265
+ }
266
+ at += 1;
267
+ return attrs;
268
+ };
269
+ const head = take();
270
+ if (head?.type !== "id" || head.value !== "digraph")
271
+ throw new DotError('expected "digraph"', head?.line ?? 1);
272
+ const nameToken = take();
273
+ if (!isWord(nameToken))
274
+ throw new DotError("expected a team name after digraph", nameToken?.line ?? lastLine);
275
+ expect("{");
276
+ const graphs = [];
277
+ const nodeStmts = [];
278
+ const edgeStmts = [];
279
+ while (!isPunct(peek(), "}")) {
280
+ const token = peek();
281
+ if (!token)
282
+ throw new DotError('missing "}" at end of file', lastLine);
283
+ if (isPunct(token, ";")) {
284
+ at += 1;
285
+ continue;
286
+ }
287
+ if (token.type === "id" && token.value === "graph" && isPunct(tokens[at + 1], "[")) {
288
+ at += 1;
289
+ graphs.push({ attrs: attrList(), line: token.line });
290
+ continue;
291
+ }
292
+ if (!isWord(token))
293
+ throw new DotError(`unexpected "${token.value}"`, token.line);
294
+ if (token.type === "id" && ["node", "edge", "subgraph"].includes(token.value) && isPunct(tokens[at + 1], "[")) {
295
+ throw new DotError(`"${token.value}" defaults are not supported; set attributes on each node or edge`, token.line);
296
+ }
297
+ at += 1;
298
+ if (isPunct(peek(), "="))
299
+ throw new DotError("graph attribute assignments are not supported; use graph [key=value]", token.line);
300
+ if (isPunct(peek(), "->")) {
301
+ const ids = [token.value];
302
+ while (isPunct(peek(), "->")) {
303
+ at += 1;
304
+ const target = take();
305
+ if (!isWord(target))
306
+ throw new DotError('expected a node name after "->"', token.line);
307
+ ids.push(target.value);
308
+ }
309
+ const attrs = isPunct(peek(), "[") ? attrList() : [];
310
+ edgeStmts.push({ ids, attrs, line: token.line });
311
+ }
312
+ else {
313
+ const attrs = isPunct(peek(), "[") ? attrList() : [];
314
+ nodeStmts.push({ id: token.value, attrs, line: token.line });
315
+ }
316
+ if (isPunct(peek(), ";"))
317
+ at += 1;
318
+ }
319
+ at += 1;
320
+ if (at < tokens.length)
321
+ throw new DotError(`unexpected "${tokens[at].value}" after the graph`, tokens[at].line);
322
+ const warnings = [];
323
+ const graphAttrs = attrMap(graphs.flatMap((graph) => graph.attrs), GRAPH_ATTRS, "graph", warnings);
324
+ let maxVisits = null;
325
+ if (graphAttrs.has("max_visits")) {
326
+ const { value, line } = graphAttrs.get("max_visits") ?? { value: "", line: 0 };
327
+ maxVisits = intIn(value, 1, DOT_VISITS_MAX);
328
+ if (maxVisits === null)
329
+ throw new DotError(`max_visits must be a whole number 1-${DOT_VISITS_MAX}; got "${value}"`, line);
330
+ }
331
+ // A Claude permission setting for this team's starts; bypassPermissions is refused at start unless OpenShell allows it.
332
+ const permissionAttr = graphAttrs.get("permission");
333
+ if (permissionAttr && claudePermissionError(permissionAttr.value))
334
+ throw new DotError(claudePermissionError(permissionAttr.value), permissionAttr.line);
335
+ // How a member lands committed branch work once its part is done.
336
+ const landingAttr = graphAttrs.get("landing");
337
+ if (landingAttr && !DOT_LANDINGS.includes(landingAttr.value))
338
+ throw new DotError(`landing must be one of ${DOT_LANDINGS.join(", ")}; got "${landingAttr.value}"`, landingAttr.line);
339
+ // The chain this team's roles fall back on, in place of defaults.json `fallbacks`.
340
+ const fallbacksAttr = graphAttrs.get("fallbacks");
341
+ const fallbacks = fallbacksAttr ? fallbacksAttr.value.split(",").map((item) => item.trim()).filter(Boolean).map((item) => checkAgent(item, fallbacksAttr.line)) : undefined;
342
+ if (fallbacksAttr && !fallbacks?.length)
343
+ throw new DotError("fallbacks needs at least one harness or harness:model", fallbacksAttr.line);
344
+ const nodes = [];
345
+ const ids = new Set();
346
+ for (const stmt of nodeStmts) {
347
+ if (ids.has(stmt.id))
348
+ throw new DotError(`node "${stmt.id}" is declared twice`, stmt.line);
349
+ ids.add(stmt.id);
350
+ nodes.push(nodeFrom(stmt.id, stmt.attrs, stmt.line, warnings));
351
+ }
352
+ if (!nodes.length)
353
+ throw new DotError("team needs at least one node", 1);
354
+ const calls = new Set(nodes.filter((node) => node.pattern).map((node) => node.id));
355
+ const edges = [];
356
+ for (const stmt of edgeStmts) {
357
+ const base = edgeFrom(stmt.attrs, stmt.line, warnings);
358
+ for (let k = 0; k < stmt.ids.length - 1; k += 1) {
359
+ const from = stmt.ids[k];
360
+ const to = stmt.ids[k + 1];
361
+ if (from === DOT_DONE)
362
+ throw new DotError(`"${DOT_DONE}" can only receive handoffs`, stmt.line);
363
+ if (!ids.has(from))
364
+ throw new DotError(`edge from unknown node "${from}"`, stmt.line);
365
+ if (to !== DOT_DONE && !ids.has(to))
366
+ throw new DotError(`edge to unknown node "${to}"`, stmt.line);
367
+ if (from === to)
368
+ throw new DotError(`edge from "${from}" to itself`, stmt.line);
369
+ if (to === DOT_DONE && base.cap !== undefined)
370
+ throw new DotError("cap applies to a handoff between roles, not to done", stmt.line);
371
+ if (calls.has(from) && !CALL_EVENTS.includes(base.on))
372
+ throw new DotError(`call node "${from}" leaves only on ${CALL_EVENTS.map((event) => `"${event}"`).join(" or ")}`, stmt.line);
373
+ edges.push({ from, to, ...base });
374
+ }
375
+ }
376
+ for (const edge of edges) {
377
+ if (edge.overflow === undefined)
378
+ continue;
379
+ const target = edges.find((other) => other.from === edge.from && other.on === edge.overflow);
380
+ if (!target || edge.overflow === edge.on)
381
+ throw new DotError(`overflow "${edge.overflow}" is not an event from "${edge.from}"`, edgeStmts.find((stmt) => stmt.ids.includes(edge.from))?.line ?? 1);
382
+ if (target?.overflow)
383
+ throw new DotError(`overflow "${edge.overflow}" from "${edge.from}" overflows again; chain one step only`, edgeStmts[0]?.line ?? 1);
384
+ }
385
+ return { name: nameToken.value, description: graphAttrs.get("description")?.value ?? "", maxVisits, ...(permissionAttr ? { permission: permissionAttr.value } : {}), ...(landingAttr ? { landing: landingAttr.value } : {}), ...(fallbacks ? { fallbacks } : {}), nodes, edges, warnings };
386
+ }
387
+ const quote = (text) => `"${String(text).replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n")}"`;
388
+ /** Team text in canonical form: graph attrs, then nodes, then edges, every edge attr written out. */
389
+ export function serializeDot(team) {
390
+ const lines = [`digraph ${quote(team.name)} {`];
391
+ const graphAttrs = [];
392
+ if (team.description)
393
+ graphAttrs.push(`description=${quote(team.description)}`);
394
+ if (team.maxVisits !== null && team.maxVisits !== undefined)
395
+ graphAttrs.push(`max_visits=${team.maxVisits}`);
396
+ if (team.permission)
397
+ graphAttrs.push(`permission=${quote(team.permission)}`);
398
+ if (team.landing)
399
+ graphAttrs.push(`landing=${quote(team.landing)}`);
400
+ if (team.fallbacks?.length)
401
+ graphAttrs.push(`fallbacks=${quote(team.fallbacks.join(","))}`);
402
+ if (graphAttrs.length)
403
+ lines.push(` graph [${graphAttrs.join(", ")}];`);
404
+ for (const node of team.nodes) {
405
+ const fields = [node.pattern ? `pattern=${quote(node.pattern)}` : `agent=${quote(node.agent)}`, `count=${node.count ?? 1}`];
406
+ if (node.skill)
407
+ fields.push(`skill=${quote(node.skill)}`);
408
+ if (node.prompt)
409
+ fields.push(`prompt=${quote(node.prompt)}`);
410
+ if (node.fallback)
411
+ fields.push(`fallback=${quote(node.fallback)}`);
412
+ if (typeof node.codeMode === "boolean")
413
+ fields.push(`code_mode=${quote(node.codeMode ? "on" : "off")}`);
414
+ lines.push(` ${quote(node.id)} [${fields.join(", ")}];`);
415
+ }
416
+ for (const edge of team.edges) {
417
+ const fields = [`on=${quote(edge.on)}`, `kind=${quote(edge.kind ?? "pair")}`, `required=${edge.required ? "true" : "false"}`];
418
+ if (edge.cap !== undefined)
419
+ fields.push(`cap=${edge.cap}`);
420
+ if (edge.overflow)
421
+ fields.push(`overflow=${quote(edge.overflow)}`);
422
+ lines.push(` ${quote(edge.from)} -> ${quote(edge.to)} [${fields.join(", ")}];`);
423
+ }
424
+ lines.push("}");
425
+ return `${lines.join("\n")}\n`;
426
+ }
@@ -0,0 +1,193 @@
1
+ /** Pattern shape: validation, the team.dot round trip, and legacy rosters read as patterns. No file access. */
2
+ import { isValidName } from "../config.js";
3
+ import { START_HARNESSES } from "../sessions/launch.js";
4
+ import { modelShape } from "../harness.js";
5
+ import { parseHarnessModel } from "../harness.js";
6
+ import { DOT_PROMPT_MAX, DotError } from "./dot.js";
7
+ export const DONE = "done";
8
+ // Same shape as NODE_ID in dot.ts.
9
+ const ROLE_ID = /^[a-z][a-z0-9_-]{0,23}$/;
10
+ const MAX_ROLES = 8;
11
+ const RESPONSIBILITY_MAX = DOT_PROMPT_MAX;
12
+ const EVENT_MAX = 60;
13
+ const CAP_MAX = 10;
14
+ const DESCRIPTION_MAX = 200;
15
+ /** Names a pattern may not take: the operator and broadcast names, DONE, harnesses, and this project's peers. */
16
+ export function reservedNames(cfg) {
17
+ return ["all", "operator", DONE, ...START_HARNESSES, ...cfg.peers];
18
+ }
19
+ /** Throws with the node's line when a node has no prompt; validatePattern's own message carries no line. */
20
+ export function checkNodePrompts(team) {
21
+ for (const node of team.nodes) {
22
+ if (!node.prompt)
23
+ throw new DotError(`role "${node.id}" needs a prompt`, node.line ?? null);
24
+ }
25
+ }
26
+ /** A parsed team in the shape validatePattern takes: a node's prompt is its role's responsibility. */
27
+ export function patternFromTeam(team) {
28
+ const counts = new Map(team.nodes.map((node) => [node.id, node.count]));
29
+ for (const edge of team.edges) {
30
+ if (edge.kind === "pair" && edge.to !== DONE && counts.get(edge.from) !== counts.get(edge.to)) {
31
+ throw new Error(`pair edge "${edge.from}" -> "${edge.to}" needs both roles to have the same count`);
32
+ }
33
+ }
34
+ return {
35
+ name: team.name,
36
+ ...(team.description ? { description: team.description } : {}),
37
+ roles: team.nodes.map((node) => {
38
+ const parsed = parseHarnessModel(node.agent);
39
+ if (!parsed)
40
+ throw new Error(`node "${node.id}" has an invalid agent "${node.agent}"`);
41
+ return { id: node.id, responsibility: node.prompt ?? "", harness: parsed.harness, ...(parsed.model ? { model: parsed.model } : {}) };
42
+ }),
43
+ handoffs: team.edges.map((edge) => ({
44
+ from: edge.from, to: edge.to, on: edge.on, required: edge.required, ...(edge.cap !== undefined ? { cap: edge.cap } : {}),
45
+ })),
46
+ };
47
+ }
48
+ /** Re-attaches the DOT-only fields validatePattern drops (count, skill, fallback, code_mode, kind, overflow, max_visits, landing, fallbacks); defaults stay out of the shape. */
49
+ export function withExtras(checked, team) {
50
+ const roles = checked.roles.map((role, index) => {
51
+ const node = team.nodes[index];
52
+ return {
53
+ ...role,
54
+ ...(node.count > 1 ? { count: node.count } : {}),
55
+ ...(node.skill ? { skill: node.skill } : {}),
56
+ ...(node.fallback ? { fallback: node.fallback } : {}),
57
+ ...(typeof node.codeMode === "boolean" ? { codeMode: node.codeMode } : {}),
58
+ ...(node.pattern ? { pattern: node.pattern } : {}),
59
+ };
60
+ });
61
+ const handoffs = checked.handoffs.map((handoff, index) => {
62
+ const edge = team.edges[index];
63
+ return { ...handoff, ...(edge.kind === "pair" ? {} : { kind: edge.kind }), ...(edge.overflow ? { overflow: edge.overflow } : {}) };
64
+ });
65
+ return { ...checked, roles, handoffs, ...(team.maxVisits ? { maxVisits: team.maxVisits } : {}), ...(team.permission ? { permission: team.permission } : {}), ...(team.landing ? { landing: team.landing } : {}), ...(team.fallbacks ? { fallbacks: team.fallbacks } : {}) };
66
+ }
67
+ /** A validated pattern as the input of a team.dot file. */
68
+ export function teamFromPattern(pattern) {
69
+ return {
70
+ name: pattern.name,
71
+ description: pattern.description ?? "",
72
+ maxVisits: pattern.maxVisits ?? null,
73
+ ...(pattern.landing ? { landing: pattern.landing } : {}),
74
+ ...(pattern.fallbacks?.length ? { fallbacks: pattern.fallbacks } : {}),
75
+ nodes: pattern.roles.map((role) => ({
76
+ id: role.id,
77
+ agent: role.model ? `${role.harness}:${role.model}` : role.harness,
78
+ count: role.count ?? 1,
79
+ ...(role.skill ? { skill: role.skill } : {}),
80
+ prompt: role.responsibility,
81
+ ...(role.fallback ? { fallback: role.fallback } : {}),
82
+ ...(typeof role.codeMode === "boolean" ? { codeMode: role.codeMode } : {}),
83
+ ...(role.pattern ? { pattern: role.pattern } : {}),
84
+ })),
85
+ edges: pattern.handoffs.map((handoff) => ({
86
+ from: handoff.from, to: handoff.to, on: handoff.on, kind: handoff.kind ?? "pair", required: handoff.required,
87
+ ...(handoff.cap !== undefined ? { cap: handoff.cap } : {}),
88
+ ...(handoff.overflow ? { overflow: handoff.overflow } : {}),
89
+ })),
90
+ };
91
+ }
92
+ /** A team as a pattern: one role per member, no handoffs. A bad member marks the entry invalid instead of throwing. */
93
+ export function legacyPattern(name, members) {
94
+ const parsed = members.map((member) => parseHarnessModel(String(member)));
95
+ const badIndex = parsed.indexOf(null);
96
+ if (badIndex >= 0)
97
+ return { name, legacy: true, invalid: `team member "${members[badIndex]}" is not a harness or harness:model`, roles: [], handoffs: [] };
98
+ const roles = parsed.map((item, index) => {
99
+ const parsedItem = item ?? { harness: "", model: "" };
100
+ return { id: `${parsedItem.harness}-${index + 1}`, responsibility: "", harness: parsedItem.harness, ...(parsedItem.model ? { model: parsedItem.model } : {}) };
101
+ });
102
+ return { name, legacy: true, roles, handoffs: [] };
103
+ }
104
+ /** Throws on the first structural problem; checks shape only (models are checked by validatePatternModels). */
105
+ export function validatePattern(raw, reserved = []) {
106
+ if (!raw || typeof raw !== "object" || Array.isArray(raw))
107
+ throw new Error("pattern must be an object");
108
+ const input = raw;
109
+ const name = input.name;
110
+ if (typeof name !== "string" || !isValidName(name))
111
+ throw new Error("pattern name must be 1-24 chars of a-z, 0-9 and single hyphens");
112
+ if (reserved.includes(name))
113
+ throw new Error(`pattern name cannot be "${name}"; it is reserved`);
114
+ if (!Array.isArray(input.roles) || input.roles.length < 1 || input.roles.length > MAX_ROLES)
115
+ throw new Error(`pattern needs 1-${MAX_ROLES} roles`);
116
+ const roles = input.roles.map((role) => {
117
+ if (!role || typeof role !== "object")
118
+ throw new Error("role must be an object");
119
+ const id = String(role.id ?? "");
120
+ if (!ROLE_ID.test(id) || id === DONE)
121
+ throw new Error(`invalid role id "${id}"`);
122
+ const responsibility = typeof role.responsibility === "string" ? role.responsibility.trim() : "";
123
+ if (!responsibility || responsibility.length > RESPONSIBILITY_MAX)
124
+ throw new Error(`role "${id}" needs a prompt of 1-${RESPONSIBILITY_MAX} characters`);
125
+ const harness = role.harness;
126
+ if (typeof harness !== "string" || !START_HARNESSES.includes(harness))
127
+ throw new Error(`role "${id}" harness must be one of ${START_HARNESSES.join(", ")}`);
128
+ const out = { id, responsibility, harness };
129
+ if (role.model !== undefined && role.model !== "") {
130
+ if (!modelShape(harness, String(role.model)))
131
+ throw new Error(`role "${id}" has an invalid model`);
132
+ out.model = String(role.model);
133
+ }
134
+ return out;
135
+ });
136
+ const ids = new Set(roles.map((role) => role.id));
137
+ if (ids.size !== roles.length)
138
+ throw new Error("duplicate role id");
139
+ if (!Array.isArray(input.handoffs) || !input.handoffs.length)
140
+ throw new Error("pattern needs at least one handoff");
141
+ const seen = new Set();
142
+ const handoffs = input.handoffs.map((handoff) => {
143
+ const from = String(handoff?.from ?? "");
144
+ const to = String(handoff?.to ?? "");
145
+ const on = typeof handoff?.on === "string" ? handoff.on.trim() : "";
146
+ if (!ids.has(from))
147
+ throw new Error(`handoff from unknown role "${from}"`);
148
+ if (!ids.has(to) && to !== DONE)
149
+ throw new Error(`handoff to unknown role "${to}"`);
150
+ if (from === to)
151
+ throw new Error(`handoff from "${from}" to itself`);
152
+ if (!on || on.length > EVENT_MAX)
153
+ throw new Error(`handoff event must be 1-${EVENT_MAX} characters`);
154
+ const key = `${from}\n${on}`;
155
+ if (seen.has(key))
156
+ throw new Error(`role "${from}" has two handoffs on "${on}"`);
157
+ seen.add(key);
158
+ const required = handoff.required;
159
+ if (typeof required !== "boolean")
160
+ throw new Error(`handoff "${from}" -> "${to}" needs required true or false`);
161
+ const out = { from, to, on, required };
162
+ if (handoff.cap !== undefined) {
163
+ const cap = handoff.cap;
164
+ if (typeof cap !== "number" || !Number.isInteger(cap) || cap < 1 || cap > CAP_MAX || to === DONE)
165
+ throw new Error(`handoff "${on}" cap must be an integer 1-${CAP_MAX} on a role-to-role handoff`);
166
+ out.cap = cap;
167
+ }
168
+ return out;
169
+ });
170
+ if (!handoffs.some((handoff) => handoff.to === DONE))
171
+ throw new Error(`pattern needs a handoff to "${DONE}"`);
172
+ const reached = new Set([roles[0].id]);
173
+ for (let grew = true; grew;) {
174
+ grew = false;
175
+ for (const handoff of handoffs) {
176
+ if (reached.has(handoff.from) && handoff.to !== DONE && !reached.has(handoff.to)) {
177
+ reached.add(handoff.to);
178
+ grew = true;
179
+ }
180
+ }
181
+ }
182
+ const stranded = roles.find((role) => !reached.has(role.id));
183
+ if (stranded)
184
+ throw new Error(`role "${stranded.id}" is not reachable from the first role "${roles[0].id}"`);
185
+ const description = typeof input.description === "string" ? input.description.trim() : "";
186
+ if (description.length > DESCRIPTION_MAX)
187
+ throw new Error(`pattern description must be at most ${DESCRIPTION_MAX} characters`);
188
+ return { name, ...(description ? { description } : {}), roles, handoffs };
189
+ }
190
+ /** Required handoffs: the run finishes only after each has fired. */
191
+ export function requiredHandoffs(pattern) {
192
+ return pattern.handoffs.filter((handoff) => handoff.required);
193
+ }