claude-code-session-manager 0.86.0 → 0.87.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 (128) hide show
  1. package/dist/assets/AgentLibrary-DyLWzZDf.js +3 -0
  2. package/dist/assets/{DataModel-Q4jhl24R.js → DataModel--mISIJ6h.js} +1 -1
  3. package/dist/assets/{History-Cj2FejEo.js → History-C2ahUXTg.js} +2 -2
  4. package/dist/assets/{Hooks-CaelQI6t.js → Hooks-BiC6oyR2.js} +3 -3
  5. package/dist/assets/{HostBilko--v7cMR8I.js → HostBilko-BPleEOld.js} +1 -1
  6. package/dist/assets/{Library-DgI9oCCZ.js → Library-Dc8Qst1R.js} +1 -1
  7. package/dist/assets/{ListDetail-DYUZN-x-.js → ListDetail-DIXh-OLX.js} +1 -1
  8. package/dist/assets/MarkdownEditor-C90bkLXK.js +1 -0
  9. package/dist/assets/{McpServers-ypCYURh3.js → McpServers-DqcbLOLZ.js} +2 -2
  10. package/dist/assets/{Memory-C2qYp-3M.js → Memory-CW62MXlh.js} +4 -4
  11. package/dist/assets/{Panel-Cj2kw-Zv.js → Panel-Bw1FhRuF.js} +1 -1
  12. package/dist/assets/Permissions-BcUC-5y8.js +3 -0
  13. package/dist/assets/{Plugins-C1Vj8_dU.js → Plugins-BnKx9flD.js} +2 -2
  14. package/dist/assets/{ProvenanceBadge-DczPNM5U.js → ProvenanceBadge-Bw5vNVPT.js} +1 -1
  15. package/dist/assets/{SaveBar-Cd_7U6Gb.js → SaveBar-CWr0O_w-.js} +1 -1
  16. package/dist/assets/Scheduler-DYdLuUqq.js +14 -0
  17. package/dist/assets/{ScopeSwitcher-DVSyI44-.js → ScopeSwitcher-CrBLbg8s.js} +1 -1
  18. package/dist/assets/Settings-DluB-vN1.js +3 -0
  19. package/dist/assets/{SkillReferenceGraph-CUv1_Q2c.js → SkillReferenceGraph-CHLSseay.js} +1 -1
  20. package/dist/assets/{Skills-C_YHkAy-.js → Skills-gNdo_HNK.js} +2 -2
  21. package/dist/assets/{SystemPrompt-B8R7T9xn.js → SystemPrompt-Cru05-Ia.js} +1 -1
  22. package/dist/assets/{TagLibrary-dj9YHWyy.js → TagLibrary-DNHY0xou.js} +1 -1
  23. package/dist/assets/{TiptapBody-DnSBUjHE.js → TiptapBody-I4lmbCgP.js} +1 -1
  24. package/dist/assets/{Toggle-CjV_BJn6.js → Toggle-bWMHjmRh.js} +1 -1
  25. package/dist/assets/{index-CDo9xBR9.css → index-DV3PorRY.css} +1 -1
  26. package/dist/assets/{index-CXFQIPhO.js → index-fc_JjdxL.js} +724 -724
  27. package/dist/assets/settingsSchema-BfhtZnGD.js +3 -0
  28. package/dist/index.html +2 -2
  29. package/package.json +14 -14
  30. package/plugins/CLAUDE.md +61 -0
  31. package/plugins/session-manager-dev/.claude-plugin/plugin.json +1 -1
  32. package/plugins/session-manager-dev/skills/builder/4-manual/SKILL.md +1 -1
  33. package/plugins/session-manager-dev/skills/ops-sweep/SKILL.md +1 -1
  34. package/scripts/scheduler-mcp-server.cjs +7 -0
  35. package/src/main/__tests__/agentModelResolve.test.cjs +100 -9
  36. package/src/main/__tests__/broadcastCoalescer.test.cjs +18 -0
  37. package/src/main/__tests__/epicMint.test.cjs +2 -2
  38. package/src/main/__tests__/health-delegation-chain.test.cjs +2 -1
  39. package/src/main/__tests__/needsReviewLedger.test.cjs +162 -0
  40. package/src/main/__tests__/opsErrorLogTelemetryTap.test.cjs +3 -3
  41. package/src/main/__tests__/pollLoop-dispatch-on-failure.test.cjs +15 -1
  42. package/src/main/__tests__/prdCreateDisposition.test.cjs +201 -0
  43. package/src/main/__tests__/prdFrontmatterDisposition.test.cjs +125 -0
  44. package/src/main/__tests__/prdLocations.test.cjs +100 -2
  45. package/src/main/__tests__/prdLocationsArchived.test.cjs +43 -1
  46. package/src/main/__tests__/prdSetDisposition.test.cjs +222 -0
  47. package/src/main/__tests__/queue-health-verdict.test.cjs +170 -0
  48. package/src/main/__tests__/queue-starvation-per-project.test.cjs +14 -2
  49. package/src/main/__tests__/queueHistory.test.cjs +63 -0
  50. package/src/main/__tests__/reconcileTiming.test.cjs +135 -0
  51. package/src/main/__tests__/scheduleJobTransitions.test.cjs +101 -1
  52. package/src/main/__tests__/scheduler-boot-orphans.test.cjs +2 -2
  53. package/src/main/__tests__/scheduler-broadcast-reconcile.test.cjs +121 -0
  54. package/src/main/__tests__/scheduler-cross-project-batch.test.cjs +43 -0
  55. package/src/main/__tests__/scheduler-guard-verdict-autoresolve.test.cjs +344 -0
  56. package/src/main/__tests__/scheduler-job-budget.test.cjs +172 -0
  57. package/src/main/__tests__/scheduler-looks-done.test.cjs +93 -3
  58. package/src/main/__tests__/scheduler-porcelain-rename.test.cjs +164 -0
  59. package/src/main/__tests__/scheduler-reap-dead-running-jobs.test.cjs +160 -0
  60. package/src/main/__tests__/scheduler-reaper-helpers-basics.test.cjs +87 -0
  61. package/src/main/__tests__/scheduler-shared-tree-guard.test.cjs +88 -0
  62. package/src/main/__tests__/scheduler-starve-escalation.test.cjs +14 -4
  63. package/src/main/__tests__/scheduler-worktree-cap-defer.test.cjs +14 -0
  64. package/src/main/chatRunner.cjs +8 -5
  65. package/src/main/health.cjs +1 -1
  66. package/src/main/historyAggregator.cjs +5 -0
  67. package/src/main/index.cjs +95 -44
  68. package/src/main/ipcSchemas.cjs +47 -0
  69. package/src/main/lib/__tests__/active-sessions.test.cjs +251 -0
  70. package/src/main/lib/__tests__/bootSelfHeal.test.cjs +107 -0
  71. package/src/main/lib/__tests__/delegationReadiness.test.cjs +322 -43
  72. package/src/main/lib/__tests__/effectiveModelInfo.test.cjs +239 -0
  73. package/src/main/lib/__tests__/gitWorktree.test.cjs +89 -0
  74. package/src/main/lib/__tests__/guardShims.test.cjs +151 -0
  75. package/src/main/lib/__tests__/opsRootAbsoluteCwd.test.cjs +5 -5
  76. package/src/main/lib/__tests__/prdDisposition.test.cjs +224 -0
  77. package/src/main/lib/__tests__/reaperHelpers.test.cjs +179 -1
  78. package/src/main/lib/__tests__/usageCircuit.test.cjs +224 -0
  79. package/src/main/lib/__tests__/watchdog-helpers.test.cjs +312 -0
  80. package/src/main/lib/__tests__/watchdog-relaunch.test.cjs +193 -0
  81. package/{scripts → src/main}/lib/activeSessions.cjs +50 -4
  82. package/src/main/lib/agentModelResolve.cjs +65 -27
  83. package/src/main/lib/bootSelfHeal.cjs +88 -0
  84. package/src/main/lib/delegationReadiness.cjs +290 -225
  85. package/src/main/lib/effectiveModelInfo.cjs +333 -0
  86. package/src/main/lib/ephemeralCwd.cjs +1 -1
  87. package/src/main/lib/epicMint.cjs +3 -3
  88. package/src/main/lib/gitWorktree.cjs +42 -12
  89. package/src/main/lib/guardShims.cjs +156 -0
  90. package/src/main/lib/jobDirtFilter.cjs +7 -2
  91. package/src/main/lib/launchFailure.cjs +2 -1
  92. package/src/main/lib/mcpToolCatalog.cjs +4 -1
  93. package/src/main/lib/needsReviewLedger.cjs +205 -0
  94. package/src/main/lib/opsErrorLog.cjs +1 -1
  95. package/src/main/lib/opsOwnership.cjs +1 -1
  96. package/src/main/lib/prdCreate.cjs +56 -1
  97. package/src/main/lib/prdDisposition.cjs +199 -0
  98. package/src/main/lib/prdFrontmatter.cjs +8 -2
  99. package/src/main/lib/prdLocations.cjs +167 -45
  100. package/src/main/lib/projectHomeAdminRoutes.cjs +4 -4
  101. package/src/main/lib/projectPageSummarySchema.cjs +1 -1
  102. package/src/main/lib/projectRootResolve.cjs +1 -1
  103. package/src/main/lib/queueHistory.cjs +19 -1
  104. package/src/main/lib/queueStore.cjs +6 -1
  105. package/src/main/lib/reaperHelpers.cjs +181 -15
  106. package/src/main/lib/scheduleJobSchema.cjs +8 -0
  107. package/src/main/lib/scheduleJobTransitions.cjs +33 -0
  108. package/src/main/lib/schedulerConfig.cjs +24 -0
  109. package/src/main/lib/usageCircuit.cjs +159 -0
  110. package/{scripts → src/main}/lib/watchdogHelpers.cjs +1 -1
  111. package/src/main/scheduler/prdParser.cjs +13 -0
  112. package/src/main/scheduler.cjs +1285 -143
  113. package/src/main/templates/PRD_AUTHORING.md +50 -0
  114. package/src/main/templates/project-pages-catalog.json +1 -1
  115. package/src/main/usage.cjs +21 -3
  116. package/src/preload/api.d.ts +92 -1
  117. package/src/preload/index.cjs +10 -0
  118. package/web/README.md +41 -0
  119. package/{scripts/render-project-pages.cjs → web/project-pages/render.cjs} +4 -4
  120. package/{scripts/render-project-pages → web/project-pages/renderer}/dist/renderer.cjs +1 -1
  121. package/{scripts/validate-project-pages-summary.cjs → web/project-pages/validate-summary.cjs} +5 -5
  122. package/dist/assets/AgentLibrary-DTFL7y8G.js +0 -3
  123. package/dist/assets/MarkdownEditor-DCIubYWf.js +0 -1
  124. package/dist/assets/Permissions-BiYZNGYW.js +0 -3
  125. package/dist/assets/Scheduler-DcLBiJBq.js +0 -14
  126. package/dist/assets/Settings-Cv-pRyms.js +0 -3
  127. package/dist/assets/settingsSchema-BJVciriw.js +0 -3
  128. /package/{scripts/project-pages-logic → web/project-pages/logic}/dist/logic.cjs +0 -0
@@ -0,0 +1,201 @@
1
+ /**
2
+ * prdCreateDisposition.test.cjs — scheduler wave-disposition PRD:
3
+ * createPrd()'s authoring-time disposition gate. A PRD landing as a wave
4
+ * ROOT (no dependsOn of its own) in an Epic that already has incomplete
5
+ * PRDs must record an explicit 'append'/'new-head' choice — required for an
6
+ * interactive caller, defaulted (and logged) for a non-interactive one.
7
+ *
8
+ * Run: timeout 120 npx vitest run src/main/__tests__/prdCreateDisposition.test.cjs
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import { test, expect, beforeEach, afterEach } from 'vitest';
14
+ const fsp = require('node:fs/promises');
15
+ const os = require('node:os');
16
+ const path = require('node:path');
17
+ const config = require('../config.cjs');
18
+ const prdParser = require('../scheduler/prdParser.cjs');
19
+ const { parsePrdFile } = require('../lib/prdFrontmatter.cjs');
20
+ const { createPrd } = require('../lib/prdCreate.cjs');
21
+
22
+ async function mkTmpPrdsDir() {
23
+ const root = await fsp.mkdtemp(path.join(os.tmpdir(), 'sm-disposition-create-prd-'));
24
+ config.addAllowedRoot(root);
25
+ const prdsDir = path.join(root, '.claude', 'prds');
26
+ await fsp.mkdir(prdsDir, { recursive: true });
27
+ return prdsDir;
28
+ }
29
+
30
+ /** Minimal fake `remote` — same shape as prdCreate.test.cjs's
31
+ * makeFakeRemoteWithPrdsDir, but listPrds also reports sourcePromptId/
32
+ * dependsOn (read back from each seeded file's real frontmatter) and
33
+ * status (from the test-supplied statusBySlug map) — the fields
34
+ * createPrd()'s disposition gate actually reads. */
35
+ function makeFakeRemote(prdsDir, statusBySlug = {}) {
36
+ return {
37
+ async allocateParallelGroup() {
38
+ return prdParser.allocateParallelGroup(prdsDir);
39
+ },
40
+ async readPrd(slug) {
41
+ try {
42
+ const text = await fsp.readFile(path.join(prdsDir, `${slug}.md`), 'utf8');
43
+ return { ok: true, text };
44
+ } catch (e) {
45
+ return { ok: false, error: e?.message };
46
+ }
47
+ },
48
+ async writePrd(slug, body) {
49
+ try {
50
+ const filePath = path.join(prdsDir, `${slug}.md`);
51
+ await config.writeTextAtomic(filePath, body, { writer: 'scheduler' });
52
+ return { ok: true };
53
+ } catch (e) {
54
+ return { ok: false, error: e?.message };
55
+ }
56
+ },
57
+ async listPrds() {
58
+ const entries = await fsp.readdir(prdsDir);
59
+ const prds = [];
60
+ for (const f of entries) {
61
+ if (!f.endsWith('.md')) continue;
62
+ const slug = f.slice(0, -3);
63
+ const raw = await fsp.readFile(path.join(prdsDir, f), 'utf8');
64
+ const { frontmatter } = parsePrdFile(raw);
65
+ prds.push({
66
+ slug,
67
+ sourcePromptId: frontmatter.sourcePromptId ?? null,
68
+ dependsOn: frontmatter.dependsOn ?? null,
69
+ status: statusBySlug[slug] ?? null,
70
+ });
71
+ }
72
+ return { prds, total: prds.length, limit: prds.length, offset: 0, hasMore: false };
73
+ },
74
+ };
75
+ }
76
+
77
+ /** Seeds a fixture PRD file directly (bypassing createPrd) at an explicit,
78
+ * already-NN-prefixed slug, so tests can assert dependsOn against a known
79
+ * name instead of a freshly-allocated one. */
80
+ async function seedPrd(prdsDir, slug, { sourcePromptId, dependsOn } = {}) {
81
+ const lines = ['---', 'title: seed', `sourcePromptId: ${sourcePromptId}`];
82
+ if (dependsOn && dependsOn.length) lines.push(`dependsOn: [${dependsOn.join(', ')}]`);
83
+ lines.push('---', '# Goal', '', 'seed body', '');
84
+ await fsp.writeFile(path.join(prdsDir, `${slug}.md`), lines.join('\n'));
85
+ }
86
+
87
+ function validBody(overrides = {}) {
88
+ return {
89
+ title: 'Wave 2 root',
90
+ cwd: os.homedir(),
91
+ estimateMinutes: 10,
92
+ goal: 'Do the follow-on work.',
93
+ acceptanceCriteria: ['a'],
94
+ implementationNotes: 'n',
95
+ ...overrides,
96
+ };
97
+ }
98
+
99
+ // This suite's own process may itself be running as a scheduled job (this
100
+ // file's disposition gate keys directly off SM_SCHEDULER_JOB_SLUG — see
101
+ // createPrd()) — never assume it's unset going in; each test controls it
102
+ // explicitly.
103
+ const originalJobSlug = process.env.SM_SCHEDULER_JOB_SLUG;
104
+ beforeEach(() => {
105
+ delete process.env.SM_SCHEDULER_JOB_SLUG;
106
+ });
107
+ afterEach(() => {
108
+ if (originalJobSlug === undefined) delete process.env.SM_SCHEDULER_JOB_SLUG;
109
+ else process.env.SM_SCHEDULER_JOB_SLUG = originalJobSlug;
110
+ });
111
+
112
+ test('refuses to write, without a disposition, a new root joining an Epic that already has an incomplete PRD', async () => {
113
+ const prdsDir = await mkTmpPrdsDir();
114
+ await seedPrd(prdsDir, '1-wave1-tail', { sourcePromptId: 'epic-1' });
115
+ const remote = makeFakeRemote(prdsDir, { '1-wave1-tail': 'pending' });
116
+
117
+ const result = await createPrd(validBody({ sourcePromptId: 'epic-1' }), remote);
118
+
119
+ expect(result.ok).toBe(false);
120
+ expect(result.status).toBe(400);
121
+ expect(result.error).toMatch(/disposition/i);
122
+ });
123
+
124
+ test('disposition: append attaches the new root behind the Epic\'s current terminal PRD', async () => {
125
+ const prdsDir = await mkTmpPrdsDir();
126
+ await seedPrd(prdsDir, '1-wave1-tail', { sourcePromptId: 'epic-1' });
127
+ const remote = makeFakeRemote(prdsDir, { '1-wave1-tail': 'pending' });
128
+
129
+ const result = await createPrd(validBody({ sourcePromptId: 'epic-1', disposition: 'append' }), remote);
130
+
131
+ expect(result.ok).toBe(true);
132
+ const written = await fsp.readFile(path.join(prdsDir, result.filename), 'utf8');
133
+ expect(written).toMatch(/^dependsOn: \[1-wave1-tail\]$/m);
134
+ expect(written).toMatch(/^disposition: append$/m);
135
+ });
136
+
137
+ test('disposition: new-head leaves the new root independent (no dependsOn)', async () => {
138
+ const prdsDir = await mkTmpPrdsDir();
139
+ await seedPrd(prdsDir, '1-wave1-tail', { sourcePromptId: 'epic-1' });
140
+ const remote = makeFakeRemote(prdsDir, { '1-wave1-tail': 'pending' });
141
+
142
+ const result = await createPrd(validBody({ sourcePromptId: 'epic-1', disposition: 'new-head' }), remote);
143
+
144
+ expect(result.ok).toBe(true);
145
+ const written = await fsp.readFile(path.join(prdsDir, result.filename), 'utf8');
146
+ expect(written).not.toContain('dependsOn');
147
+ expect(written).toMatch(/^disposition: new-head$/m);
148
+ });
149
+
150
+ test('a non-interactive caller (SM_SCHEDULER_JOB_SLUG set) with no disposition defaults to append, logged as a default', async () => {
151
+ const prdsDir = await mkTmpPrdsDir();
152
+ await seedPrd(prdsDir, '1-wave1-tail', { sourcePromptId: 'epic-1' });
153
+ const remote = makeFakeRemote(prdsDir, { '1-wave1-tail': 'pending' });
154
+ process.env.SM_SCHEDULER_JOB_SLUG = 'some-running-job';
155
+
156
+ const result = await createPrd(validBody({ sourcePromptId: 'epic-1' }), remote);
157
+
158
+ expect(result.ok).toBe(true);
159
+ const written = await fsp.readFile(path.join(prdsDir, result.filename), 'utf8');
160
+ expect(written).toMatch(/^dependsOn: \[1-wave1-tail\]$/m);
161
+ expect(written).toMatch(/^disposition: append$/m);
162
+ });
163
+
164
+ test('no disposition required for the first PRD in a brand-new Epic', async () => {
165
+ const prdsDir = await mkTmpPrdsDir();
166
+ const remote = makeFakeRemote(prdsDir);
167
+
168
+ const result = await createPrd(validBody({ sourcePromptId: 'epic-empty' }), remote);
169
+
170
+ expect(result.ok).toBe(true);
171
+ const written = await fsp.readFile(path.join(prdsDir, result.filename), 'utf8');
172
+ expect(written).not.toContain('disposition');
173
+ });
174
+
175
+ test('no disposition required when every existing PRD in the Epic is already completed', async () => {
176
+ const prdsDir = await mkTmpPrdsDir();
177
+ await seedPrd(prdsDir, '1-wave1-tail', { sourcePromptId: 'epic-done' });
178
+ const remote = makeFakeRemote(prdsDir, { '1-wave1-tail': 'completed' });
179
+
180
+ const result = await createPrd(validBody({ sourcePromptId: 'epic-done' }), remote);
181
+
182
+ expect(result.ok).toBe(true);
183
+ const written = await fsp.readFile(path.join(prdsDir, result.filename), 'utf8');
184
+ expect(written).not.toContain('disposition');
185
+ });
186
+
187
+ test('an explicit dependsOn bypasses the disposition gate entirely', async () => {
188
+ const prdsDir = await mkTmpPrdsDir();
189
+ await seedPrd(prdsDir, '1-wave1-tail', { sourcePromptId: 'epic-1' });
190
+ const remote = makeFakeRemote(prdsDir, { '1-wave1-tail': 'pending' });
191
+
192
+ const result = await createPrd(
193
+ validBody({ sourcePromptId: 'epic-1', dependsOn: ['1-wave1-tail'] }),
194
+ remote,
195
+ );
196
+
197
+ expect(result.ok).toBe(true);
198
+ const written = await fsp.readFile(path.join(prdsDir, result.filename), 'utf8');
199
+ expect(written).toMatch(/^dependsOn: \[1-wave1-tail\]$/m);
200
+ expect(written).not.toContain('disposition');
201
+ });
@@ -0,0 +1,125 @@
1
+ /**
2
+ * prdFrontmatterDisposition.test.cjs — scheduler wave-disposition PRD:
3
+ * `disposition` ('append' | 'new-head') is a recognized, patchable PRD
4
+ * frontmatter key, same pattern as `dependsOn` in
5
+ * prdFrontmatterDependsOn.test.cjs. It records the authoring-time decision
6
+ * for a wave's root PRD: does it extend the Epic's existing dependsOn chain
7
+ * (append) or start an independent, parallel-eligible chain (new-head)?
8
+ *
9
+ * Run: timeout 120 npx vitest run src/main/__tests__/prdFrontmatterDisposition.test.cjs
10
+ */
11
+
12
+ 'use strict';
13
+
14
+ import { test, expect } from 'vitest';
15
+
16
+ const { parsePrdFile, serializePrdFile, RECOGNIZED_KEYS } = require('../lib/prdFrontmatter.cjs');
17
+
18
+ test('disposition is a recognized key', () => {
19
+ expect(RECOGNIZED_KEYS.has('disposition')).toBe(true);
20
+ });
21
+
22
+ test('parsePrdFile parses disposition: append and round-trips it byte-identically when unedited', () => {
23
+ const raw = [
24
+ '---',
25
+ 'title: A second wave',
26
+ 'cwd: ~/Projects/session-manager',
27
+ 'estimateMinutes: 20',
28
+ 'dependsOn: [widget-base]',
29
+ 'disposition: append',
30
+ '---',
31
+ '# Goal',
32
+ '',
33
+ 'Extend the chain.',
34
+ '',
35
+ ].join('\n');
36
+
37
+ const { frontmatter, body } = parsePrdFile(raw);
38
+ expect(frontmatter.disposition).toBe('append');
39
+ expect(serializePrdFile(frontmatter, body)).toBe(raw);
40
+ });
41
+
42
+ test('parsePrdFile parses disposition: new-head', () => {
43
+ const raw = [
44
+ '---',
45
+ 'title: A parallel wave',
46
+ 'cwd: ~/Projects/session-manager',
47
+ 'estimateMinutes: 20',
48
+ 'disposition: new-head',
49
+ '---',
50
+ 'body',
51
+ '',
52
+ ].join('\n');
53
+
54
+ const { frontmatter } = parsePrdFile(raw);
55
+ expect(frontmatter.disposition).toBe('new-head');
56
+ });
57
+
58
+ test('parsePrdFile leaves disposition unset for an unrecognized value rather than throwing', () => {
59
+ const raw = [
60
+ '---',
61
+ 'title: A hand-edited PRD',
62
+ 'cwd: ~/Projects/session-manager',
63
+ 'estimateMinutes: 20',
64
+ 'disposition: sideways',
65
+ '---',
66
+ 'body',
67
+ '',
68
+ ].join('\n');
69
+
70
+ const { frontmatter } = parsePrdFile(raw);
71
+ expect(frontmatter.disposition).toBeUndefined();
72
+ });
73
+
74
+ test('parsePrdFile leaves disposition undefined when omitted — a PRD authored before this field existed round-trips unaffected', () => {
75
+ const raw = [
76
+ '---',
77
+ 'title: An older PRD',
78
+ 'cwd: ~/Projects/session-manager',
79
+ 'estimateMinutes: 10',
80
+ '---',
81
+ '# Goal',
82
+ '',
83
+ 'Do the thing.',
84
+ '',
85
+ ].join('\n');
86
+
87
+ const { frontmatter, body } = parsePrdFile(raw);
88
+ expect(frontmatter.disposition).toBeUndefined();
89
+ expect(serializePrdFile(frontmatter, body)).toBe(raw);
90
+ });
91
+
92
+ test('serializePrdFile emits a newly-set disposition onto a frontmatter object that lacked one', () => {
93
+ const raw = [
94
+ '---',
95
+ 'title: Legacy PRD',
96
+ 'cwd: ~/Projects/session-manager',
97
+ 'estimateMinutes: 5',
98
+ '---',
99
+ 'body',
100
+ '',
101
+ ].join('\n');
102
+
103
+ const { frontmatter, body } = parsePrdFile(raw);
104
+ const updated = serializePrdFile({ ...frontmatter, disposition: 'new-head' }, body);
105
+ expect(updated).toContain('disposition: new-head');
106
+ });
107
+
108
+ test('serializePrdFile patches an existing disposition (promoting append to new-head)', () => {
109
+ const raw = [
110
+ '---',
111
+ 'title: A second wave',
112
+ 'cwd: ~/Projects/session-manager',
113
+ 'estimateMinutes: 20',
114
+ 'dependsOn: [widget-base]',
115
+ 'disposition: append',
116
+ '---',
117
+ 'body',
118
+ '',
119
+ ].join('\n');
120
+
121
+ const { frontmatter, body } = parsePrdFile(raw);
122
+ const updated = serializePrdFile({ ...frontmatter, dependsOn: [], disposition: 'new-head' }, body);
123
+ expect(updated).toContain('disposition: new-head');
124
+ expect(updated).not.toContain('dependsOn');
125
+ });
@@ -7,12 +7,17 @@
7
7
 
8
8
  'use strict';
9
9
 
10
- import { test, expect, afterEach } from 'vitest';
10
+ import { test, expect, afterEach, vi } from 'vitest';
11
11
  const fs = require('node:fs');
12
12
  const fsp = require('node:fs/promises');
13
13
  const os = require('node:os');
14
14
  const path = require('node:path');
15
- const { resolvePrdWriteDir, resolvePrdsDirs } = require('../lib/prdLocations.cjs');
15
+ const {
16
+ resolvePrdWriteDir,
17
+ resolvePrdsDirs,
18
+ resolveEpicPrdWriteDir,
19
+ listEpicPrdDirs,
20
+ } = require('../lib/prdLocations.cjs');
16
21
 
17
22
  const tmpDirs = [];
18
23
  afterEach(async () => {
@@ -95,3 +100,96 @@ test('resolvePrdsDirs returns [] when no project has a recent transcript', () =>
95
100
  const dirs = resolvePrdsDirs(90, { projectsDir: '/nonexistent-projects-dir' });
96
101
  expect(dirs).toEqual([]);
97
102
  });
103
+
104
+ test('listEpicPrdDirs memoizes on the Epics-root mtime: no readdir on a repeat call, but a freshly minted Epic prds dir is picked up on the very next call', async () => {
105
+ const cwd = await fsp.mkdtemp(path.join(os.tmpdir(), 'sm-prdloc-memo-cwd-'));
106
+ tmpDirs.push(cwd);
107
+ fs.mkdirSync(resolveEpicPrdWriteDir(cwd, 'epic-1'), { recursive: true });
108
+
109
+ const first = listEpicPrdDirs(cwd);
110
+ expect(first).toEqual([resolveEpicPrdWriteDir(cwd, 'epic-1')]);
111
+
112
+ const readdirSpy = vi.spyOn(fs, 'readdirSync');
113
+ try {
114
+ // Nothing under the Epics root changed — the cached result comes back
115
+ // without a fresh readdir of the Epics root.
116
+ const second = listEpicPrdDirs(cwd);
117
+ expect(second).toEqual(first);
118
+ expect(readdirSpy).not.toHaveBeenCalled();
119
+ } finally {
120
+ readdirSpy.mockRestore();
121
+ }
122
+
123
+ // A brand-new Epic's prds/ dir is a NEW entry under the Epics root, which
124
+ // bumps the root's own mtime — no TTL wait required to see it.
125
+ fs.mkdirSync(resolveEpicPrdWriteDir(cwd, 'epic-2'), { recursive: true });
126
+ const third = listEpicPrdDirs(cwd);
127
+ expect(third).toEqual(
128
+ expect.arrayContaining([resolveEpicPrdWriteDir(cwd, 'epic-1'), resolveEpicPrdWriteDir(cwd, 'epic-2')]),
129
+ );
130
+ });
131
+
132
+ test('listEpicPrdDirs: an unreadable Epics root does not write a poisoned entry that survives the dir becoming readable', async () => {
133
+ const cwd = await fsp.mkdtemp(path.join(os.tmpdir(), 'sm-prdloc-unreadable-cwd-'));
134
+ tmpDirs.push(cwd);
135
+
136
+ // No Epics root at all yet.
137
+ expect(listEpicPrdDirs(cwd)).toEqual([]);
138
+
139
+ // The root now exists, with a real Epic prds dir inside it.
140
+ fs.mkdirSync(resolveEpicPrdWriteDir(cwd, 'epic-1'), { recursive: true });
141
+ expect(listEpicPrdDirs(cwd)).toEqual([resolveEpicPrdWriteDir(cwd, 'epic-1')]);
142
+ });
143
+
144
+ test('resolvePrdsDirs EDGE (non-negotiable): a new epics/<id>/prds dir created after a prior call is visible on the VERY NEXT call, no TTL wait', async () => {
145
+ const projectsDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'sm-prdloc-freshness-projects-'));
146
+ tmpDirs.push(projectsDir);
147
+ const projectCwd = await fsp.mkdtemp(path.join(os.tmpdir(), 'sm-prdloc-freshness-cwd-'));
148
+ tmpDirs.push(projectCwd);
149
+
150
+ const projDir = path.join(projectsDir, 'freshness-project');
151
+ fs.mkdirSync(projDir, { recursive: true });
152
+ fs.writeFileSync(path.join(projDir, 'session1.jsonl'), `${JSON.stringify({ cwd: projectCwd })}\n`);
153
+
154
+ const before = resolvePrdsDirs(90, { projectsDir });
155
+ const newEpicPrdDir = resolveEpicPrdWriteDir(projectCwd, 'freshly-minted-epic');
156
+ expect(before).not.toContain(newEpicPrdDir);
157
+
158
+ fs.mkdirSync(newEpicPrdDir, { recursive: true });
159
+
160
+ const after = resolvePrdsDirs(90, { projectsDir });
161
+ expect(after).toContain(newEpicPrdDir);
162
+ });
163
+
164
+ test('resolvePrdsDirs: calls with an explicit { projectsDir } opts are keyed separately per projectsDir and never share a stale cache entry', async () => {
165
+ const projectsDirA = await fsp.mkdtemp(path.join(os.tmpdir(), 'sm-prdloc-keyed-a-'));
166
+ tmpDirs.push(projectsDirA);
167
+ const projectsDirB = await fsp.mkdtemp(path.join(os.tmpdir(), 'sm-prdloc-keyed-b-'));
168
+ tmpDirs.push(projectsDirB);
169
+ const cwdA = await fsp.mkdtemp(path.join(os.tmpdir(), 'sm-prdloc-keyed-cwd-a-'));
170
+ tmpDirs.push(cwdA);
171
+ const cwdB = await fsp.mkdtemp(path.join(os.tmpdir(), 'sm-prdloc-keyed-cwd-b-'));
172
+ tmpDirs.push(cwdB);
173
+
174
+ const projDirA = path.join(projectsDirA, 'proj-a');
175
+ fs.mkdirSync(projDirA, { recursive: true });
176
+ fs.writeFileSync(path.join(projDirA, 'session1.jsonl'), `${JSON.stringify({ cwd: cwdA })}\n`);
177
+ fs.mkdirSync(resolvePrdWriteDir(cwdA), { recursive: true });
178
+
179
+ const projDirB = path.join(projectsDirB, 'proj-b');
180
+ fs.mkdirSync(projDirB, { recursive: true });
181
+ fs.writeFileSync(path.join(projDirB, 'session1.jsonl'), `${JSON.stringify({ cwd: cwdB })}\n`);
182
+ fs.mkdirSync(resolvePrdWriteDir(cwdB), { recursive: true });
183
+
184
+ const dirsA = resolvePrdsDirs(90, { projectsDir: projectsDirA });
185
+ const dirsB = resolvePrdsDirs(90, { projectsDir: projectsDirB });
186
+
187
+ expect(dirsA).toContain(resolvePrdWriteDir(cwdA));
188
+ expect(dirsA).not.toContain(resolvePrdWriteDir(cwdB));
189
+ expect(dirsB).toContain(resolvePrdWriteDir(cwdB));
190
+ expect(dirsB).not.toContain(resolvePrdWriteDir(cwdA));
191
+
192
+ // Re-querying A after B was resolved must still reflect A's own state, not
193
+ // B's — proof the two opts objects never shared one cache key.
194
+ expect(resolvePrdsDirs(90, { projectsDir: projectsDirA })).toEqual(dirsA);
195
+ });
@@ -21,7 +21,7 @@
21
21
 
22
22
  'use strict';
23
23
 
24
- import { test, expect, afterEach } from 'vitest';
24
+ import { test, expect, afterEach, vi } from 'vitest';
25
25
  const fs = require('node:fs');
26
26
  const fsp = require('node:fs/promises');
27
27
  const os = require('node:os');
@@ -150,3 +150,45 @@ test('scheduler.cjs exports candidateArchivedPrdsDirs alongside candidatePrdsDir
150
150
  expect(Array.isArray(candidatePrdsDirs())).toBe(true);
151
151
  expect(Array.isArray(candidateArchivedPrdsDirs())).toBe(true);
152
152
  });
153
+
154
+ test('resolveArchivedPrdsDirs: a prds-archived/ dir created inside an ALREADY-EXISTING Epic (the archiveCompletedPrd shape — does not bump the Epics root mtime) is bounded-stale, not stale forever', async () => {
155
+ // archiveCompletedPrd (scheduler.cjs) creates `epics/<id>/prds-archived/`
156
+ // as a new entry INSIDE an already-existing `epics/<id>/` dir — unlike a
157
+ // brand-new Epic's `prds/` (created in the same mkdirSync call as
158
+ // `epics/<id>/` itself), this does NOT bump the Epics root's own mtime,
159
+ // so the mtime-keyed freshness key alone would never invalidate. The
160
+ // short TTL backstop (same idiom as activeSessions.cjs's cwdScanCache)
161
+ // bounds that blind spot instead of leaving it stale indefinitely.
162
+ vi.useFakeTimers();
163
+ try {
164
+ const projectsDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'sm-prdloc-blindspot-projects-'));
165
+ tmpDirs.push(projectsDir);
166
+ const projectCwd = await mkEpicProject();
167
+ tmpDirs.push(projectCwd);
168
+
169
+ const epicId = `blindspot-epic-${process.pid}`;
170
+ const epicPrdsDir = resolveEpicPrdWriteDir(projectCwd, epicId);
171
+ fs.mkdirSync(epicPrdsDir, { recursive: true }); // Epic already exists, well before archiving.
172
+
173
+ const projDir = path.join(projectsDir, 'blindspot-project');
174
+ fs.mkdirSync(projDir, { recursive: true });
175
+ fs.writeFileSync(path.join(projDir, 'session1.jsonl'), `${JSON.stringify({ cwd: projectCwd })}\n`);
176
+
177
+ // Prime the cache — no prds-archived/ dir exists yet.
178
+ const before = resolveArchivedPrdsDirs(90, { projectsDir });
179
+ const epicArchiveDir = path.join(epicPrdsDir, '..', 'prds-archived');
180
+ expect(before).not.toContain(path.resolve(epicArchiveDir));
181
+
182
+ // The archiveCompletedPrd shape: a NEW prds-archived/ dir appears inside
183
+ // the ALREADY-EXISTING epic dir. This does not change the Epics root's
184
+ // own mtime.
185
+ fs.mkdirSync(epicArchiveDir, { recursive: true });
186
+
187
+ // Bounded staleness is acceptable — but it must actually be bounded.
188
+ vi.advanceTimersByTime(31_000);
189
+ const after = resolveArchivedPrdsDirs(90, { projectsDir });
190
+ expect(after.map((d) => path.resolve(d))).toContain(path.resolve(epicArchiveDir));
191
+ } finally {
192
+ vi.useRealTimers();
193
+ }
194
+ });