@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,451 @@
1
+ /*
2
+ <MODULE_CONTRACT>
3
+ <purpose>System-map loading and cross-file validation (RFC-1508). assertSystemRepository
4
+ gates system commands to profile: system repositories (SYSTEM-01); loadSystemMap reads
5
+ the four map files and never throws on malformed input (SYSTEM-MAP-01); validateSystemMap
6
+ enforces the cross-file rules (SYSTEM-MAP-02..05); edgeVerdict is the single-lookup
7
+ resolver shared with the task queue and outbox RFCs.</purpose>
8
+ <non-goals>
9
+ <item>No task, outbox, scenario or version-set behavior — those are RFC-1509..1511.</item>
10
+ <item>Do not import from @warpgogol/* (runtime) — forge os/ modules stay autonomous.</item>
11
+ </non-goals>
12
+ </MODULE_CONTRACT>
13
+ <CHANGE_SUMMARY>
14
+ <item>RFC-1508: step 2 — system repo assertion, non-throwing map loader, edgeVerdict, map checks</item>
15
+ <item>RFC-1508: review r1 — contract-exact map shape (root, folders-based local map, strict schemas), read failures stay diagnostics, stat-based folder check</item>
16
+ <item>RFC-1508: close rfc-1508 review r2 findings on folder lookup and diagnostics</item>
17
+ <item>RFC-1511: scenarios, version sets and system.versionset.record</item>
18
+ </CHANGE_SUMMARY>
19
+ */
20
+
21
+ import path from "node:path";
22
+ import YAML from "yaml";
23
+ import type { z } from "zod";
24
+
25
+ import type { Diagnostic, WorkspaceIO } from "../../src/types.ts";
26
+ import {
27
+ SYSTEM_SCENARIOS_DIR,
28
+ systemScenarioSchema,
29
+ } from "./scenario-schemas.ts";
30
+ import type { SystemScenario } from "./scenario-schemas.ts";
31
+ import {
32
+ systemEdgesSchema,
33
+ systemProductsLocalSchema,
34
+ systemProductsSchema,
35
+ systemSchema,
36
+ } from "./schemas.ts";
37
+ import type { System, SystemEdges, SystemProducts, SystemProductsLocal } from "./schemas.ts";
38
+
39
+ // ---------------------------------------------------------------------------
40
+ // Paths
41
+ // ---------------------------------------------------------------------------
42
+
43
+ export const SYSTEM_DIR = "system";
44
+ export const SYSTEM_CONFIG_FILE = "system/system.yaml";
45
+ export const SYSTEM_PRODUCTS_FILE = "system/products.yaml";
46
+ export const SYSTEM_EDGES_FILE = "system/edges.yaml";
47
+ export const SYSTEM_LOCAL_FILE = "system/products.local.yaml";
48
+ export const FORGE_CONFIG_FILE = "forge.yaml";
49
+
50
+ // ---------------------------------------------------------------------------
51
+ // Types
52
+ // ---------------------------------------------------------------------------
53
+
54
+ export interface SystemMap {
55
+ /** Repository root the map was loaded from — callers resolve folders against it. */
56
+ root: string;
57
+ system: System;
58
+ products: SystemProducts;
59
+ edges: SystemEdges;
60
+ /** Parsed products.local.yaml, or null when the file is absent (SYSTEM-MAP-05). */
61
+ local: SystemProductsLocal | null;
62
+ }
63
+
64
+ export interface SystemMapLoadResult {
65
+ /** null when any required file is missing or fails schema validation. */
66
+ map: SystemMap | null;
67
+ diagnostics: Diagnostic[];
68
+ }
69
+
70
+ export type EdgeVerdict = "own-contract" | "forbidden" | "allowed" | "undeclared";
71
+
72
+ // ---------------------------------------------------------------------------
73
+ // assertSystemRepository — SYSTEM-01
74
+ // ---------------------------------------------------------------------------
75
+
76
+ /**
77
+ * Returns a SYSTEM-01 diagnostic when `root` is not a forge repository on the
78
+ * `system` profile (missing/unreadable forge.yaml, absent profile field, or a
79
+ * different profile id), null otherwise.
80
+ */
81
+ export async function assertSystemRepository(
82
+ root: string,
83
+ io: WorkspaceIO,
84
+ ): Promise<Diagnostic | null> {
85
+ const forgeYaml = path.join(root, FORGE_CONFIG_FILE);
86
+ let parsed: unknown;
87
+ if (!(await io.exists(forgeYaml))) {
88
+ return {
89
+ ruleId: "SYSTEM-01",
90
+ severity: "error",
91
+ file: FORGE_CONFIG_FILE,
92
+ message:
93
+ "system commands require a forge repository on the system profile — forge.yaml is missing",
94
+ fixHint: "Run `forge create --in-place --profile system` to scaffold a system repository.",
95
+ };
96
+ }
97
+ try {
98
+ parsed = YAML.parse(await io.readFile(forgeYaml));
99
+ } catch (error) {
100
+ return {
101
+ ruleId: "SYSTEM-01",
102
+ severity: "error",
103
+ file: FORGE_CONFIG_FILE,
104
+ message: `forge.yaml is not readable YAML: ${error instanceof Error ? error.message : String(error)}`,
105
+ fixHint: "Repair forge.yaml — system commands require profile: system.",
106
+ };
107
+ }
108
+ const raw =
109
+ parsed !== null && typeof parsed === "object"
110
+ ? (parsed as Record<string, unknown>)["profile"]
111
+ : undefined;
112
+ const profile =
113
+ typeof raw === "string"
114
+ ? raw
115
+ : raw && typeof raw === "object"
116
+ ? (raw as { id?: unknown }).id
117
+ : undefined;
118
+ if (profile !== "system") {
119
+ return {
120
+ ruleId: "SYSTEM-01",
121
+ severity: "error",
122
+ file: FORGE_CONFIG_FILE,
123
+ message: `system commands require profile: system — this repository is on profile ${JSON.stringify(raw ?? null)}`,
124
+ fixHint: "system.map.validate only runs inside a repository created with --profile system.",
125
+ };
126
+ }
127
+ return null;
128
+ }
129
+
130
+ // ---------------------------------------------------------------------------
131
+ // File loading — SYSTEM-MAP-01
132
+ // ---------------------------------------------------------------------------
133
+
134
+ function mapDiagnostic(file: string, message: string): Diagnostic {
135
+ return {
136
+ ruleId: "SYSTEM-MAP-01",
137
+ severity: "error",
138
+ file,
139
+ message,
140
+ fixHint: "Repair or recreate the file — see the forge/system map schemas (RFC-1508).",
141
+ };
142
+ }
143
+
144
+ async function loadRequired<S extends z.ZodType>(
145
+ root: string,
146
+ rel: string,
147
+ schema: S,
148
+ io: WorkspaceIO,
149
+ ): Promise<{ data?: z.infer<S>; diagnostic?: Diagnostic }> {
150
+ const abs = path.join(root, rel);
151
+ if (!(await io.exists(abs))) {
152
+ return { diagnostic: mapDiagnostic(rel, `${rel} is missing`) };
153
+ }
154
+ let parsed: unknown;
155
+ try {
156
+ parsed = YAML.parse(await io.readFile(abs));
157
+ } catch (error) {
158
+ return {
159
+ diagnostic: mapDiagnostic(
160
+ rel,
161
+ `${rel} is not readable YAML: ${error instanceof Error ? error.message : String(error)}`,
162
+ ),
163
+ };
164
+ }
165
+ const result = schema.safeParse(parsed);
166
+ if (!result.success) {
167
+ const issues = result.error.issues
168
+ .map((i) => ` - ${i.path.join(".")}: ${i.message}`)
169
+ .join("\n");
170
+ return { diagnostic: mapDiagnostic(rel, `${rel} failed schema validation:\n${issues}`) };
171
+ }
172
+ return { data: result.data as z.infer<S> };
173
+ }
174
+
175
+ /**
176
+ * Read the four system-map files. Never throws on missing, unreadable or
177
+ * malformed input — problems surface as SYSTEM-MAP-01 diagnostics and a null
178
+ * map. An absent products.local.yaml is not an error (local: null); a
179
+ * malformed one is SYSTEM-MAP-01 like any required file.
180
+ */
181
+ export async function loadSystemMap(
182
+ root: string,
183
+ io: WorkspaceIO,
184
+ ): Promise<SystemMapLoadResult> {
185
+ const diagnostics: Diagnostic[] = [];
186
+
187
+ const [sys, pro, edg] = await Promise.all([
188
+ loadRequired(root, SYSTEM_CONFIG_FILE, systemSchema, io),
189
+ loadRequired(root, SYSTEM_PRODUCTS_FILE, systemProductsSchema, io),
190
+ loadRequired(root, SYSTEM_EDGES_FILE, systemEdgesSchema, io),
191
+ ]);
192
+ for (const part of [sys, pro, edg]) {
193
+ if (part.diagnostic) diagnostics.push(part.diagnostic);
194
+ }
195
+
196
+ let local: SystemProductsLocal | null = null;
197
+ const localAbs = path.join(root, SYSTEM_LOCAL_FILE);
198
+ if (await io.exists(localAbs)) {
199
+ const loc = await loadRequired(root, SYSTEM_LOCAL_FILE, systemProductsLocalSchema, io);
200
+ if (loc.diagnostic) {
201
+ diagnostics.push(loc.diagnostic);
202
+ } else {
203
+ local = loc.data ?? null;
204
+ }
205
+ }
206
+
207
+ if (!sys.data || !pro.data || !edg.data || diagnostics.length > 0) {
208
+ return { map: null, diagnostics };
209
+ }
210
+ return {
211
+ map: { root, system: sys.data, products: pro.data, edges: edg.data, local },
212
+ diagnostics,
213
+ };
214
+ }
215
+
216
+ // ---------------------------------------------------------------------------
217
+ // Cross-file validation — SYSTEM-MAP-02..05
218
+ // ---------------------------------------------------------------------------
219
+
220
+ function edgeKey(consumer: string, contract: string): string {
221
+ return JSON.stringify([consumer, contract]);
222
+ }
223
+
224
+ /**
225
+ * Evaluate every cross-file rule against a loaded map. Needs the io port only
226
+ * for the products.local.yaml folder checks (SYSTEM-MAP-05).
227
+ */
228
+ export async function validateSystemMap(
229
+ map: SystemMap,
230
+ io: WorkspaceIO,
231
+ ): Promise<Diagnostic[]> {
232
+ const diagnostics: Diagnostic[] = [];
233
+ const products = map.products.products;
234
+ const ids = new Set<string>();
235
+
236
+ // SYSTEM-MAP-02 — duplicate product ids
237
+ for (const product of products) {
238
+ if (ids.has(product.id)) {
239
+ diagnostics.push({
240
+ ruleId: "SYSTEM-MAP-02",
241
+ severity: "error",
242
+ file: SYSTEM_PRODUCTS_FILE,
243
+ message: `duplicate product id "${product.id}"`,
244
+ fixHint: "Product ids are unique — rename or merge the duplicate entry.",
245
+ });
246
+ }
247
+ ids.add(product.id);
248
+ }
249
+
250
+ // SYSTEM-MAP-02 — edge consumer not declared as a product;
251
+ // SYSTEM-MAP-03 — edge contract not offered by any product
252
+ for (const edge of [...map.edges.allowed, ...map.edges.forbidden]) {
253
+ if (!ids.has(edge.consumer)) {
254
+ diagnostics.push({
255
+ ruleId: "SYSTEM-MAP-02",
256
+ severity: "error",
257
+ file: SYSTEM_EDGES_FILE,
258
+ message: `edge consumer "${edge.consumer}" is not a declared product`,
259
+ fixHint: `Add "${edge.consumer}" to ${SYSTEM_PRODUCTS_FILE} or remove the edge.`,
260
+ });
261
+ }
262
+ if (!products.some((p) => p.offers.includes(edge.contract))) {
263
+ diagnostics.push({
264
+ ruleId: "SYSTEM-MAP-03",
265
+ severity: "error",
266
+ file: SYSTEM_EDGES_FILE,
267
+ message: `edge names contract "${edge.contract}" which no product offers`,
268
+ fixHint: `Declare "${edge.contract}" in a product's offers[] or remove the edge.`,
269
+ });
270
+ }
271
+ }
272
+
273
+ // SYSTEM-MAP-04 — same consumer/contract pair allowed AND forbidden
274
+ const forbidden = new Set(map.edges.forbidden.map((e) => edgeKey(e.consumer, e.contract)));
275
+ for (const edge of map.edges.allowed) {
276
+ if (forbidden.has(edgeKey(edge.consumer, edge.contract))) {
277
+ diagnostics.push({
278
+ ruleId: "SYSTEM-MAP-04",
279
+ severity: "error",
280
+ file: SYSTEM_EDGES_FILE,
281
+ message: `consumer "${edge.consumer}" / contract "${edge.contract}" is both allowed and forbidden`,
282
+ fixHint: "Keep the pair in exactly one list — forbidden wins at lookup time.",
283
+ });
284
+ }
285
+ }
286
+
287
+ // SYSTEM-MAP-05 — local checkout coverage (warnings only)
288
+ if (map.local === null) {
289
+ diagnostics.push({
290
+ ruleId: "SYSTEM-MAP-05",
291
+ severity: "warning",
292
+ file: SYSTEM_LOCAL_FILE,
293
+ message: `${SYSTEM_LOCAL_FILE} is absent — local product checkouts are unknown`,
294
+ fixHint: "Create system/products.local.yaml (gitignored) mapping product ids to folders.",
295
+ });
296
+ } else {
297
+ for (const product of products) {
298
+ if (!Object.hasOwn(map.local.folders, product.id)) {
299
+ diagnostics.push({
300
+ ruleId: "SYSTEM-MAP-05",
301
+ severity: "warning",
302
+ file: SYSTEM_LOCAL_FILE,
303
+ message: `product "${product.id}" has no folder in ${SYSTEM_LOCAL_FILE}`,
304
+ fixHint: `Add "${product.id}: <folder>" to ${SYSTEM_LOCAL_FILE} when its repository is checked out.`,
305
+ });
306
+ }
307
+ }
308
+ for (const [id, folder] of Object.entries(map.local.folders)) {
309
+ if (!ids.has(id)) {
310
+ diagnostics.push({
311
+ ruleId: "SYSTEM-MAP-05",
312
+ severity: "warning",
313
+ file: SYSTEM_LOCAL_FILE,
314
+ message: `local entry "${id}" is not a declared product`,
315
+ fixHint: `Remove "${id}" from ${SYSTEM_LOCAL_FILE} or declare it in ${SYSTEM_PRODUCTS_FILE}.`,
316
+ });
317
+ continue;
318
+ }
319
+ const abs = path.isAbsolute(folder) ? folder : path.join(map.root, folder);
320
+ let isDir = false;
321
+ try {
322
+ isDir = (await io.stat(abs)).isDirectory;
323
+ } catch {
324
+ isDir = false;
325
+ }
326
+ if (!isDir) {
327
+ diagnostics.push({
328
+ ruleId: "SYSTEM-MAP-05",
329
+ severity: "warning",
330
+ file: SYSTEM_LOCAL_FILE,
331
+ message: `product "${id}" names a local folder that does not exist: ${folder}`,
332
+ fixHint: `Check out the ${id} repository at ${folder} or remove the entry.`,
333
+ });
334
+ }
335
+ }
336
+ }
337
+
338
+ return diagnostics;
339
+ }
340
+
341
+ // ---------------------------------------------------------------------------
342
+ // Scenarios — RFC-1511 (SYSTEM-MAP-01 loading, SYSTEM-MAP-06 cross-checks)
343
+ // ---------------------------------------------------------------------------
344
+
345
+ export interface SystemScenarioLoadResult {
346
+ /** Parsed scenarios keyed by declared id; empty when the dir is absent. */
347
+ scenarios: Map<string, SystemScenario>;
348
+ diagnostics: Diagnostic[];
349
+ }
350
+
351
+ /**
352
+ * Read `system/scenarios/<id>.yaml` files against forge/system-scenario@1.
353
+ * An absent or non-directory scenarios dir means no scenarios — the folder
354
+ * is opt-in like the outbox. Malformed files surface as SYSTEM-MAP-01.
355
+ */
356
+ export async function loadScenarios(
357
+ root: string,
358
+ io: WorkspaceIO,
359
+ ): Promise<SystemScenarioLoadResult> {
360
+ const diagnostics: Diagnostic[] = [];
361
+ const scenarios = new Map<string, SystemScenario>();
362
+ const dir = path.join(root, SYSTEM_SCENARIOS_DIR);
363
+ let isDir = false;
364
+ try {
365
+ isDir = (await io.stat(dir)).isDirectory;
366
+ } catch {
367
+ isDir = false;
368
+ }
369
+ if (!isDir) return { scenarios, diagnostics };
370
+
371
+ let entries: Awaited<ReturnType<typeof io.readdir>> = [];
372
+ try {
373
+ entries = await io.readdir(dir);
374
+ } catch {
375
+ entries = [];
376
+ }
377
+ for (const entry of entries) {
378
+ if (!entry.isFile || !entry.name.endsWith(".yaml")) continue;
379
+ const rel = `${SYSTEM_SCENARIOS_DIR}/${entry.name}`;
380
+ const loaded = await loadRequired(root, rel, systemScenarioSchema, io);
381
+ if (loaded.diagnostic) {
382
+ diagnostics.push(loaded.diagnostic);
383
+ continue;
384
+ }
385
+ if (loaded.data) scenarios.set(loaded.data.id, loaded.data);
386
+ }
387
+ return { scenarios, diagnostics };
388
+ }
389
+
390
+ /**
391
+ * SYSTEM-MAP-06 — a scenario names a product the map does not declare
392
+ * (products[] and checks[].product) or a contract no product offers.
393
+ */
394
+ export function validateScenarios(
395
+ map: SystemMap,
396
+ scenarios: Map<string, SystemScenario>,
397
+ ): Diagnostic[] {
398
+ const diagnostics: Diagnostic[] = [];
399
+ const productIds = new Set(map.products.products.map((p) => p.id));
400
+ const offered = new Set(map.products.products.flatMap((p) => p.offers));
401
+ for (const scenario of scenarios.values()) {
402
+ const file = `${SYSTEM_SCENARIOS_DIR}/${scenario.id}.yaml`;
403
+ for (const named of new Set([...scenario.products, ...scenario.checks.map((c) => c.product)])) {
404
+ if (!productIds.has(named)) {
405
+ diagnostics.push({
406
+ ruleId: "SYSTEM-MAP-06",
407
+ severity: "error",
408
+ file,
409
+ message: `scenario "${scenario.id}" names undeclared product "${named}"`,
410
+ fixHint: `Declare "${named}" in ${SYSTEM_PRODUCTS_FILE} or remove it from the scenario.`,
411
+ });
412
+ }
413
+ }
414
+ for (const contract of scenario.contracts) {
415
+ if (!offered.has(contract)) {
416
+ diagnostics.push({
417
+ ruleId: "SYSTEM-MAP-06",
418
+ severity: "error",
419
+ file,
420
+ message: `scenario "${scenario.id}" names contract "${contract}" no product offers`,
421
+ fixHint: `Add the contract to a product's offers[] in ${SYSTEM_PRODUCTS_FILE} or drop it from the scenario.`,
422
+ });
423
+ }
424
+ }
425
+ }
426
+ return diagnostics;
427
+ }
428
+
429
+ // ---------------------------------------------------------------------------
430
+ // edgeVerdict — single-lookup edge resolver (RFC-1508 contract)
431
+ // ---------------------------------------------------------------------------
432
+
433
+ /**
434
+ * Fixed precedence: a contract the consumer itself offers is `own-contract`
435
+ * (product-internal call), then `forbidden`, then `allowed`, else `undeclared`.
436
+ */
437
+ export function edgeVerdict(
438
+ map: SystemMap,
439
+ consumer: string,
440
+ contract: string,
441
+ ): EdgeVerdict {
442
+ const product = map.products.products.find((p) => p.id === consumer);
443
+ if (product?.offers.includes(contract)) return "own-contract";
444
+ if (map.edges.forbidden.some((e) => e.consumer === consumer && e.contract === contract)) {
445
+ return "forbidden";
446
+ }
447
+ if (map.edges.allowed.some((e) => e.consumer === consumer && e.contract === contract)) {
448
+ return "allowed";
449
+ }
450
+ return "undeclared";
451
+ }
@@ -0,0 +1,62 @@
1
+ /*
2
+ <MODULE_CONTRACT>
3
+ <purpose>Schema contracts for business scenarios and verified version sets
4
+ (RFC-1511): forge/system-scenario@1 names products, contracts and argv checks;
5
+ forge/version-set@1 records the release-tag combination that passed every
6
+ named scenario with sha256 evidence per scenario.</purpose>
7
+ <non-goals>
8
+ <item>No command behavior — handlers live in os/system/handlers/.</item>
9
+ <item>No scheduling of scenario runs and no published-package pins (stage 3).</item>
10
+ </non-goals>
11
+ </MODULE_CONTRACT>
12
+ <KEY_DECISIONS>
13
+ <item>A check is { product, argv, timeoutSec } run with no shell — argv vectors only (DNA-89).</item>
14
+ <item>evidence maps scenario id to sha256 of that scenario's concatenated check logs — a set exists only for a run that passed (DNA-106).</item>
15
+ </KEY_DECISIONS>
16
+ <CHANGE_SUMMARY>
17
+ <item>RFC-1511: step 2 — scenario and version-set schemas</item>
18
+ <item>RFC-1511: scenarios, version sets and system.versionset.record</item>
19
+ </CHANGE_SUMMARY>
20
+ */
21
+
22
+ import { z } from "zod";
23
+
24
+ export const SYSTEM_SCENARIOS_DIR = "system/scenarios";
25
+ export const SYSTEM_VERSION_SETS_DIR = "system/version-sets";
26
+
27
+ export const systemScenarioSchema = z
28
+ .object({
29
+ schema: z.literal("forge/system-scenario@1"),
30
+ id: z.string().regex(/^[a-z0-9]+(-[a-z0-9]+)*$/),
31
+ title: z.string().min(1),
32
+ products: z.array(z.string().min(1)).min(1),
33
+ contracts: z.array(z.string().min(1)).default([]),
34
+ checks: z
35
+ .array(
36
+ z
37
+ .object({
38
+ product: z.string().min(1),
39
+ argv: z.array(z.string().min(1)).min(1),
40
+ timeoutSec: z.number().int().positive().default(600),
41
+ })
42
+ .strict(),
43
+ )
44
+ .min(1),
45
+ })
46
+ .strict();
47
+
48
+ export type SystemScenario = z.infer<typeof systemScenarioSchema>;
49
+
50
+ export const versionSetSchema = z
51
+ .object({
52
+ schema: z.literal("forge/version-set@1"),
53
+ id: z.string().regex(/^VS-\d{4}$/),
54
+ /** product id -> release tag */
55
+ products: z.record(z.string(), z.string().min(1)),
56
+ scenarios: z.array(z.string().min(1)).min(1),
57
+ evidence: z.record(z.string(), z.string().regex(/^sha256:[0-9a-f]{64}$/)),
58
+ verifiedAt: z.string().min(1),
59
+ })
60
+ .strict();
61
+
62
+ export type VersionSet = z.infer<typeof versionSetSchema>;
@@ -0,0 +1,102 @@
1
+ /*
2
+ <MODULE_CONTRACT>
3
+ <purpose>Zod schemas for the Forge system map (RFC-1508): forge/system@1,
4
+ forge/system-products@1, forge/system-edges@1 and forge/system-products-local@1.
5
+ All objects are strict — unknown keys are schema violations (SYSTEM-MAP-01).</purpose>
6
+ <non-goals>
7
+ <item>Do not enforce cross-file rules here — SYSTEM-MAP-02..05 live in repo.ts.</item>
8
+ <item>Do not import from @warpgogol/* — forge os/ modules stay autonomous.</item>
9
+ </non-goals>
10
+ </MODULE_CONTRACT>
11
+ <CHANGE_SUMMARY>
12
+ <item>RFC-1508: step 2 — system map schemas (system, products, edges, products-local)</item>
13
+ <item>RFC-1508: review r1 — contract-exact schemas: strict objects, remote/offers, folders+launcher local map, edge note + required forbidden reason</item>
14
+ </CHANGE_SUMMARY>
15
+ */
16
+
17
+ import { z } from "zod";
18
+
19
+ /** Lowercase kebab-case id shared by system and product ids. */
20
+ export const SYSTEM_ID_PATTERN = /^[a-z0-9]+(-[a-z0-9]+)*$/;
21
+
22
+ // ---------------------------------------------------------------------------
23
+ // forge/system@1 — system/system.yaml
24
+ // ---------------------------------------------------------------------------
25
+
26
+ export const systemSchema = z
27
+ .object({
28
+ schema: z.literal("forge/system@1"),
29
+ id: z.string().regex(SYSTEM_ID_PATTERN),
30
+ name: z.string().min(1),
31
+ owners: z.array(z.string().min(1)).min(1),
32
+ })
33
+ .strict();
34
+ export type System = z.infer<typeof systemSchema>;
35
+
36
+ // ---------------------------------------------------------------------------
37
+ // forge/system-products@1 — system/products.yaml
38
+ // ---------------------------------------------------------------------------
39
+
40
+ export const systemProductSchema = z
41
+ .object({
42
+ id: z.string().regex(SYSTEM_ID_PATTERN),
43
+ remote: z.string().min(1),
44
+ /** Contract ids this product offers to consumers (package or spec ids). */
45
+ offers: z.array(z.string().min(1)).default([]),
46
+ })
47
+ .strict();
48
+ export type SystemProduct = z.infer<typeof systemProductSchema>;
49
+
50
+ export const systemProductsSchema = z
51
+ .object({
52
+ schema: z.literal("forge/system-products@1"),
53
+ products: z.array(systemProductSchema),
54
+ })
55
+ .strict();
56
+ export type SystemProducts = z.infer<typeof systemProductsSchema>;
57
+
58
+ // ---------------------------------------------------------------------------
59
+ // forge/system-edges@1 — system/edges.yaml
60
+ // ---------------------------------------------------------------------------
61
+
62
+ export const systemEdgeSchema = z
63
+ .object({
64
+ consumer: z.string().min(1),
65
+ contract: z.string().min(1),
66
+ note: z.string().optional(),
67
+ })
68
+ .strict();
69
+ export type SystemEdge = z.infer<typeof systemEdgeSchema>;
70
+
71
+ /** A forbidden edge must carry its justification. */
72
+ export const systemForbiddenEdgeSchema = systemEdgeSchema.extend({
73
+ reason: z.string().min(1),
74
+ });
75
+ export type SystemForbiddenEdge = z.infer<typeof systemForbiddenEdgeSchema>;
76
+
77
+ export const systemEdgesSchema = z
78
+ .object({
79
+ schema: z.literal("forge/system-edges@1"),
80
+ allowed: z.array(systemEdgeSchema).default([]),
81
+ forbidden: z.array(systemForbiddenEdgeSchema).default([]),
82
+ })
83
+ .strict();
84
+ export type SystemEdges = z.infer<typeof systemEdgesSchema>;
85
+
86
+ // ---------------------------------------------------------------------------
87
+ // forge/system-products-local@1 — system/products.local.yaml (gitignored, optional)
88
+ // ---------------------------------------------------------------------------
89
+
90
+ export const systemProductsLocalSchema = z
91
+ .object({
92
+ schema: z.literal("forge/system-products-local@1"),
93
+ /** product id -> local checkout folder, absolute or relative to the repository root. */
94
+ folders: z.record(z.string(), z.string().min(1)),
95
+ /** Product-side launcher invoked by RFC-1509 dispatch ({worktree}/{task} substitution). */
96
+ launcher: z
97
+ .object({ argv: z.array(z.string().min(1)).min(1) })
98
+ .strict()
99
+ .optional(),
100
+ })
101
+ .strict();
102
+ export type SystemProductsLocal = z.infer<typeof systemProductsLocalSchema>;