@warpgogol/forge 9.2.3 → 9.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (252) hide show
  1. package/CHANGELOG.md +33 -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 +6 -1
  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 +12 -5
  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/flow/tests/scripts.test.ts +13 -0
  208. package/os/mission/handlers/archive.test.ts +97 -52
  209. package/os/mission/handlers/archive.ts +31 -76
  210. package/os/mission/lockfile.ts +84 -0
  211. package/os/mission/mission.module.ts +2 -1
  212. package/os/rfc/handlers/implement-stamp-lock.test.ts +133 -0
  213. package/os/rfc/handlers/implement-stamp.ts +30 -24
  214. package/os/system/handlers/outbox-validate.ts +120 -0
  215. package/os/system/handlers/system-map-validate.ts +107 -0
  216. package/os/system/handlers/system-outbox-scan.ts +320 -0
  217. package/os/system/handlers/system-task-collect.ts +305 -0
  218. package/os/system/handlers/system-task-create.ts +271 -0
  219. package/os/system/handlers/system-task-dispatch.ts +387 -0
  220. package/os/system/handlers/system-task-move.ts +353 -0
  221. package/os/system/handlers/system-task-status.ts +176 -0
  222. package/os/system/handlers/system-versionset-record.ts +476 -0
  223. package/os/system/index.ts +47 -0
  224. package/os/system/outbox-schema.ts +47 -0
  225. package/os/system/repo.ts +451 -0
  226. package/os/system/scenario-schemas.ts +62 -0
  227. package/os/system/schemas.ts +102 -0
  228. package/os/system/system.module.ts +449 -0
  229. package/os/system/task-schemas.ts +190 -0
  230. package/os/system/task-store.ts +184 -0
  231. package/os/system/tests/helpers.ts +189 -0
  232. package/os/system/tests/outbox-validate.test.ts +92 -0
  233. package/os/system/tests/system-map-validate.test.ts +321 -0
  234. package/os/system/tests/system-outbox-scan.test.ts +273 -0
  235. package/os/system/tests/system-profile.test.ts +90 -0
  236. package/os/system/tests/system-task-collect.test.ts +293 -0
  237. package/os/system/tests/system-task-dispatch.test.ts +279 -0
  238. package/os/system/tests/system-task-status.test.ts +153 -0
  239. package/os/system/tests/system-task.test.ts +330 -0
  240. package/os/system/tests/system-versionset.test.ts +314 -0
  241. package/package.json +33 -1
  242. package/profiles/forge-shell.yaml +1 -2
  243. package/profiles/knowledge.yaml +1 -1
  244. package/profiles/system.yaml +650 -0
  245. package/profiles/typescript.yaml +0 -1
  246. package/skills/fo/fo-outbox/SKILL.md +71 -0
  247. package/skills/fo/fo-system-dispatch/SKILL.md +65 -0
  248. package/skills/fo/fo-task-run/SKILL.md +75 -0
  249. package/src/index.ts +11 -4
  250. package/src/tests/fixtures/agents-generate-business-before.txt +6 -0
  251. package/src/tests/profile-flow-hooks.test.ts +15 -0
  252. package/src/tests/profile-schema.test.ts +2 -1
@@ -0,0 +1,293 @@
1
+ /*
2
+ <MODULE_CONTRACT>
3
+ <purpose>RFC-1509 acceptance tests for system.task.collect and the
4
+ .forge/task/ preservation path — report lookup (worktree vs preserved copy),
5
+ schema/task-id validation, outcome transitions, journal-driven
6
+ reported→integrated, and --mark-failed.</purpose>
7
+ </MODULE_CONTRACT>
8
+ <CHANGE_SUMMARY>
9
+ <item>RFC-1509: step 5 — collect + preservation acceptance tests</item>
10
+ <item>RFC-1509: implement RFC-1509 — system task queue, dispatch briefs and task reports</item>
11
+ <item>RFC-1509: close RFC-1509 review r1 findings — mark-failed escape, open-error refusal, dry-run coverage</item>
12
+ <item>RFC-1509: address RFC-1509 review r2 — mark-failed without product, journal-read warning, status map diagnostics, V-49 deviations</item>
13
+ </CHANGE_SUMMARY>
14
+ */
15
+
16
+ // @vitest-environment node
17
+
18
+ import { readFile, writeFile } from "node:fs/promises";
19
+ import path from "node:path";
20
+ import { describe, expect, it } from "vitest";
21
+
22
+ import { gitExec } from "../../flow/tests/helpers.ts";
23
+ import { runFlowIntegrate } from "../../flow/handlers/flow-integrate.ts";
24
+ import { runSystemTaskCollect } from "../handlers/system-task-collect.ts";
25
+ import { runSystemTaskDispatch } from "../handlers/system-task-dispatch.ts";
26
+ import { readTask } from "../task-store.ts";
27
+ import {
28
+ localMapFor,
29
+ makeContext,
30
+ makeProduct,
31
+ makeSystemRepo,
32
+ readyBrief,
33
+ taskYaml,
34
+ VALID_EDGES_YAML,
35
+ VALID_PRODUCTS_YAML,
36
+ VALID_SYSTEM_YAML,
37
+ } from "./helpers.ts";
38
+
39
+ function input(flags: Record<string, string | string[] | boolean>) {
40
+ return { argv: [], flags };
41
+ }
42
+
43
+ const DONE_REPORT = `schema: forge/task-report@1
44
+ task: T-0001
45
+ outcome: done
46
+ summary: "brief implemented"
47
+ commits: ["abc123"]
48
+ gate: pass
49
+ acceptance: []
50
+ outbox: []
51
+ `;
52
+
53
+ interface Fixture {
54
+ systemRoot: string;
55
+ productRoot: string;
56
+ worktree: string;
57
+ }
58
+
59
+ /** Dispatch T-0001 into a real alpha product; returns the worktree path. */
60
+ async function dispatched(overrides: Record<string, string> = {}): Promise<Fixture> {
61
+ const productRoot = await makeProduct();
62
+ const systemRoot = await makeSystemRepo({
63
+ "system/system.yaml": VALID_SYSTEM_YAML,
64
+ "system/products.yaml": VALID_PRODUCTS_YAML,
65
+ "system/edges.yaml": VALID_EDGES_YAML,
66
+ "system/products.local.yaml": localMapFor({ alpha: productRoot }),
67
+ "queue/tasks/T-0001.yaml": taskYaml({ state: "ready" }),
68
+ "queue/briefs/T-0001.md": readyBrief(),
69
+ ...overrides,
70
+ });
71
+ const dispatched = await runSystemTaskDispatch(input({ task: "T-0001" }), makeContext(systemRoot));
72
+ if (!dispatched.data?.dispatched) {
73
+ throw new Error(`fixture dispatch failed: ${JSON.stringify(dispatched.data?.diagnostics)}`);
74
+ }
75
+ return { systemRoot, productRoot, worktree: dispatched.data.worktree! };
76
+ }
77
+
78
+ async function writeReport(worktree: string, content = DONE_REPORT): Promise<void> {
79
+ await writeFile(path.join(worktree, ".forge", "task", "report.yaml"), content);
80
+ }
81
+
82
+ describe("system.task.collect", () => {
83
+ it("AC-11: a valid done report moves the task to reported and copies it", async () => {
84
+ const { systemRoot, worktree } = await dispatched();
85
+ await writeReport(worktree);
86
+ const result = await runSystemTaskCollect(input({ task: "T-0001" }), makeContext(systemRoot));
87
+ expect(result.exitCode).toBe(0);
88
+ expect(result.data?.state).toBe("reported");
89
+ const io = (await import("../../../src/utils/io.ts")).resolveIo(undefined);
90
+ const { task } = await readTask(systemRoot, io, "T-0001");
91
+ expect(task!.state).toBe("reported");
92
+ expect(task!.report).toBe("queue/reports/T-0001.yaml");
93
+ const copied = await readFile(path.join(systemRoot, "queue/reports/T-0001.yaml"), "utf8");
94
+ expect(copied).toContain("outcome: done");
95
+ });
96
+
97
+ it("AC-13: a missing report refuses SYSTEM-TASK-07 unless --mark-failed", async () => {
98
+ const { systemRoot } = await dispatched();
99
+ const refused = await runSystemTaskCollect(input({ task: "T-0001" }), makeContext(systemRoot));
100
+ expect(refused.exitCode).toBe(1);
101
+ expect(refused.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-07")).toBe(true);
102
+
103
+ const marked = await runSystemTaskCollect(
104
+ input({ task: "T-0001", "mark-failed": true, reason: "session abandoned" }),
105
+ makeContext(systemRoot),
106
+ );
107
+ expect(marked.exitCode).toBe(0);
108
+ expect(marked.data?.state).toBe("failed");
109
+ });
110
+
111
+ it("--mark-failed without --reason still refuses SYSTEM-TASK-07", async () => {
112
+ const { systemRoot } = await dispatched();
113
+ const result = await runSystemTaskCollect(
114
+ input({ task: "T-0001", "mark-failed": true }),
115
+ makeContext(systemRoot),
116
+ );
117
+ expect(result.exitCode).toBe(1);
118
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-07")).toBe(true);
119
+ });
120
+
121
+ it("--mark-failed stays reachable after the product folder is unmapped", async () => {
122
+ const { systemRoot } = await dispatched();
123
+ // The product mapping disappears post-dispatch — the failure escape must
124
+ // not depend on a resolvable product checkout.
125
+ await writeFile(
126
+ path.join(systemRoot, "system/products.local.yaml"),
127
+ "schema: forge/system-products-local@1\nfolders: {}\n",
128
+ );
129
+ const result = await runSystemTaskCollect(
130
+ input({ task: "T-0001", "mark-failed": true, reason: "product decommissioned" }),
131
+ makeContext(systemRoot),
132
+ );
133
+ expect(result.exitCode).toBe(0);
134
+ expect(result.data?.state).toBe("failed");
135
+ });
136
+
137
+ it("AC-14: a report naming a different task refuses SYSTEM-TASK-08", async () => {
138
+ const { systemRoot, worktree } = await dispatched();
139
+ await writeReport(worktree, DONE_REPORT.replace("task: T-0001", "task: T-0099"));
140
+ const result = await runSystemTaskCollect(input({ task: "T-0001" }), makeContext(systemRoot));
141
+ expect(result.exitCode).toBe(1);
142
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-08")).toBe(true);
143
+ });
144
+
145
+ it("a schema-invalid report refuses SYSTEM-TASK-08", async () => {
146
+ const { systemRoot, worktree } = await dispatched();
147
+ await writeReport(worktree, "schema: forge/task-report@1\ntask: T-0001\noutcome: nonsense\n");
148
+ const result = await runSystemTaskCollect(input({ task: "T-0001" }), makeContext(systemRoot));
149
+ expect(result.exitCode).toBe(1);
150
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-08")).toBe(true);
151
+ });
152
+
153
+ it("a blocked outcome moves the task to failed", async () => {
154
+ const { systemRoot, worktree } = await dispatched();
155
+ await writeReport(worktree, DONE_REPORT.replace("outcome: done", "outcome: blocked"));
156
+ const result = await SystemCollect(systemRoot);
157
+ expect(result.data?.state).toBe("failed");
158
+ });
159
+
160
+ it("AC-12/15: integrate removes the worktree yet collect still reaches integrated via the preserved report + journal", async () => {
161
+ const { systemRoot, productRoot, worktree } = await dispatched();
162
+ await writeReport(worktree);
163
+ // Commit a payload in the task worktree so integrate has something to merge.
164
+ await writeFile(path.join(worktree, "feature.txt"), "payload\n");
165
+ await gitExec(worktree, ["add", "feature.txt"]);
166
+ await gitExec(worktree, ["commit", "-m", "task payload"]);
167
+
168
+ // The product's own flow command integrates the task branch.
169
+ const integrated = await runFlowIntegrate(
170
+ input({}),
171
+ { ...makeContext(worktree), workspaceRoot: worktree },
172
+ );
173
+ if (integrated.exitCode !== 0) {
174
+ console.log("INTEGRATE DIAGNOSTICS", JSON.stringify(integrated.data?.diagnostics, null, 2));
175
+ }
176
+ expect(integrated.exitCode).toBe(0);
177
+ // Worktree is gone; the report survived under the common git dir (AC-15).
178
+ const io = (await import("../../../src/utils/io.ts")).resolveIo(undefined);
179
+ expect(await io.exists(worktree)).toBe(false);
180
+ const common = path.join(productRoot, ".git");
181
+ const preserved = path.join(common, "forge", "tasks", "T-0001", "report.yaml");
182
+ expect(await io.exists(preserved)).toBe(true);
183
+
184
+ const collected = await runSystemTaskCollect(input({ task: "T-0001" }), makeContext(systemRoot));
185
+ expect(collected.exitCode).toBe(0);
186
+ expect(collected.data?.state).toBe("integrated");
187
+ const { task } = await readTask(systemRoot, io, "T-0001");
188
+ expect(task!.state).toBe("integrated");
189
+ expect(task!.integratedCommit).toMatch(/^[0-9a-f]{40}$/);
190
+ // Both history rows recorded in a single collect call.
191
+ expect(task!.history.map((h) => h.to)).toEqual(
192
+ expect.arrayContaining(["reported", "integrated"]),
193
+ );
194
+ });
195
+
196
+ it("a needs-decision report without question refuses SYSTEM-TASK-08", async () => {
197
+ const { systemRoot, worktree } = await dispatched();
198
+ await writeReport(
199
+ worktree,
200
+ DONE_REPORT.replace("outcome: done", "outcome: needs-decision"),
201
+ );
202
+ const result = await runSystemTaskCollect(input({ task: "T-0001" }), makeContext(systemRoot));
203
+ expect(result.exitCode).toBe(1);
204
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-08")).toBe(true);
205
+ });
206
+
207
+ it("--mark-failed escapes a present-but-invalid report (fixHint is reachable)", async () => {
208
+ const { systemRoot, worktree } = await dispatched();
209
+ await writeReport(worktree, "schema: forge/task-report@1\ntask: T-0001\noutcome: nonsense\n");
210
+ const result = await runSystemTaskCollect(
211
+ input({ task: "T-0001", "mark-failed": true, reason: "report is wedged" }),
212
+ makeContext(systemRoot),
213
+ );
214
+ expect(result.exitCode).toBe(0);
215
+ expect(result.data?.state).toBe("failed");
216
+ });
217
+
218
+ it("a reported task with an unresolvable product checkout warns instead of stalling", async () => {
219
+ const systemRoot = await makeSystemRepo({
220
+ "system/system.yaml": VALID_SYSTEM_YAML,
221
+ "system/products.yaml": VALID_PRODUCTS_YAML,
222
+ "system/edges.yaml": VALID_EDGES_YAML,
223
+ "system/products.local.yaml": localMapFor({ alpha: "/definitely/not/here" }),
224
+ "queue/tasks/T-0001.yaml": taskYaml({
225
+ state: "reported",
226
+ report: "queue/reports/T-0001.yaml",
227
+ dispatch: {
228
+ branch: "auto/task-260101-01-x",
229
+ worktree: "/gone",
230
+ baseCommit: "deadbeef",
231
+ dispatchedAt: "2026-01-01T00:00:00Z",
232
+ },
233
+ }),
234
+ "queue/reports/T-0001.yaml": DONE_REPORT,
235
+ });
236
+ const result = await runSystemTaskCollect(input({ task: "T-0001" }), makeContext(systemRoot));
237
+ expect(result.exitCode).toBe(0);
238
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-05" && d.severity === "warning")).toBe(true);
239
+ expect(result.data?.state).toBe("reported");
240
+ });
241
+
242
+ it("collect --dry-run writes nothing", async () => {
243
+ const { systemRoot, worktree } = await dispatched();
244
+ await writeReport(worktree);
245
+ const result = await runSystemTaskCollect(
246
+ input({ task: "T-0001", "dry-run": true }),
247
+ makeContext(systemRoot),
248
+ );
249
+ expect(result.exitCode).toBe(0);
250
+ const io = (await import("../../../src/utils/io.ts")).resolveIo(undefined);
251
+ const { task } = await readTask(systemRoot, io, "T-0001");
252
+ expect(task!.state).toBe("dispatched");
253
+ });
254
+
255
+ it("collect refuses a task that was never dispatched (SYSTEM-TASK-02)", async () => {
256
+ const productRoot = await makeProduct();
257
+ const systemRoot = await makeSystemRepo({
258
+ "system/system.yaml": VALID_SYSTEM_YAML,
259
+ "system/products.yaml": VALID_PRODUCTS_YAML,
260
+ "system/edges.yaml": VALID_EDGES_YAML,
261
+ "system/products.local.yaml": localMapFor({ alpha: productRoot }),
262
+ "queue/tasks/T-0001.yaml": taskYaml({ state: "ready" }),
263
+ });
264
+ const result = await runSystemTaskCollect(input({ task: "T-0001" }), makeContext(systemRoot));
265
+ expect(result.exitCode).toBe(1);
266
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-02")).toBe(true);
267
+ });
268
+ });
269
+
270
+ async function SystemCollect(systemRoot: string) {
271
+ return runSystemTaskCollect(input({ task: "T-0001" }), makeContext(systemRoot));
272
+ }
273
+
274
+ // ---------------------------------------------------------------------------
275
+ // AC-15 isolation — discard also preserves .forge/task/
276
+ // ---------------------------------------------------------------------------
277
+
278
+ describe("task-file preservation on discard", () => {
279
+ it("flow.discard preserves .forge/task/ under the common git dir", async () => {
280
+ const { runFlowDiscard } = await import("../../flow/handlers/flow-discard.ts");
281
+ const { productRoot, worktree } = await dispatched();
282
+ await writeReport(worktree);
283
+ const result = await runFlowDiscard(
284
+ input({ reason: "session dropped" }),
285
+ { ...makeContext(worktree), workspaceRoot: worktree },
286
+ );
287
+ expect(result.exitCode).toBe(0);
288
+ const io = (await import("../../../src/utils/io.ts")).resolveIo(undefined);
289
+ const preserved = path.join(productRoot, ".git", "forge", "tasks", "T-0001", "report.yaml");
290
+ expect(await io.exists(preserved)).toBe(true);
291
+ expect(await io.exists(worktree)).toBe(false);
292
+ });
293
+ });
@@ -0,0 +1,279 @@
1
+ /*
2
+ <MODULE_CONTRACT>
3
+ <purpose>RFC-1509 acceptance tests for system.task.dispatch — product
4
+ resolution, edge gates, brief leak checks, branch/worktree creation,
5
+ git-exclusion of .forge/task/ and the launcher argv.</purpose>
6
+ </MODULE_CONTRACT>
7
+ <CHANGE_SUMMARY>
8
+ <item>RFC-1509: step 5 — dispatch acceptance tests</item>
9
+ <item>RFC-1509: implement RFC-1509 — system task queue, dispatch briefs and task reports</item>
10
+ <item>RFC-1509: close RFC-1509 review r1 findings — mark-failed escape, open-error refusal, dry-run coverage</item>
11
+ </CHANGE_SUMMARY>
12
+ */
13
+
14
+ // @vitest-environment node
15
+
16
+ import { describe, expect, it } from "vitest";
17
+
18
+ import { gitExec } from "../../flow/tests/helpers.ts";
19
+ import { runSystemTaskDispatch } from "../handlers/system-task-dispatch.ts";
20
+ import { readTask } from "../task-store.ts";
21
+ import {
22
+ localMapFor,
23
+ makeContext,
24
+ makeProduct,
25
+ makeSystemRepo,
26
+ readyBrief,
27
+ taskYaml,
28
+ VALID_EDGES_YAML,
29
+ VALID_PRODUCTS_YAML,
30
+ VALID_SYSTEM_YAML,
31
+ } from "./helpers.ts";
32
+
33
+ function input(flags: Record<string, string | string[] | boolean>) {
34
+ return { argv: [], flags };
35
+ }
36
+
37
+ interface DispatchFixture {
38
+ systemRoot: string;
39
+ productRoot: string;
40
+ }
41
+
42
+ /** System repo + governed alpha product + ready T-0001 wired in local map. */
43
+ async function dispatchFixture(overrides: Record<string, string> = {}): Promise<DispatchFixture> {
44
+ const productRoot = await makeProduct();
45
+ const systemRoot = await makeSystemRepo({
46
+ "system/system.yaml": VALID_SYSTEM_YAML,
47
+ "system/products.yaml": VALID_PRODUCTS_YAML,
48
+ "system/edges.yaml": VALID_EDGES_YAML,
49
+ "system/products.local.yaml": localMapFor({ alpha: productRoot, beta: "./beta" }),
50
+ "beta/.gitkeep": "",
51
+ "queue/tasks/T-0001.yaml": taskYaml({ state: "ready" }),
52
+ "queue/briefs/T-0001.md": readyBrief(),
53
+ ...overrides,
54
+ });
55
+ return { systemRoot, productRoot };
56
+ }
57
+
58
+ describe("system.task.dispatch", () => {
59
+ it("AC-4/5/6: dispatch opens auto/task-… with task owner, records branch, worktree stays clean", async () => {
60
+ const { systemRoot, productRoot } = await dispatchFixture();
61
+ const result = await runSystemTaskDispatch(input({ task: "T-0001" }), makeContext(systemRoot));
62
+ expect(result.exitCode).toBe(0);
63
+ expect(result.data?.dispatched).toBe(true);
64
+ const branch = result.data!.branch!;
65
+ expect(branch).toMatch(/^auto\/task-\d{6}-\d{2}-fixture-task$/);
66
+
67
+ // AC-5: the task file records the actual branch + worktree.
68
+ const io = (await import("../../../src/utils/io.ts")).resolveIo(undefined);
69
+ const { task } = await readTask(systemRoot, io, "T-0001");
70
+ expect(task!.state).toBe("dispatched");
71
+ expect(task!.dispatch!.branch).toBe(branch);
72
+ expect(task!.dispatch!.worktree).toBe(result.data!.worktree);
73
+ expect(task!.briefSha256).toMatch(/^[0-9a-f]{64}$/);
74
+
75
+ // Registry owner is the task.
76
+ const registry = await io.readFile(`${productRoot}/.git/forge/registry.yaml`);
77
+ expect(registry).toContain(`kind: task`);
78
+ expect(registry).toContain(`id: T-0001`);
79
+
80
+ // AC-6: the brief file exists and the worktree is porcelain-clean
81
+ // (.forge/task/ is git-excluded through the common dir's info/exclude).
82
+ const briefInTree = await io.readFile(`${result.data!.worktree}/.forge/task/brief.md`);
83
+ expect(briefInTree).toContain("forge/dispatch-brief@1");
84
+ const exclude = await io.readFile(`${productRoot}/.git/info/exclude`);
85
+ expect(exclude).toContain("/.forge/task/");
86
+ const status = await gitExec(result.data!.worktree!, ["status", "--porcelain"]);
87
+ expect(status.stdout.trim()).toBe("");
88
+ });
89
+
90
+ it("AC-9: --dry-run validates but opens no branch and writes nothing", async () => {
91
+ const { systemRoot, productRoot } = await dispatchFixture();
92
+ const result = await runSystemTaskDispatch(
93
+ input({ task: "T-0001", "dry-run": true }),
94
+ makeContext(systemRoot),
95
+ );
96
+ expect(result.exitCode).toBe(0);
97
+ const branches = await gitExec(productRoot, ["branch", "--list", "auto/*"]);
98
+ expect(branches.stdout.trim()).toBe("");
99
+ const io = (await import("../../../src/utils/io.ts")).resolveIo(undefined);
100
+ const { task } = await readTask(systemRoot, io, "T-0001");
101
+ expect(task!.state).toBe("ready");
102
+ });
103
+
104
+ it("AC-7: a forbidden contract edge refuses dispatch (SYSTEM-TASK-03)", async () => {
105
+ const { systemRoot, productRoot } = await dispatchFixture({
106
+ "system/edges.yaml": `schema: forge/system-edges@1
107
+ allowed: []
108
+ forbidden:
109
+ - consumer: alpha
110
+ contract: secret-contract
111
+ reason: never
112
+ `,
113
+ "queue/tasks/T-0001.yaml": taskYaml({ state: "ready", contracts: ["secret-contract"] }),
114
+ });
115
+ const result = await runSystemTaskDispatch(input({ task: "T-0001" }), makeContext(systemRoot));
116
+ expect(result.exitCode).toBe(1);
117
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-03")).toBe(true);
118
+ const branches = await gitExec(productRoot, ["branch", "--list", "auto/*"]);
119
+ expect(branches.stdout.trim()).toBe("");
120
+ });
121
+
122
+ it("undeclared contract edge warns (SYSTEM-TASK-04) but dispatches", async () => {
123
+ const { systemRoot } = await dispatchFixture({
124
+ "queue/tasks/T-0001.yaml": taskYaml({ state: "ready", contracts: ["undeclared-x"] }),
125
+ });
126
+ const result = await runSystemTaskDispatch(input({ task: "T-0001" }), makeContext(systemRoot));
127
+ expect(result.exitCode).toBe(0);
128
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-04")).toBe(true);
129
+ expect(result.data?.dispatched).toBe(true);
130
+ });
131
+
132
+ it("AC-8: a brief leaking the system id refuses (SYSTEM-TASK-06)", async () => {
133
+ const { systemRoot, productRoot } = await dispatchFixture();
134
+ // Embed the system id (demo-system) in the brief body.
135
+ const { writeFile } = await import("node:fs/promises");
136
+ const path = await import("node:path");
137
+ await writeFile(
138
+ path.join(systemRoot, "queue/briefs/T-0001.md"),
139
+ readyBrief() + "\nContext: the demo-system repository wants this.\n",
140
+ );
141
+ const result = await runSystemTaskDispatch(input({ task: "T-0001" }), makeContext(systemRoot));
142
+ expect(result.exitCode).toBe(1);
143
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-06")).toBe(true);
144
+ const branches = await gitExec(productRoot, ["branch", "--list", "auto/*"]);
145
+ expect(branches.stdout.trim()).toBe("");
146
+ });
147
+
148
+ it("a brief leaking another product's folder refuses (SYSTEM-TASK-06)", async () => {
149
+ const beta = await makeProduct("forge-beta-");
150
+ const { systemRoot } = await dispatchFixture({
151
+ "system/products.local.yaml": localMapFor({ alpha: (await makeProduct()), beta }),
152
+ "queue/briefs/T-0001.md": readyBrief() + `\nSibling checkout: ${beta}\n`,
153
+ });
154
+ const result = await runSystemTaskDispatch(input({ task: "T-0001" }), makeContext(systemRoot));
155
+ expect(result.exitCode).toBe(1);
156
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-06")).toBe(true);
157
+ });
158
+
159
+ it("dispatch refuses a non-ready task (SYSTEM-TASK-02)", async () => {
160
+ const { systemRoot } = await dispatchFixture({
161
+ "queue/tasks/T-0001.yaml": taskYaml({ state: "proposed" }),
162
+ });
163
+ const result = await runSystemTaskDispatch(input({ task: "T-0001" }), makeContext(systemRoot));
164
+ expect(result.exitCode).toBe(1);
165
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-02")).toBe(true);
166
+ });
167
+
168
+ it("dispatch refuses a product without a local folder (SYSTEM-TASK-05)", async () => {
169
+ const { systemRoot } = await dispatchFixture({
170
+ "system/products.local.yaml": localMapFor({ beta: "./beta" }),
171
+ });
172
+ const result = await runSystemTaskDispatch(input({ task: "T-0001" }), makeContext(systemRoot));
173
+ expect(result.exitCode).toBe(1);
174
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-05")).toBe(true);
175
+ });
176
+
177
+ it("AC-10: launcher argv substitutes {worktree} and {task}", async () => {
178
+ const productRoot = await makeProduct();
179
+ const log = `${productRoot}/launcher.log`;
180
+ const systemRoot = await makeSystemRepo({
181
+ "system/system.yaml": VALID_SYSTEM_YAML,
182
+ "system/products.yaml": VALID_PRODUCTS_YAML,
183
+ "system/edges.yaml": VALID_EDGES_YAML,
184
+ "system/products.local.yaml": localMapFor(
185
+ { alpha: productRoot },
186
+ ["sh", "-c", `echo "$1|$2" > ${log}`, "sh", "{worktree}", "{task}"],
187
+ ),
188
+ "beta/.gitkeep": "",
189
+ "queue/tasks/T-0001.yaml": taskYaml({ state: "ready" }),
190
+ "queue/briefs/T-0001.md": readyBrief(),
191
+ });
192
+ const result = await runSystemTaskDispatch(input({ task: "T-0001" }), makeContext(systemRoot));
193
+ expect(result.exitCode).toBe(0);
194
+ expect(result.data?.launcherRan).toBe(true);
195
+ expect(result.data?.launcherExit).toBe(0);
196
+ const { readFile } = await import("node:fs/promises");
197
+ const logged = await readFile(log, "utf8");
198
+ expect(logged.trim()).toBe(`${result.data!.worktree}|T-0001`);
199
+ });
200
+
201
+ it("a failed worktree-setup hook leaves the task ready and writes nothing (FLOW-OPEN-07)", async () => {
202
+ const { systemRoot, productRoot } = await dispatchFixture();
203
+ const { mkdir, writeFile } = await import("node:fs/promises");
204
+ const path = await import("node:path");
205
+ await mkdir(path.join(productRoot, "hooks"), { recursive: true });
206
+ await writeFile(path.join(productRoot, "hooks", "worktree-setup.sh"), "exit 7\n");
207
+ const result = await runSystemTaskDispatch(input({ task: "T-0001" }), makeContext(systemRoot));
208
+ expect(result.exitCode).toBe(1);
209
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "FLOW-OPEN-07")).toBe(true);
210
+ expect(result.data?.dispatched).toBe(false);
211
+ const io = (await import("../../../src/utils/io.ts")).resolveIo(undefined);
212
+ const { task } = await readTask(systemRoot, io, "T-0001");
213
+ expect(task!.state).toBe("ready");
214
+ expect(task!.dispatch).toBeNull();
215
+ // The launcher must not run into the broken worktree.
216
+ expect(result.data?.launcherRan).toBe(false);
217
+ });
218
+
219
+ it("dispatch --dry-run exercises the governed check — an ungoverned folder fails", async () => {
220
+ const { mkdtemp } = await import("node:fs/promises");
221
+ const os = await import("node:os");
222
+ const path = await import("node:path");
223
+ const ungoverned = await mkdtemp(path.join(os.tmpdir(), "ungoverned-"));
224
+ const { systemRoot } = await dispatchFixture({
225
+ "system/products.local.yaml": localMapFor({ alpha: ungoverned }),
226
+ });
227
+ const result = await runSystemTaskDispatch(
228
+ input({ task: "T-0001", "dry-run": true }),
229
+ makeContext(systemRoot),
230
+ );
231
+ expect(result.exitCode).toBe(1);
232
+ expect(
233
+ result.data?.diagnostics.some((d) => d.ruleId === "FLOW-OPEN-06" || d.ruleId === "FLOW-OPEN-01"),
234
+ ).toBe(true);
235
+ });
236
+
237
+ it("a brief whose frontmatter names a different task refuses (SYSTEM-TASK-02)", async () => {
238
+ const { systemRoot } = await dispatchFixture({
239
+ "queue/briefs/T-0001.md": readyBrief("T-0099"),
240
+ });
241
+ const result = await runSystemTaskDispatch(input({ task: "T-0001" }), makeContext(systemRoot));
242
+ expect(result.exitCode).toBe(1);
243
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-02")).toBe(true);
244
+ });
245
+
246
+ it("a brief naming the resolved sibling folder refuses (SYSTEM-TASK-06)", async () => {
247
+ const { mkdtemp } = await import("node:fs/promises");
248
+ const os = await import("node:os");
249
+ const path = await import("node:path");
250
+ const betaParent = await mkdtemp(path.join(os.tmpdir(), "beta-parent-"));
251
+ const beta = path.join(betaParent, "beta-checkout");
252
+ await (await import("node:fs/promises")).mkdir(beta, { recursive: true });
253
+ const { systemRoot } = await dispatchFixture({
254
+ "system/products.local.yaml": localMapFor({ alpha: (await makeProduct()), beta }),
255
+ "queue/briefs/T-0001.md": readyBrief() + `\nSibling checkout lives at ${beta}\n`,
256
+ });
257
+ const result = await runSystemTaskDispatch(input({ task: "T-0001" }), makeContext(systemRoot));
258
+ expect(result.exitCode).toBe(1);
259
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-06")).toBe(true);
260
+ });
261
+
262
+ it("launcher non-zero exit warns SYSTEM-TASK-09 but the task stays dispatched", async () => {
263
+ const productRoot = await makeProduct();
264
+ const systemRoot = await makeSystemRepo({
265
+ "system/system.yaml": VALID_SYSTEM_YAML,
266
+ "system/products.yaml": VALID_PRODUCTS_YAML,
267
+ "system/edges.yaml": VALID_EDGES_YAML,
268
+ "system/products.local.yaml": localMapFor({ alpha: productRoot }, ["sh", "-c", "exit 3"]),
269
+ "beta/.gitkeep": "",
270
+ "queue/tasks/T-0001.yaml": taskYaml({ state: "ready" }),
271
+ "queue/briefs/T-0001.md": readyBrief(),
272
+ });
273
+ const result = await runSystemTaskDispatch(input({ task: "T-0001" }), makeContext(systemRoot));
274
+ expect(result.exitCode).toBe(0);
275
+ expect(result.data?.dispatched).toBe(true);
276
+ expect(result.data?.launcherExit).toBe(3);
277
+ expect(result.data?.diagnostics.some((d) => d.ruleId === "SYSTEM-TASK-09" && d.severity === "warning")).toBe(true);
278
+ });
279
+ });