@warpgogol/forge 9.2.3 → 9.3.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 (247) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/bin/cli.ts +3 -2
  3. package/dist/bin/cli.js +3 -2
  4. package/dist/bin/cli.js.map +1 -1
  5. package/dist/os/adr/handlers/implement-stamp.d.ts +6 -0
  6. package/dist/os/adr/handlers/implement-stamp.d.ts.map +1 -1
  7. package/dist/os/adr/handlers/implement-stamp.js +23 -15
  8. package/dist/os/adr/handlers/implement-stamp.js.map +1 -1
  9. package/dist/os/checks/handlers/counters-journal.d.ts.map +1 -1
  10. package/dist/os/checks/handlers/counters-journal.js +9 -1
  11. package/dist/os/checks/handlers/counters-journal.js.map +1 -1
  12. package/dist/os/core/core.module.d.ts.map +1 -1
  13. package/dist/os/core/core.module.js +27 -36
  14. package/dist/os/core/core.module.js.map +1 -1
  15. package/dist/os/core/lock-holder.d.ts +32 -0
  16. package/dist/os/core/lock-holder.d.ts.map +1 -0
  17. package/dist/os/core/lock-holder.js +81 -0
  18. package/dist/os/core/lock-holder.js.map +1 -0
  19. package/dist/os/core/stamp-lock.d.ts +30 -0
  20. package/dist/os/core/stamp-lock.d.ts.map +1 -0
  21. package/dist/os/core/stamp-lock.js +112 -0
  22. package/dist/os/core/stamp-lock.js.map +1 -0
  23. package/dist/os/flow/digest.d.ts +19 -0
  24. package/dist/os/flow/digest.d.ts.map +1 -1
  25. package/dist/os/flow/digest.js +33 -4
  26. package/dist/os/flow/digest.js.map +1 -1
  27. package/dist/os/flow/flow.module.d.ts.map +1 -1
  28. package/dist/os/flow/flow.module.js +61 -6
  29. package/dist/os/flow/flow.module.js.map +1 -1
  30. package/dist/os/flow/handlers/flow-cut.d.ts +8 -0
  31. package/dist/os/flow/handlers/flow-cut.d.ts.map +1 -1
  32. package/dist/os/flow/handlers/flow-cut.js +91 -5
  33. package/dist/os/flow/handlers/flow-cut.js.map +1 -1
  34. package/dist/os/flow/handlers/flow-digest.d.ts.map +1 -1
  35. package/dist/os/flow/handlers/flow-digest.js +6 -0
  36. package/dist/os/flow/handlers/flow-digest.js.map +1 -1
  37. package/dist/os/flow/handlers/flow-discard.d.ts.map +1 -1
  38. package/dist/os/flow/handlers/flow-discard.js +17 -13
  39. package/dist/os/flow/handlers/flow-discard.js.map +1 -1
  40. package/dist/os/flow/handlers/flow-fix.d.ts.map +1 -1
  41. package/dist/os/flow/handlers/flow-fix.js +99 -10
  42. package/dist/os/flow/handlers/flow-fix.js.map +1 -1
  43. package/dist/os/flow/handlers/flow-integrate.d.ts.map +1 -1
  44. package/dist/os/flow/handlers/flow-integrate.js +32 -15
  45. package/dist/os/flow/handlers/flow-integrate.js.map +1 -1
  46. package/dist/os/flow/handlers/flow-publication-retire.d.ts +17 -0
  47. package/dist/os/flow/handlers/flow-publication-retire.d.ts.map +1 -0
  48. package/dist/os/flow/handlers/flow-publication-retire.js +132 -0
  49. package/dist/os/flow/handlers/flow-publication-retire.js.map +1 -0
  50. package/dist/os/flow/handlers/flow-push.d.ts +7 -0
  51. package/dist/os/flow/handlers/flow-push.d.ts.map +1 -1
  52. package/dist/os/flow/handlers/flow-push.js +106 -15
  53. package/dist/os/flow/handlers/flow-push.js.map +1 -1
  54. package/dist/os/flow/locks.d.ts +6 -1
  55. package/dist/os/flow/locks.d.ts.map +1 -1
  56. package/dist/os/flow/locks.js +53 -23
  57. package/dist/os/flow/locks.js.map +1 -1
  58. package/dist/os/flow/publication-order.d.ts +25 -0
  59. package/dist/os/flow/publication-order.d.ts.map +1 -0
  60. package/dist/os/flow/publication-order.js +75 -0
  61. package/dist/os/flow/publication-order.js.map +1 -0
  62. package/dist/os/flow/publish.d.ts +18 -0
  63. package/dist/os/flow/publish.d.ts.map +1 -1
  64. package/dist/os/flow/publish.js +58 -2
  65. package/dist/os/flow/publish.js.map +1 -1
  66. package/dist/os/flow/registry.d.ts +1 -1
  67. package/dist/os/flow/registry.d.ts.map +1 -1
  68. package/dist/os/flow/registry.js +2 -1
  69. package/dist/os/flow/registry.js.map +1 -1
  70. package/dist/os/flow/release-tags.d.ts +1 -0
  71. package/dist/os/flow/release-tags.d.ts.map +1 -1
  72. package/dist/os/flow/release-tags.js +5 -4
  73. package/dist/os/flow/release-tags.js.map +1 -1
  74. package/dist/os/flow/scripts.d.ts +1 -1
  75. package/dist/os/flow/scripts.d.ts.map +1 -1
  76. package/dist/os/flow/scripts.js +1 -0
  77. package/dist/os/flow/scripts.js.map +1 -1
  78. package/dist/os/flow/task-files.d.ts +11 -0
  79. package/dist/os/flow/task-files.d.ts.map +1 -0
  80. package/dist/os/flow/task-files.js +51 -0
  81. package/dist/os/flow/task-files.js.map +1 -0
  82. package/dist/os/flow/tests/helpers.d.ts +4 -1
  83. package/dist/os/flow/tests/helpers.d.ts.map +1 -1
  84. package/dist/os/flow/tests/helpers.js +9 -5
  85. package/dist/os/flow/tests/helpers.js.map +1 -1
  86. package/dist/os/mission/handlers/archive.d.ts.map +1 -1
  87. package/dist/os/mission/handlers/archive.js +30 -74
  88. package/dist/os/mission/handlers/archive.js.map +1 -1
  89. package/dist/os/mission/lockfile.d.ts +21 -0
  90. package/dist/os/mission/lockfile.d.ts.map +1 -0
  91. package/dist/os/mission/lockfile.js +76 -0
  92. package/dist/os/mission/lockfile.js.map +1 -0
  93. package/dist/os/mission/mission.module.d.ts.map +1 -1
  94. package/dist/os/mission/mission.module.js +2 -1
  95. package/dist/os/mission/mission.module.js.map +1 -1
  96. package/dist/os/rfc/handlers/implement-stamp.d.ts +6 -0
  97. package/dist/os/rfc/handlers/implement-stamp.d.ts.map +1 -1
  98. package/dist/os/rfc/handlers/implement-stamp.js +24 -19
  99. package/dist/os/rfc/handlers/implement-stamp.js.map +1 -1
  100. package/dist/os/system/handlers/outbox-validate.d.ts +8 -0
  101. package/dist/os/system/handlers/outbox-validate.d.ts.map +1 -0
  102. package/dist/os/system/handlers/outbox-validate.js +102 -0
  103. package/dist/os/system/handlers/outbox-validate.js.map +1 -0
  104. package/dist/os/system/handlers/system-map-validate.d.ts +13 -0
  105. package/dist/os/system/handlers/system-map-validate.d.ts.map +1 -0
  106. package/dist/os/system/handlers/system-map-validate.js +78 -0
  107. package/dist/os/system/handlers/system-map-validate.js.map +1 -0
  108. package/dist/os/system/handlers/system-outbox-scan.d.ts +10 -0
  109. package/dist/os/system/handlers/system-outbox-scan.d.ts.map +1 -0
  110. package/dist/os/system/handlers/system-outbox-scan.js +195 -0
  111. package/dist/os/system/handlers/system-outbox-scan.js.map +1 -0
  112. package/dist/os/system/handlers/system-task-collect.d.ts +13 -0
  113. package/dist/os/system/handlers/system-task-collect.d.ts.map +1 -0
  114. package/dist/os/system/handlers/system-task-collect.js +238 -0
  115. package/dist/os/system/handlers/system-task-collect.js.map +1 -0
  116. package/dist/os/system/handlers/system-task-create.d.ts +32 -0
  117. package/dist/os/system/handlers/system-task-create.d.ts.map +1 -0
  118. package/dist/os/system/handlers/system-task-create.js +216 -0
  119. package/dist/os/system/handlers/system-task-create.js.map +1 -0
  120. package/dist/os/system/handlers/system-task-dispatch.d.ts +14 -0
  121. package/dist/os/system/handlers/system-task-dispatch.d.ts.map +1 -0
  122. package/dist/os/system/handlers/system-task-dispatch.js +290 -0
  123. package/dist/os/system/handlers/system-task-dispatch.js.map +1 -0
  124. package/dist/os/system/handlers/system-task-move.d.ts +12 -0
  125. package/dist/os/system/handlers/system-task-move.d.ts.map +1 -0
  126. package/dist/os/system/handlers/system-task-move.js +217 -0
  127. package/dist/os/system/handlers/system-task-move.js.map +1 -0
  128. package/dist/os/system/handlers/system-task-status.d.ts +20 -0
  129. package/dist/os/system/handlers/system-task-status.d.ts.map +1 -0
  130. package/dist/os/system/handlers/system-task-status.js +139 -0
  131. package/dist/os/system/handlers/system-task-status.js.map +1 -0
  132. package/dist/os/system/handlers/system-versionset-record.d.ts +12 -0
  133. package/dist/os/system/handlers/system-versionset-record.d.ts.map +1 -0
  134. package/dist/os/system/handlers/system-versionset-record.js +332 -0
  135. package/dist/os/system/handlers/system-versionset-record.js.map +1 -0
  136. package/dist/os/system/index.d.ts +6 -0
  137. package/dist/os/system/index.d.ts.map +1 -0
  138. package/dist/os/system/index.js +18 -0
  139. package/dist/os/system/index.js.map +1 -0
  140. package/dist/os/system/outbox-schema.d.ts +30 -0
  141. package/dist/os/system/outbox-schema.d.ts.map +1 -0
  142. package/dist/os/system/outbox-schema.js +29 -0
  143. package/dist/os/system/outbox-schema.js.map +1 -0
  144. package/dist/os/system/repo.d.ts +64 -0
  145. package/dist/os/system/repo.d.ts.map +1 -0
  146. package/dist/os/system/repo.js +364 -0
  147. package/dist/os/system/repo.js.map +1 -0
  148. package/dist/os/system/scenario-schemas.d.ts +26 -0
  149. package/dist/os/system/scenario-schemas.d.ts.map +1 -0
  150. package/dist/os/system/scenario-schemas.js +53 -0
  151. package/dist/os/system/scenario-schemas.js.map +1 -0
  152. package/dist/os/system/schemas.d.ts +63 -0
  153. package/dist/os/system/schemas.d.ts.map +1 -0
  154. package/dist/os/system/schemas.js +83 -0
  155. package/dist/os/system/schemas.js.map +1 -0
  156. package/dist/os/system/system.module.d.ts +3 -0
  157. package/dist/os/system/system.module.d.ts.map +1 -0
  158. package/dist/os/system/system.module.js +429 -0
  159. package/dist/os/system/system.module.js.map +1 -0
  160. package/dist/os/system/task-schemas.d.ts +108 -0
  161. package/dist/os/system/task-schemas.d.ts.map +1 -0
  162. package/dist/os/system/task-schemas.js +152 -0
  163. package/dist/os/system/task-schemas.js.map +1 -0
  164. package/dist/os/system/task-store.d.ts +41 -0
  165. package/dist/os/system/task-store.d.ts.map +1 -0
  166. package/dist/os/system/task-store.js +142 -0
  167. package/dist/os/system/task-store.js.map +1 -0
  168. package/dist/os/system/tests/helpers.d.ts +30 -0
  169. package/dist/os/system/tests/helpers.d.ts.map +1 -0
  170. package/dist/os/system/tests/helpers.js +178 -0
  171. package/dist/os/system/tests/helpers.js.map +1 -0
  172. package/dist/src/index.d.ts +3 -0
  173. package/dist/src/index.d.ts.map +1 -1
  174. package/dist/src/index.js +5 -4
  175. package/dist/src/index.js.map +1 -1
  176. package/os/adr/handlers/implement-stamp-lock.test.ts +90 -0
  177. package/os/adr/handlers/implement-stamp.ts +29 -20
  178. package/os/checks/handlers/counters-journal.test.ts +10 -0
  179. package/os/checks/handlers/counters-journal.ts +8 -1
  180. package/os/core/core.module.ts +32 -36
  181. package/os/core/lock-holder.ts +88 -0
  182. package/os/core/stamp-lock.ts +142 -0
  183. package/os/flow/digest.ts +46 -4
  184. package/os/flow/flow.module.ts +62 -6
  185. package/os/flow/handlers/flow-cut.ts +118 -5
  186. package/os/flow/handlers/flow-digest.ts +6 -0
  187. package/os/flow/handlers/flow-discard.ts +22 -13
  188. package/os/flow/handlers/flow-fix.ts +116 -10
  189. package/os/flow/handlers/flow-integrate.ts +31 -15
  190. package/os/flow/handlers/flow-publication-retire.ts +156 -0
  191. package/os/flow/handlers/flow-push.ts +128 -13
  192. package/os/flow/locks.ts +75 -25
  193. package/os/flow/publication-order.ts +99 -0
  194. package/os/flow/publish.ts +73 -2
  195. package/os/flow/registry.ts +5 -2
  196. package/os/flow/release-tags.ts +5 -4
  197. package/os/flow/scripts.ts +2 -0
  198. package/os/flow/task-files.ts +59 -0
  199. package/os/flow/tests/flow-cut-prepare.test.ts +298 -0
  200. package/os/flow/tests/flow-cut.test.ts +5 -1
  201. package/os/flow/tests/flow-publication-retire.test.ts +191 -0
  202. package/os/flow/tests/flow-push-supersede.test.ts +221 -0
  203. package/os/flow/tests/flow-task-files.test.ts +59 -0
  204. package/os/flow/tests/helpers.ts +9 -5
  205. package/os/flow/tests/locks.test.ts +219 -0
  206. package/os/flow/tests/publication-order.test.ts +102 -0
  207. package/os/mission/handlers/archive.test.ts +97 -52
  208. package/os/mission/handlers/archive.ts +31 -76
  209. package/os/mission/lockfile.ts +84 -0
  210. package/os/mission/mission.module.ts +2 -1
  211. package/os/rfc/handlers/implement-stamp-lock.test.ts +133 -0
  212. package/os/rfc/handlers/implement-stamp.ts +30 -24
  213. package/os/system/handlers/outbox-validate.ts +120 -0
  214. package/os/system/handlers/system-map-validate.ts +107 -0
  215. package/os/system/handlers/system-outbox-scan.ts +320 -0
  216. package/os/system/handlers/system-task-collect.ts +305 -0
  217. package/os/system/handlers/system-task-create.ts +271 -0
  218. package/os/system/handlers/system-task-dispatch.ts +387 -0
  219. package/os/system/handlers/system-task-move.ts +353 -0
  220. package/os/system/handlers/system-task-status.ts +176 -0
  221. package/os/system/handlers/system-versionset-record.ts +476 -0
  222. package/os/system/index.ts +47 -0
  223. package/os/system/outbox-schema.ts +47 -0
  224. package/os/system/repo.ts +451 -0
  225. package/os/system/scenario-schemas.ts +62 -0
  226. package/os/system/schemas.ts +102 -0
  227. package/os/system/system.module.ts +449 -0
  228. package/os/system/task-schemas.ts +190 -0
  229. package/os/system/task-store.ts +184 -0
  230. package/os/system/tests/helpers.ts +189 -0
  231. package/os/system/tests/outbox-validate.test.ts +92 -0
  232. package/os/system/tests/system-map-validate.test.ts +321 -0
  233. package/os/system/tests/system-outbox-scan.test.ts +273 -0
  234. package/os/system/tests/system-profile.test.ts +90 -0
  235. package/os/system/tests/system-task-collect.test.ts +293 -0
  236. package/os/system/tests/system-task-dispatch.test.ts +279 -0
  237. package/os/system/tests/system-task-status.test.ts +153 -0
  238. package/os/system/tests/system-task.test.ts +330 -0
  239. package/os/system/tests/system-versionset.test.ts +314 -0
  240. package/package.json +33 -1
  241. package/profiles/system.yaml +650 -0
  242. package/skills/fo/fo-outbox/SKILL.md +71 -0
  243. package/skills/fo/fo-system-dispatch/SKILL.md +65 -0
  244. package/skills/fo/fo-task-run/SKILL.md +75 -0
  245. package/src/index.ts +11 -4
  246. package/src/tests/fixtures/agents-generate-business-before.txt +6 -0
  247. package/src/tests/profile-schema.test.ts +2 -1
@@ -0,0 +1,387 @@
1
+ /*
2
+ <MODULE_CONTRACT>
3
+ <purpose>system.task.dispatch (RFC-1509) — turn a `ready` task into a product
4
+ task branch. Resolves the product folder through products.local.yaml
5
+ (SYSTEM-TASK-05), enforces edge verdicts (forbidden → SYSTEM-TASK-03,
6
+ undeclared → SYSTEM-TASK-04 warning), refuses briefs that leak the system
7
+ identity or other products' folders (SYSTEM-TASK-06), opens an
8
+ auto/task-… branch via openFlowBranchCore with owner {kind: task, id},
9
+ git-excludes .forge/task/ through the common git dir's info/exclude, writes
10
+ the brief into the worktree, and records dispatch + briefSha256 on the task.
11
+ A launcher argv from products.local.yaml runs best-effort (SYSTEM-TASK-09).</purpose>
12
+ <non-goals>
13
+ <item>No product-side commits, integrates or pushes — the session owns those.</item>
14
+ </non-goals>
15
+ </MODULE_CONTRACT>
16
+ <CHANGE_SUMMARY>
17
+ <item>RFC-1509: step 3 — system.task.dispatch handler</item>
18
+ <item>RFC-1509: implement RFC-1509 — system task queue, dispatch briefs and task reports</item>
19
+ <item>RFC-1509: close RFC-1509 review r1 findings — mark-failed escape, open-error refusal, dry-run coverage</item>
20
+ </CHANGE_SUMMARY>
21
+ */
22
+
23
+ import { createHash } from "node:crypto";
24
+ import path from "node:path";
25
+
26
+ import { resolveIo } from "../../../src/utils/io.ts";
27
+ import type {
28
+ Diagnostic,
29
+ ForgeCommandInput,
30
+ ForgeCommandResult,
31
+ ForgeRuntimeContext,
32
+ WorkspaceIO,
33
+ } from "../../../src/types.ts";
34
+ import { openFlowBranchCore } from "../../flow/handlers/flow-open.ts";
35
+ import { commonGitDir } from "../../flow/registry.ts";
36
+ import { assertSystemRepository, edgeVerdict, loadSystemMap, type SystemMap } from "../repo.ts";
37
+ import type { SystemTask } from "../task-schemas.ts";
38
+ import { readBrief, readTask, writeTask } from "../task-store.ts";
39
+
40
+ export interface SystemTaskDispatchResult {
41
+ command: "system.task.dispatch";
42
+ task: string | null;
43
+ product: string | null;
44
+ branch: string | null;
45
+ worktree: string | null;
46
+ dispatched: boolean;
47
+ launcherRan: boolean;
48
+ launcherExit: number | null;
49
+ diagnostics: Diagnostic[];
50
+ }
51
+
52
+ function refusal(ruleId: string, message: string, fixHint?: string): Diagnostic {
53
+ return { ruleId, severity: "error", message, ...(fixHint ? { fixHint } : {}) };
54
+ }
55
+
56
+ function flagString(input: ForgeCommandInput, name: string): string | null {
57
+ const v = input.flags[name];
58
+ return typeof v === "string" && v.length > 0 ? v : null;
59
+ }
60
+
61
+ /** Resolve a product's local checkout — SYSTEM-TASK-05 on any gap. */
62
+ async function resolveProductRoot(
63
+ root: string,
64
+ io: WorkspaceIO,
65
+ map: SystemMap,
66
+ productId: string,
67
+ ): Promise<{ root: string | null; diagnostic: Diagnostic | null }> {
68
+ const declared = map.products.products.some((p) => p.id === productId);
69
+ if (!declared) {
70
+ return {
71
+ root: null,
72
+ diagnostic: refusal(
73
+ "SYSTEM-TASK-05",
74
+ `product '${productId}' is not declared in system/products.yaml`,
75
+ "add the product to system/products.yaml",
76
+ ),
77
+ };
78
+ }
79
+ const folders = map.local?.folders ?? {};
80
+ const raw = Object.prototype.hasOwnProperty.call(folders, productId)
81
+ ? folders[productId]
82
+ : undefined;
83
+ if (raw === undefined) {
84
+ return {
85
+ root: null,
86
+ diagnostic: refusal(
87
+ "SYSTEM-TASK-05",
88
+ `product '${productId}' has no folder in system/products.local.yaml`,
89
+ `add folders.${productId}: <checkout> to products.local.yaml`,
90
+ ),
91
+ };
92
+ }
93
+ const resolved = path.isAbsolute(raw) ? raw : path.resolve(root, raw);
94
+ try {
95
+ const stat = await io.stat(resolved);
96
+ if (!stat.isDirectory) {
97
+ return {
98
+ root: null,
99
+ diagnostic: refusal(
100
+ "SYSTEM-TASK-05",
101
+ `product folder for '${productId}' is not a directory: ${resolved}`,
102
+ ),
103
+ };
104
+ }
105
+ } catch {
106
+ return {
107
+ root: null,
108
+ diagnostic: refusal(
109
+ "SYSTEM-TASK-05",
110
+ `product folder for '${productId}' does not exist: ${resolved}`,
111
+ ),
112
+ };
113
+ }
114
+ return { root: resolved, diagnostic: null };
115
+ }
116
+
117
+ /**
118
+ * SYSTEM-TASK-06 — the brief must not mention the system's identity or any
119
+ * other product's local folder. The task's own product folder is expected in
120
+ * paths the operator wrote; every OTHER product's folder is a leak.
121
+ */
122
+ function briefLeakDiagnostics(
123
+ map: SystemMap,
124
+ task: SystemTask,
125
+ briefContent: string,
126
+ ): Diagnostic[] {
127
+ const diagnostics: Diagnostic[] = [];
128
+ const needles: { label: string; value: string }[] = [
129
+ { label: `system id '${map.system.id}'`, value: map.system.id },
130
+ ...(map.system.name ? [{ label: `system name '${map.system.name}'`, value: map.system.name }] : []),
131
+ ];
132
+ for (const product of map.products.products) {
133
+ if (product.id === task.product) continue;
134
+ const folders = map.local?.folders ?? {};
135
+ const folder = Object.prototype.hasOwnProperty.call(folders, product.id)
136
+ ? folders[product.id]
137
+ : undefined;
138
+ if (folder) {
139
+ needles.push({ label: `folder of product '${product.id}'`, value: folder });
140
+ // A relative folder leaks under its resolved absolute form too.
141
+ if (!path.isAbsolute(folder)) {
142
+ needles.push({
143
+ label: `resolved folder of product '${product.id}'`,
144
+ value: path.resolve(map.root, folder),
145
+ });
146
+ }
147
+ }
148
+ }
149
+ for (const needle of needles) {
150
+ if (needle.value.length > 0 && briefContent.includes(needle.value)) {
151
+ diagnostics.push(
152
+ refusal(
153
+ "SYSTEM-TASK-06",
154
+ `dispatch brief for ${task.id} leaks ${needle.label}`,
155
+ "scrub the brief — product sessions learn nothing about the system repository or sibling products",
156
+ ),
157
+ );
158
+ }
159
+ }
160
+ return diagnostics;
161
+ }
162
+
163
+ export async function runSystemTaskDispatch(
164
+ input: ForgeCommandInput,
165
+ context: ForgeRuntimeContext,
166
+ ): Promise<ForgeCommandResult<SystemTaskDispatchResult>> {
167
+ const io = resolveIo(context.io);
168
+ const root = context.workspaceRoot;
169
+ const diagnostics: Diagnostic[] = [];
170
+ const dryRun = context.dryRun || input.flags["dry-run"] === true;
171
+
172
+ const refusalDiag = await assertSystemRepository(root, io);
173
+ if (refusalDiag) diagnostics.push(refusalDiag);
174
+
175
+ const id = flagString(input, "task");
176
+ if (id === null) {
177
+ diagnostics.push(refusal("SYSTEM-TASK-01", "--task is required", "see forge system.task.dispatch --help"));
178
+ }
179
+
180
+ let map: SystemMap | null = null;
181
+ let task: SystemTask | null = null;
182
+ if (diagnostics.every((d) => d.severity !== "error")) {
183
+ const loaded = await loadSystemMap(root, io);
184
+ diagnostics.push(...loaded.diagnostics.filter((d) => d.ruleId === "SYSTEM-MAP-01"));
185
+ map = loaded.map;
186
+ if (map === null) {
187
+ diagnostics.push(
188
+ refusal("SYSTEM-TASK-05", "system map failed to load — see SYSTEM-MAP diagnostics"),
189
+ );
190
+ }
191
+ const read = await readTask(root, io, id!);
192
+ if (read.diagnostic) diagnostics.push(read.diagnostic);
193
+ task = read.task;
194
+ }
195
+
196
+ if (task !== null && task.state !== "ready") {
197
+ diagnostics.push(
198
+ refusal(
199
+ "SYSTEM-TASK-02",
200
+ `${task.id} is ${task.state} — dispatch requires ready`,
201
+ "system.task.move --to ready first",
202
+ ),
203
+ );
204
+ }
205
+
206
+ let productRoot: string | null = null;
207
+ let briefContent: string | null = null;
208
+ if (task !== null && map !== null && diagnostics.every((d) => d.severity !== "error")) {
209
+ const resolved = await resolveProductRoot(root, io, map, task.product);
210
+ if (resolved.diagnostic) diagnostics.push(resolved.diagnostic);
211
+ productRoot = resolved.root;
212
+
213
+ for (const contract of task.contracts) {
214
+ const verdict = edgeVerdict(map, task.product, contract);
215
+ if (verdict === "forbidden") {
216
+ diagnostics.push(
217
+ refusal(
218
+ "SYSTEM-TASK-03",
219
+ `contract '${contract}' is a forbidden edge for ${task.product}`,
220
+ "renegotiate system/edges.yaml before dispatching this task",
221
+ ),
222
+ );
223
+ } else if (verdict === "undeclared") {
224
+ diagnostics.push({
225
+ ruleId: "SYSTEM-TASK-04",
226
+ severity: "warning",
227
+ message: `contract '${contract}' is undeclared for ${task.product} — declare it in system/edges.yaml`,
228
+ });
229
+ }
230
+ }
231
+
232
+ const brief = await readBrief(root, io, task.id);
233
+ if (brief === null) {
234
+ diagnostics.push(
235
+ refusal(
236
+ "SYSTEM-TASK-02",
237
+ `dispatch brief for ${task.id} is missing at queue/briefs/${task.id}.md`,
238
+ "create the brief before dispatching",
239
+ ),
240
+ );
241
+ } else {
242
+ briefContent = brief.content;
243
+ // The brief is bound to its task — a copy-pasted brief from another
244
+ // task/product would dispatch the wrong instructions silently.
245
+ const fm = brief.frontmatter;
246
+ if (fm !== null && (fm.task !== task.id || fm.product !== task.product)) {
247
+ diagnostics.push(
248
+ refusal(
249
+ "SYSTEM-TASK-02",
250
+ `brief frontmatter names task=${String(fm.task)} product=${String(fm.product)} — expected ${task.id} / ${task.product}`,
251
+ "fix the frontmatter or move the brief to the right queue/briefs file",
252
+ ),
253
+ );
254
+ }
255
+ diagnostics.push(...briefLeakDiagnostics(map, task, brief.content));
256
+ }
257
+ }
258
+
259
+ const errors = () => diagnostics.filter((d) => d.severity === "error").length;
260
+
261
+ let branch: string | null = null;
262
+ let worktree: string | null = null;
263
+ if (task !== null && productRoot !== null && briefContent !== null && errors() === 0) {
264
+ // dryRun forwards into openFlowBranchCore so the governed/branch checks a
265
+ // real dispatch would hit are exercised without creating anything.
266
+ const opened = await openFlowBranchCore(productRoot, io, {
267
+ kind: "task",
268
+ slug: task.slug,
269
+ brief: task.id,
270
+ owner: { kind: "task", id: task.id },
271
+ dryRun,
272
+ now: new Date(),
273
+ });
274
+ diagnostics.push(...opened.diagnostics);
275
+ // An opened:false with empty diagnostics is the dry-run success shape;
276
+ // in a real run any error diagnostic (incl. FLOW-OPEN-07 setup failure)
277
+ // means the task must stay ready — the worktree/entry are kept for
278
+ // operator inspection per flow.open's contract.
279
+ const openFailed =
280
+ opened.diagnostics.some((d) => d.severity === "error") ||
281
+ (!opened.opened && !dryRun);
282
+ if (!openFailed && opened.branch && opened.worktree && !dryRun) {
283
+ branch = opened.branch;
284
+ worktree = opened.worktree;
285
+
286
+ // RFC-1509: .forge/task/ must never become a tracked artifact — extend
287
+ // the common git dir's info/exclude once (idempotent).
288
+ const common = await commonGitDir(io, productRoot);
289
+ const infoDir = path.join(common, "info");
290
+ const excludeFile = path.join(infoDir, "exclude");
291
+ const excludeLine = "/.forge/task/";
292
+ await io.mkdir(infoDir);
293
+ const existing = (await io.exists(excludeFile)) ? await io.readFile(excludeFile) : "";
294
+ if (!existing.split(/\r?\n/).some((line) => line.trim() === excludeLine)) {
295
+ await io.appendFile(
296
+ excludeFile,
297
+ `${existing.length > 0 && !existing.endsWith("\n") ? "\n" : ""}${excludeLine}\n`,
298
+ );
299
+ }
300
+
301
+ const taskDir = path.join(worktree, ".forge", "task");
302
+ await io.mkdir(taskDir);
303
+ await io.writeFile(path.join(taskDir, "brief.md"), briefContent);
304
+
305
+ const briefSha = createHash("sha256").update(briefContent, "utf8").digest("hex");
306
+ const next: SystemTask = {
307
+ ...task,
308
+ state: "dispatched",
309
+ briefSha256: briefSha,
310
+ dispatch: {
311
+ branch,
312
+ worktree,
313
+ baseCommit: opened.baseSha ?? "",
314
+ dispatchedAt: new Date().toISOString(),
315
+ },
316
+ history: [
317
+ ...task.history,
318
+ {
319
+ at: new Date().toISOString(),
320
+ from: task.state,
321
+ to: "dispatched",
322
+ by: "operator",
323
+ },
324
+ ],
325
+ };
326
+ await writeTask(root, io, next);
327
+ task = next;
328
+ }
329
+ }
330
+
331
+ let launcherRan = false;
332
+ let launcherExit: number | null = null;
333
+ if (task !== null && task.state === "dispatched" && worktree !== null && map?.local?.launcher) {
334
+ const argv = map.local.launcher.argv.map((a) =>
335
+ a.replace(/\{worktree\}/g, worktree!).replace(/\{task\}/g, task!.id),
336
+ );
337
+ launcherRan = true;
338
+ try {
339
+ const result = await io.exec(argv[0], argv.slice(1), { cwd: productRoot! });
340
+ launcherExit = result.exitCode;
341
+ if (result.exitCode !== 0) {
342
+ diagnostics.push({
343
+ ruleId: "SYSTEM-TASK-09",
344
+ severity: "warning",
345
+ message: `launcher exited ${result.exitCode}: ${result.stderr.trim() || result.stdout.trim()}`,
346
+ });
347
+ }
348
+ } catch (error) {
349
+ diagnostics.push({
350
+ ruleId: "SYSTEM-TASK-09",
351
+ severity: "warning",
352
+ message: `launcher failed to spawn: ${error instanceof Error ? error.message : String(error)}`,
353
+ });
354
+ }
355
+ }
356
+
357
+ const errs = errors();
358
+ const dispatched = task?.state === "dispatched" && !dryRun;
359
+ return {
360
+ data: {
361
+ command: "system.task.dispatch",
362
+ task: task?.id ?? id,
363
+ product: task?.product ?? null,
364
+ branch,
365
+ worktree,
366
+ dispatched,
367
+ launcherRan,
368
+ launcherExit,
369
+ diagnostics,
370
+ },
371
+ exitCode: errs > 0 ? 1 : 0,
372
+ summary:
373
+ task === null
374
+ ? `system.task.dispatch refused (${errs} error(s))`
375
+ : dryRun
376
+ ? `system.task.dispatch would open auto/task-…-${task.slug} for ${task.id} in ${task.product}`
377
+ : dispatched
378
+ ? `system.task.dispatch opened ${branch} for ${task.id} — worktree ${worktree}`
379
+ : `system.task.dispatch refused ${task.id} (${errs} error(s))`,
380
+ nextSteps: dispatched
381
+ ? [
382
+ { action: `cd ${worktree}`, kind: "required" },
383
+ { action: `read .forge/task/brief.md and follow it`, kind: "required" },
384
+ ]
385
+ : [],
386
+ };
387
+ }