@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
@@ -12,19 +12,22 @@ verification evidence, and atomically mutates RFC frontmatter.
12
12
  </non-goals>
13
13
  </MODULE_CONTRACT>
14
14
  <CHANGE_SUMMARY>
15
- <item>RFC-1333: CX-1/CX-2 fixes — docs/** binding, waiver covers a missing report, IMP-12 for probe-less RFCs; merge-aware fail-closed binding.</item>
16
- <item>RFC-1391: make the formal verification tier checker-bound (RFC-1391)</item>
17
15
  <item>RFC-1453: executed outcome probes, IMP-14 surface gate, outcome.serve + V-47/V-48</item>
18
16
  <item>RFC-1454: review deviations recorded in the RFC — IMP-15 gate, V-49 provenance, skill contract</item>
19
17
  <item>RFC-1483: self-sufficient implement stamp — default repair pass, ordered remediation checklist, --no-repair</item>
20
- <history>RFC-0268, RFC-0476, RFC-0756, RFC-0795, RFC-0997, RFC-1053, RFC-1097, RFC-1249, RFC-1271, RFC-1306, RFC-1310, RFC-1321, RFC-1322, RFC-1330, RFC-1333, RFC-1383, RFC-IMP-07</history>
18
+ <item>RFC-1504: step 4 — shared stamp lock for rfc and adr stamp handlers</item>
19
+ <item>RFC-1504: review fixes — legacy acquiredAt fallback, atomic break claim, verified stamp release, onStale warnings</item>
20
+ <history>RFC-0268, RFC-0476, RFC-0756, RFC-0795, RFC-0997, RFC-1053, RFC-1097, RFC-1249, RFC-1271, RFC-1306, RFC-1310, RFC-1321, RFC-1322, RFC-1330, RFC-1333, RFC-1383, RFC-1391, RFC-IMP-07</history>
21
21
  </CHANGE_SUMMARY>
22
22
  */
23
23
 
24
- import { execFile, writeFileSync } from "../../../src/utils/sync-fs.ts";
24
+ import { execFile } from "../../../src/utils/sync-fs.ts";
25
25
  import { ambientIo, resolveIo } from "../../../src/utils/io.ts";
26
26
 
27
- import { join, dirname, relative, resolve, sep } from "node:path";
27
+ import { join, relative, resolve, sep } from "node:path";
28
+
29
+ import { acquireStampLock } from "../../core/stamp-lock.ts";
30
+ import type { StampLockHeld, StampLockOptions } from "../../core/stamp-lock.ts";
28
31
 
29
32
  import { writeFileAtomic } from "../../../src/utils/fs-atomic.ts";
30
33
  import { parse as yamlParse } from "yaml";
@@ -237,29 +240,27 @@ function lockFilePath(workspaceRoot: string, rfcId: string): string {
237
240
  return join(workspaceRoot, ".rfc-locks", `${slug}.lock`);
238
241
  }
239
242
 
240
- async function acquireRfcLock(
243
+ // RFC-1504: shared dead-holder recovery — a lock recording this host and a
244
+ // dead pid (or a foreign boot id) is broken and retried; a live pid still
245
+ // returns acquired:false → RFC-IMP-05. Exported for the lock tests.
246
+ export async function acquireRfcLock(
241
247
  workspaceRoot: string,
242
248
  rfcId: string,
243
- ): Promise<{ acquired: boolean; lockPath: string }> {
249
+ opts: StampLockOptions = {},
250
+ ): Promise<{ acquired: boolean; lockPath: string; held?: StampLockHeld }> {
244
251
  const lockPath = lockFilePath(workspaceRoot, rfcId);
245
- const lockDir = dirname(lockPath);
246
- await io.mkdir(lockDir);
247
-
248
- // Atomic lock acquisition via O_EXCL file creation
249
- try {
250
- writeFileSync(
251
- lockPath,
252
- JSON.stringify({ pid: process.pid, acquiredAt: new Date().toISOString() }),
253
- { flag: "wx" },
254
- );
255
- return { acquired: true, lockPath };
256
- } catch {
257
- return { acquired: false, lockPath };
258
- }
252
+ return acquireStampLock(lockPath, opts);
259
253
  }
260
254
 
261
- async function releaseRfcLock(lockPath: string): Promise<void> {
255
+ // RFC-1504: release only the lock we hold — after a dead/stale break a
256
+ // contender may have written a fresh payload, and unlinking it would break
257
+ // mutual exclusion entirely. Mirrors withFlowLock's ownership check.
258
+ async function releaseRfcLock(lockPath: string, held?: StampLockHeld): Promise<void> {
262
259
  try {
260
+ if (held !== undefined) {
261
+ const parsed = JSON.parse(await io.readFile(lockPath)) as { pid?: number; at?: string };
262
+ if (parsed.pid !== held.pid || parsed.at !== held.at) return;
263
+ }
263
264
  await io.rm(lockPath);
264
265
  } catch {
265
266
  // Lock file already removed — safe
@@ -1178,7 +1179,12 @@ export async function runRfcImplementStamp(
1178
1179
  // Under the default repair pass the lock must precede the FIRST write —
1179
1180
  // which is the emit-if-needed envelope refresh, not the stamp mutation —
1180
1181
  // so all envelope-independent gates above abort before this point.
1181
- const { acquired, lockPath } = await acquireRfcLock(workspaceRoot, targetId);
1182
+ const { acquired, lockPath, held } = await acquireRfcLock(workspaceRoot, targetId, {
1183
+ onStale: (info) =>
1184
+ logger.warn(
1185
+ `broke ${info.reason} stamp lock ${info.path} (pid ${info.pid ?? "?"} on ${info.host ?? "?"})`,
1186
+ ),
1187
+ });
1182
1188
  if (!acquired) {
1183
1189
  violations.push({
1184
1190
  rule: "RFC-IMP-05",
@@ -1504,7 +1510,7 @@ export async function runRfcImplementStamp(
1504
1510
  ],
1505
1511
  };
1506
1512
  } finally {
1507
- await releaseRfcLock(lockPath);
1513
+ await releaseRfcLock(lockPath, held);
1508
1514
  }
1509
1515
  }
1510
1516
 
@@ -0,0 +1,120 @@
1
+ /*
2
+ <MODULE_CONTRACT>
3
+ <purpose>outbox.validate (RFC-1510) — the single validator of the outbox
4
+ contract (DNA-91). Reads docs/outbox/*.yaml in ANY repository profile,
5
+ checks every entry against forge/outbox@1 (OUTBOX-01) and binds each entry's
6
+ id to its filename stem (OUTBOX-02). An absent docs/outbox/ is a pass —
7
+ outbox adoption is opt-in per product.</purpose>
8
+ <non-goals>
9
+ <item>No system-side checks — matching and the identity-leak warning are
10
+ system.outbox.scan's job; a product cannot know a system's name.</item>
11
+ </non-goals>
12
+ </MODULE_CONTRACT>
13
+ <CHANGE_SUMMARY>
14
+ <item>RFC-1510: step 2 — profile-agnostic outbox entry validator</item>
15
+ <item>RFC-1510: implement RFC-1510 — product outbox for dependency-contract requests</item>
16
+ <item>RFC-1510: close RFC-1510 review r1 findings — folder crash guard, ls-tree allowlist, OUTBOX-02 on develop</item>
17
+ <item>RFC-1510: close RFC-1510 review r2 — declare OUTBOX-02 in scan rules, flat-scope test, readdir TOCTOU guard</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 { OUTBOX_DIR, outboxEntrySchema } from "../outbox-schema.ts";
32
+
33
+ export interface OutboxValidateResult {
34
+ command: "outbox.validate";
35
+ checked: number;
36
+ diagnostics: Diagnostic[];
37
+ }
38
+
39
+ function schemaIssues(error: { issues: { path: PropertyKey[]; message: string }[] }): string {
40
+ return error.issues
41
+ .map((issue) => `${issue.path.map(String).join(".") || "(root)"}: ${issue.message}`)
42
+ .join("; ");
43
+ }
44
+
45
+ export async function runOutboxValidate(
46
+ _input: ForgeCommandInput,
47
+ context: ForgeRuntimeContext,
48
+ ): Promise<ForgeCommandResult<OutboxValidateResult>> {
49
+ const io = resolveIo(context.io);
50
+ const root = context.workspaceRoot;
51
+ const diagnostics: Diagnostic[] = [];
52
+ const dir = path.join(root, OUTBOX_DIR);
53
+
54
+ let checked = 0;
55
+ let isDir = false;
56
+ try {
57
+ isDir = (await io.stat(dir)).isDirectory;
58
+ } catch {
59
+ isDir = false;
60
+ }
61
+ let entries: Awaited<ReturnType<typeof io.readdir>> = [];
62
+ if (isDir) {
63
+ try {
64
+ entries = await io.readdir(dir);
65
+ } catch {
66
+ entries = [];
67
+ }
68
+ }
69
+ for (const entry of entries) {
70
+ if (!entry.isFile || !entry.name.endsWith(".yaml")) continue;
71
+ const rel = path.posix.join(OUTBOX_DIR, entry.name);
72
+ const file = path.join(dir, entry.name);
73
+ checked += 1;
74
+
75
+ let raw: unknown;
76
+ try {
77
+ raw = YAML.parse(await io.readFile(file));
78
+ } catch (error) {
79
+ diagnostics.push({
80
+ ruleId: "OUTBOX-01",
81
+ severity: "error",
82
+ file: rel,
83
+ message: `${rel} is not parseable YAML: ${error instanceof Error ? error.message : String(error)}`,
84
+ fixHint: "Repair the entry — see forge/outbox@1.",
85
+ });
86
+ continue;
87
+ }
88
+ const parsed = outboxEntrySchema.safeParse(raw);
89
+ if (!parsed.success) {
90
+ diagnostics.push({
91
+ ruleId: "OUTBOX-01",
92
+ severity: "error",
93
+ file: rel,
94
+ message: `${rel} fails forge/outbox@1: ${schemaIssues(parsed.error)}`,
95
+ fixHint: "See the fo-outbox skill — an entry names a contract, need, why and state.",
96
+ });
97
+ continue;
98
+ }
99
+ const stem = entry.name.slice(0, -".yaml".length);
100
+ if (parsed.data.id !== stem) {
101
+ diagnostics.push({
102
+ ruleId: "OUTBOX-02",
103
+ severity: "error",
104
+ file: rel,
105
+ message: `${rel} declares id ${parsed.data.id} but the filename stem is ${stem}`,
106
+ fixHint: "Rename the file or fix the id — the filename stem is the canonical id.",
107
+ });
108
+ }
109
+ }
110
+
111
+ const errors = diagnostics.filter((d) => d.severity === "error").length;
112
+ return {
113
+ data: { command: "outbox.validate", checked, diagnostics },
114
+ exitCode: errors > 0 ? 1 : 0,
115
+ summary:
116
+ checked === 0
117
+ ? "outbox.validate: no docs/outbox/ entries — pass"
118
+ : `outbox.validate: ${checked} entr${checked === 1 ? "y" : "ies"}, ${errors} error(s)`,
119
+ };
120
+ }
@@ -0,0 +1,107 @@
1
+ /*
2
+ <MODULE_CONTRACT>
3
+ <purpose>system.map.validate command handler (RFC-1508) — gates on the system
4
+ profile (SYSTEM-01), loads the system map non-throwing (SYSTEM-MAP-01), then
5
+ evaluates the cross-file rules (SYSTEM-MAP-02..05). Read-only: exit 1 iff an
6
+ error-severity diagnostic exists.</purpose>
7
+ <non-goals>
8
+ <item>Do not mutate any files.</item>
9
+ <item>No task, outbox, scenario or version-set behavior — RFC-1509..1511.</item>
10
+ </non-goals>
11
+ </MODULE_CONTRACT>
12
+ <CHANGE_SUMMARY>
13
+ <item>RFC-1508: step 3 — system.map.validate handler</item>
14
+ <item>RFC-1508: implement RFC-1508 — system profile, system map schemas and system.map.validate</item>
15
+ <item>RFC-1508: align rfc-1508 schemas with the normative contract after review r1</item>
16
+ <item>RFC-1511: scenarios, version sets and system.versionset.record</item>
17
+ </CHANGE_SUMMARY>
18
+ */
19
+
20
+ import { resolveIo } from "../../../src/utils/io.ts";
21
+ import type {
22
+ Diagnostic,
23
+ ForgeCommandInput,
24
+ ForgeCommandResult,
25
+ ForgeRuntimeContext,
26
+ } from "../../../src/types.ts";
27
+ import {
28
+ assertSystemRepository,
29
+ loadScenarios,
30
+ loadSystemMap,
31
+ validateScenarios,
32
+ validateSystemMap,
33
+ } from "../repo.ts";
34
+
35
+ export interface SystemMapValidateResult {
36
+ command: "system.map.validate";
37
+ status: "pass" | "warn" | "fail";
38
+ systemId: string | null;
39
+ products: number;
40
+ allowed: number;
41
+ forbidden: number;
42
+ localFolders: number | null;
43
+ diagnostics: Diagnostic[];
44
+ }
45
+
46
+ export async function runSystemMapValidate(
47
+ _input: ForgeCommandInput,
48
+ context: ForgeRuntimeContext,
49
+ ): Promise<ForgeCommandResult<SystemMapValidateResult>> {
50
+ const io = resolveIo(context.io);
51
+ const root = context.workspaceRoot;
52
+ const diagnostics: Diagnostic[] = [];
53
+
54
+ const refusal = await assertSystemRepository(root, io);
55
+ let map: Awaited<ReturnType<typeof loadSystemMap>>["map"] = null;
56
+ if (refusal) {
57
+ diagnostics.push(refusal);
58
+ } else {
59
+ const loaded = await loadSystemMap(root, io);
60
+ diagnostics.push(...loaded.diagnostics);
61
+ map = loaded.map;
62
+ if (map) {
63
+ diagnostics.push(...(await validateSystemMap(map, io)));
64
+ const scenarios = await loadScenarios(root, io);
65
+ diagnostics.push(...scenarios.diagnostics);
66
+ diagnostics.push(...validateScenarios(map, scenarios.scenarios));
67
+ }
68
+ }
69
+
70
+ const errors = diagnostics.filter((d) => d.severity === "error").length;
71
+ const warnings = diagnostics.filter((d) => d.severity === "warning").length;
72
+
73
+ if (context.outputFormat === "pretty") {
74
+ for (const d of diagnostics) {
75
+ const line = `${d.ruleId} · ${d.file ? `${d.file}: ` : ""}${d.message}`;
76
+ if (d.severity === "error") context.logger.error(line);
77
+ else if (d.severity === "warning") context.logger.warn(line);
78
+ else context.logger.info(line);
79
+ }
80
+ }
81
+
82
+ const result: SystemMapValidateResult = {
83
+ command: "system.map.validate",
84
+ status: errors > 0 ? "fail" : warnings > 0 ? "warn" : "pass",
85
+ systemId: map?.system.id ?? null,
86
+ products: map?.products.products.length ?? 0,
87
+ allowed: map?.edges.allowed.length ?? 0,
88
+ forbidden: map?.edges.forbidden.length ?? 0,
89
+ localFolders: map?.local ? Object.keys(map.local.folders).length : null,
90
+ diagnostics,
91
+ };
92
+
93
+ return {
94
+ data: result,
95
+ exitCode: errors > 0 ? 1 : 0,
96
+ summary: `system.map.validate: ${errors} error(s), ${warnings} warning(s) — ${result.products} product(s), ${result.allowed} allowed and ${result.forbidden} forbidden edge(s)`,
97
+ nextSteps:
98
+ errors > 0
99
+ ? [{ action: "Fix the error-severity system-map diagnostics above", kind: "required" }]
100
+ : [
101
+ {
102
+ action: "Declare products, offered contracts and edges under system/ as the system grows",
103
+ kind: "optional",
104
+ },
105
+ ],
106
+ };
107
+ }
@@ -0,0 +1,320 @@
1
+ /*
2
+ <MODULE_CONTRACT>
3
+ <purpose>system.outbox.scan (RFC-1510) — read every product's committed
4
+ docs/outbox entries from its develop branch (uncommitted worktree files do
5
+ not count), match each open entry's contract to the offering product,
6
+ classify the edge and print OutboxProposal rows. With --create, one proposed
7
+ task per entry lands in the task queue (origin outbox:&lt;product&gt;:&lt;id&gt;)
8
+ — rescanning never duplicates. SYSTEM-OUTBOX-03 flags a product whose
9
+ forge.yaml or outbox entry names the system id — the leak check lives here
10
+ because a product cannot know the system's name.</purpose>
11
+ <non-goals>
12
+ <item>No dispatch — proposals stop at the queue (RFC-1509 takes over).</item>
13
+ </non-goals>
14
+ </MODULE_CONTRACT>
15
+ <CHANGE_SUMMARY>
16
+ <item>RFC-1510: step 3 — develop-branch outbox scan and deduplicated task creation</item>
17
+ <item>RFC-1510: implement RFC-1510 — product outbox for dependency-contract requests</item>
18
+ <item>RFC-1510: close RFC-1510 review r1 findings — folder crash guard, ls-tree allowlist, OUTBOX-02 on develop</item>
19
+ <item>RFC-1510: close RFC-1510 review r2 — declare OUTBOX-02 in scan rules, flat-scope test, readdir TOCTOU guard</item>
20
+ </CHANGE_SUMMARY>
21
+ */
22
+
23
+ import path from "node:path";
24
+ import YAML from "yaml";
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 {
35
+ OUTBOX_DIR,
36
+ type OutboxEntry,
37
+ type OutboxProposal,
38
+ outboxEntrySchema,
39
+ } from "../outbox-schema.ts";
40
+ import { assertSystemRepository, edgeVerdict, loadSystemMap } from "../repo.ts";
41
+ import { listTasks } from "../task-store.ts";
42
+ import { createTask, writeTaskWithBrief } from "./system-task-create.ts";
43
+
44
+ export interface SystemOutboxScanResult {
45
+ command: "system.outbox.scan";
46
+ proposals: OutboxProposal[];
47
+ created: string[];
48
+ diagnostics: Diagnostic[];
49
+ }
50
+
51
+ function diag(
52
+ ruleId: string,
53
+ severity: Diagnostic["severity"],
54
+ message: string,
55
+ fixHint?: string,
56
+ ): Diagnostic {
57
+ return fixHint ? { ruleId, severity, message, fixHint } : { ruleId, severity, message };
58
+ }
59
+
60
+ /** Word-boundary match for a kebab system id inside a product-side file. */
61
+ function systemIdNeedleFor(id: string): RegExp {
62
+ const escaped = id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
63
+ return new RegExp(`(^|[^a-z0-9-])${escaped}([^a-z0-9-]|$)`);
64
+ }
65
+
66
+ /** Committed file contents on the product's develop, or null when unreadable. */
67
+ async function gitShow(
68
+ io: WorkspaceIO,
69
+ repo: string,
70
+ spec: string,
71
+ ): Promise<string | null> {
72
+ const res = await io.exec("git", ["show", spec], { cwd: repo });
73
+ return res.exitCode === 0 ? res.stdout : null;
74
+ }
75
+
76
+ export async function runSystemOutboxScan(
77
+ input: ForgeCommandInput,
78
+ context: ForgeRuntimeContext,
79
+ ): Promise<ForgeCommandResult<SystemOutboxScanResult>> {
80
+ const io = resolveIo(context.io);
81
+ const root = context.workspaceRoot;
82
+ const diagnostics: Diagnostic[] = [];
83
+ const dryRun = context.dryRun || input.flags["dry-run"] === true;
84
+ const create = input.flags["create"] === true;
85
+
86
+ const refusal = await assertSystemRepository(root, io);
87
+ if (refusal) diagnostics.push(refusal);
88
+
89
+ const loaded = diagnostics.every((d) => d.severity !== "error")
90
+ ? await loadSystemMap(root, io)
91
+ : { map: null, diagnostics: [] };
92
+ diagnostics.push(...loaded.diagnostics);
93
+ const map = loaded.map;
94
+
95
+ const proposals: OutboxProposal[] = [];
96
+ const created: string[] = [];
97
+
98
+ if (map !== null) {
99
+ const listed = await listTasks(root, io);
100
+ diagnostics.push(...listed.diagnostics);
101
+ const tasks = listed.tasks;
102
+ const originRef = (product: string, entry: string) => `${product}:${entry}`;
103
+ const existingByOrigin = new Map<string, string>();
104
+ for (const task of tasks) {
105
+ if (task.origin.kind === "outbox" && task.origin.ref !== null) {
106
+ existingByOrigin.set(task.origin.ref, task.id);
107
+ }
108
+ }
109
+
110
+ for (const product of map.products.products) {
111
+ const folder = Object.prototype.hasOwnProperty.call(
112
+ map.local?.folders ?? {},
113
+ product.id,
114
+ )
115
+ ? map.local!.folders[product.id]
116
+ : undefined;
117
+ if (folder === undefined) {
118
+ diagnostics.push(
119
+ diag(
120
+ "SYSTEM-OUTBOX-04",
121
+ "warning",
122
+ `product '${product.id}' has no folder in system/products.local.yaml — skipped`,
123
+ ),
124
+ );
125
+ continue;
126
+ }
127
+ const productRoot = path.isAbsolute(folder) ? folder : path.resolve(map.root, folder);
128
+
129
+ // A stale products.local.yaml folder must warn, not crash — io.exec
130
+ // rejects on a nonexistent cwd before git ever runs.
131
+ let isDir = false;
132
+ try {
133
+ isDir = (await io.stat(productRoot)).isDirectory;
134
+ } catch {
135
+ isDir = false;
136
+ }
137
+ if (!isDir) {
138
+ diagnostics.push(
139
+ diag(
140
+ "SYSTEM-OUTBOX-04",
141
+ "warning",
142
+ `product '${product.id}' names a local folder that does not exist: ${folder} — skipped`,
143
+ ),
144
+ );
145
+ continue;
146
+ }
147
+
148
+ let tree: Awaited<ReturnType<WorkspaceIO["exec"]>>;
149
+ try {
150
+ tree = await io.exec(
151
+ "git",
152
+ ["ls-tree", "--name-only", "develop", `${OUTBOX_DIR}/`],
153
+ { cwd: productRoot },
154
+ );
155
+ } catch {
156
+ diagnostics.push(
157
+ diag(
158
+ "SYSTEM-OUTBOX-04",
159
+ "warning",
160
+ `product '${product.id}' is not a readable git checkout — skipped`,
161
+ ),
162
+ );
163
+ continue;
164
+ }
165
+ if (tree.exitCode !== 0) {
166
+ diagnostics.push(
167
+ diag(
168
+ "SYSTEM-OUTBOX-04",
169
+ "warning",
170
+ `product '${product.id}' has no readable develop branch — skipped`,
171
+ ),
172
+ );
173
+ continue;
174
+ }
175
+
176
+ // SYSTEM-OUTBOX-03 — a product file must never name the system. The
177
+ // check is limited to forge.yaml and outbox entries: older prose in
178
+ // product docs is deliberately out of scope (audit A2).
179
+ const forgeYaml = await gitShow(io, productRoot, "develop:forge.yaml");
180
+ const systemIdNeedle = systemIdNeedleFor(map.system.id);
181
+ if (forgeYaml !== null && systemIdNeedle.test(forgeYaml)) {
182
+ diagnostics.push(
183
+ diag(
184
+ "SYSTEM-OUTBOX-03",
185
+ "warning",
186
+ `product '${product.id}' forge.yaml contains the system id '${map.system.id}'`,
187
+ "A product must not know its system — remove the reference.",
188
+ ),
189
+ );
190
+ }
191
+
192
+ for (const rel of tree.stdout.split("\n").map((l) => l.trim()).filter((l) => l.endsWith(".yaml"))) {
193
+ const raw = await gitShow(io, productRoot, `develop:${rel}`);
194
+ if (raw === null) continue;
195
+ if (systemIdNeedle.test(raw)) {
196
+ diagnostics.push(
197
+ diag(
198
+ "SYSTEM-OUTBOX-03",
199
+ "warning",
200
+ `product '${product.id}' outbox entry ${rel} contains the system id '${map.system.id}'`,
201
+ "An entry names a contract — never the system or a serving product.",
202
+ ),
203
+ );
204
+ }
205
+ let parsed: OutboxEntry | null = null;
206
+ try {
207
+ const candidate = outboxEntrySchema.safeParse(YAML.parse(raw));
208
+ if (candidate.success) {
209
+ parsed = candidate.data;
210
+ } else {
211
+ diagnostics.push(
212
+ diag(
213
+ "OUTBOX-01",
214
+ "error",
215
+ `${product.id} ${rel} fails forge/outbox@1 on develop`,
216
+ "Run outbox.validate in the product repository.",
217
+ ),
218
+ );
219
+ }
220
+ } catch {
221
+ diagnostics.push(
222
+ diag(
223
+ "OUTBOX-01",
224
+ "error",
225
+ `${product.id} ${rel} is not parseable YAML on develop`,
226
+ "Run outbox.validate in the product repository.",
227
+ ),
228
+ );
229
+ }
230
+ if (
231
+ parsed !== null &&
232
+ path.posix.basename(rel, ".yaml") !== parsed.id
233
+ ) {
234
+ diagnostics.push(
235
+ diag(
236
+ "OUTBOX-02",
237
+ "error",
238
+ `${product.id} ${rel} declares id ${parsed.id} but the filename stem is ${path.posix.basename(rel, ".yaml")}`,
239
+ "Rename the file or fix the id — the filename stem is the canonical id.",
240
+ ),
241
+ );
242
+ continue;
243
+ }
244
+ if (parsed === null || parsed.state !== "open") continue;
245
+
246
+ const target =
247
+ map.products.products.find((p) => p.offers.includes(parsed!.contract))?.id ?? null;
248
+ const verdict = edgeVerdict(map, product.id, parsed.contract);
249
+ const ref = originRef(product.id, parsed.id);
250
+ const proposal: OutboxProposal = {
251
+ product: product.id,
252
+ entry: parsed.id,
253
+ contract: parsed.contract,
254
+ target,
255
+ verdict,
256
+ taskId: existingByOrigin.get(ref) ?? null,
257
+ };
258
+ if (target === null) {
259
+ diagnostics.push(
260
+ diag(
261
+ "SYSTEM-OUTBOX-01",
262
+ "warning",
263
+ `no product offers contract '${parsed.contract}' requested by ${product.id} in ${parsed.id} — no task created`,
264
+ ),
265
+ );
266
+ }
267
+ if (verdict === "forbidden") {
268
+ diagnostics.push(
269
+ diag(
270
+ "SYSTEM-OUTBOX-02",
271
+ "error",
272
+ `edge from '${product.id}' to contract '${parsed.contract}' is forbidden — ${parsed.id} creates no task`,
273
+ "Resolve the forbidden edge in system/edges.yaml or withdraw the entry.",
274
+ ),
275
+ );
276
+ }
277
+ if (
278
+ create &&
279
+ proposal.taskId === null &&
280
+ target !== null &&
281
+ verdict !== "forbidden" &&
282
+ !dryRun
283
+ ) {
284
+ const { task } = await createTask(root, io, {
285
+ product: target,
286
+ slug: parsed.id.toLowerCase(),
287
+ title: `Serve ${parsed.id} for ${product.id}: ${parsed.contract}`,
288
+ kind: "change",
289
+ contracts: [parsed.contract],
290
+ origin: { kind: "outbox", ref },
291
+ reason: `outbox scan — ${parsed.why}`,
292
+ });
293
+ await writeTaskWithBrief(root, io, task);
294
+ proposal.taskId = task.id;
295
+ existingByOrigin.set(ref, task.id);
296
+ created.push(task.id);
297
+ }
298
+ proposals.push(proposal);
299
+ }
300
+ }
301
+ }
302
+
303
+ const errors = diagnostics.filter((d) => d.severity === "error").length;
304
+ return {
305
+ data: { command: "system.outbox.scan", proposals, created, diagnostics },
306
+ exitCode: errors > 0 ? 1 : 0,
307
+ summary: `system.outbox.scan: ${proposals.length} proposal(s), ${created.length} task(s) created, ${errors} error(s)`,
308
+ nextSteps:
309
+ proposals.length > 0
310
+ ? [
311
+ {
312
+ action: create
313
+ ? "Review the proposed tasks — system.task.move --task <id> --to ready once the brief is filled"
314
+ : "Re-run with --create to propose tasks for the matched entries",
315
+ kind: "optional",
316
+ },
317
+ ]
318
+ : [],
319
+ };
320
+ }