@ambystech/ambykit 1.3.1 → 1.5.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 (310) hide show
  1. package/CHANGELOG.md +95 -1
  2. package/README.md +8 -0
  3. package/dist/cli/activity.d.ts +5 -1
  4. package/dist/cli/activity.js +22 -8
  5. package/dist/cli/activity.js.map +1 -1
  6. package/dist/cli/add.js +12 -5
  7. package/dist/cli/add.js.map +1 -1
  8. package/dist/cli/analyze.js +68 -4
  9. package/dist/cli/analyze.js.map +1 -1
  10. package/dist/cli/base-command.js +6 -0
  11. package/dist/cli/base-command.js.map +1 -1
  12. package/dist/cli/check.js +18 -3
  13. package/dist/cli/check.js.map +1 -1
  14. package/dist/cli/dashboard.js +3 -1
  15. package/dist/cli/dashboard.js.map +1 -1
  16. package/dist/cli/directive.d.ts +7 -0
  17. package/dist/cli/directive.js +43 -0
  18. package/dist/cli/directive.js.map +1 -0
  19. package/dist/cli/fleet.d.ts +16 -0
  20. package/dist/cli/fleet.js +472 -0
  21. package/dist/cli/fleet.js.map +1 -0
  22. package/dist/cli/graph.d.ts +13 -0
  23. package/dist/cli/graph.js +40 -0
  24. package/dist/cli/graph.js.map +1 -0
  25. package/dist/cli/index.js +10 -0
  26. package/dist/cli/index.js.map +1 -1
  27. package/dist/cli/io/fsops.d.ts +4 -0
  28. package/dist/cli/io/fsops.js +62 -0
  29. package/dist/cli/io/fsops.js.map +1 -1
  30. package/dist/cli/mcp.d.ts +21 -0
  31. package/dist/cli/mcp.js +27 -0
  32. package/dist/cli/mcp.js.map +1 -0
  33. package/dist/cli/sync.d.ts +2 -0
  34. package/dist/cli/sync.js +31 -5
  35. package/dist/cli/sync.js.map +1 -1
  36. package/dist/cli/trace.d.ts +14 -0
  37. package/dist/cli/trace.js +89 -0
  38. package/dist/cli/trace.js.map +1 -0
  39. package/dist/cli/ui/dashboard/feature-chart.js +15 -2
  40. package/dist/cli/ui/dashboard/feature-chart.js.map +1 -1
  41. package/dist/cli/ui/dashboard/feature-view.js +11 -1
  42. package/dist/cli/ui/dashboard/feature-view.js.map +1 -1
  43. package/dist/cli/ui/dashboard/graph-view.d.ts +44 -0
  44. package/dist/cli/ui/dashboard/graph-view.js +78 -0
  45. package/dist/cli/ui/dashboard/graph-view.js.map +1 -0
  46. package/dist/cli/ui/dashboard/help-overlay.d.ts +1 -1
  47. package/dist/cli/ui/dashboard/help-overlay.js +9 -0
  48. package/dist/cli/ui/dashboard/help-overlay.js.map +1 -1
  49. package/dist/cli/ui/dashboard/quality-view.d.ts +8 -0
  50. package/dist/cli/ui/dashboard/quality-view.js +31 -0
  51. package/dist/cli/ui/dashboard/quality-view.js.map +1 -0
  52. package/dist/cli/ui/dashboard/story-detail.d.ts +1 -1
  53. package/dist/cli/ui/dashboard/story-detail.js +10 -3
  54. package/dist/cli/ui/dashboard/story-detail.js.map +1 -1
  55. package/dist/cli/ui/dashboard/tokens.d.ts +26 -0
  56. package/dist/cli/ui/dashboard/tokens.js +26 -0
  57. package/dist/cli/ui/dashboard/tokens.js.map +1 -1
  58. package/dist/cli/ui/interactive/overview.d.ts +13 -1
  59. package/dist/cli/ui/interactive/overview.js +62 -0
  60. package/dist/cli/ui/interactive/overview.js.map +1 -1
  61. package/dist/cli/ui/tool-options.d.ts +3 -2
  62. package/dist/cli/ui/tool-options.js +3 -3
  63. package/dist/cli/ui/tool-options.js.map +1 -1
  64. package/dist/cli/update.d.ts +1 -1
  65. package/dist/cli/update.js +8 -3
  66. package/dist/cli/update.js.map +1 -1
  67. package/dist/core/activity.d.ts +15 -2
  68. package/dist/core/activity.js +19 -4
  69. package/dist/core/activity.js.map +1 -1
  70. package/dist/core/analyze.d.ts +80 -2
  71. package/dist/core/analyze.js +200 -61
  72. package/dist/core/analyze.js.map +1 -1
  73. package/dist/core/checkouts.js +2 -0
  74. package/dist/core/checkouts.js.map +1 -1
  75. package/dist/core/config.d.ts +28 -1
  76. package/dist/core/config.js +44 -0
  77. package/dist/core/config.js.map +1 -1
  78. package/dist/core/dashboard.d.ts +4 -0
  79. package/dist/core/dashboard.js +5 -3
  80. package/dist/core/dashboard.js.map +1 -1
  81. package/dist/core/decisions.d.ts +60 -0
  82. package/dist/core/decisions.js +231 -0
  83. package/dist/core/decisions.js.map +1 -0
  84. package/dist/core/emit.d.ts +16 -1
  85. package/dist/core/emit.js +49 -5
  86. package/dist/core/emit.js.map +1 -1
  87. package/dist/core/features.js +6 -1
  88. package/dist/core/features.js.map +1 -1
  89. package/dist/core/fleet/claims.d.ts +45 -0
  90. package/dist/core/fleet/claims.js +85 -0
  91. package/dist/core/fleet/claims.js.map +1 -0
  92. package/dist/core/fleet/directives.d.ts +56 -0
  93. package/dist/core/fleet/directives.js +99 -0
  94. package/dist/core/fleet/directives.js.map +1 -0
  95. package/dist/core/fleet/flow-input.d.ts +6 -0
  96. package/dist/core/fleet/flow-input.js +35 -0
  97. package/dist/core/fleet/flow-input.js.map +1 -0
  98. package/dist/core/fleet/index.d.ts +9 -0
  99. package/dist/core/fleet/index.js +10 -0
  100. package/dist/core/fleet/index.js.map +1 -0
  101. package/dist/core/fleet/lane-status.d.ts +39 -0
  102. package/dist/core/fleet/lane-status.js +63 -0
  103. package/dist/core/fleet/lane-status.js.map +1 -0
  104. package/dist/core/fleet/lock.d.ts +23 -0
  105. package/dist/core/fleet/lock.js +103 -0
  106. package/dist/core/fleet/lock.js.map +1 -0
  107. package/dist/core/fleet/notes.d.ts +31 -0
  108. package/dist/core/fleet/notes.js +56 -0
  109. package/dist/core/fleet/notes.js.map +1 -0
  110. package/dist/core/fleet/run-flow.d.ts +157 -0
  111. package/dist/core/fleet/run-flow.js +205 -0
  112. package/dist/core/fleet/run-flow.js.map +1 -0
  113. package/dist/core/fleet/runs.d.ts +46 -0
  114. package/dist/core/fleet/runs.js +182 -0
  115. package/dist/core/fleet/runs.js.map +1 -0
  116. package/dist/core/fleet/types.d.ts +63 -0
  117. package/dist/core/fleet/types.js +11 -0
  118. package/dist/core/fleet/types.js.map +1 -0
  119. package/dist/core/graph-format.d.ts +51 -0
  120. package/dist/core/graph-format.js +99 -0
  121. package/dist/core/graph-format.js.map +1 -0
  122. package/dist/core/graph-paths.d.ts +27 -0
  123. package/dist/core/graph-paths.js +94 -0
  124. package/dist/core/graph-paths.js.map +1 -0
  125. package/dist/core/graph-scope.d.ts +7 -0
  126. package/dist/core/graph-scope.js +31 -0
  127. package/dist/core/graph-scope.js.map +1 -0
  128. package/dist/core/graph-types.d.ts +55 -0
  129. package/dist/core/graph-types.js +6 -0
  130. package/dist/core/graph-types.js.map +1 -0
  131. package/dist/core/graph.d.ts +14 -0
  132. package/dist/core/graph.js +144 -0
  133. package/dist/core/graph.js.map +1 -0
  134. package/dist/core/index.d.ts +13 -0
  135. package/dist/core/index.js +13 -0
  136. package/dist/core/index.js.map +1 -1
  137. package/dist/core/markers.d.ts +44 -0
  138. package/dist/core/markers.js +139 -0
  139. package/dist/core/markers.js.map +1 -0
  140. package/dist/core/merge-json.d.ts +15 -0
  141. package/dist/core/merge-json.js +46 -0
  142. package/dist/core/merge-json.js.map +1 -0
  143. package/dist/core/moved-targets.d.ts +23 -0
  144. package/dist/core/moved-targets.js +23 -0
  145. package/dist/core/moved-targets.js.map +1 -0
  146. package/dist/core/overview-types.d.ts +27 -1
  147. package/dist/core/overview-types.js.map +1 -1
  148. package/dist/core/overview.d.ts +1 -1
  149. package/dist/core/overview.js +91 -74
  150. package/dist/core/overview.js.map +1 -1
  151. package/dist/core/path-guard.d.ts +5 -0
  152. package/dist/core/path-guard.js +9 -0
  153. package/dist/core/path-guard.js.map +1 -0
  154. package/dist/core/paths.d.ts +2 -0
  155. package/dist/core/paths.js +4 -0
  156. package/dist/core/paths.js.map +1 -1
  157. package/dist/core/query.d.ts +91 -0
  158. package/dist/core/query.js +197 -0
  159. package/dist/core/query.js.map +1 -0
  160. package/dist/core/report.d.ts +36 -0
  161. package/dist/core/report.js +62 -0
  162. package/dist/core/report.js.map +1 -0
  163. package/dist/core/rules.js +3 -0
  164. package/dist/core/rules.js.map +1 -1
  165. package/dist/core/scaffold.d.ts +2 -2
  166. package/dist/core/scaffold.js +3 -1
  167. package/dist/core/scaffold.js.map +1 -1
  168. package/dist/core/subagents.d.ts +29 -0
  169. package/dist/core/subagents.js +52 -0
  170. package/dist/core/subagents.js.map +1 -0
  171. package/dist/core/tasks-edit.d.ts +34 -0
  172. package/dist/core/tasks-edit.js +115 -0
  173. package/dist/core/tasks-edit.js.map +1 -0
  174. package/dist/core/test-design.d.ts +94 -0
  175. package/dist/core/test-design.js +325 -0
  176. package/dist/core/test-design.js.map +1 -0
  177. package/dist/core/trace-cite.d.ts +16 -0
  178. package/dist/core/trace-cite.js +24 -0
  179. package/dist/core/trace-cite.js.map +1 -0
  180. package/dist/core/trace-coverage.d.ts +8 -0
  181. package/dist/core/trace-coverage.js +40 -0
  182. package/dist/core/trace-coverage.js.map +1 -0
  183. package/dist/core/trace-design.d.ts +6 -0
  184. package/dist/core/trace-design.js +16 -0
  185. package/dist/core/trace-design.js.map +1 -0
  186. package/dist/core/trace-io.d.ts +14 -0
  187. package/dist/core/trace-io.js +97 -0
  188. package/dist/core/trace-io.js.map +1 -0
  189. package/dist/core/trace-requirements.d.ts +9 -0
  190. package/dist/core/trace-requirements.js +27 -0
  191. package/dist/core/trace-requirements.js.map +1 -0
  192. package/dist/core/trace-tests.d.ts +7 -0
  193. package/dist/core/trace-tests.js +27 -0
  194. package/dist/core/trace-tests.js.map +1 -0
  195. package/dist/core/trace-types.d.ts +81 -0
  196. package/dist/core/trace-types.js +3 -0
  197. package/dist/core/trace-types.js.map +1 -0
  198. package/dist/core/trace.d.ts +18 -0
  199. package/dist/core/trace.js +109 -0
  200. package/dist/core/trace.js.map +1 -0
  201. package/dist/core/types.d.ts +43 -1
  202. package/dist/emitters/antigravity-cli.d.ts +4 -0
  203. package/dist/emitters/antigravity-cli.js +8 -0
  204. package/dist/emitters/antigravity-cli.js.map +1 -1
  205. package/dist/emitters/antigravity.d.ts +6 -0
  206. package/dist/emitters/antigravity.js +8 -0
  207. package/dist/emitters/antigravity.js.map +1 -1
  208. package/dist/emitters/base-emitter.d.ts +28 -0
  209. package/dist/emitters/base-emitter.js +41 -1
  210. package/dist/emitters/base-emitter.js.map +1 -1
  211. package/dist/emitters/claude.d.ts +12 -2
  212. package/dist/emitters/claude.js +34 -5
  213. package/dist/emitters/claude.js.map +1 -1
  214. package/dist/emitters/copilot-cli.d.ts +7 -1
  215. package/dist/emitters/copilot-cli.js +9 -0
  216. package/dist/emitters/copilot-cli.js.map +1 -1
  217. package/dist/emitters/copilot.d.ts +5 -0
  218. package/dist/emitters/copilot.js +23 -1
  219. package/dist/emitters/copilot.js.map +1 -1
  220. package/dist/emitters/cursor.d.ts +3 -0
  221. package/dist/emitters/cursor.js +8 -1
  222. package/dist/emitters/cursor.js.map +1 -1
  223. package/dist/emitters/index.d.ts +10 -3
  224. package/dist/emitters/index.js +6 -6
  225. package/dist/emitters/index.js.map +1 -1
  226. package/dist/emitters/opencode.d.ts +8 -0
  227. package/dist/emitters/opencode.js +23 -0
  228. package/dist/emitters/opencode.js.map +1 -1
  229. package/dist/index.d.ts +5 -0
  230. package/dist/index.js +4 -0
  231. package/dist/index.js.map +1 -1
  232. package/dist/mcp/protocol.d.ts +44 -0
  233. package/dist/mcp/protocol.js +114 -0
  234. package/dist/mcp/protocol.js.map +1 -0
  235. package/dist/mcp/registry.d.ts +40 -0
  236. package/dist/mcp/registry.js +44 -0
  237. package/dist/mcp/registry.js.map +1 -0
  238. package/dist/mcp/server.d.ts +7 -0
  239. package/dist/mcp/server.js +14 -0
  240. package/dist/mcp/server.js.map +1 -0
  241. package/dist/mcp/tools/common.d.ts +12 -0
  242. package/dist/mcp/tools/common.js +12 -0
  243. package/dist/mcp/tools/common.js.map +1 -0
  244. package/dist/mcp/tools/fleet.d.ts +10 -0
  245. package/dist/mcp/tools/fleet.js +102 -0
  246. package/dist/mcp/tools/fleet.js.map +1 -0
  247. package/dist/mcp/tools/index.d.ts +3 -0
  248. package/dist/mcp/tools/index.js +13 -0
  249. package/dist/mcp/tools/index.js.map +1 -0
  250. package/dist/mcp/tools/read.d.ts +6 -0
  251. package/dist/mcp/tools/read.js +62 -0
  252. package/dist/mcp/tools/read.js.map +1 -0
  253. package/dist/mcp/tools/write.d.ts +6 -0
  254. package/dist/mcp/tools/write.js +34 -0
  255. package/dist/mcp/tools/write.js.map +1 -0
  256. package/dist/plugins/api.d.ts +39 -0
  257. package/dist/plugins/api.js +28 -0
  258. package/dist/plugins/api.js.map +1 -0
  259. package/dist/plugins/index.d.ts +11 -0
  260. package/dist/plugins/index.js +9 -0
  261. package/dist/plugins/index.js.map +1 -0
  262. package/dist/plugins/loader.d.ts +35 -0
  263. package/dist/plugins/loader.js +117 -0
  264. package/dist/plugins/loader.js.map +1 -0
  265. package/dist/plugins/manifest.d.ts +21 -0
  266. package/dist/plugins/manifest.js +53 -0
  267. package/dist/plugins/manifest.js.map +1 -0
  268. package/dist/plugins/registry.d.ts +36 -0
  269. package/dist/plugins/registry.js +30 -0
  270. package/dist/plugins/registry.js.map +1 -0
  271. package/dist/plugins/report.d.ts +9 -0
  272. package/dist/plugins/report.js +19 -0
  273. package/dist/plugins/report.js.map +1 -0
  274. package/dist/plugins/semver.d.ts +23 -0
  275. package/dist/plugins/semver.js +106 -0
  276. package/dist/plugins/semver.js.map +1 -0
  277. package/dist/plugins/session.d.ts +18 -0
  278. package/dist/plugins/session.js +22 -0
  279. package/dist/plugins/session.js.map +1 -0
  280. package/dist/testkit/contract.d.ts +33 -0
  281. package/dist/testkit/contract.js +118 -0
  282. package/dist/testkit/contract.js.map +1 -0
  283. package/dist/testkit/index.d.ts +15 -0
  284. package/dist/testkit/index.js +15 -0
  285. package/dist/testkit/index.js.map +1 -0
  286. package/docs/architecture.md +33 -5
  287. package/docs/artifacts/decision-record.md +54 -0
  288. package/docs/artifacts/tasks.md +37 -0
  289. package/docs/artifacts/tests.md +43 -0
  290. package/docs/cli-reference.md +178 -5
  291. package/docs/concepts/mcp-server.md +93 -0
  292. package/docs/contributing.md +9 -0
  293. package/docs/index.md +1 -1
  294. package/docs/plugin-api.md +124 -0
  295. package/docs/plugin-split.md +55 -0
  296. package/docs/plugin-trust.md +51 -0
  297. package/docs/tool-compatibility.md +54 -6
  298. package/docs/workflow.md +37 -5
  299. package/package.json +5 -2
  300. package/src/agents/worker.md +37 -0
  301. package/src/prompts/converge.md +8 -3
  302. package/src/prompts/decide.md +31 -0
  303. package/src/prompts/implement.md +15 -7
  304. package/src/prompts/revise.md +4 -2
  305. package/src/prompts/run.md +100 -0
  306. package/src/prompts/tasks.md +13 -3
  307. package/src/roles/security.md +17 -0
  308. package/src/templates/adr.md +29 -0
  309. package/src/templates/tasks.md +6 -0
  310. package/src/templates/tests.md +29 -0
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Fleet state types (feature 025). The single contract the fleet family builds on: 019 (prompt layer),
3
+ * 028 (`ambykit run`) and 029 (control room) import these and keep no run or state logic of their own.
4
+ * Files and invariants are in `specs/025-mcp-server/data-model.md`.
5
+ */
6
+ export declare const RUN_STATUSES: readonly ["active", "paused", "waiting-person", "done"];
7
+ export type RunStatus = (typeof RUN_STATUSES)[number];
8
+ export declare const RUN_STAGES: readonly ["working", "qa", "architect"];
9
+ export type RunStage = (typeof RUN_STAGES)[number];
10
+ /** Unfinished runs are the ones a report can still belong to. */
11
+ export declare const UNFINISHED: readonly RunStatus[];
12
+ export interface RunGate {
13
+ id: string;
14
+ after: string;
15
+ state: "pending" | "approved";
16
+ }
17
+ /** Spec 019 US-7: how overlapping lanes are handled, recorded before work starts. */
18
+ export interface LaneOverlap {
19
+ mode: "serialized" | "squad";
20
+ /** Short names of the lanes this lane overlaps with. */
21
+ with: string[];
22
+ /** For a squad: which lane is the single writer of each shared path. */
23
+ writers?: Record<string, string>;
24
+ /** Merge order, short lane names. */
25
+ mergeOrder?: string[];
26
+ }
27
+ export interface RunLane {
28
+ /** Full lane name `NNN-slug.lane-p-k`. */
29
+ lane: string;
30
+ assistant: string;
31
+ tasks: string[];
32
+ /** Optional overlap-decision record (019 FR-019). */
33
+ overlap?: LaneOverlap;
34
+ /** Automatic restarts used after a stall (019 FR-026); the lead pauses the lane when `fleet.restartLimit` is reached. */
35
+ restarts?: number;
36
+ /** The worker finished this lane's tasks (019 `fleet update --worker-done`). */
37
+ done?: boolean;
38
+ }
39
+ /** `.amby/fleet/<run>/run.json`. 019 writes phase/stage/round/limit/needsDecision; 028 gates/approvals/escalations/lanes. */
40
+ export interface RunRecord {
41
+ run: string;
42
+ feature: string;
43
+ status: RunStatus;
44
+ phase: number;
45
+ stage: RunStage;
46
+ round: number;
47
+ limit: number;
48
+ needsDecision: boolean;
49
+ gates: RunGate[];
50
+ lanes: RunLane[];
51
+ approvals: unknown[];
52
+ escalations: unknown[];
53
+ createdAt: string;
54
+ }
55
+ export declare const DIRECTIVE_ACTIONS: readonly ["continue", "pause", "stop"];
56
+ export type DirectiveAction = (typeof DIRECTIVE_ACTIONS)[number];
57
+ /** `.amby/fleet/<run>/directives/<lane>.json`. */
58
+ export interface Directive {
59
+ action: DirectiveAction;
60
+ at: string;
61
+ }
62
+ /** Derived, never stored (FR-022). `none` = no directive, or nothing is waiting on one. */
63
+ export type DirectiveStatus = "none" | "pending" | "pause requested" | "paused" | "obeyed";
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Fleet state types (feature 025). The single contract the fleet family builds on: 019 (prompt layer),
3
+ * 028 (`ambykit run`) and 029 (control room) import these and keep no run or state logic of their own.
4
+ * Files and invariants are in `specs/025-mcp-server/data-model.md`.
5
+ */
6
+ export const RUN_STATUSES = ["active", "paused", "waiting-person", "done"];
7
+ export const RUN_STAGES = ["working", "qa", "architect"];
8
+ /** Unfinished runs are the ones a report can still belong to. */
9
+ export const UNFINISHED = ["active", "paused", "waiting-person"];
10
+ export const DIRECTIVE_ACTIONS = ["continue", "pause", "stop"];
11
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/core/fleet/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,QAAQ,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,CAAU,CAAC;AAGpF,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,SAAS,EAAE,IAAI,EAAE,WAAW,CAAU,CAAC;AAGlE,iEAAiE;AACjE,MAAM,CAAC,MAAM,UAAU,GAAyB,CAAC,QAAQ,EAAE,QAAQ,EAAE,gBAAgB,CAAC,CAAC;AAiDvF,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,UAAU,EAAE,OAAO,EAAE,MAAM,CAAU,CAAC"}
@@ -0,0 +1,51 @@
1
+ import type { DependencyGraph, EdgeKind } from "./graph-types.js";
2
+ /**
3
+ * Feature 023: pure formatters for the dependency graph (FR-007, FR-008). Strings only; no filesystem,
4
+ * no terminal capability checks, so the output is identical everywhere (plain text, no glyph needed).
5
+ */
6
+ /** Canonical ids back to their written form: `US9` -> `US-9`, `014US2` -> `014:US-2`. */
7
+ export declare function prettyRef(dep: string): string;
8
+ /** Version of the `--json` shape documented in specs/023-dependency-graph/contracts/graph-json.md. */
9
+ export declare const GRAPH_JSON_VERSION = 1;
10
+ export interface GraphJson {
11
+ version: number;
12
+ nodes: {
13
+ id: string;
14
+ feature: string;
15
+ story: string;
16
+ title: string;
17
+ status: string;
18
+ priority: string;
19
+ }[];
20
+ edges: {
21
+ from: string;
22
+ to: string;
23
+ kind: EdgeKind;
24
+ }[];
25
+ featureEdges: {
26
+ from: string;
27
+ to: string;
28
+ via: {
29
+ from: string;
30
+ to: string;
31
+ kind: EdgeKind;
32
+ }[];
33
+ }[];
34
+ broken: {
35
+ from: string;
36
+ to: string;
37
+ kind: EdgeKind;
38
+ reason: string;
39
+ }[];
40
+ cycles: string[][];
41
+ }
42
+ /** FR-008: the stable machine shape. Fields are only ever added. */
43
+ export declare function toJson(graph: DependencyGraph): GraphJson;
44
+ /** Readable text: problems first (FR-010), then feature dependencies, then each story and what it waits on. */
45
+ export declare function toText(graph: DependencyGraph): string;
46
+ /**
47
+ * Mermaid flowchart: one subgraph per feature, a solid arrow for `depends-on`, a dashed arrow for
48
+ * `blocked-by`, and a thick arrow between subgraphs for each derived feature edge. `A --> B` reads
49
+ * "A depends on B". Syntax is checked structurally in tests; a renderer check is a manual step.
50
+ */
51
+ export declare function toMermaid(graph: DependencyGraph): string;
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Feature 023: pure formatters for the dependency graph (FR-007, FR-008). Strings only; no filesystem,
3
+ * no terminal capability checks, so the output is identical everywhere (plain text, no glyph needed).
4
+ */
5
+ /** Canonical ids back to their written form: `US9` -> `US-9`, `014US2` -> `014:US-2`. */
6
+ export function prettyRef(dep) {
7
+ const m = /^(?:(\d+))?US(\d+)$/.exec(dep);
8
+ return m ? `${m[1] ? `${m[1]}:` : ""}US-${m[2]}` : dep;
9
+ }
10
+ const idOf = (graph) => {
11
+ const ids = new Map(graph.nodes.map((n) => [n.key, n.id]));
12
+ return (key) => ids.get(key) ?? key;
13
+ };
14
+ const reasonText = (b) => (b.reason === "unknown-feature" ? "unknown feature" : "unknown story");
15
+ /** Version of the `--json` shape documented in specs/023-dependency-graph/contracts/graph-json.md. */
16
+ export const GRAPH_JSON_VERSION = 1;
17
+ /** FR-008: the stable machine shape. Fields are only ever added. */
18
+ export function toJson(graph) {
19
+ const id = idOf(graph);
20
+ const edge = (e) => ({ from: id(e.from), to: id(e.to), kind: e.kind });
21
+ return {
22
+ version: GRAPH_JSON_VERSION,
23
+ nodes: graph.nodes.map((n) => ({
24
+ id: n.id,
25
+ feature: n.feature,
26
+ story: n.story,
27
+ title: n.title,
28
+ status: n.status,
29
+ priority: n.priority,
30
+ })),
31
+ edges: graph.edges.map(edge),
32
+ featureEdges: graph.featureEdges.map((f) => ({ from: f.from, to: f.to, via: f.via.map(edge) })),
33
+ broken: graph.broken.map((b) => ({ from: id(b.from), to: prettyRef(b.to), kind: b.kind, reason: b.reason })),
34
+ cycles: graph.cycles.map((c) => c.stories.map(id)),
35
+ };
36
+ }
37
+ /** Readable text: problems first (FR-010), then feature dependencies, then each story and what it waits on. */
38
+ export function toText(graph) {
39
+ const id = idOf(graph);
40
+ const out = [];
41
+ const features = new Set(graph.nodes.map((n) => n.feature)).size;
42
+ out.push(`Dependency graph: ${graph.nodes.length} stories, ${graph.edges.length} dependencies, ${features} features.`);
43
+ if (graph.broken.length > 0 || graph.cycles.length > 0) {
44
+ out.push("", "Problems:");
45
+ for (const b of graph.broken) {
46
+ out.push(` [broken] ${id(b.from)} ${b.kind} ${prettyRef(b.to)} — ${reasonText(b)}`);
47
+ }
48
+ for (const c of graph.cycles) {
49
+ const ring = c.stories.map(id);
50
+ out.push(` [cycle] ${[...ring, ring[0] ?? ""].join(" -> ")}`);
51
+ }
52
+ }
53
+ if (graph.featureEdges.length > 0) {
54
+ out.push("", "Feature dependencies:");
55
+ for (const f of graph.featureEdges) {
56
+ out.push(` ${f.from} -> ${f.to} (${f.via.map((e) => `${id(e.from)} ${e.kind} ${id(e.to)}`).join("; ")})`);
57
+ }
58
+ }
59
+ const edgesFrom = new Map();
60
+ for (const e of graph.edges)
61
+ edgesFrom.set(e.from, [...(edgesFrom.get(e.from) ?? []), e]);
62
+ const stories = graph.nodes.filter((n) => edgesFrom.has(n.key));
63
+ if (stories.length > 0) {
64
+ out.push("", "Stories with dependencies:");
65
+ for (const n of stories) {
66
+ out.push(` ${n.id}${n.title ? ` — ${n.title}` : ""}${n.status ? ` [${n.status}]` : ""}`);
67
+ for (const e of edgesFrom.get(n.key) ?? [])
68
+ out.push(` ${e.kind} ${id(e.to)}`);
69
+ }
70
+ }
71
+ return out.join("\n");
72
+ }
73
+ const safeId = (prefix, value) => `${prefix}_${value.replace(/[^A-Za-z0-9]/g, "_")}`;
74
+ const safeLabel = (value) => value.replace(/"/g, "#quot;").replace(/[\r\n]+/g, " ");
75
+ /**
76
+ * Mermaid flowchart: one subgraph per feature, a solid arrow for `depends-on`, a dashed arrow for
77
+ * `blocked-by`, and a thick arrow between subgraphs for each derived feature edge. `A --> B` reads
78
+ * "A depends on B". Syntax is checked structurally in tests; a renderer check is a manual step.
79
+ */
80
+ export function toMermaid(graph) {
81
+ const out = ["graph TD", " %% A --> B: A depends on B; A -.-> B: A is blocked by B"];
82
+ const nodeId = (key) => safeId("s", key);
83
+ const byFeature = new Map();
84
+ for (const n of graph.nodes)
85
+ byFeature.set(n.feature, [...(byFeature.get(n.feature) ?? []), n]);
86
+ for (const [feature, nodes] of byFeature) {
87
+ out.push(` subgraph ${safeId("f", feature)}["${safeLabel(feature)}"]`);
88
+ for (const n of nodes)
89
+ out.push(` ${nodeId(n.key)}["${safeLabel(`${n.id} ${n.title}`.trim())}"]`);
90
+ out.push(" end");
91
+ }
92
+ for (const e of graph.edges) {
93
+ out.push(` ${nodeId(e.from)} ${e.kind === "blocked-by" ? "-.->" : "-->"} ${nodeId(e.to)}`);
94
+ }
95
+ for (const f of graph.featureEdges)
96
+ out.push(` ${safeId("f", f.from)} ==> ${safeId("f", f.to)}`);
97
+ return out.join("\n");
98
+ }
99
+ //# sourceMappingURL=graph-format.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph-format.js","sourceRoot":"","sources":["../../src/core/graph-format.ts"],"names":[],"mappings":"AAEA;;;GAGG;AAEH,yFAAyF;AACzF,MAAM,UAAU,SAAS,CAAC,GAAW;IACnC,MAAM,CAAC,GAAG,qBAAqB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC1C,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC;AACzD,CAAC;AAED,MAAM,IAAI,GAA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AK,EAAE,CAAC;QAC5B,GAAG,CAAC,IAAI,CAAC,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,KAAK,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IAC9F,CAAC;IACD,KAAK,MAAM,CAAC,IAAI,KAAK,CAAC,YAAY;QAAE,GAAG,CAAC,IAAI,CAAC,KAAK,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IAClG,OAAO,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACxB,CAAC"}
@@ -0,0 +1,27 @@
1
+ import type { DependencyGraph, StoryNode } from "./graph-types.js";
2
+ /**
3
+ * Feature 023: questions asked of a built graph (FR-012, FR-013). Pure functions of a `DependencyGraph`;
4
+ * no filesystem. Loaded by `analyze` and, lazily, by the interactive graph view, never by the one-shot
5
+ * dashboard (FR-015).
6
+ */
7
+ /** One unmet blocker of a story: a story that is not done yet, or a dependency that does not resolve. */
8
+ export interface Blocker {
9
+ /** The blocking node's key, or the reference as written when `broken`. */
10
+ key: string;
11
+ broken: boolean;
12
+ }
13
+ /**
14
+ * The unmet blockers of a story: every `depends-on` / `blocked-by` target that is not done, then every
15
+ * dependency that does not resolve (a missing story can never be done). Each target is listed once,
16
+ * even when written under both kinds. This is the rule `analyze` has always used.
17
+ */
18
+ export declare function blockersOf(graph: DependencyGraph, key: string): Blocker[];
19
+ /** FR-012: the not-done stories with no unmet blocker, in graph order. */
20
+ export declare function buildable(graph: DependencyGraph): StoryNode[];
21
+ /**
22
+ * FR-013: the longest chain of not-done stories, in dependency order (a story's prerequisite comes
23
+ * before it). Only edges between not-done stories on the acyclic part of the graph count: done stories
24
+ * are no longer in the way, and stories on a cycle have no order. Ties go to the earliest node in graph
25
+ * order (feature, then declaration), at every step, so the answer is stable across runs.
26
+ */
27
+ export declare function criticalPath(graph: DependencyGraph): StoryNode[];
@@ -0,0 +1,94 @@
1
+ const indexes = new WeakMap();
2
+ /** Lookup tables for a graph, built once per graph object. */
3
+ function indexOf(graph) {
4
+ const cached = indexes.get(graph);
5
+ if (cached)
6
+ return cached;
7
+ const edgesFrom = new Map();
8
+ for (const e of graph.edges)
9
+ edgesFrom.set(e.from, [...(edgesFrom.get(e.from) ?? []), e]);
10
+ const brokenFrom = new Map();
11
+ for (const b of graph.broken)
12
+ brokenFrom.set(b.from, [...(brokenFrom.get(b.from) ?? []), b]);
13
+ const index = { byKey: new Map(graph.nodes.map((n) => [n.key, n])), edgesFrom, brokenFrom };
14
+ indexes.set(graph, index);
15
+ return index;
16
+ }
17
+ const isDone = (n) => n?.status === "done";
18
+ /**
19
+ * The unmet blockers of a story: every `depends-on` / `blocked-by` target that is not done, then every
20
+ * dependency that does not resolve (a missing story can never be done). Each target is listed once,
21
+ * even when written under both kinds. This is the rule `analyze` has always used.
22
+ */
23
+ export function blockersOf(graph, key) {
24
+ const { byKey, edgesFrom, brokenFrom } = indexOf(graph);
25
+ const out = [];
26
+ const seen = new Set();
27
+ for (const e of edgesFrom.get(key) ?? []) {
28
+ if (seen.has(e.to))
29
+ continue;
30
+ seen.add(e.to);
31
+ if (!isDone(byKey.get(e.to)))
32
+ out.push({ key: e.to, broken: false });
33
+ }
34
+ for (const b of brokenFrom.get(key) ?? []) {
35
+ if (seen.has(`?${b.to}`))
36
+ continue;
37
+ seen.add(`?${b.to}`);
38
+ out.push({ key: b.to, broken: true });
39
+ }
40
+ return out;
41
+ }
42
+ /** FR-012: the not-done stories with no unmet blocker, in graph order. */
43
+ export function buildable(graph) {
44
+ return graph.nodes.filter((n) => !isDone(n) && blockersOf(graph, n.key).length === 0);
45
+ }
46
+ /**
47
+ * FR-013: the longest chain of not-done stories, in dependency order (a story's prerequisite comes
48
+ * before it). Only edges between not-done stories on the acyclic part of the graph count: done stories
49
+ * are no longer in the way, and stories on a cycle have no order. Ties go to the earliest node in graph
50
+ * order (feature, then declaration), at every step, so the answer is stable across runs.
51
+ */
52
+ export function criticalPath(graph) {
53
+ const { byKey, edgesFrom } = indexOf(graph);
54
+ const onCycle = new Set(graph.cycles.flatMap((c) => c.stories));
55
+ const order = new Map(graph.nodes.map((n, i) => [n.key, i]));
56
+ const open = (key) => !isDone(byKey.get(key)) && !onCycle.has(key);
57
+ // length[k] = the longest chain that ends at k; via[k] = the prerequisite that chain comes through.
58
+ const length = new Map();
59
+ const via = new Map();
60
+ const visit = (key) => {
61
+ const known = length.get(key);
62
+ if (known !== undefined)
63
+ return known;
64
+ let best = 0;
65
+ let from = null;
66
+ for (const e of edgesFrom.get(key) ?? []) {
67
+ if (!open(e.to))
68
+ continue;
69
+ const len = visit(e.to);
70
+ if (len > best || (len === best && from !== null && (order.get(e.to) ?? 0) < (order.get(from) ?? 0))) {
71
+ best = len;
72
+ from = e.to;
73
+ }
74
+ }
75
+ length.set(key, best + 1);
76
+ via.set(key, from);
77
+ return best + 1;
78
+ };
79
+ let end = null;
80
+ for (const n of graph.nodes) {
81
+ if (!open(n.key))
82
+ continue;
83
+ if (end === null || visit(n.key) > (length.get(end) ?? 0))
84
+ end = n.key;
85
+ }
86
+ const chain = [];
87
+ for (let k = end; k !== null; k = via.get(k) ?? null) {
88
+ const node = byKey.get(k);
89
+ if (node)
90
+ chain.push(node);
91
+ }
92
+ return chain.reverse();
93
+ }
94
+ //# sourceMappingURL=graph-paths.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph-paths.js","sourceRoot":"","sources":["../../src/core/graph-paths.ts"],"names":[],"mappings":"AAqBA,MAAM,OAAO,GAAG,IAAI,OAAO,EAA0B,CAAC;AAEtD,8DAA8D;AAC9D,SAAS,OAAO,CAAC,KAAsB;IACrC,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,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AG,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;gBACrG,IAAI,GAAG,GAAG,CAAC;gBACX,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC;YACd,CAAC;QACH,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,GAAG,CAAC,CAAC,CAAC;QAC1B,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QACnB,OAAO,IAAI,GAAG,CAAC,CAAC;IAClB,CAAC,CAAC;IAEF,IAAI,GAAG,GAAkB,IAAI,CAAC;IAC9B,KAAK,MAAM,CAAC,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;QAC5B,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC;YAAE,SAAS;QAC3B,IAAI,GAAG,KAAK,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAAE,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC;IACzE,CAAC;IACD,MAAM,KAAK,GAAgB,EAAE,CAAC;IAC9B,KAAK,IAAI,CAAC,GAAkB,GAAG,EAAE,CAAC,KAAK,IAAI,EAAE,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC;QACpE,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAC1B,IAAI,IAAI;YAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IACD,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC;AACzB,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { DependencyGraph } from "./graph-types.js";
2
+ /**
3
+ * FR-009: narrow a graph to one feature and its direct neighbours, in both directions (features it
4
+ * depends on and features that depend on it). `ref` matches the feature directory, its number or its
5
+ * display ref. Returns null when no feature matches. Pure; the input is not changed.
6
+ */
7
+ export declare function scopeToFeature(graph: DependencyGraph, ref: string): DependencyGraph | null;
@@ -0,0 +1,31 @@
1
+ /**
2
+ * FR-009: narrow a graph to one feature and its direct neighbours, in both directions (features it
3
+ * depends on and features that depend on it). `ref` matches the feature directory, its number or its
4
+ * display ref. Returns null when no feature matches. Pure; the input is not changed.
5
+ */
6
+ export function scopeToFeature(graph, ref) {
7
+ const features = new Map();
8
+ for (const n of graph.nodes)
9
+ features.set(n.feature, { dir: n.feature, ref: n.ref });
10
+ const numberOf = (dir) => /^(\d+)/.exec(dir)?.[1] ?? dir;
11
+ const focus = [...features.values()].filter((f) => f.dir === ref || f.ref === ref || numberOf(f.dir) === ref);
12
+ if (focus.length === 0)
13
+ return null;
14
+ const keep = new Set(focus.map((f) => f.dir));
15
+ for (const e of graph.featureEdges) {
16
+ if (keep.has(e.from))
17
+ keep.add(e.to);
18
+ if (focus.some((f) => f.dir === e.to))
19
+ keep.add(e.from);
20
+ }
21
+ const nodes = graph.nodes.filter((n) => keep.has(n.feature));
22
+ const kept = new Set(nodes.map((n) => n.key));
23
+ return {
24
+ nodes,
25
+ edges: graph.edges.filter((e) => kept.has(e.from) && kept.has(e.to)),
26
+ featureEdges: graph.featureEdges.filter((e) => keep.has(e.from) && keep.has(e.to)),
27
+ broken: graph.broken.filter((b) => kept.has(b.from)),
28
+ cycles: graph.cycles.filter((c) => c.stories.every((s) => kept.has(s))),
29
+ };
30
+ }
31
+ //# sourceMappingURL=graph-scope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph-scope.js","sourceRoot":"","sources":["../../src/core/graph-scope.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,KAAsB,EAAE,GAAW;IAChE,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAwC,CAAC;IACjE,KAAK,MAAM,CAAC,IAAI,KAAK,CAAC,KAAK;QAA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,CAAC,CAAC;IAC7D,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAC9C,OAAO;QACL,KAAK;QACL,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACpE,YAAY,EAAE,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QAClF,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QACpD,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;KACxE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Feature 023: the shared dependency graph (types). Pure data, no behaviour. See `graph.ts` for
3
+ * `buildGraph`, `graph-paths.ts` for buildable/critical-path, `graph-format.ts` for text/JSON/Mermaid.
4
+ */
5
+ /** `depends-on` = needs that story done first; `blocked-by` = something external blocks it. A story may use both. */
6
+ export type EdgeKind = "depends-on" | "blocked-by";
7
+ /** Why a dependency does not resolve (FR-004). */
8
+ export type BrokenReason = "unknown-story" | "unknown-feature";
9
+ export interface StoryNode {
10
+ /** Collision-safe internal key: `<feature dir>:<canonical story id>`, e.g. `905-top:US1`. */
11
+ key: string;
12
+ /** Display ID qualified by the feature's display ref, e.g. `905:US-1` (the directory name when two features share a number). */
13
+ id: string;
14
+ /** Feature directory, e.g. `905-top`. */
15
+ feature: string;
16
+ /** Feature display ref, e.g. `905`. */
17
+ ref: string;
18
+ /** Story ID as written, e.g. `US-1`. */
19
+ story: string;
20
+ title: string;
21
+ status: string;
22
+ priority: string;
23
+ }
24
+ /** `from` depends on / is blocked by `to`. Both ends are node keys. */
25
+ export interface StoryEdge {
26
+ from: string;
27
+ to: string;
28
+ kind: EdgeKind;
29
+ }
30
+ /** Derived (FR-002): feature `from` depends on feature `to`; `via` records the story edges that cause it. Ends are feature dirs. */
31
+ export interface FeatureEdge {
32
+ from: string;
33
+ to: string;
34
+ via: StoryEdge[];
35
+ }
36
+ /** A dependency that names something that does not exist (FR-004): `from` is a node key, `to` is the reference as written. */
37
+ export interface BrokenEdge {
38
+ from: string;
39
+ to: string;
40
+ kind: EdgeKind;
41
+ reason: BrokenReason;
42
+ }
43
+ /** A dependency cycle (FR-005): node keys in dependency order; a self-dependency is a cycle of one. */
44
+ export interface GraphCycle {
45
+ stories: string[];
46
+ }
47
+ export interface DependencyGraph {
48
+ /** Sorted by feature directory, then story number. */
49
+ nodes: StoryNode[];
50
+ /** Grouped by `from` node (in node order), each node's edges in declaration order, depends-on before blocked-by. */
51
+ edges: StoryEdge[];
52
+ featureEdges: FeatureEdge[];
53
+ broken: BrokenEdge[];
54
+ cycles: GraphCycle[];
55
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Feature 023: the shared dependency graph (types). Pure data, no behaviour. See `graph.ts` for
3
+ * `buildGraph`, `graph-paths.ts` for buildable/critical-path, `graph-format.ts` for text/JSON/Mermaid.
4
+ */
5
+ export {};
6
+ //# sourceMappingURL=graph-types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph-types.js","sourceRoot":"","sources":["../../src/core/graph-types.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
@@ -0,0 +1,14 @@
1
+ import type { DependencyGraph } from "./graph-types.js";
2
+ import type { FeatureSnapshot } from "./overview-types.js";
3
+ /**
4
+ * Parse a dependency as stored on a story (`US1`, or `014US2` for a cross-feature `014:US-2`).
5
+ * `ref` is the target feature's number, or null when the dependency stays inside the story's feature.
6
+ * The single definition of the cross-feature syntax; nothing else resolves dependencies.
7
+ */
8
+ export declare function parseDependencyRef(dep: string): {
9
+ ref: string | null;
10
+ local: string;
11
+ };
12
+ /** Collision-safe node key: story ids restart per feature, and two features can share a number. */
13
+ export declare function nodeKey(featureDir: string, canonicalId: string): string;
14
+ export declare function buildGraph(features: FeatureSnapshot[]): DependencyGraph;
@@ -0,0 +1,144 @@
1
+ import { canonicalStoryId } from "./dashboard.js";
2
+ import { compareFeatureDirs } from "./features.js";
3
+ /**
4
+ * Feature 023: the one place that turns the stories of every feature into a dependency graph (FR-001).
5
+ * Pure: a function of `FeatureSnapshot[]`, no filesystem, no mutation of its input, same output for the
6
+ * same specs whatever order the features arrive in (FR-006). `analyze`, the dashboard, `ambykit graph`
7
+ * and later the run scheduler and the MCP server all read dependencies from here (FR-011).
8
+ */
9
+ const QUALIFIED = /^(\d+)[:/]?(US\d+)$/;
10
+ /**
11
+ * Parse a dependency as stored on a story (`US1`, or `014US2` for a cross-feature `014:US-2`).
12
+ * `ref` is the target feature's number, or null when the dependency stays inside the story's feature.
13
+ * The single definition of the cross-feature syntax; nothing else resolves dependencies.
14
+ */
15
+ export function parseDependencyRef(dep) {
16
+ const canonical = canonicalStoryId(dep);
17
+ const m = QUALIFIED.exec(canonical);
18
+ return m ? { ref: m[1] ?? null, local: m[2] ?? canonical } : { ref: null, local: canonical };
19
+ }
20
+ /** Collision-safe node key: story ids restart per feature, and two features can share a number. */
21
+ export function nodeKey(featureDir, canonicalId) {
22
+ return `${featureDir}:${canonicalId}`;
23
+ }
24
+ export function buildGraph(features) {
25
+ const sorted = [...features].sort((a, b) => compareFeatureDirs(a.dir, b.dir));
26
+ const nodes = [];
27
+ const byKey = new Map();
28
+ for (const f of sorted) {
29
+ for (const s of f.stories) {
30
+ const key = nodeKey(f.dir, s.id);
31
+ if (byKey.has(key))
32
+ continue; // a story id written twice in one spec: the first one wins
33
+ const node = {
34
+ key,
35
+ id: `${f.displayRef}:${s.displayId}`,
36
+ feature: f.dir,
37
+ ref: f.displayRef,
38
+ story: s.displayId,
39
+ title: s.title,
40
+ status: s.status,
41
+ priority: s.priority,
42
+ };
43
+ nodes.push(node);
44
+ byKey.set(key, node);
45
+ }
46
+ }
47
+ // A `NNN:US-#` reference targets the first feature carrying that number (same rule the dashboard used).
48
+ const featureByRef = new Map();
49
+ for (const f of sorted)
50
+ if (!featureByRef.has(f.ref))
51
+ featureByRef.set(f.ref, f);
52
+ const edges = [];
53
+ const broken = [];
54
+ const done = new Set();
55
+ for (const f of sorted) {
56
+ for (const s of f.stories) {
57
+ const from = nodeKey(f.dir, s.id);
58
+ if (done.has(from))
59
+ continue;
60
+ done.add(from);
61
+ const declared = [
62
+ ["depends-on", s.dependsOn],
63
+ ["blocked-by", s.blockedBy],
64
+ ];
65
+ for (const [kind, deps] of declared) {
66
+ const seen = new Set();
67
+ for (const dep of deps) {
68
+ if (seen.has(dep))
69
+ continue;
70
+ seen.add(dep);
71
+ const { ref, local } = parseDependencyRef(dep);
72
+ const target = ref === null ? f : featureByRef.get(ref);
73
+ if (!target) {
74
+ broken.push({ from, to: dep, kind, reason: "unknown-feature" });
75
+ continue;
76
+ }
77
+ const to = nodeKey(target.dir, local);
78
+ if (byKey.has(to))
79
+ edges.push({ from, to, kind });
80
+ else
81
+ broken.push({ from, to: dep, kind, reason: "unknown-story" });
82
+ }
83
+ }
84
+ }
85
+ }
86
+ return { nodes, edges, featureEdges: featureEdgesOf(edges, byKey), broken, cycles: cyclesOf(nodes, edges) };
87
+ }
88
+ /** FR-002: feature A depends on feature B when a story of A has an edge to a story of B; both kinds count. */
89
+ function featureEdgesOf(edges, byKey) {
90
+ const grouped = new Map();
91
+ for (const e of edges) {
92
+ const from = byKey.get(e.from)?.feature;
93
+ const to = byKey.get(e.to)?.feature;
94
+ if (!from || !to || from === to)
95
+ continue;
96
+ const id = `${from}\u0000${to}`;
97
+ const entry = grouped.get(id) ?? { from, to, via: [] };
98
+ entry.via.push({ ...e });
99
+ grouped.set(id, entry);
100
+ }
101
+ return [...grouped.values()].sort((a, b) => compareFeatureDirs(a.from, b.from) || compareFeatureDirs(a.to, b.to));
102
+ }
103
+ /**
104
+ * FR-005: each dependency cycle as the ordered keys on it (a self-dependency is a cycle of one). One
105
+ * depth-first pass; a back edge to a node still on the stack closes a cycle.
106
+ */
107
+ function cyclesOf(nodes, edges) {
108
+ const next = new Map();
109
+ for (const e of edges) {
110
+ const list = next.get(e.from) ?? [];
111
+ if (!list.includes(e.to))
112
+ list.push(e.to);
113
+ next.set(e.from, list);
114
+ }
115
+ const color = new Map(); // absent = unseen, 1 = on stack, 2 = finished
116
+ const stack = [];
117
+ const cycles = [];
118
+ const known = new Set();
119
+ const visit = (key) => {
120
+ color.set(key, 1);
121
+ stack.push(key);
122
+ for (const to of next.get(key) ?? []) {
123
+ const c = color.get(to);
124
+ if (c === 1) {
125
+ const stories = stack.slice(stack.indexOf(to));
126
+ const id = [...stories].sort().join("\u0000");
127
+ if (!known.has(id)) {
128
+ known.add(id);
129
+ cycles.push({ stories });
130
+ }
131
+ }
132
+ else if (c === undefined) {
133
+ visit(to);
134
+ }
135
+ }
136
+ stack.pop();
137
+ color.set(key, 2);
138
+ };
139
+ for (const n of nodes)
140
+ if (!color.has(n.key))
141
+ visit(n.key);
142
+ return cycles;
143
+ }
144
+ //# sourceMappingURL=graph.js.map