@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,353 @@
1
+ /*
2
+ <MODULE_CONTRACT>
3
+ <purpose>system.task.move (RFC-1509) — the state-machine command. Applies
4
+ every move-owned transition (proposed→ready/cancelled, ready/failed→parked,
5
+ parked→ready, reported→ready, failed→ready, integrated→verified,
6
+ verified→done) and refuses everything else with SYSTEM-TASK-02. Ready-checks
7
+ validate the dispatch-brief frontmatter, dependency completion and edge
8
+ verdicts; verified-checks name a version-set file that lists the product.</purpose>
9
+ <non-goals>
10
+ <item>No dispatch or collect transitions — those live in their own commands.</item>
11
+ </non-goals>
12
+ </MODULE_CONTRACT>
13
+ <CHANGE_SUMMARY>
14
+ <item>RFC-1509: step 3 — system.task.move handler</item>
15
+ <item>RFC-1509: implement RFC-1509 — system task queue, dispatch briefs and task reports</item>
16
+ <item>RFC-1509: close RFC-1509 review r1 findings — mark-failed escape, open-error refusal, dry-run coverage</item>
17
+ <item>RFC-1511: accept recorded products map in verified gate, guard folders lookup and exec edges (RFC-1511 review r1)</item>
18
+ </CHANGE_SUMMARY>
19
+ */
20
+
21
+ import path from "node:path";
22
+ import YAML from "yaml";
23
+
24
+ import { resolveIo } from "../../../src/utils/io.ts";
25
+ import type {
26
+ Diagnostic,
27
+ ForgeCommandInput,
28
+ ForgeCommandResult,
29
+ ForgeRuntimeContext,
30
+ } from "../../../src/types.ts";
31
+ import { assertSystemRepository, edgeVerdict, loadSystemMap, type SystemMap } from "../repo.ts";
32
+ import {
33
+ dispatchBriefFrontmatterSchema,
34
+ findTransition,
35
+ TASK_STATES,
36
+ type SystemTask,
37
+ type TaskState,
38
+ } from "../task-schemas.ts";
39
+ import { readBrief, readTask, writeTask } from "../task-store.ts";
40
+ import { BRIEF_GOAL_PLACEHOLDER } from "./system-task-create.ts";
41
+
42
+ export interface SystemTaskMoveResult {
43
+ command: "system.task.move";
44
+ task: string | null;
45
+ from: TaskState | null;
46
+ to: TaskState | null;
47
+ moved: boolean;
48
+ diagnostics: Diagnostic[];
49
+ }
50
+
51
+ function flagString(input: ForgeCommandInput, name: string): string | null {
52
+ const v = input.flags[name];
53
+ return typeof v === "string" && v.length > 0 ? v : null;
54
+ }
55
+
56
+ function refusal(
57
+ ruleId: string,
58
+ message: string,
59
+ fixHint?: string,
60
+ file?: string,
61
+ ): Diagnostic {
62
+ return { ruleId, severity: "error", message, ...(fixHint ? { fixHint } : {}), ...(file ? { file } : {}) };
63
+ }
64
+
65
+ /**
66
+ * The `→ ready` check (proposed / parked / failed → ready): brief frontmatter
67
+ * must validate and carry a real goal, every dependsOn task must be done, and
68
+ * every contract edge must be non-forbidden. Failure → SYSTEM-TASK-02 diag;
69
+ * forbidden → SYSTEM-TASK-03; undeclared → SYSTEM-TASK-04 warning.
70
+ */
71
+ async function readyChecks(
72
+ root: string,
73
+ io: Parameters<typeof loadSystemMap>[1],
74
+ map: SystemMap,
75
+ task: SystemTask,
76
+ diagnostics: Diagnostic[],
77
+ ): Promise<void> {
78
+ const brief = await readBrief(root, io, task.id);
79
+ if (brief === null || brief.frontmatter === null) {
80
+ diagnostics.push(
81
+ refusal(
82
+ "SYSTEM-TASK-02",
83
+ `${task.id} → ready: queue/briefs/${task.id}.md is missing or its frontmatter is not YAML`,
84
+ "create the dispatch brief before moving to ready",
85
+ ),
86
+ );
87
+ } else {
88
+ const parsed = dispatchBriefFrontmatterSchema.safeParse(brief.frontmatter);
89
+ if (!parsed.success) {
90
+ diagnostics.push(
91
+ refusal(
92
+ "SYSTEM-TASK-02",
93
+ `${task.id} → ready: brief frontmatter fails forge/dispatch-brief@1 — ${parsed.error.issues
94
+ .map((i) => `${i.path.join(".") || "(root)"}: ${i.message}`)
95
+ .join("; ")}`,
96
+ "fix the brief frontmatter per the schema",
97
+ ),
98
+ );
99
+ } else if (parsed.data.goal.trim() === BRIEF_GOAL_PLACEHOLDER) {
100
+ diagnostics.push(
101
+ refusal(
102
+ "SYSTEM-TASK-02",
103
+ `${task.id} → ready: brief goal is still the create-scaffold placeholder`,
104
+ "write the real goal in the brief frontmatter",
105
+ ),
106
+ );
107
+ } else if (parsed.data.task !== task.id || parsed.data.product !== task.product) {
108
+ diagnostics.push(
109
+ refusal(
110
+ "SYSTEM-TASK-02",
111
+ `${task.id} → ready: brief frontmatter names task=${parsed.data.task} product=${parsed.data.product}, expected ${task.id} / ${task.product}`,
112
+ "fix the frontmatter — the brief must be bound to this task",
113
+ ),
114
+ );
115
+ }
116
+ }
117
+
118
+ for (const dep of task.dependsOn) {
119
+ const { task: depTask, diagnostic } = await readTask(root, io, dep);
120
+ if (diagnostic) {
121
+ diagnostics.push(
122
+ refusal("SYSTEM-TASK-02", `${task.id} → ready: ${diagnostic.message}`),
123
+ );
124
+ } else if (depTask && depTask.state !== "done") {
125
+ diagnostics.push(
126
+ refusal(
127
+ "SYSTEM-TASK-02",
128
+ `${task.id} → ready: dependency ${dep} is ${depTask.state}, not done`,
129
+ `finish ${dep} first or drop it from dependsOn`,
130
+ ),
131
+ );
132
+ }
133
+ }
134
+
135
+ for (const contract of task.contracts) {
136
+ const verdict = edgeVerdict(map, task.product, contract);
137
+ if (verdict === "forbidden") {
138
+ diagnostics.push(
139
+ refusal(
140
+ "SYSTEM-TASK-03",
141
+ `${task.id} → ready: contract '${contract}' is a forbidden edge for ${task.product}`,
142
+ "renegotiate the edge in system/edges.yaml before dispatching",
143
+ ),
144
+ );
145
+ } else if (verdict === "undeclared") {
146
+ diagnostics.push({
147
+ ruleId: "SYSTEM-TASK-04",
148
+ severity: "warning",
149
+ message: `${task.id} → ready: contract '${contract}' is undeclared for ${task.product} — allowed, but declare it in system/edges.yaml`,
150
+ });
151
+ }
152
+ }
153
+ }
154
+
155
+ /**
156
+ * `integrated → verified` check: --version-set <id> must name a file
157
+ * system/version-sets/<id>.yaml listing task.product.
158
+ */
159
+ async function verifiedChecks(
160
+ root: string,
161
+ io: Parameters<typeof loadSystemMap>[1],
162
+ task: SystemTask,
163
+ versionSet: string | null,
164
+ diagnostics: Diagnostic[],
165
+ ): Promise<void> {
166
+ if (versionSet === null) {
167
+ diagnostics.push(
168
+ refusal(
169
+ "SYSTEM-TASK-02",
170
+ `${task.id} → verified: --version-set <id> is required`,
171
+ "pass the verified version-set id published by the system repo",
172
+ ),
173
+ );
174
+ return;
175
+ }
176
+ const file = path.join(root, "system", "version-sets", `${versionSet}.yaml`);
177
+ if (!(await io.exists(file))) {
178
+ diagnostics.push(
179
+ refusal(
180
+ "SYSTEM-TASK-02",
181
+ `${task.id} → verified: no version-set file at system/version-sets/${versionSet}.yaml`,
182
+ "publish the version-set file first (RFC-1511)",
183
+ ),
184
+ );
185
+ return;
186
+ }
187
+ try {
188
+ const raw = YAML.parse(await io.readFile(file));
189
+ // RFC-1511: forge/version-set@1 records products as a { id: tag } map;
190
+ // the earlier provisional array forms (string | {id}) stay accepted.
191
+ const rawProducts =
192
+ raw !== null && typeof raw === "object"
193
+ ? (raw as Record<string, unknown>).products
194
+ : undefined;
195
+ const listed = Array.isArray(rawProducts)
196
+ ? rawProducts.some(
197
+ (p) =>
198
+ p === task.product ||
199
+ (p !== null && typeof p === "object" && (p as Record<string, unknown>).id === task.product),
200
+ )
201
+ : rawProducts !== null && typeof rawProducts === "object"
202
+ ? Object.hasOwn(rawProducts, task.product)
203
+ : false;
204
+ if (!listed) {
205
+ diagnostics.push(
206
+ refusal(
207
+ "SYSTEM-TASK-02",
208
+ `${task.id} → verified: version-set ${versionSet} does not list product ${task.product}`,
209
+ "pick the version-set that verified this product",
210
+ ),
211
+ );
212
+ }
213
+ } catch (error) {
214
+ diagnostics.push(
215
+ refusal(
216
+ "SYSTEM-TASK-02",
217
+ `${task.id} → verified: system/version-sets/${versionSet}.yaml is not readable YAML: ${error instanceof Error ? error.message : String(error)}`,
218
+ ),
219
+ );
220
+ }
221
+ }
222
+
223
+ export async function runSystemTaskMove(
224
+ input: ForgeCommandInput,
225
+ context: ForgeRuntimeContext,
226
+ ): Promise<ForgeCommandResult<SystemTaskMoveResult>> {
227
+ const io = resolveIo(context.io);
228
+ const root = context.workspaceRoot;
229
+ const diagnostics: Diagnostic[] = [];
230
+ const dryRun = context.dryRun || input.flags["dry-run"] === true;
231
+
232
+ const refusalDiag = await assertSystemRepository(root, io);
233
+ if (refusalDiag) diagnostics.push(refusalDiag);
234
+
235
+ const id = flagString(input, "task");
236
+ const to = flagString(input, "to");
237
+ const reason = flagString(input, "reason");
238
+ const versionSet = flagString(input, "version-set");
239
+
240
+ if (id === null || to === null) {
241
+ diagnostics.push(
242
+ refusal("SYSTEM-TASK-02", "--task and --to are required", "see forge system.task.move --help"),
243
+ );
244
+ }
245
+
246
+ let task: SystemTask | null = null;
247
+ let from: TaskState | null = null;
248
+ if (diagnostics.every((d) => d.severity !== "error")) {
249
+ const loaded = await readTask(root, io, id!);
250
+ if (loaded.diagnostic) diagnostics.push(loaded.diagnostic);
251
+ task = loaded.task;
252
+ from = task?.state ?? null;
253
+ }
254
+
255
+ let map: SystemMap | null = null;
256
+ if (diagnostics.every((d) => d.severity !== "error") && task !== null) {
257
+ if (!(TASK_STATES as readonly string[]).includes(to!)) {
258
+ diagnostics.push(
259
+ refusal(
260
+ "SYSTEM-TASK-02",
261
+ `${task.id}: '${to}' is not a task state`,
262
+ `states: ${TASK_STATES.join(", ")}`,
263
+ ),
264
+ );
265
+ } else {
266
+ const transition = findTransition(task.state, to as TaskState);
267
+ if (transition === null) {
268
+ diagnostics.push(
269
+ refusal(
270
+ "SYSTEM-TASK-02",
271
+ `${task.id}: ${task.state} → ${to} is not a legal transition`,
272
+ "see the RFC-1509 transition table",
273
+ ),
274
+ );
275
+ } else if (transition.owner !== "move") {
276
+ diagnostics.push(
277
+ refusal(
278
+ "SYSTEM-TASK-02",
279
+ `${task.id}: ${task.state} → ${to} is owned by system.task.${transition.owner}, not move`,
280
+ `run system.task.${transition.owner} --task ${task.id}`,
281
+ ),
282
+ );
283
+ } else if (transition.needsReason && (reason === null || reason.trim() === "")) {
284
+ diagnostics.push(
285
+ refusal(
286
+ "SYSTEM-TASK-02",
287
+ `${task.id}: ${task.state} → ${to} requires --reason`,
288
+ "the history row records why the task changed course",
289
+ ),
290
+ );
291
+ } else {
292
+ // Load the map only when a check needs it.
293
+ const loadedMap = await loadSystemMap(root, io);
294
+ diagnostics.push(...loadedMap.diagnostics.filter((d) => d.ruleId === "SYSTEM-MAP-01"));
295
+ map = loadedMap.map;
296
+ if (to === "ready" && map !== null) {
297
+ await readyChecks(root, io, map, task, diagnostics);
298
+ } else if (to === "verified") {
299
+ await verifiedChecks(root, io, task, versionSet, diagnostics);
300
+ }
301
+ }
302
+ }
303
+ }
304
+
305
+ const errors = diagnostics.filter((d) => d.severity === "error").length;
306
+ const canMove = task !== null && errors === 0;
307
+ if (canMove && !dryRun) {
308
+ const next = {
309
+ ...task!,
310
+ state: to as TaskState,
311
+ history: [
312
+ ...task!.history,
313
+ {
314
+ at: new Date().toISOString(),
315
+ from: task!.state,
316
+ to: to as TaskState,
317
+ by: "operator",
318
+ ...(reason ? { reason } : {}),
319
+ },
320
+ ],
321
+ };
322
+ await writeTask(root, io, next);
323
+ }
324
+
325
+ return {
326
+ data: {
327
+ command: "system.task.move",
328
+ task: task?.id ?? id,
329
+ from,
330
+ to: (to as TaskState) ?? null,
331
+ moved: canMove && !dryRun,
332
+ diagnostics,
333
+ },
334
+ exitCode: errors > 0 ? 1 : 0,
335
+ summary:
336
+ task === null
337
+ ? `system.task.move refused (${errors} error(s))`
338
+ : canMove
339
+ ? dryRun
340
+ ? `system.task.move would move ${task.id} ${from} → ${to}`
341
+ : `system.task.move moved ${task.id} ${from} → ${to}`
342
+ : `system.task.move refused ${task.id} ${from} → ${to} (${errors} error(s))`,
343
+ nextSteps:
344
+ canMove && !dryRun && to === "ready"
345
+ ? [
346
+ {
347
+ action: `system.task.dispatch --task ${task!.id}`,
348
+ kind: "optional",
349
+ },
350
+ ]
351
+ : [],
352
+ };
353
+ }
@@ -0,0 +1,176 @@
1
+ /*
2
+ <MODULE_CONTRACT>
3
+ <purpose>system.task.status (RFC-1509) — read-only projection of every task
4
+ plus the product-side flow facts for dispatched work. The system task file
5
+ is authoritative for the state; the product layer adds registry/journal
6
+ evidence: branch registered, integrated/discarded per journal, orphaned
7
+ (no registry entry AND no journal integrate/discard for dispatch.branch),
8
+ or unknown when the product repo cannot be read.</purpose>
9
+ <non-goals>
10
+ <item>Never mutates — no task writes, no registry writes.</item>
11
+ </non-goals>
12
+ </MODULE_CONTRACT>
13
+ <CHANGE_SUMMARY>
14
+ <item>RFC-1509: step 3 — system.task.status handler</item>
15
+ <item>RFC-1509: implement RFC-1509 — system task queue, dispatch briefs and task reports</item>
16
+ <item>RFC-1509: close RFC-1509 review r1 findings — mark-failed escape, open-error refusal, dry-run coverage</item>
17
+ <item>RFC-1509: address RFC-1509 review r2 — mark-failed without product, journal-read warning, status map diagnostics, V-49 deviations</item>
18
+ </CHANGE_SUMMARY>
19
+ */
20
+
21
+ import path from "node:path";
22
+
23
+ import { resolveIo } from "../../../src/utils/io.ts";
24
+ import type {
25
+ Diagnostic,
26
+ ForgeCommandInput,
27
+ ForgeCommandResult,
28
+ ForgeRuntimeContext,
29
+ } from "../../../src/types.ts";
30
+ import { collectFlowStatus } from "../../flow/handlers/flow-status.ts";
31
+ import { commonGitDir, readJournal } from "../../flow/registry.ts";
32
+ import { assertSystemRepository, loadSystemMap } from "../repo.ts";
33
+ import type { SystemTask } from "../task-schemas.ts";
34
+ import { listTasks, readTask } from "../task-store.ts";
35
+
36
+ export type TaskProductStatus =
37
+ | "registered"
38
+ | "integrated"
39
+ | "discarded"
40
+ | "orphaned"
41
+ | "unknown"
42
+ | "not-dispatched";
43
+
44
+ export interface TaskStatusRow {
45
+ id: string;
46
+ title: string;
47
+ product: string;
48
+ kind: string;
49
+ state: SystemTask["state"];
50
+ branch: string | null;
51
+ productStatus: TaskProductStatus;
52
+ productMessage: string | null;
53
+ }
54
+
55
+ export interface SystemTaskStatusResult {
56
+ command: "system.task.status";
57
+ tasks: TaskStatusRow[];
58
+ diagnostics: Diagnostic[];
59
+ }
60
+
61
+ function flagString(input: ForgeCommandInput, name: string): string | null {
62
+ const v = input.flags[name];
63
+ return typeof v === "string" && v.length > 0 ? v : null;
64
+ }
65
+
66
+ export async function runSystemTaskStatus(
67
+ input: ForgeCommandInput,
68
+ context: ForgeRuntimeContext,
69
+ ): Promise<ForgeCommandResult<SystemTaskStatusResult>> {
70
+ const io = resolveIo(context.io);
71
+ const root = context.workspaceRoot;
72
+ const diagnostics: Diagnostic[] = [];
73
+
74
+ const refusalDiag = await assertSystemRepository(root, io);
75
+ if (refusalDiag) diagnostics.push(refusalDiag);
76
+
77
+ const only = flagString(input, "task");
78
+ let tasks: SystemTask[] = [];
79
+ if (diagnostics.every((d) => d.severity !== "error")) {
80
+ if (only !== null) {
81
+ const read = await readTask(root, io, only);
82
+ if (read.diagnostic) diagnostics.push(read.diagnostic);
83
+ if (read.task) tasks = [read.task];
84
+ } else {
85
+ const listed = await listTasks(root, io);
86
+ diagnostics.push(...listed.diagnostics);
87
+ tasks = listed.tasks;
88
+ }
89
+ }
90
+
91
+ let map: Awaited<ReturnType<typeof loadSystemMap>>["map"] = null;
92
+ if (tasks.length > 0) {
93
+ const loaded = await loadSystemMap(root, io);
94
+ // Surface map-load diagnostics — a corrupt products.local.yaml must not
95
+ // masquerade as a plain "no folder" productMessage below.
96
+ diagnostics.push(...loaded.diagnostics);
97
+ map = loaded.map;
98
+ }
99
+
100
+ const rows: TaskStatusRow[] = [];
101
+ for (const task of tasks) {
102
+ const row: TaskStatusRow = {
103
+ id: task.id,
104
+ title: task.title,
105
+ product: task.product,
106
+ kind: task.kind,
107
+ state: task.state,
108
+ branch: task.dispatch?.branch ?? null,
109
+ productStatus: "not-dispatched",
110
+ productMessage: null,
111
+ };
112
+ if (task.dispatch !== null && task.state !== "cancelled") {
113
+ const folders = map?.local?.folders ?? {};
114
+ const folder = Object.prototype.hasOwnProperty.call(folders, task.product)
115
+ ? folders[task.product]
116
+ : undefined;
117
+ if (folder === undefined || map === null) {
118
+ row.productStatus = "unknown";
119
+ row.productMessage =
120
+ map === null
121
+ ? "system map failed to load — see diagnostics"
122
+ : "no products.local.yaml folder for this product";
123
+ } else {
124
+ const productRoot = path.isAbsolute(folder) ? folder : path.resolve(root, folder);
125
+ try {
126
+ const status = await collectFlowStatus(productRoot, io);
127
+ const entry = status.data.branches.find(
128
+ (b) => b.registered && b.name === task.dispatch!.branch,
129
+ );
130
+ let journalEvent: "integrate" | "discard" | null = null;
131
+ let journalReadable = true;
132
+ try {
133
+ const common = await commonGitDir(io, productRoot);
134
+ const events = await readJournal(io, common);
135
+ const found = events.find(
136
+ (e) =>
137
+ (e.event === "integrate" || e.event === "discard") &&
138
+ e.branch === task.dispatch!.branch,
139
+ );
140
+ journalEvent = found?.event === "discard" ? "discard" : found ? "integrate" : null;
141
+ } catch {
142
+ journalReadable = false;
143
+ }
144
+ if (status.registryError !== null || !journalReadable) {
145
+ // Orphaned is a negative claim — corrupt evidence must not assert it.
146
+ row.productStatus = "unknown";
147
+ row.productMessage = status.registryError
148
+ ? `product registry is corrupt: ${status.registryError.message}`
149
+ : "product journal is unreadable";
150
+ } else if (entry !== undefined) {
151
+ row.productStatus = "registered";
152
+ row.productMessage = `worktree ${entry.worktree ?? "(none)"}`;
153
+ } else if (journalEvent === "integrate") {
154
+ row.productStatus = "integrated";
155
+ } else if (journalEvent === "discard") {
156
+ row.productStatus = "discarded";
157
+ } else {
158
+ row.productStatus = "orphaned";
159
+ row.productMessage = `branch ${task.dispatch.branch} has no registry entry and no journal integrate/discard event`;
160
+ }
161
+ } catch (error) {
162
+ row.productStatus = "unknown";
163
+ row.productMessage = `product repo unreadable: ${error instanceof Error ? error.message : String(error)}`;
164
+ }
165
+ }
166
+ }
167
+ rows.push(row);
168
+ }
169
+
170
+ const errors = diagnostics.filter((d) => d.severity === "error").length;
171
+ return {
172
+ data: { command: "system.task.status", tasks: rows, diagnostics },
173
+ exitCode: errors > 0 ? 1 : 0,
174
+ summary: `system.task.status: ${rows.length} task(s)${errors > 0 ? ` — ${errors} error(s)` : ""}`,
175
+ };
176
+ }