opencastle 1.4.2 → 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 (128) hide show
  1. package/dist/cli/bootstrap-on-init.test.js +5 -5
  2. package/dist/cli/bootstrap-on-init.test.js.map +1 -1
  3. package/dist/cli/bootstrap.d.ts +17 -0
  4. package/dist/cli/bootstrap.d.ts.map +1 -1
  5. package/dist/cli/bootstrap.js +284 -312
  6. package/dist/cli/bootstrap.js.map +1 -1
  7. package/dist/cli/bootstrap.test.js +22 -16
  8. package/dist/cli/bootstrap.test.js.map +1 -1
  9. package/dist/cli/convoy/engine.d.ts.map +1 -1
  10. package/dist/cli/convoy/engine.js +8 -2
  11. package/dist/cli/convoy/engine.js.map +1 -1
  12. package/dist/cli/convoy/spec-types.d.ts +15 -0
  13. package/dist/cli/convoy/spec-types.d.ts.map +1 -1
  14. package/dist/cli/detect.d.ts +4 -0
  15. package/dist/cli/detect.d.ts.map +1 -1
  16. package/dist/cli/detect.js +1 -1
  17. package/dist/cli/detect.js.map +1 -1
  18. package/dist/cli/doctor.js +4 -4
  19. package/dist/cli/doctor.js.map +1 -1
  20. package/dist/cli/init.test.js +4 -5
  21. package/dist/cli/init.test.js.map +1 -1
  22. package/dist/cli/log.d.ts +2 -2
  23. package/dist/cli/log.d.ts.map +1 -1
  24. package/dist/cli/log.js +1 -1
  25. package/dist/cli/log.js.map +1 -1
  26. package/dist/cli/log.test.js +1 -1
  27. package/dist/cli/log.test.js.map +1 -1
  28. package/dist/cli/managed-paths.d.ts +1 -1
  29. package/dist/cli/managed-paths.d.ts.map +1 -1
  30. package/dist/cli/managed-paths.js +0 -1
  31. package/dist/cli/managed-paths.js.map +1 -1
  32. package/dist/cli/pipeline.d.ts.map +1 -1
  33. package/dist/cli/pipeline.flow.test.js +134 -27
  34. package/dist/cli/pipeline.flow.test.js.map +1 -1
  35. package/dist/cli/pipeline.js +113 -34
  36. package/dist/cli/pipeline.js.map +1 -1
  37. package/dist/cli/plan.d.ts +22 -0
  38. package/dist/cli/plan.d.ts.map +1 -1
  39. package/dist/cli/plan.js +43 -6
  40. package/dist/cli/plan.js.map +1 -1
  41. package/dist/cli/project-facts.d.ts +103 -0
  42. package/dist/cli/project-facts.d.ts.map +1 -0
  43. package/dist/cli/project-facts.js +577 -0
  44. package/dist/cli/project-facts.js.map +1 -0
  45. package/dist/cli/project-facts.test.d.ts +2 -0
  46. package/dist/cli/project-facts.test.d.ts.map +1 -0
  47. package/dist/cli/project-facts.test.js +209 -0
  48. package/dist/cli/project-facts.test.js.map +1 -0
  49. package/dist/cli/run/adapters/claude.d.ts +13 -0
  50. package/dist/cli/run/adapters/claude.d.ts.map +1 -1
  51. package/dist/cli/run/adapters/claude.js +30 -1
  52. package/dist/cli/run/adapters/claude.js.map +1 -1
  53. package/dist/cli/run/adapters/claude.test.js +34 -1
  54. package/dist/cli/run/adapters/claude.test.js.map +1 -1
  55. package/dist/cli/run/adapters/codex.d.ts.map +1 -1
  56. package/dist/cli/run/adapters/codex.js +4 -0
  57. package/dist/cli/run/adapters/codex.js.map +1 -1
  58. package/dist/cli/run/adapters/codex.test.js +8 -0
  59. package/dist/cli/run/adapters/codex.test.js.map +1 -1
  60. package/dist/cli/run/adapters/copilot.d.ts.map +1 -1
  61. package/dist/cli/run/adapters/copilot.js +4 -1
  62. package/dist/cli/run/adapters/copilot.js.map +1 -1
  63. package/dist/cli/run/adapters/copilot.test.js +9 -0
  64. package/dist/cli/run/adapters/copilot.test.js.map +1 -1
  65. package/dist/cli/run/adapters/stub-cli.test-helper.d.ts +2 -0
  66. package/dist/cli/run/adapters/stub-cli.test-helper.d.ts.map +1 -1
  67. package/dist/cli/run/adapters/stub-cli.test-helper.js +16 -1
  68. package/dist/cli/run/adapters/stub-cli.test-helper.js.map +1 -1
  69. package/dist/cli/run/platform.d.ts +12 -0
  70. package/dist/cli/run/platform.d.ts.map +1 -1
  71. package/dist/cli/run/platform.js +49 -0
  72. package/dist/cli/run/platform.js.map +1 -1
  73. package/dist/cli/run/platform.test.js +21 -1
  74. package/dist/cli/run/platform.test.js.map +1 -1
  75. package/dist/cli/stack-config.js +1 -1
  76. package/dist/cli/stack-config.js.map +1 -1
  77. package/package.json +1 -1
  78. package/src/cli/bootstrap-on-init.test.ts +5 -5
  79. package/src/cli/bootstrap.test.ts +22 -16
  80. package/src/cli/bootstrap.ts +361 -373
  81. package/src/cli/convoy/engine.ts +9 -2
  82. package/src/cli/convoy/spec-types.ts +15 -0
  83. package/src/cli/detect.ts +1 -1
  84. package/src/cli/doctor.ts +4 -4
  85. package/src/cli/init.test.ts +4 -5
  86. package/src/cli/log.test.ts +1 -1
  87. package/src/cli/log.ts +2 -2
  88. package/src/cli/managed-paths.ts +0 -1
  89. package/src/cli/pipeline.flow.test.ts +143 -28
  90. package/src/cli/pipeline.ts +141 -34
  91. package/src/cli/plan.ts +51 -6
  92. package/src/cli/project-facts.test.ts +228 -0
  93. package/src/cli/project-facts.ts +647 -0
  94. package/src/cli/run/adapters/claude.test.ts +39 -1
  95. package/src/cli/run/adapters/claude.ts +27 -1
  96. package/src/cli/run/adapters/codex.test.ts +9 -0
  97. package/src/cli/run/adapters/codex.ts +3 -0
  98. package/src/cli/run/adapters/copilot.test.ts +11 -0
  99. package/src/cli/run/adapters/copilot.ts +3 -1
  100. package/src/cli/run/adapters/stub-cli.test-helper.ts +16 -1
  101. package/src/cli/run/platform.test.ts +23 -1
  102. package/src/cli/run/platform.ts +50 -0
  103. package/src/cli/stack-config.ts +1 -1
  104. package/src/orchestrator/agent-workflows/data-pipeline.md +2 -2
  105. package/src/orchestrator/agent-workflows/feature-implementation.md +1 -1
  106. package/src/orchestrator/agent-workflows/schema-changes.md +1 -1
  107. package/src/orchestrator/agents/team-lead.agent.md +1 -1
  108. package/src/orchestrator/customizations/README.md +27 -84
  109. package/src/orchestrator/prompts/bootstrap-customizations.prompt.md +38 -238
  110. package/src/orchestrator/prompts/brainstorm.prompt.md +1 -1
  111. package/src/orchestrator/prompts/implement-feature.prompt.md +6 -6
  112. package/src/orchestrator/skills/agent-memory/KNOWLEDGE-GRAPH.md +1 -1
  113. package/src/orchestrator/skills/agent-memory/SKILL.md +7 -16
  114. package/src/orchestrator/skills/documentation-standards/SKILL.md +1 -1
  115. package/src/orchestrator/skills/observability-logging/SKILL.md +1 -1
  116. package/src/orchestrator/skills/panel-majority-vote/REFERENCE.md +1 -1
  117. package/src/orchestrator/skills/team-lead-reference/SKILL.md +2 -2
  118. package/src/orchestrator/customizations/AGENT-EXPERTISE.md +0 -325
  119. package/src/orchestrator/customizations/AGENT-FAILURES.md +0 -69
  120. package/src/orchestrator/customizations/AGENT-PERFORMANCE.md +0 -55
  121. package/src/orchestrator/customizations/DISPUTES.md +0 -162
  122. package/src/orchestrator/customizations/KNOWLEDGE-GRAPH.md +0 -10
  123. package/src/orchestrator/customizations/agents/skill-matrix.md +0 -111
  124. package/src/orchestrator/customizations/logs/events.ndjson +0 -0
  125. package/src/orchestrator/customizations/project/decisions.md +0 -31
  126. package/src/orchestrator/customizations/project/docs-structure.md +0 -34
  127. package/src/orchestrator/customizations/project/roadmap.md +0 -24
  128. /package/src/orchestrator/{customizations/logs/README.md → skills/observability-logging/LOG-SCHEMA.md} +0 -0
@@ -389,15 +389,22 @@ function buildDlqMarkdownEntry(
389
389
  return { marker, entry }
390
390
  }
391
391
 
392
+ /** The heading a ledger starts with: `init` no longer installs the files empty, so the first entry creates them. */
393
+ const LEDGER_TITLE: Record<string, string> = {
394
+ 'AGENT-FAILURES.md': '# Agent Failures\n\nDelegations that failed for good: the dead-letter queue. Format: the **team-lead-reference** skill.\n',
395
+ 'DISPUTES.md': '# Disputes\n\nDisagreements review could not settle, for a person to decide. Format: the **team-lead-reference** skill.\n',
396
+ }
397
+
392
398
  function appendLedger(repoRoot: string, file: string, marker: string, entry: string): void {
393
399
  const mdPath = join(resolve(repoRoot), '.opencastle', file)
400
+ let exists = true
394
401
  try {
395
402
  if (readFileSync(mdPath, 'utf8').includes(marker)) return
396
403
  } catch {
397
- // File doesn't exist yet — will create
404
+ exists = false
398
405
  }
399
406
  mkdirSync(dirname(mdPath), { recursive: true })
400
- appendFileSync(mdPath, entry)
407
+ appendFileSync(mdPath, (exists ? '' : LEDGER_TITLE[file] ?? '') + entry)
401
408
  }
402
409
 
403
410
  /**
@@ -176,6 +176,21 @@ export interface ExecuteOptions {
176
176
  permissionMode?: PermissionMode;
177
177
  /** A model name the runtime understands; omitted means the runtime's own default. */
178
178
  model?: string;
179
+ /**
180
+ * How much the model reasons before it answers, where the runtime lets a
181
+ * caller choose; omitted means the runtime's own default. Planning asks for
182
+ * less: measured on Claude Code, a PRD took 129s at the default and 36s at
183
+ * `low`, with the same sections, because most of the session was thinking.
184
+ */
185
+ effort?: 'low' | 'medium' | 'high';
186
+ /**
187
+ * A self-contained, read-only session: everything it needs is in its prompt
188
+ * or in the files it reads. It starts no MCP server, loads no skill or
189
+ * command, and has only the tools that read files — so it cannot spend its
190
+ * time on a web search or a sub-agent. Runtimes that cannot narrow a session
191
+ * this way run it as usual.
192
+ */
193
+ lean?: boolean;
179
194
  /**
180
195
  * @deprecated Ignored. Agents read the project's own MCP config, which
181
196
  * `opencastle sync` compiles for every assistant; writing a second copy
package/src/cli/detect.ts CHANGED
@@ -196,7 +196,7 @@ const AUTH: DetectionRule[] = [
196
196
  ];
197
197
 
198
198
  // Mapping of npm package names to detection labels
199
- const PACKAGE_DETECTIONS: Record<string, { category: string; label: string }> = {
199
+ export const PACKAGE_DETECTIONS: Record<string, { category: string; label: string }> = {
200
200
  'next': { category: 'frameworks', label: 'next' },
201
201
  'nuxt': { category: 'frameworks', label: 'nuxt' },
202
202
  'astro': { category: 'frameworks', label: 'astro' },
package/src/cli/doctor.ts CHANGED
@@ -226,13 +226,13 @@ async function checkLogs(projectRoot: string): Promise<CheckResult> {
226
226
  const dir = resolve(projectRoot, '.opencastle', 'logs');
227
227
  if (!existsSync(dir)) {
228
228
  // A local run artefact, not part of the project, so a fresh clone has none —
229
- // and calling that a failure made `doctor` exit 1 on every clone. `sync`
230
- // creates it; until then "no runs yet" is the honest reading, not a fault.
229
+ // and calling that a failure made `doctor` exit 1 on every clone. The first
230
+ // `opencastle log` creates it; until then "none yet" is the honest reading,
231
+ // and not something to warn about on every new install.
231
232
  return {
232
233
  ok: true,
233
- warning: true,
234
234
  label: 'Observability logs',
235
- detail: 'no logs yet — created on the next sync',
235
+ detail: 'none yet — created when an agent first logs a session',
236
236
  };
237
237
  }
238
238
  // A diagnostic does not write. This used to create five empty `.ndjson` files
@@ -396,15 +396,14 @@ describe('VS Code adapter install', () => {
396
396
  expect(existsSync(join(tempDir, '.vscode', 'mcp.json'))).toBe(true)
397
397
  })
398
398
 
399
- it('creates all observability log files in .opencastle/logs', async () => {
399
+ it('installs no empty logs or ledgers: the first record creates them', async () => {
400
400
  const adapter = await IDE_ADAPTERS['vscode']()
401
401
  await adapter.install(PKG_ROOT, tempDir, STACK_EMPTY, EMPTY_REPO_INFO)
402
402
  await scaffoldCustomizations(PKG_ROOT, tempDir, STACK_EMPTY)
403
403
 
404
- const logsDir = join(tempDir, '.opencastle', 'logs')
405
- expect(existsSync(logsDir)).toBe(true)
406
- for (const file of ['events.ndjson']) {
407
- expect(existsSync(join(logsDir, file))).toBe(true)
404
+ const dir = join(tempDir, '.opencastle')
405
+ for (const file of ['logs', 'AGENT-FAILURES.md', 'DISPUTES.md', 'AGENT-EXPERTISE.md', 'AGENT-PERFORMANCE.md', 'KNOWLEDGE-GRAPH.md']) {
406
+ expect(existsSync(join(dir, file)), file).toBe(false)
408
407
  }
409
408
  })
410
409
 
@@ -5,7 +5,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
5
5
  import log, { LOG_RECORDS, parseLogArgs } from './log.js'
6
6
 
7
7
  const repoRoot = resolve(import.meta.dirname, '..', '..')
8
- const README = join(repoRoot, 'src', 'orchestrator', 'customizations', 'logs', 'README.md')
8
+ const README = join(repoRoot, 'src', 'orchestrator', 'skills', 'observability-logging', 'LOG-SCHEMA.md')
9
9
  const SKILL = join(repoRoot, 'src', 'orchestrator', 'skills', 'observability-logging', 'SKILL.md')
10
10
 
11
11
  /** The `opencastle log …` command lines in a document's sh blocks, continuation lines joined. */
package/src/cli/log.ts CHANGED
@@ -13,7 +13,7 @@ import type { CliContext } from './types.js'
13
13
  * `--type session` with no fields, `--outcome banana`, a value-less `--model`
14
14
  * stored as `true`, `--task 007` stored as the number 7, `--agnet`, and a
15
15
  * caller-supplied `--timestamp yesterday`. Each record type is now checked
16
- * against the schema in `customizations/logs/README.md`, field by field, and a
16
+ * against the schema in `skills/observability-logging/LOG-SCHEMA.md`, field by field, and a
17
17
  * record that does not match is refused with every reason at once.
18
18
  */
19
19
 
@@ -34,7 +34,7 @@ const attempt: Field = { kind: 'count', required: true, min: 1 }
34
34
 
35
35
  const OUTCOMES = ['success', 'partial', 'failed'] as const
36
36
 
37
- /** The record schemas, as `customizations/logs/README.md` documents them. `log.test.ts` holds the two together. */
37
+ /** The record schemas, as `skills/observability-logging/LOG-SCHEMA.md` documents them. `log.test.ts` holds the two together. */
38
38
  export const LOG_RECORDS: Record<string, Record<string, Field>> = {
39
39
  session: {
40
40
  agent: req('text'),
@@ -179,7 +179,6 @@ export function removeOwnedFiles(projectRoot: string, dir: string, owns: (_rel:
179
179
  */
180
180
  export const REQUIRED_CUSTOMIZATIONS = [
181
181
  'agents/skill-matrix.json',
182
- 'agents/skill-matrix.md',
183
182
  'agents/agent-registry.md',
184
183
  ] as const
185
184
 
@@ -95,10 +95,17 @@ interface Call {
95
95
  permissionMode?: string
96
96
  cwd?: string
97
97
  prompt: string
98
+ effort?: string
99
+ lean?: boolean
98
100
  }
99
101
 
100
- function stubAdapter(answers: Record<string, Answer> = {}): { adapter: AgentAdapter; calls: Call[] } {
102
+ /** An answer that never comes: the session runs until it is stopped. */
103
+ const never = (): Promise<string> => new Promise(() => {})
104
+
105
+ function stubAdapter(answers: Record<string, Answer> = {}): { adapter: AgentAdapter; calls: Call[]; killed: string[] } {
101
106
  const calls: Call[] = []
107
+ const killed: string[] = []
108
+ const stoppers = new Map<string, Array<() => void>>()
102
109
  const seen = new Map<string, number>()
103
110
  const defaults: Record<string, Answer> = {
104
111
  'generate-prd': PRD,
@@ -113,15 +120,30 @@ function stubAdapter(answers: Record<string, Answer> = {}): { adapter: AgentAdap
113
120
  name: 'stub',
114
121
  isAvailable: async () => true,
115
122
  async execute(task: Task, options?: ExecuteOptions) {
116
- calls.push({ template: task.id, permissionMode: options?.permissionMode, cwd: options?.cwd, prompt: task.prompt })
123
+ calls.push({
124
+ template: task.id,
125
+ permissionMode: options?.permissionMode,
126
+ cwd: options?.cwd,
127
+ prompt: task.prompt,
128
+ effort: options?.effort,
129
+ lean: options?.lean,
130
+ })
117
131
  const nth = (seen.get(task.id) ?? 0) + 1
118
132
  seen.set(task.id, nth)
119
133
  const answer = answers[task.id] ?? defaults[task.id]
120
- const output = typeof answer === 'function' ? await answer(task.prompt, nth) : answer
134
+ // Stopped like a real runtime: the process ends and reports a failure.
135
+ const stopped = new Promise<null>((r) => stoppers.set(task.id, [...(stoppers.get(task.id) ?? []), () => r(null)]))
136
+ const output = await Promise.race([typeof answer === 'function' ? answer(task.prompt, nth) : answer, stopped])
137
+ if (output === null) return { success: false, output: 'stopped', exitCode: 143 }
121
138
  return { success: true, output, exitCode: 0 }
122
139
  },
140
+ kill(task: Task) {
141
+ killed.push(task.id)
142
+ for (const stop of stoppers.get(task.id) ?? []) stop()
143
+ stoppers.delete(task.id)
144
+ },
123
145
  }
124
- return { adapter, calls }
146
+ return { adapter, calls, killed }
125
147
  }
126
148
 
127
149
  const templates = (calls: Call[]): string[] => calls.map((c) => c.template)
@@ -157,10 +179,14 @@ function plan(adapter: AgentAdapter, extra: Partial<Parameters<typeof planConvoy
157
179
  // ── Sessions ────────────────────────────────────────────────────────────────
158
180
 
159
181
  describe('planConvoy: sessions', () => {
160
- it('needs four sessions when every answer is good, with no semantic review of the plan', async () => {
182
+ it('starts five sessions when every answer is good, with no semantic review of the plan', async () => {
161
183
  const { adapter, calls } = stubAdapter()
162
184
  const outcome = await plan(adapter)
163
- expect([...templates(calls)].sort()).toEqual(['assess-complexity', 'generate-convoy', 'generate-prd', 'validate-prd'])
185
+ // The plan from the request is dropped once the size says a PRD is needed;
186
+ // the plan from the PRD is the one kept.
187
+ expect([...templates(calls)].sort()).toEqual([
188
+ 'assess-complexity', 'generate-convoy', 'generate-convoy', 'generate-prd', 'validate-prd',
189
+ ])
164
190
  expect(outcome.problems).toEqual([])
165
191
  })
166
192
 
@@ -177,26 +203,96 @@ describe('planConvoy: sessions', () => {
177
203
  it('sends the PRD to generate-convoy once', async () => {
178
204
  const { adapter, calls } = stubAdapter()
179
205
  await plan(adapter)
180
- const prompt = calls.find((c) => c.template === 'generate-convoy')!.prompt
181
- expect(prompt.match(/PRD-MARKER/g)).toHaveLength(1)
182
- expect(prompt).not.toMatch(/\{\{(goal|context)\}\}/)
206
+ const fromPrd = calls.filter((c) => c.template === 'generate-convoy' && c.prompt.includes('PRD-MARKER'))
207
+ expect(fromPrd).toHaveLength(1)
208
+ expect(fromPrd[0].prompt.match(/PRD-MARKER/g)).toHaveLength(1)
209
+ expect(fromPrd[0].prompt).not.toMatch(/\{\{(goal|context)\}\}/)
183
210
  })
184
211
 
185
212
  it('plans a small change straight from the request, with no PRD', async () => {
186
213
  const { adapter, calls } = stubAdapter({ 'assess-complexity': fence({ ...SINGLE, complexity: 'low' }) })
187
214
  const outcome = await plan(adapter)
188
- expect(templates(calls)).toEqual(['assess-complexity', 'generate-convoy'])
215
+ // The PRD starts beside the size too; here it finished first, and is not kept.
216
+ expect([...templates(calls)].sort()).toEqual(['assess-complexity', 'generate-convoy', 'generate-prd'])
189
217
  expect(outcome.prdPath).toBeNull()
190
218
  expect(existsSync(join(root, '.opencastle', 'prds'))).toBe(false)
191
- expect(calls[1].prompt).toContain('There is no PRD: this is a small change')
219
+ expect(calls.find((c) => c.template === 'generate-convoy')!.prompt).toContain('There is no PRD: this is a small change')
192
220
  expect(outcome.problems).toEqual([])
193
221
  })
194
222
 
223
+ it('sizes a request and plans it at the same time', async () => {
224
+ let inFlight = 0
225
+ let most = 0
226
+ const overlap = (answer: string) => async () => {
227
+ inFlight++
228
+ most = Math.max(most, inFlight)
229
+ await new Promise((r) => setTimeout(r, 20))
230
+ inFlight--
231
+ return answer
232
+ }
233
+ const { adapter } = stubAdapter({
234
+ 'assess-complexity': overlap(fence({ ...SINGLE, complexity: 'low' })),
235
+ 'generate-convoy': overlap(fence(PLAN)),
236
+ })
237
+ await plan(adapter)
238
+ expect(most).toBe(2)
239
+ })
240
+
241
+ it('stops the PRD as soon as the size says the change is small', async () => {
242
+ const { adapter, killed } = stubAdapter({
243
+ 'assess-complexity': fence({ ...SINGLE, complexity: 'low' }),
244
+ 'generate-prd': never,
245
+ })
246
+ const outcome = await plan(adapter)
247
+ expect(killed).toEqual(['generate-prd'])
248
+ expect(outcome.prdPath).toBeNull()
249
+ expect(outcome.plan.name).toBe('Dark mode')
250
+ })
251
+
252
+ it('stops the plan from the request as soon as the size says a PRD is needed', async () => {
253
+ const { adapter, calls, killed } = stubAdapter({
254
+ 'generate-convoy': (prompt) => (prompt.includes('PRD-MARKER') ? fence(PLAN) : never()),
255
+ })
256
+ const outcome = await plan(adapter)
257
+ expect(killed).toEqual(['generate-convoy'])
258
+ expect(outcome.prdPath).not.toBeNull()
259
+ expect(outcome.plan.name).toBe('Dark mode')
260
+ // A stopped session counts, and makes the cost a lower bound.
261
+ expect(outcome.planning?.sessions).toBe(calls.length)
262
+ expect(outcome.planning?.costComplete).toBe(false)
263
+ })
264
+
265
+ it('plans from the PRD beside its review, and again from the fixed PRD when the review fails', async () => {
266
+ const fixedPrd = PRD.replace('PRD-MARKER', 'FIXED-MARKER')
267
+ const { adapter, calls, killed } = stubAdapter({
268
+ 'validate-prd': (_p, n) => fence({ valid: n > 1, issues: ['US-1: unclear'] }),
269
+ 'fix-prd': fixedPrd,
270
+ // The plan from the request ends at once; the one from the draft PRD
271
+ // runs until it is stopped.
272
+ 'generate-convoy': (prompt) => (prompt.includes('PRD-MARKER') ? never() : fence(PLAN)),
273
+ })
274
+ const outcome = await plan(adapter)
275
+ expect(killed).toEqual(['generate-convoy'])
276
+ const last = calls.filter((c) => c.template === 'generate-convoy').at(-1)!
277
+ expect(last.prompt).toContain('FIXED-MARKER')
278
+ expect(outcome.problems).toEqual([])
279
+ })
280
+
281
+ it('asks for medium effort for the plan and low for every other step, all lean', async () => {
282
+ const { adapter, calls } = stubAdapter({ 'validate-prd': (_p, n) => fence({ valid: n > 1, issues: ['x'] }) })
283
+ await plan(adapter, { critic: true })
284
+ expect(new Set(calls.map((c) => c.template)).size).toBeGreaterThanOrEqual(6)
285
+ for (const call of calls) {
286
+ expect(call.effort, call.template).toBe(call.template === 'generate-convoy' ? 'medium' : 'low')
287
+ expect(call.lean, call.template).toBe(true)
288
+ }
289
+ })
290
+
195
291
  it('sizes a larger request once, and does not size its PRD again', async () => {
196
292
  const { adapter, calls } = stubAdapter()
197
293
  await plan(adapter)
198
294
  expect(count(calls, 'assess-complexity')).toBe(1)
199
- expect(templates(calls)[0]).toBe('assess-complexity')
295
+ expect(templates(calls).slice(0, 3).sort()).toEqual(['assess-complexity', 'generate-convoy', 'generate-prd'])
200
296
  })
201
297
 
202
298
  it('fixes an invalid PRD at most twice, then plans from it anyway', async () => {
@@ -204,7 +300,10 @@ describe('planConvoy: sessions', () => {
204
300
  const outcome = await plan(adapter)
205
301
  expect(count(calls, 'fix-prd')).toBe(2)
206
302
  expect(count(calls, 'validate-prd')).toBe(3)
207
- expect(calls).toHaveLength(8)
303
+ // Planned from the request, from the draft beside its review, and from the PRD as fixed.
304
+ expect(count(calls, 'generate-convoy')).toBe(3)
305
+ expect(calls).toHaveLength(10)
306
+ expect(templates(calls).at(-1)).toBe('generate-convoy')
208
307
  expect(outcome.problems).toEqual([])
209
308
  })
210
309
 
@@ -212,7 +311,7 @@ describe('planConvoy: sessions', () => {
212
311
  const { adapter, calls } = stubAdapter({ 'validate-prd': (_p, n) => fence({ valid: n > 1, issues: ['x'] }) })
213
312
  await plan(adapter)
214
313
  expect(count(calls, 'fix-prd')).toBe(1)
215
- expect(calls).toHaveLength(6)
314
+ expect(calls).toHaveLength(8)
216
315
  })
217
316
 
218
317
  it('asks fix-convoy only when the checks fail, and never validate-convoy', async () => {
@@ -224,14 +323,21 @@ describe('planConvoy: sessions', () => {
224
323
  const outcome = await plan(adapter)
225
324
  expect(count(calls, 'fix-convoy')).toBe(1)
226
325
  expect(count(calls, 'validate-convoy')).toBe(0)
227
- expect(calls).toHaveLength(5)
326
+ expect(calls).toHaveLength(6)
228
327
  expect(outcome.problems).toEqual([])
229
328
  })
230
329
 
231
330
  it('retries an unreadable plan once', async () => {
232
- const { adapter, calls } = stubAdapter({ 'generate-convoy': (_p, n) => (n === 1 ? 'Sorry, here you go: {' : fence(PLAN)) })
233
- await plan(adapter)
234
- expect(count(calls, 'generate-convoy')).toBe(2)
331
+ // The plan that is kept, from the PRD; the one from the request is dropped
332
+ // as soon as the size is known, whether or not it got to its own retry.
333
+ let fromPrd = 0
334
+ const { adapter, calls } = stubAdapter({
335
+ 'generate-convoy': (prompt) =>
336
+ prompt.includes('PRD-MARKER') && fromPrd++ === 0 ? 'Sorry, here you go: {' : fence(PLAN),
337
+ })
338
+ const outcome = await plan(adapter)
339
+ expect(calls.filter((c) => c.template === 'generate-convoy' && c.prompt.includes('PRD-MARKER'))).toHaveLength(2)
340
+ expect(outcome.plan.name).toBe('Dark mode')
235
341
  })
236
342
 
237
343
  it('keeps the unreadable answer for inspection when the retry fails too', async () => {
@@ -242,14 +348,21 @@ describe('planConvoy: sessions', () => {
242
348
 
243
349
  it('is bounded in the worst case, and reports what is left', async () => {
244
350
  const bad = { ...PLAN, tasks: [PLAN.tasks[0], { ...PLAN.tasks[1], timeout: 'soon' }] }
351
+ let fromFixed = 0
245
352
  const { adapter, calls } = stubAdapter({
246
353
  'validate-prd': fence({ valid: false, issues: ['x'] }),
247
- 'generate-convoy': (_p, n) => (n === 1 ? '```json\n{ "name": "x", "tasks": [\n```' : fence(bad)),
354
+ // Only the plan that is kept, from the PRD as fixed, is unreadable once.
355
+ 'generate-convoy': (prompt) =>
356
+ prompt.includes('PRD-MARKER') && calls.some((c) => c.template === 'fix-prd') && fromFixed++ === 0
357
+ ? '```json\n{ "name": "x", "tasks": [\n```'
358
+ : fence(bad),
248
359
  'fix-convoy': fence([{ task_id: 'toggle', field: 'timeout', value: 'later' }]),
249
360
  })
250
361
  const outcome = await plan(adapter, { critic: true })
251
- // generate-prd, validate-prd ‖ assess, 2 × (fix-prd + validate-prd), 2 × generate-convoy, 2 × fix-convoy.
252
- expect(calls).toHaveLength(11)
362
+ // assess ‖ the plan from the request, generate-prd, validate-prd ‖ the plan
363
+ // from the draft, 2 × (fix-prd + validate-prd), 2 × generate-convoy (one
364
+ // unreadable), 2 × fix-convoy.
365
+ expect(calls).toHaveLength(13)
253
366
  expect(count(calls, 'validate-convoy')).toBe(0) // no review of a plan that cannot run
254
367
  expect(outcome.problems.join('\n')).toContain('timeout')
255
368
  })
@@ -276,7 +389,7 @@ describe('planConvoy: sessions', () => {
276
389
  })
277
390
  const outcome = await plan(adapter, { critic: true })
278
391
  expect(templates(calls).slice(-2)).toEqual(['validate-convoy', 'fix-convoy'])
279
- expect(calls).toHaveLength(6)
392
+ expect(calls).toHaveLength(7)
280
393
  expect(outcome.plan.tasks[1].prompt).toBe('Add the toggle; read docs first.')
281
394
 
282
395
  const broken = stubAdapter({
@@ -342,9 +455,10 @@ describe('planConvoy: groups', () => {
342
455
  const { adapter, calls } = stubAdapter({ 'assess-complexity': fence(CHAIN), 'generate-convoy': groups.answer })
343
456
  await plan(adapter)
344
457
  expect(groups.most()).toBe(3)
345
- expect(count(calls, 'generate-convoy')).toBe(3)
346
- // The request is sized, then the PRD again: groups name the PRD's phases.
347
- expect(calls).toHaveLength(7)
458
+ expect(calls.filter((c) => c.template === 'generate-convoy' && c.prompt.includes('**Group name:**'))).toHaveLength(3)
459
+ // The request is sized (beside its dropped plan), then the PRD again:
460
+ // groups name the PRD's phases.
461
+ expect(calls).toHaveLength(8)
348
462
  const docs = calls.find((c) => c.template === 'generate-convoy' && c.prompt.includes('**Group name:** docs'))!
349
463
  expect(docs.prompt).not.toContain('Phase 1 — Theme')
350
464
  })
@@ -528,7 +642,7 @@ describe('convoy "<task>": show the plan, ask once', () => {
528
642
  await planTask({ args: [], pkgRoot }, 'add dark mode')
529
643
  expect(adapters.resolveAdapter).toHaveBeenCalledTimes(1)
530
644
  expect(adapters.resolveAdapter).toHaveBeenCalledWith({ projectRoot: root, explicit: null })
531
- expect(stub.calls.length).toBe(4)
645
+ expect(stub.calls.length).toBe(5)
532
646
  })
533
647
 
534
648
  it('stops before planning when no runtime can be found', async () => {
@@ -600,8 +714,9 @@ describe('the plan says what planning spent', () => {
600
714
  })
601
715
 
602
716
  it('counts the sessions planConvoy ran', async () => {
603
- const { adapter } = stubAdapter()
717
+ const { adapter, calls } = stubAdapter()
604
718
  const outcome = await plan(adapter)
605
- expect(outcome.planning?.sessions).toBe(4)
719
+ expect(outcome.planning?.sessions).toBe(5)
720
+ expect(calls).toHaveLength(5)
606
721
  })
607
722
  })