@wopr-network/defcon 0.2.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 (243) hide show
  1. package/README.md +274 -0
  2. package/dist/api/router.d.ts +24 -0
  3. package/dist/api/router.js +44 -0
  4. package/dist/api/server.d.ts +13 -0
  5. package/dist/api/server.js +280 -0
  6. package/dist/api/wire-types.d.ts +46 -0
  7. package/dist/api/wire-types.js +5 -0
  8. package/dist/config/db-path.d.ts +1 -0
  9. package/dist/config/db-path.js +1 -0
  10. package/dist/config/exporter.d.ts +3 -0
  11. package/dist/config/exporter.js +87 -0
  12. package/dist/config/index.d.ts +4 -0
  13. package/dist/config/index.js +4 -0
  14. package/dist/config/seed-loader.d.ts +10 -0
  15. package/dist/config/seed-loader.js +108 -0
  16. package/dist/config/zod-schemas.d.ts +165 -0
  17. package/dist/config/zod-schemas.js +283 -0
  18. package/dist/cors.d.ts +8 -0
  19. package/dist/cors.js +21 -0
  20. package/dist/engine/constants.d.ts +1 -0
  21. package/dist/engine/constants.js +1 -0
  22. package/dist/engine/engine.d.ts +69 -0
  23. package/dist/engine/engine.js +485 -0
  24. package/dist/engine/event-emitter.d.ts +9 -0
  25. package/dist/engine/event-emitter.js +19 -0
  26. package/dist/engine/event-types.d.ts +105 -0
  27. package/dist/engine/event-types.js +1 -0
  28. package/dist/engine/flow-spawner.d.ts +8 -0
  29. package/dist/engine/flow-spawner.js +28 -0
  30. package/dist/engine/gate-command-validator.d.ts +6 -0
  31. package/dist/engine/gate-command-validator.js +46 -0
  32. package/dist/engine/gate-evaluator.d.ts +12 -0
  33. package/dist/engine/gate-evaluator.js +233 -0
  34. package/dist/engine/handlebars.d.ts +9 -0
  35. package/dist/engine/handlebars.js +51 -0
  36. package/dist/engine/index.d.ts +12 -0
  37. package/dist/engine/index.js +7 -0
  38. package/dist/engine/invocation-builder.d.ts +18 -0
  39. package/dist/engine/invocation-builder.js +58 -0
  40. package/dist/engine/on-enter.d.ts +8 -0
  41. package/dist/engine/on-enter.js +102 -0
  42. package/dist/engine/ssrf-guard.d.ts +22 -0
  43. package/dist/engine/ssrf-guard.js +159 -0
  44. package/dist/engine/state-machine.d.ts +12 -0
  45. package/dist/engine/state-machine.js +74 -0
  46. package/dist/execution/active-runner.d.ts +45 -0
  47. package/dist/execution/active-runner.js +165 -0
  48. package/dist/execution/admin-schemas.d.ts +116 -0
  49. package/dist/execution/admin-schemas.js +125 -0
  50. package/dist/execution/cli.d.ts +57 -0
  51. package/dist/execution/cli.js +498 -0
  52. package/dist/execution/handlers/admin.d.ts +67 -0
  53. package/dist/execution/handlers/admin.js +200 -0
  54. package/dist/execution/handlers/flow.d.ts +25 -0
  55. package/dist/execution/handlers/flow.js +289 -0
  56. package/dist/execution/handlers/query.d.ts +31 -0
  57. package/dist/execution/handlers/query.js +64 -0
  58. package/dist/execution/index.d.ts +4 -0
  59. package/dist/execution/index.js +3 -0
  60. package/dist/execution/mcp-helpers.d.ts +42 -0
  61. package/dist/execution/mcp-helpers.js +23 -0
  62. package/dist/execution/mcp-server.d.ts +33 -0
  63. package/dist/execution/mcp-server.js +1020 -0
  64. package/dist/execution/provision-worktree.d.ts +16 -0
  65. package/dist/execution/provision-worktree.js +123 -0
  66. package/dist/execution/tool-schemas.d.ts +40 -0
  67. package/dist/execution/tool-schemas.js +44 -0
  68. package/dist/gates/blocking-graph.d.ts +26 -0
  69. package/dist/gates/blocking-graph.js +102 -0
  70. package/dist/gates/test/bad-return-gate.d.ts +1 -0
  71. package/dist/gates/test/bad-return-gate.js +4 -0
  72. package/dist/gates/test/passing-gate.d.ts +2 -0
  73. package/dist/gates/test/passing-gate.js +3 -0
  74. package/dist/gates/test/slow-gate.d.ts +2 -0
  75. package/dist/gates/test/slow-gate.js +5 -0
  76. package/dist/gates/test/throwing-gate.d.ts +1 -0
  77. package/dist/gates/test/throwing-gate.js +3 -0
  78. package/dist/logger.d.ts +8 -0
  79. package/dist/logger.js +12 -0
  80. package/dist/main.d.ts +14 -0
  81. package/dist/main.js +28 -0
  82. package/dist/repositories/drizzle/entity.repo.d.ts +27 -0
  83. package/dist/repositories/drizzle/entity.repo.js +190 -0
  84. package/dist/repositories/drizzle/event.repo.d.ts +12 -0
  85. package/dist/repositories/drizzle/event.repo.js +24 -0
  86. package/dist/repositories/drizzle/flow.repo.d.ts +22 -0
  87. package/dist/repositories/drizzle/flow.repo.js +364 -0
  88. package/dist/repositories/drizzle/gate.repo.d.ts +16 -0
  89. package/dist/repositories/drizzle/gate.repo.js +98 -0
  90. package/dist/repositories/drizzle/index.d.ts +6 -0
  91. package/dist/repositories/drizzle/index.js +7 -0
  92. package/dist/repositories/drizzle/invocation.repo.d.ts +23 -0
  93. package/dist/repositories/drizzle/invocation.repo.js +199 -0
  94. package/dist/repositories/drizzle/schema.d.ts +1932 -0
  95. package/dist/repositories/drizzle/schema.js +155 -0
  96. package/dist/repositories/drizzle/transition-log.repo.d.ts +11 -0
  97. package/dist/repositories/drizzle/transition-log.repo.js +42 -0
  98. package/dist/repositories/interfaces.d.ts +321 -0
  99. package/dist/repositories/interfaces.js +2 -0
  100. package/dist/src/api/router.d.ts +24 -0
  101. package/dist/src/api/router.js +44 -0
  102. package/dist/src/api/server.d.ts +13 -0
  103. package/dist/src/api/server.js +280 -0
  104. package/dist/src/api/wire-types.d.ts +46 -0
  105. package/dist/src/api/wire-types.js +5 -0
  106. package/dist/src/config/db-path.d.ts +1 -0
  107. package/dist/src/config/db-path.js +1 -0
  108. package/dist/src/config/exporter.d.ts +3 -0
  109. package/dist/src/config/exporter.js +87 -0
  110. package/dist/src/config/index.d.ts +4 -0
  111. package/dist/src/config/index.js +4 -0
  112. package/dist/src/config/seed-loader.d.ts +14 -0
  113. package/dist/src/config/seed-loader.js +131 -0
  114. package/dist/src/config/zod-schemas.d.ts +165 -0
  115. package/dist/src/config/zod-schemas.js +283 -0
  116. package/dist/src/cors.d.ts +8 -0
  117. package/dist/src/cors.js +21 -0
  118. package/dist/src/engine/constants.d.ts +1 -0
  119. package/dist/src/engine/constants.js +1 -0
  120. package/dist/src/engine/engine.d.ts +69 -0
  121. package/dist/src/engine/engine.js +485 -0
  122. package/dist/src/engine/event-emitter.d.ts +9 -0
  123. package/dist/src/engine/event-emitter.js +19 -0
  124. package/dist/src/engine/event-types.d.ts +105 -0
  125. package/dist/src/engine/event-types.js +1 -0
  126. package/dist/src/engine/flow-spawner.d.ts +8 -0
  127. package/dist/src/engine/flow-spawner.js +28 -0
  128. package/dist/src/engine/gate-command-validator.d.ts +6 -0
  129. package/dist/src/engine/gate-command-validator.js +46 -0
  130. package/dist/src/engine/gate-evaluator.d.ts +12 -0
  131. package/dist/src/engine/gate-evaluator.js +233 -0
  132. package/dist/src/engine/handlebars.d.ts +9 -0
  133. package/dist/src/engine/handlebars.js +51 -0
  134. package/dist/src/engine/index.d.ts +12 -0
  135. package/dist/src/engine/index.js +7 -0
  136. package/dist/src/engine/invocation-builder.d.ts +18 -0
  137. package/dist/src/engine/invocation-builder.js +58 -0
  138. package/dist/src/engine/on-enter.d.ts +8 -0
  139. package/dist/src/engine/on-enter.js +102 -0
  140. package/dist/src/engine/ssrf-guard.d.ts +22 -0
  141. package/dist/src/engine/ssrf-guard.js +159 -0
  142. package/dist/src/engine/state-machine.d.ts +12 -0
  143. package/dist/src/engine/state-machine.js +74 -0
  144. package/dist/src/execution/active-runner.d.ts +45 -0
  145. package/dist/src/execution/active-runner.js +165 -0
  146. package/dist/src/execution/admin-schemas.d.ts +116 -0
  147. package/dist/src/execution/admin-schemas.js +125 -0
  148. package/dist/src/execution/cli.d.ts +57 -0
  149. package/dist/src/execution/cli.js +501 -0
  150. package/dist/src/execution/handlers/admin.d.ts +67 -0
  151. package/dist/src/execution/handlers/admin.js +200 -0
  152. package/dist/src/execution/handlers/flow.d.ts +25 -0
  153. package/dist/src/execution/handlers/flow.js +289 -0
  154. package/dist/src/execution/handlers/query.d.ts +31 -0
  155. package/dist/src/execution/handlers/query.js +64 -0
  156. package/dist/src/execution/index.d.ts +4 -0
  157. package/dist/src/execution/index.js +3 -0
  158. package/dist/src/execution/mcp-helpers.d.ts +42 -0
  159. package/dist/src/execution/mcp-helpers.js +23 -0
  160. package/dist/src/execution/mcp-server.d.ts +33 -0
  161. package/dist/src/execution/mcp-server.js +1020 -0
  162. package/dist/src/execution/provision-worktree.d.ts +16 -0
  163. package/dist/src/execution/provision-worktree.js +123 -0
  164. package/dist/src/execution/tool-schemas.d.ts +40 -0
  165. package/dist/src/execution/tool-schemas.js +44 -0
  166. package/dist/src/logger.d.ts +8 -0
  167. package/dist/src/logger.js +12 -0
  168. package/dist/src/main.d.ts +14 -0
  169. package/dist/src/main.js +28 -0
  170. package/dist/src/repositories/drizzle/entity.repo.d.ts +27 -0
  171. package/dist/src/repositories/drizzle/entity.repo.js +190 -0
  172. package/dist/src/repositories/drizzle/event.repo.d.ts +12 -0
  173. package/dist/src/repositories/drizzle/event.repo.js +24 -0
  174. package/dist/src/repositories/drizzle/flow.repo.d.ts +22 -0
  175. package/dist/src/repositories/drizzle/flow.repo.js +364 -0
  176. package/dist/src/repositories/drizzle/gate.repo.d.ts +16 -0
  177. package/dist/src/repositories/drizzle/gate.repo.js +98 -0
  178. package/dist/src/repositories/drizzle/index.d.ts +6 -0
  179. package/dist/src/repositories/drizzle/index.js +7 -0
  180. package/dist/src/repositories/drizzle/invocation.repo.d.ts +23 -0
  181. package/dist/src/repositories/drizzle/invocation.repo.js +199 -0
  182. package/dist/src/repositories/drizzle/schema.d.ts +1932 -0
  183. package/dist/src/repositories/drizzle/schema.js +155 -0
  184. package/dist/src/repositories/drizzle/transition-log.repo.d.ts +11 -0
  185. package/dist/src/repositories/drizzle/transition-log.repo.js +42 -0
  186. package/dist/src/repositories/interfaces.d.ts +321 -0
  187. package/dist/src/repositories/interfaces.js +2 -0
  188. package/dist/src/utils/redact.d.ts +2 -0
  189. package/dist/src/utils/redact.js +62 -0
  190. package/dist/utils/redact.d.ts +2 -0
  191. package/dist/utils/redact.js +62 -0
  192. package/drizzle/.gitkeep +0 -0
  193. package/drizzle/0000_simple_surge.sql +144 -0
  194. package/drizzle/0001_peaceful_marvel_apes.sql +18 -0
  195. package/drizzle/0002_add_invocations_created_at.sql +1 -0
  196. package/drizzle/0003_drop_integration_config.sql +1 -0
  197. package/drizzle/0004_add_flow_discipline.sql +2 -0
  198. package/drizzle/0004_lucky_silverclaw.sql +5 -0
  199. package/drizzle/0005_old_blue_shield.sql +2 -0
  200. package/drizzle/0006_solid_magik.sql +2 -0
  201. package/drizzle/0007_fancy_luke_cage.sql +1 -0
  202. package/drizzle/0008_thick_dark_beast.sql +1 -0
  203. package/drizzle/0009_brief_midnight.sql +1 -0
  204. package/drizzle/0010_amusing_bastion.sql +1 -0
  205. package/drizzle/meta/0000_snapshot.json +996 -0
  206. package/drizzle/meta/0004_snapshot.json +1008 -0
  207. package/drizzle/meta/0005_snapshot.json +1023 -0
  208. package/drizzle/meta/0006_snapshot.json +1037 -0
  209. package/drizzle/meta/0007_snapshot.json +1044 -0
  210. package/drizzle/meta/0008_snapshot.json +1051 -0
  211. package/drizzle/meta/0009_snapshot.json +1058 -0
  212. package/drizzle/meta/0010_snapshot.json +1065 -0
  213. package/drizzle/meta/_journal.json +83 -0
  214. package/gates/.gitkeep +0 -0
  215. package/gates/blocking-graph.d.ts +26 -0
  216. package/gates/blocking-graph.js +102 -0
  217. package/gates/blocking-graph.ts +121 -0
  218. package/gates/check-design-posted.sh +39 -0
  219. package/gates/check-merge.sh +51 -0
  220. package/gates/check-pr-capacity.sh +17 -0
  221. package/gates/check-review-ready.sh +47 -0
  222. package/gates/check-spec-posted.sh +34 -0
  223. package/gates/check-unblocked.sh +56 -0
  224. package/gates/ci-green.sh +9 -0
  225. package/gates/merge-queue.sh +14 -0
  226. package/gates/review-bots-ready.sh +9 -0
  227. package/gates/spec-posted.sh +31 -0
  228. package/gates/test/bad-return-gate.d.ts +1 -0
  229. package/gates/test/bad-return-gate.js +4 -0
  230. package/gates/test/bad-return-gate.ts +4 -0
  231. package/gates/test/passing-gate.d.ts +2 -0
  232. package/gates/test/passing-gate.js +3 -0
  233. package/gates/test/passing-gate.ts +5 -0
  234. package/gates/test/slow-gate.d.ts +2 -0
  235. package/gates/test/slow-gate.js +5 -0
  236. package/gates/test/slow-gate.ts +7 -0
  237. package/gates/test/throwing-gate.d.ts +1 -0
  238. package/gates/test/throwing-gate.js +3 -0
  239. package/gates/test/throwing-gate.ts +3 -0
  240. package/gates/test-fail.sh +2 -0
  241. package/gates/test-pass.sh +2 -0
  242. package/gates/timeout-gate-script.sh +3 -0
  243. package/package.json +64 -0
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Wire types for defcon's REST and MCP APIs.
3
+ * Exported for consumers (e.g. norad) to import instead of duplicating.
4
+ */
5
+ export type ClaimResponse = {
6
+ next_action: "check_back";
7
+ retry_after_ms: number;
8
+ message: string;
9
+ } | {
10
+ worker_id?: string;
11
+ entity_id: string;
12
+ invocation_id: string;
13
+ flow: string | null;
14
+ stage: string;
15
+ prompt: string;
16
+ context: Record<string, unknown> | null;
17
+ worker_notice?: string;
18
+ };
19
+ export type ReportResponse = {
20
+ next_action: "continue";
21
+ new_state: string;
22
+ gates_passed?: string[];
23
+ prompt: string | null;
24
+ context: Record<string, unknown> | null;
25
+ } | {
26
+ next_action: "waiting";
27
+ new_state: null;
28
+ gated: true;
29
+ gate_output: string;
30
+ gateName: string;
31
+ failure_prompt: string | null;
32
+ } | {
33
+ next_action: "check_back";
34
+ message: string;
35
+ retry_after_ms: number;
36
+ timeout_prompt?: string;
37
+ } | {
38
+ next_action: "completed";
39
+ new_state: string;
40
+ gates_passed?: string[];
41
+ prompt: null;
42
+ context: null;
43
+ };
44
+ export interface CreateEntityResponse {
45
+ id: string;
46
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Wire types for defcon's REST and MCP APIs.
3
+ * Exported for consumers (e.g. norad) to import instead of duplicating.
4
+ */
5
+ export {};
@@ -0,0 +1 @@
1
+ export declare const DB_PATH = "./agentic-flow.db";
@@ -0,0 +1 @@
1
+ export const DB_PATH = "./agentic-flow.db";
@@ -0,0 +1,3 @@
1
+ import type { IFlowRepository, IGateRepository } from "../repositories/interfaces.js";
2
+ import type { SeedFile } from "./zod-schemas.js";
3
+ export declare function exportSeed(flowRepo: IFlowRepository, gateRepo: IGateRepository): Promise<SeedFile>;
@@ -0,0 +1,87 @@
1
+ export async function exportSeed(flowRepo, gateRepo) {
2
+ const flows = await flowRepo.listAll();
3
+ // Fetch all gates once, build ID->name map (no N+1)
4
+ const allGates = await gateRepo.listAll();
5
+ const gateIdToName = new Map(allGates.map((g) => [g.id, g.name]));
6
+ const gateById = new Map(allGates.map((g) => [g.id, g]));
7
+ const exportedFlows = flows.filter((f) => f.discipline !== null);
8
+ // Collect gate IDs referenced by transitions of exported flows only
9
+ const referencedGateIds = new Set();
10
+ for (const flow of exportedFlows) {
11
+ for (const t of flow.transitions) {
12
+ if (t.gateId)
13
+ referencedGateIds.add(t.gateId);
14
+ }
15
+ }
16
+ // Build gates array from referenced gates (already loaded)
17
+ const gateEntries = [];
18
+ for (const gateId of referencedGateIds) {
19
+ const gate = gateById.get(gateId);
20
+ if (!gate)
21
+ continue;
22
+ if (gate.type === "command" && gate.command) {
23
+ gateEntries.push({
24
+ name: gate.name,
25
+ type: "command",
26
+ command: gate.command,
27
+ timeoutMs: gate.timeoutMs ?? undefined,
28
+ });
29
+ }
30
+ else if (gate.type === "function" && gate.functionRef) {
31
+ gateEntries.push({
32
+ name: gate.name,
33
+ type: "function",
34
+ functionRef: gate.functionRef,
35
+ timeoutMs: gate.timeoutMs ?? undefined,
36
+ });
37
+ }
38
+ else if (gate.type === "api" && gate.apiConfig) {
39
+ gateEntries.push({
40
+ name: gate.name,
41
+ type: "api",
42
+ apiConfig: gate.apiConfig,
43
+ timeoutMs: gate.timeoutMs ?? undefined,
44
+ });
45
+ }
46
+ }
47
+ const seedFlows = exportedFlows.map((f) => ({
48
+ name: f.name,
49
+ description: f.description ?? undefined,
50
+ entitySchema: f.entitySchema ?? undefined,
51
+ initialState: f.initialState,
52
+ maxConcurrent: f.maxConcurrent,
53
+ maxConcurrentPerRepo: f.maxConcurrentPerRepo,
54
+ affinityWindowMs: f.affinityWindowMs,
55
+ gateTimeoutMs: f.gateTimeoutMs ?? undefined,
56
+ version: f.version,
57
+ createdBy: f.createdBy ?? undefined,
58
+ discipline: f.discipline,
59
+ defaultModelTier: f.defaultModelTier ?? undefined,
60
+ timeoutPrompt: f.timeoutPrompt ?? undefined,
61
+ }));
62
+ const seedStates = exportedFlows.flatMap((f) => f.states.map((s) => ({
63
+ name: s.name,
64
+ flowName: f.name,
65
+ modelTier: s.modelTier ?? undefined,
66
+ mode: s.mode,
67
+ promptTemplate: s.promptTemplate ?? undefined,
68
+ constraints: s.constraints ?? undefined,
69
+ })));
70
+ const seedTransitions = exportedFlows.flatMap((f) => f.transitions.map((t) => ({
71
+ flowName: f.name,
72
+ fromState: t.fromState,
73
+ toState: t.toState,
74
+ trigger: t.trigger,
75
+ gateName: t.gateId ? gateIdToName.get(t.gateId) : undefined,
76
+ condition: t.condition ?? undefined,
77
+ priority: t.priority,
78
+ spawnFlow: t.spawnFlow ?? undefined,
79
+ spawnTemplate: t.spawnTemplate ?? undefined,
80
+ })));
81
+ return {
82
+ flows: seedFlows,
83
+ states: seedStates,
84
+ gates: gateEntries,
85
+ transitions: seedTransitions,
86
+ };
87
+ }
@@ -0,0 +1,4 @@
1
+ export { exportSeed } from "./exporter.js";
2
+ export type { LoadSeedOptions, LoadSeedResult } from "./seed-loader.js";
3
+ export { loadSeed } from "./seed-loader.js";
4
+ export * from "./zod-schemas.js";
@@ -0,0 +1,4 @@
1
+ // Config module — seed loader, Zod schemas
2
+ export { exportSeed } from "./exporter.js";
3
+ export { loadSeed } from "./seed-loader.js";
4
+ export * from "./zod-schemas.js";
@@ -0,0 +1,10 @@
1
+ import type Database from "better-sqlite3";
2
+ import type { IFlowRepository, IGateRepository } from "../repositories/interfaces.js";
3
+ export interface LoadSeedOptions {
4
+ allowedRoot?: string;
5
+ }
6
+ export interface LoadSeedResult {
7
+ flows: number;
8
+ gates: number;
9
+ }
10
+ export declare function loadSeed(seedPath: string, flowRepo: IFlowRepository, gateRepo: IGateRepository, sqlite: InstanceType<typeof Database>, options?: LoadSeedOptions): Promise<LoadSeedResult>;
@@ -0,0 +1,108 @@
1
+ import { readFileSync, realpathSync } from "node:fs";
2
+ import { relative, resolve, sep } from "node:path";
3
+ import { SeedFileSchema } from "./zod-schemas.js";
4
+ export async function loadSeed(seedPath, flowRepo, gateRepo, sqlite, options) {
5
+ const allowedRoot = options?.allowedRoot ?? process.cwd();
6
+ const resolvedRoot = resolve(allowedRoot);
7
+ const resolvedSeed = resolve(seedPath);
8
+ const lexicalRel = relative(resolvedRoot, resolvedSeed);
9
+ if (lexicalRel === ".." || lexicalRel.startsWith(`..${sep}`)) {
10
+ throw new Error(`Seed path escapes allowed root: ${resolvedSeed} is not under ${resolvedRoot}`);
11
+ }
12
+ const realSeed = realpathSync(resolvedSeed);
13
+ let realRoot;
14
+ try {
15
+ realRoot = realpathSync(resolvedRoot);
16
+ }
17
+ catch {
18
+ realRoot = resolvedRoot;
19
+ }
20
+ const realRel = relative(realRoot, realSeed);
21
+ if (realRel === ".." || realRel.startsWith(`..${sep}`)) {
22
+ throw new Error(`Seed path escapes allowed root: resolved symlink ${realSeed} is not under ${realRoot}`);
23
+ }
24
+ const raw = readFileSync(realSeed, "utf-8");
25
+ return parseSeedAndLoad(parseJson(raw, realSeed), flowRepo, gateRepo, sqlite);
26
+ }
27
+ function parseJson(raw, path) {
28
+ try {
29
+ return JSON.parse(raw);
30
+ }
31
+ catch (e) {
32
+ const msg = e instanceof Error ? e.message : String(e);
33
+ throw new Error(`Invalid JSON in seed file: ${path}: ${msg}`, { cause: e });
34
+ }
35
+ }
36
+ async function parseSeedAndLoad(json, flowRepo, gateRepo, sqlite) {
37
+ const parsed = SeedFileSchema.parse(json);
38
+ sqlite.exec("BEGIN");
39
+ try {
40
+ // 1. Create gates first (transitions reference them by name)
41
+ const gateNameToId = new Map();
42
+ for (const g of parsed.gates) {
43
+ const gate = await gateRepo.create({
44
+ name: g.name,
45
+ type: g.type,
46
+ command: "command" in g ? g.command : undefined,
47
+ functionRef: "functionRef" in g ? g.functionRef : undefined,
48
+ apiConfig: "apiConfig" in g ? g.apiConfig : undefined,
49
+ timeoutMs: g.timeoutMs,
50
+ failurePrompt: g.failurePrompt,
51
+ timeoutPrompt: g.timeoutPrompt,
52
+ });
53
+ gateNameToId.set(g.name, gate.id);
54
+ }
55
+ // 2. Create flows, states, and transitions
56
+ for (const f of parsed.flows) {
57
+ const flow = await flowRepo.create({
58
+ name: f.name,
59
+ description: f.description,
60
+ entitySchema: f.entitySchema,
61
+ initialState: f.initialState,
62
+ maxConcurrent: f.maxConcurrent,
63
+ maxConcurrentPerRepo: f.maxConcurrentPerRepo,
64
+ gateTimeoutMs: f.gateTimeoutMs,
65
+ createdBy: f.createdBy,
66
+ discipline: f.discipline,
67
+ defaultModelTier: f.defaultModelTier,
68
+ timeoutPrompt: f.timeoutPrompt,
69
+ });
70
+ const flowStates = parsed.states.filter((s) => s.flowName === f.name);
71
+ for (const s of flowStates) {
72
+ await flowRepo.addState(flow.id, {
73
+ name: s.name,
74
+ modelTier: s.modelTier,
75
+ mode: s.mode,
76
+ promptTemplate: s.promptTemplate,
77
+ constraints: s.constraints,
78
+ onEnter: s.onEnter,
79
+ });
80
+ }
81
+ const flowTransitions = parsed.transitions.filter((t) => t.flowName === f.name);
82
+ for (const t of flowTransitions) {
83
+ if (t.gateName && !gateNameToId.has(t.gateName)) {
84
+ throw new Error(`Transition from "${t.fromState}" to "${t.toState}" in flow "${f.name}" references unknown gate "${t.gateName}"`);
85
+ }
86
+ await flowRepo.addTransition(flow.id, {
87
+ fromState: t.fromState,
88
+ toState: t.toState,
89
+ trigger: t.trigger,
90
+ gateId: t.gateName ? gateNameToId.get(t.gateName) : undefined,
91
+ condition: t.condition,
92
+ priority: t.priority,
93
+ spawnFlow: t.spawnFlow,
94
+ spawnTemplate: t.spawnTemplate,
95
+ });
96
+ }
97
+ }
98
+ sqlite.exec("COMMIT");
99
+ }
100
+ catch (err) {
101
+ sqlite.exec("ROLLBACK");
102
+ throw err;
103
+ }
104
+ return {
105
+ flows: parsed.flows.length,
106
+ gates: parsed.gates.length,
107
+ };
108
+ }
@@ -0,0 +1,165 @@
1
+ import { z } from "zod/v4";
2
+ export declare const FlowDefinitionSchema: z.ZodObject<{
3
+ name: z.ZodString;
4
+ description: z.ZodOptional<z.ZodString>;
5
+ entitySchema: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
6
+ initialState: z.ZodString;
7
+ maxConcurrent: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
8
+ maxConcurrentPerRepo: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
9
+ affinityWindowMs: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
10
+ gateTimeoutMs: z.ZodOptional<z.ZodNumber>;
11
+ version: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
12
+ createdBy: z.ZodOptional<z.ZodString>;
13
+ discipline: z.ZodString;
14
+ defaultModelTier: z.ZodOptional<z.ZodString>;
15
+ timeoutPrompt: z.ZodOptional<z.ZodString>;
16
+ }, z.core.$strip>;
17
+ export declare const OnEnterSchema: z.ZodObject<{
18
+ command: z.ZodString;
19
+ artifacts: z.ZodArray<z.ZodString>;
20
+ timeout_ms: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
21
+ }, z.core.$strip>;
22
+ export declare const StateDefinitionSchema: z.ZodObject<{
23
+ name: z.ZodString;
24
+ flowName: z.ZodString;
25
+ modelTier: z.ZodOptional<z.ZodString>;
26
+ mode: z.ZodDefault<z.ZodOptional<z.ZodEnum<{
27
+ passive: "passive";
28
+ active: "active";
29
+ }>>>;
30
+ promptTemplate: z.ZodOptional<z.ZodString>;
31
+ constraints: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
32
+ onEnter: z.ZodOptional<z.ZodObject<{
33
+ command: z.ZodString;
34
+ artifacts: z.ZodArray<z.ZodString>;
35
+ timeout_ms: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
36
+ }, z.core.$strip>>;
37
+ }, z.core.$strip>;
38
+ export declare const CommandGateSchema: z.ZodObject<{
39
+ name: z.ZodString;
40
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
41
+ failurePrompt: z.ZodOptional<z.ZodString>;
42
+ timeoutPrompt: z.ZodOptional<z.ZodString>;
43
+ type: z.ZodLiteral<"command">;
44
+ command: z.ZodString;
45
+ }, z.core.$strip>;
46
+ export declare const FunctionGateSchema: z.ZodObject<{
47
+ name: z.ZodString;
48
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
49
+ failurePrompt: z.ZodOptional<z.ZodString>;
50
+ timeoutPrompt: z.ZodOptional<z.ZodString>;
51
+ type: z.ZodLiteral<"function">;
52
+ functionRef: z.ZodString;
53
+ }, z.core.$strip>;
54
+ export declare const ApiGateSchema: z.ZodObject<{
55
+ name: z.ZodString;
56
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
57
+ failurePrompt: z.ZodOptional<z.ZodString>;
58
+ timeoutPrompt: z.ZodOptional<z.ZodString>;
59
+ type: z.ZodLiteral<"api">;
60
+ apiConfig: z.ZodRecord<z.ZodString, z.ZodUnknown>;
61
+ }, z.core.$strip>;
62
+ export declare const GateDefinitionSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
63
+ name: z.ZodString;
64
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
65
+ failurePrompt: z.ZodOptional<z.ZodString>;
66
+ timeoutPrompt: z.ZodOptional<z.ZodString>;
67
+ type: z.ZodLiteral<"command">;
68
+ command: z.ZodString;
69
+ }, z.core.$strip>, z.ZodObject<{
70
+ name: z.ZodString;
71
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
72
+ failurePrompt: z.ZodOptional<z.ZodString>;
73
+ timeoutPrompt: z.ZodOptional<z.ZodString>;
74
+ type: z.ZodLiteral<"function">;
75
+ functionRef: z.ZodString;
76
+ }, z.core.$strip>, z.ZodObject<{
77
+ name: z.ZodString;
78
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
79
+ failurePrompt: z.ZodOptional<z.ZodString>;
80
+ timeoutPrompt: z.ZodOptional<z.ZodString>;
81
+ type: z.ZodLiteral<"api">;
82
+ apiConfig: z.ZodRecord<z.ZodString, z.ZodUnknown>;
83
+ }, z.core.$strip>], "type">;
84
+ export declare const TransitionRuleSchema: z.ZodObject<{
85
+ flowName: z.ZodString;
86
+ fromState: z.ZodString;
87
+ toState: z.ZodString;
88
+ trigger: z.ZodString;
89
+ gateName: z.ZodOptional<z.ZodString>;
90
+ condition: z.ZodOptional<z.ZodString>;
91
+ priority: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
92
+ spawnFlow: z.ZodOptional<z.ZodString>;
93
+ spawnTemplate: z.ZodOptional<z.ZodString>;
94
+ }, z.core.$strip>;
95
+ export declare const SeedFileSchema: z.ZodObject<{
96
+ flows: z.ZodArray<z.ZodObject<{
97
+ name: z.ZodString;
98
+ description: z.ZodOptional<z.ZodString>;
99
+ entitySchema: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
100
+ initialState: z.ZodString;
101
+ maxConcurrent: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
102
+ maxConcurrentPerRepo: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
103
+ affinityWindowMs: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
104
+ gateTimeoutMs: z.ZodOptional<z.ZodNumber>;
105
+ version: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
106
+ createdBy: z.ZodOptional<z.ZodString>;
107
+ discipline: z.ZodString;
108
+ defaultModelTier: z.ZodOptional<z.ZodString>;
109
+ timeoutPrompt: z.ZodOptional<z.ZodString>;
110
+ }, z.core.$strip>>;
111
+ states: z.ZodArray<z.ZodObject<{
112
+ name: z.ZodString;
113
+ flowName: z.ZodString;
114
+ modelTier: z.ZodOptional<z.ZodString>;
115
+ mode: z.ZodDefault<z.ZodOptional<z.ZodEnum<{
116
+ passive: "passive";
117
+ active: "active";
118
+ }>>>;
119
+ promptTemplate: z.ZodOptional<z.ZodString>;
120
+ constraints: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
121
+ onEnter: z.ZodOptional<z.ZodObject<{
122
+ command: z.ZodString;
123
+ artifacts: z.ZodArray<z.ZodString>;
124
+ timeout_ms: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
125
+ }, z.core.$strip>>;
126
+ }, z.core.$strip>>;
127
+ gates: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
128
+ name: z.ZodString;
129
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
130
+ failurePrompt: z.ZodOptional<z.ZodString>;
131
+ timeoutPrompt: z.ZodOptional<z.ZodString>;
132
+ type: z.ZodLiteral<"command">;
133
+ command: z.ZodString;
134
+ }, z.core.$strip>, z.ZodObject<{
135
+ name: z.ZodString;
136
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
137
+ failurePrompt: z.ZodOptional<z.ZodString>;
138
+ timeoutPrompt: z.ZodOptional<z.ZodString>;
139
+ type: z.ZodLiteral<"function">;
140
+ functionRef: z.ZodString;
141
+ }, z.core.$strip>, z.ZodObject<{
142
+ name: z.ZodString;
143
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
144
+ failurePrompt: z.ZodOptional<z.ZodString>;
145
+ timeoutPrompt: z.ZodOptional<z.ZodString>;
146
+ type: z.ZodLiteral<"api">;
147
+ apiConfig: z.ZodRecord<z.ZodString, z.ZodUnknown>;
148
+ }, z.core.$strip>], "type">>>>;
149
+ transitions: z.ZodArray<z.ZodObject<{
150
+ flowName: z.ZodString;
151
+ fromState: z.ZodString;
152
+ toState: z.ZodString;
153
+ trigger: z.ZodString;
154
+ gateName: z.ZodOptional<z.ZodString>;
155
+ condition: z.ZodOptional<z.ZodString>;
156
+ priority: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
157
+ spawnFlow: z.ZodOptional<z.ZodString>;
158
+ spawnTemplate: z.ZodOptional<z.ZodString>;
159
+ }, z.core.$strip>>;
160
+ }, z.core.$strict>;
161
+ export type FlowDefinition = z.infer<typeof FlowDefinitionSchema>;
162
+ export type StateDefinition = z.infer<typeof StateDefinitionSchema>;
163
+ export type GateDefinition = z.infer<typeof GateDefinitionSchema>;
164
+ export type TransitionRule = z.infer<typeof TransitionRuleSchema>;
165
+ export type SeedFile = z.infer<typeof SeedFileSchema>;