mstro-app 0.3.8 → 0.4.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 (109) hide show
  1. package/LICENSE +191 -21
  2. package/PRIVACY.md +286 -62
  3. package/README.md +81 -58
  4. package/bin/commands/status.js +1 -1
  5. package/dist/server/cli/headless/claude-invoker.d.ts.map +1 -1
  6. package/dist/server/cli/headless/claude-invoker.js +22 -12
  7. package/dist/server/cli/headless/claude-invoker.js.map +1 -1
  8. package/dist/server/cli/headless/headless-logger.d.ts +10 -0
  9. package/dist/server/cli/headless/headless-logger.d.ts.map +1 -0
  10. package/dist/server/cli/headless/headless-logger.js +66 -0
  11. package/dist/server/cli/headless/headless-logger.js.map +1 -0
  12. package/dist/server/cli/headless/mcp-config.d.ts.map +1 -1
  13. package/dist/server/cli/headless/mcp-config.js +6 -5
  14. package/dist/server/cli/headless/mcp-config.js.map +1 -1
  15. package/dist/server/cli/headless/runner.d.ts.map +1 -1
  16. package/dist/server/cli/headless/runner.js +4 -0
  17. package/dist/server/cli/headless/runner.js.map +1 -1
  18. package/dist/server/cli/headless/stall-assessor.d.ts +21 -0
  19. package/dist/server/cli/headless/stall-assessor.d.ts.map +1 -1
  20. package/dist/server/cli/headless/stall-assessor.js +100 -24
  21. package/dist/server/cli/headless/stall-assessor.js.map +1 -1
  22. package/dist/server/cli/headless/tool-watchdog.d.ts +0 -12
  23. package/dist/server/cli/headless/tool-watchdog.d.ts.map +1 -1
  24. package/dist/server/cli/headless/tool-watchdog.js +22 -9
  25. package/dist/server/cli/headless/tool-watchdog.js.map +1 -1
  26. package/dist/server/cli/headless/types.d.ts +8 -1
  27. package/dist/server/cli/headless/types.d.ts.map +1 -1
  28. package/dist/server/cli/improvisation-session-manager.d.ts +16 -0
  29. package/dist/server/cli/improvisation-session-manager.d.ts.map +1 -1
  30. package/dist/server/cli/improvisation-session-manager.js +94 -11
  31. package/dist/server/cli/improvisation-session-manager.js.map +1 -1
  32. package/dist/server/mcp/bouncer-cli.d.ts +3 -0
  33. package/dist/server/mcp/bouncer-cli.d.ts.map +1 -0
  34. package/dist/server/mcp/bouncer-cli.js +54 -0
  35. package/dist/server/mcp/bouncer-cli.js.map +1 -0
  36. package/dist/server/services/plan/composer.d.ts +4 -0
  37. package/dist/server/services/plan/composer.d.ts.map +1 -0
  38. package/dist/server/services/plan/composer.js +181 -0
  39. package/dist/server/services/plan/composer.js.map +1 -0
  40. package/dist/server/services/plan/dependency-resolver.d.ts +28 -0
  41. package/dist/server/services/plan/dependency-resolver.d.ts.map +1 -0
  42. package/dist/server/services/plan/dependency-resolver.js +154 -0
  43. package/dist/server/services/plan/dependency-resolver.js.map +1 -0
  44. package/dist/server/services/plan/executor.d.ts +110 -0
  45. package/dist/server/services/plan/executor.d.ts.map +1 -0
  46. package/dist/server/services/plan/executor.js +641 -0
  47. package/dist/server/services/plan/executor.js.map +1 -0
  48. package/dist/server/services/plan/parser.d.ts +11 -0
  49. package/dist/server/services/plan/parser.d.ts.map +1 -0
  50. package/dist/server/services/plan/parser.js +445 -0
  51. package/dist/server/services/plan/parser.js.map +1 -0
  52. package/dist/server/services/plan/state-reconciler.d.ts +2 -0
  53. package/dist/server/services/plan/state-reconciler.d.ts.map +1 -0
  54. package/dist/server/services/plan/state-reconciler.js +145 -0
  55. package/dist/server/services/plan/state-reconciler.js.map +1 -0
  56. package/dist/server/services/plan/types.d.ts +121 -0
  57. package/dist/server/services/plan/types.d.ts.map +1 -0
  58. package/dist/server/services/plan/types.js +4 -0
  59. package/dist/server/services/plan/types.js.map +1 -0
  60. package/dist/server/services/plan/watcher.d.ts +14 -0
  61. package/dist/server/services/plan/watcher.d.ts.map +1 -0
  62. package/dist/server/services/plan/watcher.js +69 -0
  63. package/dist/server/services/plan/watcher.js.map +1 -0
  64. package/dist/server/services/websocket/file-explorer-handlers.js +20 -0
  65. package/dist/server/services/websocket/file-explorer-handlers.js.map +1 -1
  66. package/dist/server/services/websocket/handler.d.ts.map +1 -1
  67. package/dist/server/services/websocket/handler.js +21 -0
  68. package/dist/server/services/websocket/handler.js.map +1 -1
  69. package/dist/server/services/websocket/plan-handlers.d.ts +6 -0
  70. package/dist/server/services/websocket/plan-handlers.d.ts.map +1 -0
  71. package/dist/server/services/websocket/plan-handlers.js +494 -0
  72. package/dist/server/services/websocket/plan-handlers.js.map +1 -0
  73. package/dist/server/services/websocket/quality-handlers.d.ts.map +1 -1
  74. package/dist/server/services/websocket/quality-handlers.js +384 -12
  75. package/dist/server/services/websocket/quality-handlers.js.map +1 -1
  76. package/dist/server/services/websocket/quality-persistence.d.ts +45 -0
  77. package/dist/server/services/websocket/quality-persistence.d.ts.map +1 -0
  78. package/dist/server/services/websocket/quality-persistence.js +187 -0
  79. package/dist/server/services/websocket/quality-persistence.js.map +1 -0
  80. package/dist/server/services/websocket/quality-service.d.ts +12 -2
  81. package/dist/server/services/websocket/quality-service.d.ts.map +1 -1
  82. package/dist/server/services/websocket/quality-service.js +162 -18
  83. package/dist/server/services/websocket/quality-service.js.map +1 -1
  84. package/dist/server/services/websocket/types.d.ts +2 -2
  85. package/dist/server/services/websocket/types.d.ts.map +1 -1
  86. package/package.json +3 -3
  87. package/server/cli/headless/claude-invoker.ts +25 -12
  88. package/server/cli/headless/headless-logger.ts +78 -0
  89. package/server/cli/headless/mcp-config.ts +6 -5
  90. package/server/cli/headless/runner.ts +4 -0
  91. package/server/cli/headless/stall-assessor.ts +131 -24
  92. package/server/cli/headless/tool-watchdog.ts +10 -9
  93. package/server/cli/headless/types.ts +10 -1
  94. package/server/cli/improvisation-session-manager.ts +118 -11
  95. package/server/mcp/bouncer-cli.ts +73 -0
  96. package/server/services/plan/composer.ts +199 -0
  97. package/server/services/plan/dependency-resolver.ts +182 -0
  98. package/server/services/plan/executor.ts +700 -0
  99. package/server/services/plan/parser.ts +491 -0
  100. package/server/services/plan/state-reconciler.ts +174 -0
  101. package/server/services/plan/types.ts +166 -0
  102. package/server/services/plan/watcher.ts +73 -0
  103. package/server/services/websocket/file-explorer-handlers.ts +20 -0
  104. package/server/services/websocket/handler.ts +21 -0
  105. package/server/services/websocket/plan-handlers.ts +592 -0
  106. package/server/services/websocket/quality-handlers.ts +450 -12
  107. package/server/services/websocket/quality-persistence.ts +250 -0
  108. package/server/services/websocket/quality-service.ts +183 -18
  109. package/server/services/websocket/types.ts +48 -2
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env node
2
+ // Copyright (c) 2025-present Mstro, Inc. All rights reserved.
3
+ // Licensed under the MIT License. See LICENSE file for details.
4
+
5
+ /**
6
+ * Bouncer CLI — stdin/stdout wrapper for Claude Code PreToolUse hooks.
7
+ *
8
+ * Reads a tool use request from stdin (JSON), runs it through the full
9
+ * 2-layer bouncer (pattern matching + Haiku AI), and writes the decision
10
+ * to stdout in the format Claude Code hooks expect.
11
+ *
12
+ * Input format (from Claude Code hook):
13
+ * { "tool_name": "Bash", "input": { "command": "rm -rf /" } }
14
+ *
15
+ * Output format (to Claude Code hook):
16
+ * { "decision": "allow"|"deny", "reason": "..." }
17
+ */
18
+
19
+ import type { BouncerReviewRequest } from './bouncer-integration.js';
20
+ import { reviewOperation } from './bouncer-integration.js';
21
+
22
+ function buildOperation(toolName: string, toolInput: Record<string, unknown>): string {
23
+ const prefix = `${toolName}: `;
24
+ if (toolName === 'Bash' && toolInput.command) return prefix + String(toolInput.command);
25
+ if (toolName === 'Edit' && toolInput.file_path) return prefix + String(toolInput.file_path);
26
+ if (toolName === 'Write' && toolInput.file_path) return prefix + String(toolInput.file_path);
27
+ return prefix + JSON.stringify(toolInput).slice(0, 500);
28
+ }
29
+
30
+ async function evaluate(rawInput: string): Promise<{ decision: string; reason: string }> {
31
+ if (!rawInput.trim()) {
32
+ return { decision: 'allow', reason: 'Empty input' };
33
+ }
34
+
35
+ let parsed: { tool_name?: string; toolName?: string; input?: Record<string, unknown>; toolInput?: Record<string, unknown> };
36
+ try {
37
+ parsed = JSON.parse(rawInput);
38
+ } catch {
39
+ return { decision: 'allow', reason: 'Invalid JSON input' };
40
+ }
41
+
42
+ const toolName = parsed.tool_name || parsed.toolName || 'unknown';
43
+ const toolInput = parsed.input || parsed.toolInput || {};
44
+
45
+ const request: BouncerReviewRequest = {
46
+ operation: buildOperation(toolName, toolInput),
47
+ context: {
48
+ purpose: 'Tool use request from Claude Code hook',
49
+ workingDirectory: process.cwd(),
50
+ toolName,
51
+ toolInput,
52
+ },
53
+ };
54
+
55
+ const result = await reviewOperation(request);
56
+ return {
57
+ decision: result.decision === 'deny' ? 'deny' : 'allow',
58
+ reason: result.reasoning,
59
+ };
60
+ }
61
+
62
+ async function main(): Promise<void> {
63
+ let rawInput = '';
64
+ for await (const chunk of process.stdin) {
65
+ rawInput += chunk;
66
+ }
67
+ const result = await evaluate(rawInput);
68
+ console.log(JSON.stringify(result));
69
+ }
70
+
71
+ main().catch(() => {
72
+ console.log(JSON.stringify({ decision: 'allow', reason: 'Bouncer crash' }));
73
+ });
@@ -0,0 +1,199 @@
1
+ // Copyright (c) 2025-present Mstro, Inc. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file for details.
3
+
4
+ /**
5
+ * Plan Composer — Handles natural language prompts for PPS creation/editing.
6
+ *
7
+ * When a planPrompt message arrives, this builds a context-enriched prompt
8
+ * against the .pm/ (or legacy .plan/) directory and spawns a scoped
9
+ * HeadlessRunner session to execute it.
10
+ */
11
+
12
+ import { existsSync, readFileSync } from 'node:fs';
13
+ import { join } from 'node:path';
14
+ import { runWithFileLogger } from '../../cli/headless/headless-logger.js';
15
+ import { HeadlessRunner, type ToolUseEvent } from '../../cli/headless/index.js';
16
+ import type { HandlerContext } from '../websocket/handler-context.js';
17
+ import type { WSContext } from '../websocket/types.js';
18
+ import { getNextId, parsePlanDirectory, resolvePmDir } from './parser.js';
19
+
20
+ const PROMPT_TOOL_MESSAGES: Record<string, string> = {
21
+ Glob: 'Discovering project files...',
22
+ Read: 'Reading project structure...',
23
+ Grep: 'Searching codebase...',
24
+ Write: 'Creating project files...',
25
+ Edit: 'Updating project files...',
26
+ Bash: 'Running commands...',
27
+ };
28
+
29
+ function getPromptToolCompleteMessage(event: ToolUseEvent): string | null {
30
+ const input = event.completeInput;
31
+ if (!input) return null;
32
+ if (event.toolName === 'Write' && input.file_path) {
33
+ const filename = String(input.file_path).split('/').pop() ?? '';
34
+ return `Created ${filename}`;
35
+ }
36
+ if (event.toolName === 'Edit' && input.file_path) {
37
+ const filename = String(input.file_path).split('/').pop() ?? '';
38
+ return `Updated ${filename}`;
39
+ }
40
+ if (event.toolName === 'Read' && input.file_path) {
41
+ return `Read ${String(input.file_path).split('/').slice(-2).join('/')}`;
42
+ }
43
+ return null;
44
+ }
45
+
46
+ function createPromptProgressTracker() {
47
+ const seenToolStarts = new Set<string>();
48
+
49
+ return (event: ToolUseEvent): string | null => {
50
+ if (event.type === 'tool_start' && event.toolName) {
51
+ if (seenToolStarts.has(event.toolName)) return null;
52
+ seenToolStarts.add(event.toolName);
53
+ return PROMPT_TOOL_MESSAGES[event.toolName] ?? null;
54
+ }
55
+ if (event.type === 'tool_complete') return getPromptToolCompleteMessage(event);
56
+ return null;
57
+ };
58
+ }
59
+
60
+ function readFileOrEmpty(path: string): string {
61
+ try {
62
+ if (existsSync(path)) return readFileSync(path, 'utf-8');
63
+ } catch { /* skip */ }
64
+ return '';
65
+ }
66
+
67
+ export async function handlePlanPrompt(
68
+ ctx: HandlerContext,
69
+ ws: WSContext,
70
+ userPrompt: string,
71
+ workingDir: string,
72
+ ): Promise<void> {
73
+ const pmDir = resolvePmDir(workingDir) ?? join(workingDir, '.pm');
74
+ const stateContent = readFileOrEmpty(join(pmDir, 'STATE.md'));
75
+ const projectContent = readFileOrEmpty(join(pmDir, 'project.md'));
76
+
77
+ // Compute next available IDs
78
+ const fullState = parsePlanDirectory(workingDir);
79
+ let idInfo = '';
80
+ if (fullState) {
81
+ const nextIS = getNextId(fullState.issues, 'IS');
82
+ const nextBG = getNextId(fullState.issues, 'BG');
83
+ const nextEP = getNextId(fullState.issues, 'EP');
84
+ idInfo = `Next available IDs: ${nextIS}, ${nextBG}, ${nextEP}`;
85
+ }
86
+
87
+ // Read existing epic files to provide context
88
+ let epicContext = '';
89
+ if (fullState) {
90
+ const existingEpics = fullState.issues.filter((i: { type: string }) => i.type === 'epic');
91
+ if (existingEpics.length > 0) {
92
+ epicContext = `\nExisting epics:\n${existingEpics.map((e: { id: string; title: string; path: string; children: string[] }) => `- ${e.id}: ${e.title} (${e.path}, children: ${e.children.length})`).join('\n')}\n`;
93
+ }
94
+ }
95
+
96
+ const enrichedPrompt = `You are managing a project in the .pm/ directory format (Project Plan Spec).
97
+ The project's current state is:
98
+
99
+ <state>
100
+ ${stateContent || 'No STATE.md exists yet'}
101
+ </state>
102
+
103
+ <project>
104
+ ${projectContent || 'No project.md yet'}
105
+ </project>
106
+
107
+ ${idInfo}
108
+ ${epicContext}
109
+
110
+ Follow these rules:
111
+ - When creating .pm/ files, use YAML front matter + markdown body
112
+ - When modifying issues, preserve all existing YAML fields you don't change
113
+ - After any state change, update STATE.md to reflect the new status
114
+ - Use the next available ID for new entities
115
+ - Respond briefly describing what you did
116
+
117
+ Issue scoping rules (critical for execution quality):
118
+ - Each issue is executed by a single AI agent with its own context window
119
+ - Issues estimated at 1-3 story points execute well (focused, single concern)
120
+ - Issues at 5 story points are viable if scoped to one subsystem
121
+ - Issues at 8+ story points MUST be decomposed into smaller sub-issues
122
+ - Issues at 13+ story points MUST become an epic with child issues
123
+ - Each issue should touch one logical concern (one component, one service, one data flow)
124
+ - If an issue requires work across multiple subsystems, split it into one issue per subsystem with blocked_by edges between them
125
+ - Research/investigation issues should be separate from implementation issues
126
+
127
+ Epic creation rules (when user asks for a feature with sub-tasks or an epic):
128
+ - Create an EP-*.md file in .pm/backlog/ with type: epic and a children: [] field in front matter
129
+ - Create individual IS-*.md (or BG-*.md) files for each child issue
130
+ - Each child issue must have epic: backlog/EP-XXX.md in its front matter
131
+ - The epic's children field must list all child paths: [backlog/IS-001.md, backlog/IS-002.md, ...]
132
+ - Set blocked_by between child issues where there are natural dependencies
133
+ - Give each child issue clear acceptance criteria and files to modify when possible
134
+ - Set appropriate priorities (P0-P3) based on the issue's importance within the epic
135
+
136
+ User request: ${userPrompt}`;
137
+
138
+ try {
139
+ ctx.broadcastToAll({
140
+ type: 'planPromptProgress',
141
+ data: { message: 'Starting project planning...' },
142
+ });
143
+
144
+ const runner = new HeadlessRunner({
145
+ workingDir,
146
+ directPrompt: enrichedPrompt,
147
+ outputCallback: (text: string) => {
148
+ ctx.send(ws, {
149
+ type: 'planPromptStreaming',
150
+ data: { token: text },
151
+ });
152
+ },
153
+ toolUseCallback: (() => {
154
+ const getProgressMessage = createPromptProgressTracker();
155
+ return (event: ToolUseEvent) => {
156
+ const message = getProgressMessage(event);
157
+ if (message) {
158
+ ctx.broadcastToAll({
159
+ type: 'planPromptProgress',
160
+ data: { message },
161
+ });
162
+ }
163
+ };
164
+ })(),
165
+ });
166
+
167
+ ctx.broadcastToAll({
168
+ type: 'planPromptProgress',
169
+ data: { message: 'Claude is planning your project...' },
170
+ });
171
+
172
+ const result = await runWithFileLogger('pm-compose', () => runner.run());
173
+
174
+ ctx.broadcastToAll({
175
+ type: 'planPromptProgress',
176
+ data: { message: 'Finalizing project plan...' },
177
+ });
178
+
179
+ ctx.send(ws, {
180
+ type: 'planPromptResponse',
181
+ data: {
182
+ response: result.completed ? 'Prompt executed successfully.' : (result.error || 'Unknown error'),
183
+ success: result.completed,
184
+ error: result.error || null,
185
+ },
186
+ });
187
+
188
+ // Re-parse and broadcast updated state
189
+ const updatedState = parsePlanDirectory(workingDir);
190
+ if (updatedState) {
191
+ ctx.broadcastToAll({ type: 'planStateUpdated', data: updatedState });
192
+ }
193
+ } catch (error) {
194
+ ctx.send(ws, {
195
+ type: 'planError',
196
+ data: { error: error instanceof Error ? error.message : String(error) },
197
+ });
198
+ }
199
+ }
@@ -0,0 +1,182 @@
1
+ // Copyright (c) 2025-present Mstro, Inc. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file for details.
3
+
4
+ /**
5
+ * Dependency Resolver — Validates and computes the dependency DAG.
6
+ *
7
+ * Builds adjacency list from blocked_by/blocks fields, detects cycles,
8
+ * and computes the "ready to work" set.
9
+ */
10
+
11
+ import type { Issue } from './types.js';
12
+
13
+ /**
14
+ * Detect cycles in the dependency graph.
15
+ * Returns the first cycle found as an array of issue IDs, or null if acyclic.
16
+ */
17
+ export function detectCycles(issues: Issue[]): string[] | null {
18
+ const issueByPath = new Map<string, Issue>();
19
+ for (const issue of issues) {
20
+ issueByPath.set(issue.path, issue);
21
+ }
22
+
23
+ // DFS with coloring: 0=white, 1=gray, 2=black
24
+ const color = new Map<string, number>();
25
+ const parent = new Map<string, string>();
26
+
27
+ for (const issue of issues) {
28
+ color.set(issue.path, 0);
29
+ }
30
+
31
+ for (const issue of issues) {
32
+ if (color.get(issue.path) === 0) {
33
+ const cycle = dfs(issue.path, issueByPath, color, parent);
34
+ if (cycle) return cycle;
35
+ }
36
+ }
37
+
38
+ return null;
39
+ }
40
+
41
+ function dfs(
42
+ path: string,
43
+ issueByPath: Map<string, Issue>,
44
+ color: Map<string, number>,
45
+ parent: Map<string, string>,
46
+ ): string[] | null {
47
+ color.set(path, 1); // Gray
48
+ const issue = issueByPath.get(path);
49
+ if (!issue) {
50
+ color.set(path, 2);
51
+ return null;
52
+ }
53
+
54
+ for (const dep of issue.blocks) {
55
+ if (!issueByPath.has(dep)) continue;
56
+ const depColor = color.get(dep);
57
+ if (depColor === 1) {
58
+ // Found cycle — reconstruct
59
+ const cycle = [dep, path];
60
+ let cur = path;
61
+ while (parent.has(cur) && parent.get(cur) !== dep) {
62
+ cur = parent.get(cur)!;
63
+ cycle.push(cur);
64
+ }
65
+ return cycle.map(p => issueByPath.get(p)?.id || p);
66
+ }
67
+ if (depColor === 0) {
68
+ parent.set(dep, path);
69
+ const cycle = dfs(dep, issueByPath, color, parent);
70
+ if (cycle) return cycle;
71
+ }
72
+ }
73
+
74
+ color.set(path, 2); // Black
75
+ return null;
76
+ }
77
+
78
+ /**
79
+ * Compute the set of issues that are ready to work on.
80
+ * An issue is ready if:
81
+ * - It's not an epic
82
+ * - Its status is backlog or todo (not started, done, or cancelled)
83
+ * - All its blocked_by items are done or cancelled
84
+ *
85
+ * If epicScope is provided, only returns issues belonging to that epic.
86
+ */
87
+ export function resolveReadyToWork(issues: Issue[], epicScope?: string): Issue[] {
88
+ const issueByPath = new Map<string, Issue>();
89
+ for (const issue of issues) {
90
+ issueByPath.set(issue.path, issue);
91
+ }
92
+
93
+ const readyStatuses = new Set(['backlog', 'todo']);
94
+ const doneStatuses = new Set(['done', 'cancelled']);
95
+
96
+ const priorityOrder: Record<string, number> = { P0: 0, P1: 1, P2: 2, P3: 3 };
97
+
98
+ // Build set of child paths for epic scoping
99
+ let epicChildPaths: Set<string> | null = null;
100
+ if (epicScope) {
101
+ const epic = issueByPath.get(epicScope);
102
+ if (epic) {
103
+ epicChildPaths = new Set(epic.children);
104
+ // Also include issues that reference this epic via their epic field
105
+ for (const issue of issues) {
106
+ if (issue.epic === epicScope) epicChildPaths.add(issue.path);
107
+ }
108
+ }
109
+ }
110
+
111
+ return issues
112
+ .filter(issue => {
113
+ if (issue.type === 'epic') return false;
114
+ if (!readyStatuses.has(issue.status)) return false;
115
+
116
+ // If scoped to an epic, only include that epic's children
117
+ if (epicChildPaths && !epicChildPaths.has(issue.path)) return false;
118
+
119
+ // Check all blockers are resolved
120
+ if (issue.blockedBy.length > 0) {
121
+ const allResolved = issue.blockedBy.every(bp => {
122
+ const blocker = issueByPath.get(bp);
123
+ return blocker && doneStatuses.has(blocker.status);
124
+ });
125
+ if (!allResolved) return false;
126
+ }
127
+
128
+ return true;
129
+ })
130
+ .sort((a, b) => {
131
+ // Sort by priority (P0 first)
132
+ return (priorityOrder[a.priority] ?? 9) - (priorityOrder[b.priority] ?? 9);
133
+ });
134
+ }
135
+
136
+ /**
137
+ * Compute the critical path through incomplete issues.
138
+ * Returns the longest chain of dependent issues.
139
+ */
140
+ export function computeCriticalPath(issues: Issue[]): Issue[] {
141
+ const issueByPath = new Map<string, Issue>();
142
+ for (const issue of issues) {
143
+ issueByPath.set(issue.path, issue);
144
+ }
145
+
146
+ const doneStatuses = new Set(['done', 'cancelled']);
147
+ const incompleteIssues = issues.filter(i => !doneStatuses.has(i.status) && i.type !== 'epic');
148
+
149
+ // Compute longest path using DFS with memoization
150
+ const longestFrom = new Map<string, Issue[]>();
151
+
152
+ function getLongest(path: string): Issue[] {
153
+ if (longestFrom.has(path)) return longestFrom.get(path)!;
154
+
155
+ const issue = issueByPath.get(path);
156
+ if (!issue || doneStatuses.has(issue.status)) {
157
+ longestFrom.set(path, []);
158
+ return [];
159
+ }
160
+
161
+ // Set sentinel before recursing to break cycles
162
+ longestFrom.set(path, [issue]);
163
+
164
+ let best: Issue[] = [];
165
+ for (const dep of issue.blocks) {
166
+ const sub = getLongest(dep);
167
+ if (sub.length > best.length) best = sub;
168
+ }
169
+
170
+ const result = [issue, ...best];
171
+ longestFrom.set(path, result);
172
+ return result;
173
+ }
174
+
175
+ let criticalPath: Issue[] = [];
176
+ for (const issue of incompleteIssues) {
177
+ const path = getLongest(issue.path);
178
+ if (path.length > criticalPath.length) criticalPath = path;
179
+ }
180
+
181
+ return criticalPath;
182
+ }