ticket-to-pr 1.3.0 → 1.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.
package/README.md CHANGED
@@ -214,6 +214,9 @@ ticket-to-pr --dry-run --once
214
214
  | `doctor` | Diagnostic check — verifies environment, Notion connectivity, database schema, tools, and projects |
215
215
  | `model` | View current models and available options |
216
216
  | `model <review\|execute\|both> <model>` | Set the Claude model for an agent. Accepts aliases (`opus`, `sonnet`, `haiku`) or full model IDs. |
217
+ | `learnings` | View accumulated project learnings from past agent runs |
218
+ | `learnings <project>` | View learnings for a specific project |
219
+ | `learnings clear <project>` | Clear a project's learnings file |
217
220
  | *(none)* | Continuous polling every 30s |
218
221
  | `--once` | Poll once, wait for agents to finish, exit |
219
222
  | `--dry-run` | Poll and log what would happen, don't run agents |
@@ -351,6 +354,23 @@ Available model aliases:
351
354
 
352
355
  You can also pass a full model ID directly (e.g. `ticket-to-pr model review claude-sonnet-4-5-20250929`). Changes are saved to `.env.local` and take effect on the next poll cycle.
353
356
 
357
+ ### `learnings` — Project Memory
358
+
359
+ TicketToPR accumulates learnings from every agent run — successes, failures, patterns, and mistakes. These are automatically injected into future agent prompts so the AI gets smarter about your project over time.
360
+
361
+ ```bash
362
+ # View all project learnings
363
+ ticket-to-pr learnings
364
+
365
+ # View learnings for a specific project
366
+ ticket-to-pr learnings MyProject
367
+
368
+ # Clear learnings for a project (start fresh)
369
+ ticket-to-pr learnings clear MyProject
370
+ ```
371
+
372
+ Learnings are stored in each project directory at `.ticket-to-pr/learnings.md` (auto-gitignored). Failed tickets are especially valuable — the agent learns what not to do next time.
373
+
354
374
  ### Your First Ticket
355
375
 
356
376
  1. Click **"+ New"** on your Notion board
@@ -424,7 +444,7 @@ The review agent explores your codebase without modifying anything:
424
444
 
425
445
  - **Tools**: Read, Glob, Grep, Task
426
446
  - **Context**: Reads your project's `CLAUDE.md` for architecture rules. If `blockedFiles` are configured, the review agent factors those constraints into scoring.
427
- - **Output**: Ease score, confidence score, implementation spec, impact report, affected files, risks
447
+ - **Output**: Ease score, confidence score, implementation spec, impact report, affected files, risks, **acceptance test cases**
428
448
  - **Budget**: $2.00 max, 25 turns max
429
449
  - **Typical cost**: $0.15 - $0.50
430
450
 
@@ -446,7 +466,7 @@ The review agent explores your codebase without modifying anything:
446
466
 
447
467
  ### Execute Agent (Write Access)
448
468
 
449
- The execute agent implements the code based on the spec:
469
+ The execute agent implements the code based on the spec. When the review agent generates acceptance tests, the execute agent follows a **test-first workflow** — writing test files before implementation code.
450
470
 
451
471
  - **Tools**: Read, Glob, Grep, Edit, Write + limited Bash (git, build, test only)
452
472
  - **Dev access** (opt-in): When `devAccess` is enabled, additionally allows `npx tsx`, `node`, `npm run`, `npx vitest`, `npx jest`, `npx prisma`, `python`, and `curl` to localhost/127.0.0.1 only
@@ -459,14 +479,15 @@ The execute agent implements the code based on the spec:
459
479
 
460
480
  1. TicketToPR **fetches the latest** from `origin/<baseBranch>` (configurable per project, auto-detected by default)
461
481
  2. Creates branch `notion/{8-char-id}/{ticket-slug}` based on the fresh remote state
462
- 3. Claude implements changes and makes atomic commits
463
- 4. TicketToPR runs your build command (if configured)
464
- 5. If `blockedFiles` patterns are configured, validates no off-limits files were touched
465
- 6. Build passes + no blocked file violations: pushes branch to origin
466
- 7. Creates a GitHub PR via `gh pr create` targeting the base branch (unless `skipPR` is enabled)
467
- 8. PR URL written back to the Notion ticket
468
- 9. Ticket moves to **PR Ready**
469
- 10. Build fails or blocked file violation: no code is pushed, ticket moves to **Failed**
482
+ 3. Claude implements changes and makes atomic commits (test-first if acceptance tests were generated)
483
+ 4. **Diff review**: a lightweight Haiku agent reviews the diff against the spec — catches issues before push
484
+ 5. TicketToPR runs your build command (if configured)
485
+ 6. If `blockedFiles` patterns are configured, validates no off-limits files were touched
486
+ 7. All checks pass: pushes branch to origin
487
+ 8. Creates a GitHub PR via `gh pr create` targeting the base branch (unless `skipPR` is enabled)
488
+ 9. PR URL written back to the Notion ticket
489
+ 10. Ticket moves to **PR Ready**
490
+ 11. Any check fails (diff review, build, blocked files): no code is pushed, ticket moves to **Failed**
470
491
 
471
492
  ## Costs
472
493
 
package/dist/cli.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export declare function runDoctor(): Promise<void>;
2
2
  export declare function runInit(): Promise<void>;
3
3
  export declare function runModel(args: string[]): Promise<void>;
4
+ export declare function runLearnings(args: string[]): Promise<void>;
package/dist/cli.js CHANGED
@@ -2,7 +2,8 @@ import { createInterface } from 'node:readline';
2
2
  import { execSync } from 'node:child_process';
3
3
  import { readFileSync, existsSync, writeFileSync } from 'node:fs';
4
4
  import { join } from 'node:path';
5
- import { mask, shellEscape, writeEnvFile, updateProjectsFile, getDefaultBranch, parseEnvFile } from './lib/utils.js';
5
+ import { mask, shellEscape, writeEnvFile, updateProjectsFile, getDefaultBranch, parseEnvFile, readLearnings } from './lib/utils.js';
6
+ import { unlinkSync } from 'node:fs';
6
7
  import { getProjectNames, getProjectDir, getBaseBranch, getBlockedFiles, getSkipPR } from './lib/projects.js';
7
8
  import { CONFIG_DIR } from './lib/paths.js';
8
9
  // -- Colors --
@@ -849,3 +850,78 @@ export async function runModel(args) {
849
850
  }
850
851
  console.log(`${DIM}Saved to .env.local. Takes effect on next poll cycle.${RESET}\n`);
851
852
  }
853
+ // -- Learnings --
854
+ export async function runLearnings(args) {
855
+ const projectNames = getProjectNames();
856
+ if (projectNames.length === 0) {
857
+ console.log(`${RED}No projects configured.${RESET} Run ${DIM}ticket-to-pr init${RESET} first.`);
858
+ process.exitCode = 1;
859
+ return;
860
+ }
861
+ const subCmd = args[0]?.toLowerCase();
862
+ const projectArg = args[1];
863
+ // ticket-to-pr learnings clear <project>
864
+ if (subCmd === 'clear') {
865
+ if (!projectArg) {
866
+ console.log(`${RED}Usage: ticket-to-pr learnings clear <project>${RESET}`);
867
+ console.log(`${DIM}Projects: ${projectNames.join(', ')}${RESET}`);
868
+ process.exitCode = 1;
869
+ return;
870
+ }
871
+ const dir = getProjectDir(projectArg);
872
+ if (!dir) {
873
+ console.log(`${RED}Unknown project "${projectArg}".${RESET} Available: ${projectNames.join(', ')}`);
874
+ process.exitCode = 1;
875
+ return;
876
+ }
877
+ const learningsPath = join(dir, '.ticket-to-pr', 'learnings.md');
878
+ if (existsSync(learningsPath)) {
879
+ unlinkSync(learningsPath);
880
+ printStatus(true, `Cleared learnings for ${projectArg}`);
881
+ }
882
+ else {
883
+ console.log(`${DIM}No learnings file found for ${projectArg}.${RESET}`);
884
+ }
885
+ return;
886
+ }
887
+ // ticket-to-pr learnings [project]
888
+ // If a project name is given, show only that project
889
+ const projectsToShow = subCmd && projectNames.includes(subCmd)
890
+ ? [subCmd]
891
+ : subCmd && getProjectDir(subCmd)
892
+ ? [subCmd]
893
+ : projectNames;
894
+ // If an unknown arg was passed
895
+ if (subCmd && subCmd !== 'clear' && !getProjectDir(subCmd) && !projectNames.some(p => p.toLowerCase() === subCmd)) {
896
+ console.log(`${RED}Unknown project or subcommand "${args[0]}".${RESET}`);
897
+ console.log(`\n${BOLD}Usage:${RESET}`);
898
+ console.log(` ticket-to-pr learnings ${DIM}# view all projects${RESET}`);
899
+ console.log(` ticket-to-pr learnings <project> ${DIM}# view one project${RESET}`);
900
+ console.log(` ticket-to-pr learnings clear <project> ${DIM}# clear a project's learnings${RESET}`);
901
+ console.log(`\n${DIM}Projects: ${projectNames.join(', ')}${RESET}`);
902
+ process.exitCode = 1;
903
+ return;
904
+ }
905
+ let anyFound = false;
906
+ for (const name of projectsToShow) {
907
+ const dir = getProjectDir(name);
908
+ if (!dir)
909
+ continue;
910
+ const content = readLearnings(dir);
911
+ if (content) {
912
+ anyFound = true;
913
+ console.log(`\n${BOLD}${name}${RESET} ${DIM}${dir}/.ticket-to-pr/learnings.md${RESET}\n`);
914
+ console.log(content);
915
+ }
916
+ else {
917
+ console.log(`\n${BOLD}${name}${RESET} ${DIM}no learnings yet${RESET}`);
918
+ }
919
+ }
920
+ if (!anyFound) {
921
+ console.log(`\n${DIM}Learnings accumulate automatically as tickets are processed.${RESET}`);
922
+ }
923
+ console.log(`\n${BOLD}Commands:${RESET}`);
924
+ console.log(` ticket-to-pr learnings ${DIM}# view all projects${RESET}`);
925
+ console.log(` ticket-to-pr learnings <project> ${DIM}# view one project${RESET}`);
926
+ console.log(` ticket-to-pr learnings clear <project> ${DIM}# clear a project's learnings${RESET}\n`);
927
+ }
package/dist/config.d.ts CHANGED
@@ -11,10 +11,13 @@ export declare const CONFIG: {
11
11
  };
12
12
  readonly REVIEW_BUDGET_USD: 2;
13
13
  readonly EXECUTE_BUDGET_USD: 15;
14
+ readonly DIFF_REVIEW_BUDGET_USD: 0.5;
14
15
  readonly REVIEW_MODEL: string;
15
16
  readonly EXECUTE_MODEL: string;
17
+ readonly DIFF_REVIEW_MODEL: string;
16
18
  readonly REVIEW_MAX_TURNS: 25;
17
19
  readonly EXECUTE_MAX_TURNS: 50;
20
+ readonly DIFF_REVIEW_MAX_TURNS: 10;
18
21
  readonly STALE_LOCK_MS: number;
19
22
  readonly MAX_CONCURRENT_AGENTS: number;
20
23
  readonly FREE_MAX_PROJECTS: 1;
@@ -47,8 +50,32 @@ export declare const REVIEW_OUTPUT_SCHEMA: {
47
50
  readonly risks: {
48
51
  readonly type: "string";
49
52
  };
53
+ readonly testCases: {
54
+ readonly type: "array";
55
+ readonly items: {
56
+ readonly type: "string";
57
+ };
58
+ };
59
+ };
60
+ readonly required: readonly ["easeScore", "confidenceScore", "spec", "impactReport", "affectedFiles", "testCases"];
61
+ };
62
+ export declare const DIFF_REVIEW_SCHEMA: {
63
+ readonly type: "object";
64
+ readonly properties: {
65
+ readonly approved: {
66
+ readonly type: "boolean";
67
+ };
68
+ readonly issues: {
69
+ readonly type: "array";
70
+ readonly items: {
71
+ readonly type: "string";
72
+ };
73
+ };
74
+ readonly summary: {
75
+ readonly type: "string";
76
+ };
50
77
  };
51
- readonly required: readonly ["easeScore", "confidenceScore", "spec", "impactReport", "affectedFiles"];
78
+ readonly required: readonly ["approved", "issues", "summary"];
52
79
  };
53
80
  export interface NotionTicket {
54
81
  id: string;
@@ -69,6 +96,12 @@ export interface ReviewOutput {
69
96
  impactReport: string;
70
97
  affectedFiles: string[];
71
98
  risks?: string;
99
+ testCases: string[];
100
+ }
101
+ export interface DiffReviewOutput {
102
+ approved: boolean;
103
+ issues: string[];
104
+ summary: string;
72
105
  }
73
106
  export interface LockEntry {
74
107
  mode: 'review' | 'execute';
package/dist/config.js CHANGED
@@ -46,6 +46,7 @@ export const CONFIG = {
46
46
  // Agent budgets
47
47
  REVIEW_BUDGET_USD: 2.00,
48
48
  EXECUTE_BUDGET_USD: 15.00,
49
+ DIFF_REVIEW_BUDGET_USD: 0.50,
49
50
  // Agent models (env override → default)
50
51
  get REVIEW_MODEL() {
51
52
  return process.env.REVIEW_MODEL || 'claude-sonnet-4-6';
@@ -53,9 +54,13 @@ export const CONFIG = {
53
54
  get EXECUTE_MODEL() {
54
55
  return process.env.EXECUTE_MODEL || 'claude-opus-4-6';
55
56
  },
57
+ get DIFF_REVIEW_MODEL() {
58
+ return process.env.DIFF_REVIEW_MODEL || 'claude-haiku-4-5-20251001';
59
+ },
56
60
  // Agent limits
57
61
  REVIEW_MAX_TURNS: 25,
58
62
  EXECUTE_MAX_TURNS: 50,
63
+ DIFF_REVIEW_MAX_TURNS: 10,
59
64
  // Stale lock timeout (30 minutes)
60
65
  STALE_LOCK_MS: 30 * 60 * 1000,
61
66
  // Maximum concurrent agents (review + execute combined)
@@ -75,6 +80,17 @@ export const REVIEW_OUTPUT_SCHEMA = {
75
80
  impactReport: { type: 'string' },
76
81
  affectedFiles: { type: 'array', items: { type: 'string' } },
77
82
  risks: { type: 'string' },
83
+ testCases: { type: 'array', items: { type: 'string' } },
84
+ },
85
+ required: ['easeScore', 'confidenceScore', 'spec', 'impactReport', 'affectedFiles', 'testCases'],
86
+ };
87
+ // JSON schema for diff review agent structured output
88
+ export const DIFF_REVIEW_SCHEMA = {
89
+ type: 'object',
90
+ properties: {
91
+ approved: { type: 'boolean' },
92
+ issues: { type: 'array', items: { type: 'string' } },
93
+ summary: { type: 'string' },
78
94
  },
79
- required: ['easeScore', 'confidenceScore', 'spec', 'impactReport', 'affectedFiles'],
95
+ required: ['approved', 'issues', 'summary'],
80
96
  };
package/dist/index.js CHANGED
@@ -2,8 +2,8 @@ import { readFileSync } from 'node:fs';
2
2
  import { execSync } from 'node:child_process';
3
3
  import { join } from 'node:path';
4
4
  import { query } from '@anthropic-ai/claude-agent-sdk';
5
- import { CONFIG, REVIEW_OUTPUT_SCHEMA, isPro } from './config.js';
6
- import { sleep, clamp, extractJsonFromOutput, shellEscape, extractNumber, loadEnv, parseEnvFile, createWorktree, removeWorktree, getDefaultBranch, validateNoBlockedFiles } from './lib/utils.js';
5
+ import { CONFIG, REVIEW_OUTPUT_SCHEMA, DIFF_REVIEW_SCHEMA, isPro } from './config.js';
6
+ import { sleep, clamp, extractJsonFromOutput, shellEscape, extractNumber, loadEnv, parseEnvFile, createWorktree, removeWorktree, getDefaultBranch, validateNoBlockedFiles, readLearnings, appendLearning } from './lib/utils.js';
7
7
  import { getProjectDir, getProjectNames, getBuildCommand, getBaseBranch, getBlockedFiles, getSkipPR, getDevAccess, getEnvFile } from './lib/projects.js';
8
8
  import { fetchTicketsByStatus, fetchTicketDetails, writeReviewResults, writeExecutionResults, moveTicketStatus, writeFailure, addComment, } from './lib/notion.js';
9
9
  import { PACKAGE_ROOT, CONFIG_DIR } from './lib/paths.js';
@@ -13,11 +13,14 @@ loadEnv(join(CONFIG_DIR, '.env.local'));
13
13
  delete process.env.CLAUDECODE;
14
14
  // -- Subcommand routing --
15
15
  const subcommand = process.argv[2];
16
- if (subcommand === 'init' || subcommand === 'doctor' || subcommand === 'model') {
17
- const { runInit, runDoctor, runModel } = await import('./cli.js');
16
+ if (subcommand === 'init' || subcommand === 'doctor' || subcommand === 'model' || subcommand === 'learnings') {
17
+ const { runInit, runDoctor, runModel, runLearnings } = await import('./cli.js');
18
18
  if (subcommand === 'model') {
19
19
  await runModel(process.argv.slice(3));
20
20
  }
21
+ else if (subcommand === 'learnings') {
22
+ await runLearnings(process.argv.slice(3));
23
+ }
21
24
  else {
22
25
  await (subcommand === 'init' ? runInit() : runDoctor());
23
26
  }
@@ -48,6 +51,7 @@ function log(color, label, msg) {
48
51
  // -- Prompt loading (bundled with the package) --
49
52
  const reviewPrompt = readFileSync(join(PACKAGE_ROOT, 'prompts', 'review.md'), 'utf-8');
50
53
  const executePrompt = readFileSync(join(PACKAGE_ROOT, 'prompts', 'execute.md'), 'utf-8');
54
+ const diffReviewPrompt = readFileSync(join(PACKAGE_ROOT, 'prompts', 'diff-review.md'), 'utf-8');
51
55
  // -- Agent Runner --
52
56
  async function runReviewAgent(ticket) {
53
57
  const projectDir = getProjectDir(ticket.project);
@@ -72,6 +76,10 @@ async function runReviewAgent(ticket) {
72
76
  if (blockedFiles.length > 0) {
73
77
  promptParts.push('', '## BLOCKED FILES — CANNOT BE MODIFIED', 'The following file patterns are off-limits. Factor this into your scoring — if the natural implementation would touch these files, lower the ease and confidence scores and note it in risks.', '', ...blockedFiles.map(p => `- \`${p}\``));
74
78
  }
79
+ const learnings = readLearnings(projectDir);
80
+ if (learnings) {
81
+ promptParts.push('', '## Project Learnings', 'These are patterns and lessons learned from previous work on this project:', '', learnings);
82
+ }
75
83
  const prompt = promptParts.join('\n');
76
84
  const messages = query({
77
85
  prompt,
@@ -139,6 +147,7 @@ async function runReviewAgent(ticket) {
139
147
  impactReport: String(parsed.impactReport ?? ''),
140
148
  affectedFiles: Array.isArray(parsed.affectedFiles) ? parsed.affectedFiles.map(String) : [],
141
149
  risks: parsed.risks ? String(parsed.risks) : undefined,
150
+ testCases: Array.isArray(parsed.testCases) ? parsed.testCases.map(String) : [],
142
151
  };
143
152
  await writeReviewResults(ticket.id, results);
144
153
  await moveTicketStatus(ticket.id, CONFIG.COLUMNS.SCORED);
@@ -151,8 +160,121 @@ async function runReviewAgent(ticket) {
151
160
  `Cost: $${cost.toFixed(2)} | Duration: ${duration}s`,
152
161
  ].join('\n');
153
162
  await addComment(ticket.id, comment);
163
+ appendLearning(projectDir, [
164
+ `**Review: ${ticket.title}**`,
165
+ `Ease: ${results.easeScore}/10, Confidence: ${results.confidenceScore}/10`,
166
+ `Affected files: ${results.affectedFiles.join(', ')}`,
167
+ results.risks ? `Risks: ${results.risks}` : '',
168
+ ].filter(Boolean).join('\n'));
154
169
  log(GREEN, 'REVIEW', `Done: ease=${results.easeScore} confidence=${results.confidenceScore} cost=$${cost.toFixed(2)}`);
155
170
  }
171
+ async function runDiffReviewAgent(worktreeDir, baseBranch, spec, description, affectedFiles) {
172
+ // Get the full diff
173
+ let diff = '';
174
+ try {
175
+ diff = execSync(`git diff origin/${shellEscape(baseBranch)}...HEAD`, { cwd: worktreeDir, stdio: 'pipe', maxBuffer: 10 * 1024 * 1024 }).toString();
176
+ }
177
+ catch {
178
+ // If diff fails (e.g. no commits), treat as empty
179
+ diff = '';
180
+ }
181
+ // Nothing to review if no changes
182
+ if (!diff.trim()) {
183
+ return {
184
+ result: { approved: true, issues: [], summary: 'No changes to review' },
185
+ cost: 0,
186
+ };
187
+ }
188
+ // Truncate very large diffs to stay within budget
189
+ const maxDiffLength = 100_000;
190
+ const truncatedDiff = diff.length > maxDiffLength
191
+ ? diff.slice(0, maxDiffLength) + '\n\n... (diff truncated)'
192
+ : diff;
193
+ const prompt = [
194
+ diffReviewPrompt,
195
+ '',
196
+ '## Spec',
197
+ spec || '(no spec provided)',
198
+ '',
199
+ '## Ticket Description',
200
+ description || '(no description provided)',
201
+ '',
202
+ '## Affected Files (from review)',
203
+ affectedFiles.length > 0 ? affectedFiles.map(f => `- ${f}`).join('\n') : '(none listed)',
204
+ '',
205
+ '## Diff',
206
+ '```diff',
207
+ truncatedDiff,
208
+ '```',
209
+ ].join('\n');
210
+ const messages = query({
211
+ prompt,
212
+ options: {
213
+ model: CONFIG.DIFF_REVIEW_MODEL,
214
+ cwd: worktreeDir,
215
+ allowedTools: ['Read'],
216
+ maxTurns: CONFIG.DIFF_REVIEW_MAX_TURNS,
217
+ maxBudgetUsd: CONFIG.DIFF_REVIEW_BUDGET_USD,
218
+ permissionMode: 'bypassPermissions',
219
+ allowDangerouslySkipPermissions: true,
220
+ systemPrompt: { type: 'preset', preset: 'claude_code' },
221
+ outputFormat: {
222
+ type: 'json_schema',
223
+ schema: DIFF_REVIEW_SCHEMA,
224
+ },
225
+ stderr: (data) => {
226
+ if (data.trim())
227
+ log(DIM, 'STDERR', data.trim());
228
+ },
229
+ },
230
+ });
231
+ let output = '';
232
+ let structuredOutput = undefined;
233
+ let cost = 0;
234
+ for await (const message of messages) {
235
+ if (message.type === 'assistant') {
236
+ const content = message.message?.content;
237
+ if (Array.isArray(content)) {
238
+ for (const block of content) {
239
+ if (block.type === 'text') {
240
+ output = block.text;
241
+ }
242
+ }
243
+ }
244
+ else if (typeof content === 'string') {
245
+ output = content;
246
+ }
247
+ }
248
+ if (message.type === 'result') {
249
+ cost = message.total_cost_usd ?? 0;
250
+ if (message.subtype !== 'success') {
251
+ throw new Error(`Diff review agent failed: ${message.subtype}`);
252
+ }
253
+ if ('structured_output' in message && message.structured_output != null) {
254
+ structuredOutput = message.structured_output;
255
+ }
256
+ if (message.result) {
257
+ output = message.result;
258
+ }
259
+ }
260
+ }
261
+ const parsed = structuredOutput ?? extractJsonFromOutput(output);
262
+ if (!parsed) {
263
+ // If we can't parse the output, default to approved to avoid blocking
264
+ return {
265
+ result: { approved: true, issues: [], summary: 'Diff review agent did not return structured output; defaulting to approved' },
266
+ cost,
267
+ };
268
+ }
269
+ return {
270
+ result: {
271
+ approved: Boolean(parsed.approved),
272
+ issues: Array.isArray(parsed.issues) ? parsed.issues.map(String) : [],
273
+ summary: String(parsed.summary ?? ''),
274
+ },
275
+ cost,
276
+ };
277
+ }
156
278
  async function runExecuteAgent(ticket) {
157
279
  const projectDir = getProjectDir(ticket.project);
158
280
  if (!projectDir) {
@@ -200,12 +322,20 @@ async function runExecuteAgent(ticket) {
200
322
  '**Page Content**:',
201
323
  ticket.bodyBlocks,
202
324
  ];
325
+ // Highlight acceptance tests if present in the spec
326
+ if (ticket.spec && ticket.spec.includes('## Acceptance Tests')) {
327
+ promptParts.push('', '**IMPORTANT**: The spec above includes Acceptance Tests. Write test files FIRST, then implement code to make them pass. Run the tests to verify.');
328
+ }
203
329
  if (blockedFiles.length > 0) {
204
330
  promptParts.push('', '## BLOCKED FILES — DO NOT TOUCH', 'The following file patterns are off-limits. Do NOT create, modify, or delete any files matching these patterns. Violations will cause the entire run to fail.', '', ...blockedFiles.map((p) => `- \`${p}\``));
205
331
  }
206
332
  if (devAccess) {
207
333
  promptParts.push('', '## DEV ENVIRONMENT ACCESS', 'You have access to run scripts and dev tools in this project. Use this to:', '- Write and run scripts to understand database schema or existing data', '- Hit local API endpoints with curl to understand response shapes', '- Run tests to verify your implementation', '- Use ORM tools (e.g. `npx prisma studio`) to inspect the data model', '', '### Rules', '- Do NOT run database migrations (`prisma migrate`, `db push`, `alembic`, etc.)', '- Do NOT drop, truncate, or bulk-delete data', '- Do NOT make requests to external/production hosts — only localhost and 127.0.0.1', '- Clean up any temporary scripts you create before your final commit', '- If you create test data, document it in a commit message so reviewers know');
208
334
  }
335
+ const learnings = readLearnings(projectDir);
336
+ if (learnings) {
337
+ promptParts.push('', '## Project Learnings', 'These are patterns and lessons learned from previous work on this project:', '', learnings);
338
+ }
209
339
  const prompt = promptParts.join('\n');
210
340
  // Build agent environment when envFile is configured
211
341
  let agentEnv;
@@ -267,6 +397,14 @@ async function runExecuteAgent(ticket) {
267
397
  // If branch doesn't exist or no commits, count is 0
268
398
  commitCount = 0;
269
399
  }
400
+ // Post-execution: diff review
401
+ log(YELLOW, 'REVIEW', 'Running diff review...');
402
+ const diffReview = await runDiffReviewAgent(worktreeDir, baseBranch, ticket.spec ?? '', ticket.description, []);
403
+ cost += diffReview.cost;
404
+ if (!diffReview.result.approved) {
405
+ throw new Error(`Diff review failed:\n${diffReview.result.issues.map(i => ` - ${i}`).join('\n')}`);
406
+ }
407
+ log(GREEN, 'REVIEW', `Diff review passed: ${diffReview.result.summary}`);
270
408
  // Post-execution: validate build
271
409
  const buildCmd = getBuildCommand(ticket.project);
272
410
  let buildPassed = true;
@@ -346,6 +484,11 @@ async function runExecuteAgent(ticket) {
346
484
  `Cost: $${cost.toFixed(2)} | Duration: ${duration}s`,
347
485
  ].join('\n');
348
486
  await addComment(ticket.id, comment);
487
+ appendLearning(projectDir, [
488
+ `**Execute: ${ticket.title}**`,
489
+ `Branch: ${branchName}, Commits: ${commitCount}`,
490
+ `Cost: $${cost.toFixed(2)}`,
491
+ ].join('\n'));
349
492
  log(GREEN, 'EXECUTE', `Done: branch=${branchName} cost=$${cost.toFixed(2)}${prUrl ? ` pr=${prUrl}` : ''}`);
350
493
  }
351
494
  catch (error) {
@@ -359,6 +502,10 @@ async function runExecuteAgent(ticket) {
359
502
  `Cost: $${cost.toFixed(2)} | Duration: ${duration}s`,
360
503
  ].join('\n');
361
504
  await addComment(ticket.id, comment);
505
+ appendLearning(projectDir, [
506
+ `**Failed execute: ${ticket.title}**`,
507
+ `Error: ${errMsg.slice(0, 200)}`,
508
+ ].join('\n'));
362
509
  throw error;
363
510
  }
364
511
  finally {
@@ -404,6 +551,16 @@ async function handleTicket(mode, ticket) {
404
551
  ].join('\n');
405
552
  await addComment(ticket.id, comment);
406
553
  }
554
+ // Append failure learning (execute handles its own in runExecuteAgent catch)
555
+ if (mode === 'review') {
556
+ const projectDir = getProjectDir(ticket.project);
557
+ if (projectDir) {
558
+ appendLearning(projectDir, [
559
+ `**Failed review: ${ticket.title}**`,
560
+ `Error: ${errMsg.slice(0, 200)}`,
561
+ ].join('\n'));
562
+ }
563
+ }
407
564
  try {
408
565
  await writeFailure(ticket.id, errMsg);
409
566
  }
@@ -129,12 +129,17 @@ export async function fetchTicketDetails(pageId) {
129
129
  * Write review results back to the ticket properties.
130
130
  */
131
131
  export async function writeReviewResults(pageId, results) {
132
+ // Build spec content, appending test cases if present
133
+ let specContent = results.spec;
134
+ if (results.testCases && results.testCases.length > 0) {
135
+ specContent += '\n\n## Acceptance Tests\n' + results.testCases.map(tc => `- ${tc}`).join('\n');
136
+ }
132
137
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
133
138
  const properties = {
134
139
  Ease: { number: results.easeScore },
135
140
  Confidence: { number: results.confidenceScore },
136
141
  Spec: {
137
- rich_text: chunkRichText(results.spec),
142
+ rich_text: chunkRichText(specContent),
138
143
  },
139
144
  Impact: {
140
145
  rich_text: chunkRichText(`${results.impactReport}\n\nFiles: ${results.affectedFiles.join(', ')}${results.risks ? `\n\nRisks: ${results.risks}` : ''}`),
@@ -26,3 +26,5 @@ export declare function ensureWorktreesIgnored(projectDir: string): void;
26
26
  export declare function createWorktree(projectDir: string, branchName: string, worktreeDir: string, baseBranch?: string): void;
27
27
  export declare function validateNoBlockedFiles(worktreeDir: string, baseBranch: string, blockedPatterns: string[]): string[];
28
28
  export declare function removeWorktree(projectDir: string, worktreeDir: string): void;
29
+ export declare function readLearnings(projectDir: string): string;
30
+ export declare function appendLearning(projectDir: string, entry: string): void;
package/dist/lib/utils.js CHANGED
@@ -204,11 +204,21 @@ export function _resetDefaultBranchCache() {
204
204
  export function ensureWorktreesIgnored(projectDir) {
205
205
  const gitignorePath = join(projectDir, '.gitignore');
206
206
  try {
207
- const content = existsSync(gitignorePath) ? readFileSync(gitignorePath, 'utf-8') : '';
207
+ let content = existsSync(gitignorePath) ? readFileSync(gitignorePath, 'utf-8') : '';
208
208
  const lines = content.split('\n');
209
+ let modified = false;
209
210
  if (!lines.some((line) => line.trim() === '.worktrees' || line.trim() === '.worktrees/')) {
210
211
  const separator = content.length > 0 && !content.endsWith('\n') ? '\n' : '';
211
- writeFileSync(gitignorePath, content + separator + '.worktrees/\n');
212
+ content = content + separator + '.worktrees/\n';
213
+ modified = true;
214
+ }
215
+ if (!lines.some((line) => line.trim() === '.ticket-to-pr' || line.trim() === '.ticket-to-pr/')) {
216
+ const separator = content.length > 0 && !content.endsWith('\n') ? '\n' : '';
217
+ content = content + separator + '.ticket-to-pr/\n';
218
+ modified = true;
219
+ }
220
+ if (modified) {
221
+ writeFileSync(gitignorePath, content);
212
222
  }
213
223
  }
214
224
  catch {
@@ -351,3 +361,36 @@ export function removeWorktree(projectDir, worktreeDir) {
351
361
  }
352
362
  }
353
363
  }
364
+ // -- Per-project learnings --
365
+ const MAX_LEARNINGS_ENTRIES = 100;
366
+ export function readLearnings(projectDir) {
367
+ const learningsPath = join(projectDir, '.ticket-to-pr', 'learnings.md');
368
+ try {
369
+ return readFileSync(learningsPath, 'utf-8');
370
+ }
371
+ catch {
372
+ return '';
373
+ }
374
+ }
375
+ export function appendLearning(projectDir, entry) {
376
+ const dir = join(projectDir, '.ticket-to-pr');
377
+ mkdirSync(dir, { recursive: true });
378
+ const learningsPath = join(dir, 'learnings.md');
379
+ let content = '';
380
+ try {
381
+ content = readFileSync(learningsPath, 'utf-8');
382
+ }
383
+ catch {
384
+ // File doesn't exist yet
385
+ }
386
+ // Add timestamped entry
387
+ const timestamp = new Date().toISOString().slice(0, 10);
388
+ const newEntry = `### ${timestamp}\n${entry}\n`;
389
+ content = content + '\n' + newEntry;
390
+ // Trim to max entries
391
+ const entries = content.split(/(?=^### \d{4}-\d{2}-\d{2}$)/m).filter(e => e.trim());
392
+ if (entries.length > MAX_LEARNINGS_ENTRIES) {
393
+ content = entries.slice(-MAX_LEARNINGS_ENTRIES).join('');
394
+ }
395
+ writeFileSync(learningsPath, content.trim() + '\n');
396
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ticket-to-pr",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Drag a Notion ticket, get a pull request. AI-powered dev automation.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -0,0 +1,28 @@
1
+ You are a code reviewer checking a diff against its specification.
2
+
3
+ ## Your Task
4
+ 1. Read the diff carefully
5
+ 2. Compare it against the original spec and ticket description
6
+ 3. Check for common issues
7
+ 4. Approve or reject with specific reasons
8
+
9
+ ## Check For
10
+ - Does the diff implement what the spec asked for?
11
+ - Are there modified files not mentioned in the affected files list?
12
+ - Any hardcoded values, debug code, console.logs, or TODOs left behind?
13
+ - Any obvious security issues (exposed secrets, SQL injection, XSS)?
14
+ - Any deleted tests or reduced test coverage?
15
+ - Are imports and exports consistent?
16
+ - Does the code follow the patterns visible in the diff context?
17
+
18
+ ## Output
19
+ Return a JSON object:
20
+ ```json
21
+ {
22
+ "approved": true/false,
23
+ "issues": ["issue 1", "issue 2"],
24
+ "summary": "Brief summary of the review"
25
+ }
26
+ ```
27
+
28
+ If approved is false, be specific about what needs to change. An empty issues array with approved: true means the diff looks good.
@@ -17,5 +17,15 @@ You have been given a ticket with an implementation spec. Follow the spec and im
17
17
  11. If your prompt includes a "BLOCKED FILES" section, you MUST NOT modify any files matching those patterns. Violations will cause the entire run to fail.
18
18
  12. If your prompt includes a "DEV ENVIRONMENT ACCESS" section, you may run scripts and dev tools as described. Always prefer reading code directly over running scripts when possible.
19
19
 
20
+ ## Test-First Development
21
+ If the spec includes acceptance tests, follow this workflow:
22
+ 1. Read the acceptance tests carefully before writing any code
23
+ 2. Write a test file first that captures the acceptance criteria as executable tests
24
+ 3. Implement the code to make the tests pass
25
+ 4. Run the tests to verify your implementation
26
+ 5. If tests fail, fix the implementation until they pass
27
+
28
+ If no test framework is configured in the project, implement the code directly but use the acceptance tests as a checklist — verify each criterion is met before committing.
29
+
20
30
  ## When Done
21
31
  Commit all changes with a final commit message summarizing what was done. The commit message should reference the ticket title.
package/prompts/review.md CHANGED
@@ -30,10 +30,23 @@ You MUST end your response with a JSON code block containing exactly these field
30
30
  "spec": "<step-by-step implementation plan in markdown>",
31
31
  "impactReport": "<which files change and why, in markdown>",
32
32
  "affectedFiles": ["<file1>", "<file2>"],
33
- "risks": "<any concerns or blockers, optional>"
33
+ "risks": "<any concerns or blockers, optional>",
34
+ "testCases": ["<test case 1>", "<test case 2>", "..."]
34
35
  }
35
36
  ```
36
37
 
38
+ ### Test Cases
39
+
40
+ Generate 3-8 acceptance test cases depending on ticket complexity. These are framework-agnostic acceptance criteria (not full test files) that the execute agent must satisfy.
41
+
42
+ - Write each test case as a "GIVEN... WHEN... THEN..." statement or a simple assertion
43
+ - Focus on verifiable outcomes, not implementation details
44
+ - Cover happy path, edge cases, and error handling as appropriate
45
+ - Examples:
46
+ - "GET /api/health returns 200 with JSON body containing status:'ok' and a valid ISO timestamp"
47
+ - "Calling formatDate(null) returns empty string"
48
+ - "GIVEN a user is not authenticated WHEN they request /api/private THEN they receive a 401 response"
49
+
37
50
  ## Rules
38
51
  - DO NOT modify any files. You are read-only.
39
52
  - Be honest about confidence. A low score is valuable information.