edsger 0.58.0 → 0.60.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 (38) hide show
  1. package/dist/api/cross-product.js +0 -1
  2. package/dist/api/issues/issue-utils.js +0 -1
  3. package/dist/api/issues/update-issue.js +1 -1
  4. package/dist/commands/agent-workflow/chat-worker.js +1 -1
  5. package/dist/commands/checklists/index.js +1 -1
  6. package/dist/commands/product-techniques/index.d.ts +15 -0
  7. package/dist/commands/product-techniques/index.js +37 -0
  8. package/dist/commands/recipes/index.d.ts +15 -0
  9. package/dist/commands/recipes/index.js +34 -0
  10. package/dist/commands/workflow/executors/phase-executor.js +1 -1
  11. package/dist/index.js +24 -1
  12. package/dist/phases/analyze-logs/index.js +1 -1
  13. package/dist/phases/bug-fixing/context-fetcher.js +4 -2
  14. package/dist/phases/find-features/index.js +1 -1
  15. package/dist/phases/product-techniques/index.d.ts +52 -0
  16. package/dist/phases/product-techniques/index.js +268 -0
  17. package/dist/phases/product-techniques/mcp-server.d.ts +41 -0
  18. package/dist/phases/product-techniques/mcp-server.js +96 -0
  19. package/dist/phases/product-techniques/prompts.d.ts +19 -0
  20. package/dist/phases/product-techniques/prompts.js +66 -0
  21. package/dist/phases/product-techniques/types.d.ts +13 -0
  22. package/dist/phases/product-techniques/types.js +13 -0
  23. package/dist/phases/recipes/index.d.ts +56 -0
  24. package/dist/phases/recipes/index.js +301 -0
  25. package/dist/phases/recipes/mcp-server.d.ts +63 -0
  26. package/dist/phases/recipes/mcp-server.js +204 -0
  27. package/dist/phases/recipes/prompts.d.ts +35 -0
  28. package/dist/phases/recipes/prompts.js +105 -0
  29. package/dist/phases/recipes/types.d.ts +42 -0
  30. package/dist/phases/recipes/types.js +16 -0
  31. package/dist/phases/screen-flow/mcp-server.d.ts +1 -1
  32. package/dist/services/branches.js +3 -3
  33. package/dist/services/phase-hooks/hook-executor.js +1 -1
  34. package/dist/services/phase-ratings.js +1 -1
  35. package/dist/services/product-logs.js +1 -1
  36. package/dist/services/pull-requests.js +3 -3
  37. package/package.json +1 -1
  38. package/vitest.config.ts +1 -0
@@ -56,7 +56,6 @@ export async function listAllReadyIssues(verbose) {
56
56
  const allIssues = (data || []).map((row) => {
57
57
  // The `product` join lands as a nested object; lift its name onto
58
58
  // the row to keep the wire shape stable.
59
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- shape from PostgREST join
60
59
  const r = row;
61
60
  return {
62
61
  ...r,
@@ -41,7 +41,6 @@ export async function claimNextIssue(productId, verbose) {
41
41
  }
42
42
  // RPC returns rows with `issue_*` prefixed column names — unwrap to
43
43
  // the IssueInfo shape callers already consume.
44
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- raw RPC row
45
44
  const r = row;
46
45
  const issue = {
47
46
  id: r.issue_id,
@@ -91,7 +91,7 @@ export async function markWorkflowPhaseCompleted(issueId, phaseName, verbose) {
91
91
  // Fall through to MCP
92
92
  }
93
93
  }
94
- if (issue == null) {
94
+ if (!issue) {
95
95
  const response = (await callMcpEndpoint('issues/get', {
96
96
  issue_id: issueId,
97
97
  }));
@@ -354,7 +354,7 @@ function startRealtime() {
354
354
  registerChannel(payload.new);
355
355
  // A channel created with messages already in it (unlikely but
356
356
  // possible if INSERTs race) deserves an immediate sweep.
357
- const id = payload.new.id;
357
+ const { id } = payload.new;
358
358
  if (id) {
359
359
  void claimAndProcess(id);
360
360
  }
@@ -116,7 +116,7 @@ export async function runChecklists(options) {
116
116
  ? createChecklistsMcpServer({ kind: 'team', teamId: teamId })
117
117
  : createChecklistsMcpServer({
118
118
  kind: 'product',
119
- productId: productId,
119
+ productId,
120
120
  });
121
121
  const promptParts = isTeamScope
122
122
  ? [
@@ -0,0 +1,15 @@
1
+ /**
2
+ * CLI command: edsger product-techniques <productId> --techniques-id <id>
3
+ *
4
+ * Clones the product's repo, asks Claude to write a catalogue of the
5
+ * techniques the repo uses, and stores the markdown body in
6
+ * product_techniques. The desktop UI creates a pending row first then
7
+ * invokes the CLI with --techniques-id; the CLI flips status running →
8
+ * success/failed and populates the content/summary fields.
9
+ */
10
+ export interface ProductTechniquesCliOptions {
11
+ techniquesId: string;
12
+ guidance?: string;
13
+ verbose?: boolean;
14
+ }
15
+ export declare function runProductTechniques(productId: string, options: ProductTechniquesCliOptions): Promise<void>;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * CLI command: edsger product-techniques <productId> --techniques-id <id>
3
+ *
4
+ * Clones the product's repo, asks Claude to write a catalogue of the
5
+ * techniques the repo uses, and stores the markdown body in
6
+ * product_techniques. The desktop UI creates a pending row first then
7
+ * invokes the CLI with --techniques-id; the CLI flips status running →
8
+ * success/failed and populates the content/summary fields.
9
+ */
10
+ import { runProductTechniquesPhase } from '../../phases/product-techniques/index.js';
11
+ import { logError, logInfo, logSuccess } from '../../utils/logger.js';
12
+ export async function runProductTechniques(productId, options) {
13
+ const { techniquesId, guidance, verbose } = options;
14
+ if (!productId) {
15
+ throw new Error('Product ID is required for product-techniques');
16
+ }
17
+ if (!techniquesId) {
18
+ throw new Error('--techniques-id is required (the pending product_techniques row id)');
19
+ }
20
+ logInfo(`Starting techniques generation for product ${productId}`);
21
+ const result = await runProductTechniquesPhase({
22
+ productId,
23
+ techniquesId,
24
+ guidance,
25
+ verbose,
26
+ });
27
+ if (result.status === 'success') {
28
+ logSuccess(result.message);
29
+ if (result.summary) {
30
+ logInfo(`\nSummary: ${result.summary}`);
31
+ }
32
+ }
33
+ else {
34
+ logError(result.message);
35
+ process.exit(1);
36
+ }
37
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * CLI command: edsger recipes <productId> --scan-id <id>
3
+ *
4
+ * Clones the product's repo, asks Claude to identify implementation recipes
5
+ * the product uses, and persists them via the recipes / product_recipes
6
+ * tables. The desktop UI creates a pending recipe_scans row first then
7
+ * invokes the CLI with --scan-id; the CLI flips status running →
8
+ * success/failed and writes recipes via the MCP toolkit.
9
+ */
10
+ export interface RecipesCliOptions {
11
+ scanId: string;
12
+ guidance?: string;
13
+ verbose?: boolean;
14
+ }
15
+ export declare function runRecipes(productId: string, options: RecipesCliOptions): Promise<void>;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * CLI command: edsger recipes <productId> --scan-id <id>
3
+ *
4
+ * Clones the product's repo, asks Claude to identify implementation recipes
5
+ * the product uses, and persists them via the recipes / product_recipes
6
+ * tables. The desktop UI creates a pending recipe_scans row first then
7
+ * invokes the CLI with --scan-id; the CLI flips status running →
8
+ * success/failed and writes recipes via the MCP toolkit.
9
+ */
10
+ import { runRecipesPhase } from '../../phases/recipes/index.js';
11
+ import { logError, logInfo, logSuccess } from '../../utils/logger.js';
12
+ export async function runRecipes(productId, options) {
13
+ const { scanId, guidance, verbose } = options;
14
+ if (!productId) {
15
+ throw new Error('Product ID is required for recipes');
16
+ }
17
+ if (!scanId) {
18
+ throw new Error('--scan-id is required (the pending recipe_scans row id)');
19
+ }
20
+ logInfo(`Starting recipes scan for product ${productId}`);
21
+ const result = await runRecipesPhase({
22
+ productId,
23
+ scanId,
24
+ guidance,
25
+ verbose,
26
+ });
27
+ if (result.status === 'success') {
28
+ logSuccess(result.message);
29
+ }
30
+ else {
31
+ logError(result.message);
32
+ process.exit(1);
33
+ }
34
+ }
@@ -3,7 +3,7 @@
3
3
  */
4
4
  import { checkApprovalBeforePhaseExecution } from '../../../api/issues/approval-checker.js';
5
5
  import { updateIssueStatusForPhase } from '../../../api/issues/index.js';
6
- import { logIssuePhaseEvent, } from '../../../services/audit-logs.js';
6
+ import { logIssuePhaseEvent } from '../../../services/audit-logs.js';
7
7
  import { getChecklistsForPhase, processChecklistItemResultsFromResponse, processChecklistResultsFromResponse, validateChecklistsForPhase, validateRequiredChecklistResults, } from '../../../services/checklist.js';
8
8
  import { runHooksForPhase } from '../../../services/phase-hooks/index.js';
9
9
  import { logDebug, logInfo, logWarning } from '../../../utils/logger.js';
package/dist/index.js CHANGED
@@ -19,17 +19,18 @@ import { runFindBugs } from './commands/find-bugs/index.js';
19
19
  import { runFindFeatures } from './commands/find-features/index.js';
20
20
  import { parseCategoriesOption, runFindSmells, } from './commands/find-smells/index.js';
21
21
  import { runGrowthAnalysis } from './commands/growth-analysis/index.js';
22
- import { runScreenFlow } from './commands/screen-flow/index.js';
23
22
  import { runInit } from './commands/init/index.js';
24
23
  import { runIntelligence } from './commands/intelligence/index.js';
25
24
  import { runIssueAnalysisCommand } from './commands/issue-analysis/index.js';
26
25
  import { runPRResolve } from './commands/pr-resolve/index.js';
27
26
  import { runPRReview } from './commands/pr-review/index.js';
28
27
  import { runProductTestCases } from './commands/product-test-cases/index.js';
28
+ import { runRecipes } from './commands/recipes/index.js';
29
29
  import { runQualityBenchmarkCli } from './commands/quality-benchmark/index.js';
30
30
  import { runRefactor } from './commands/refactor/refactor.js';
31
31
  import { runReleaseSyncCommand } from './commands/release-sync/index.js';
32
32
  import { runRunSheetCommand } from './commands/run-sheet/index.js';
33
+ import { runScreenFlow } from './commands/screen-flow/index.js';
33
34
  import { runSmokeTestCommand } from './commands/smoke-test/index.js';
34
35
  import { runSyncGithubIssues } from './commands/sync-github-issues/index.js';
35
36
  import { runSyncSentryIssues } from './commands/sync-sentry-issues/index.js';
@@ -694,6 +695,28 @@ program
694
695
  }
695
696
  });
696
697
  // ============================================================
698
+ // Subcommand: edsger recipes <productId>
699
+ // ============================================================
700
+ program
701
+ .command('recipes <productId>')
702
+ .description("Scan a product's repo for the implementation recipes it uses (which services/tools are chained together to deliver each capability) and persist them via the recipes / product_recipes tables. Writes against the pending recipe_scans row identified by --scan-id.")
703
+ .requiredOption('--scan-id <id>', 'Pending recipe_scans row id to drive (created by the desktop UI before invocation)')
704
+ .option('-g, --guidance <text>', 'Human direction for the AI (focus areas, exclusions)')
705
+ .option('-v, --verbose', 'Verbose output')
706
+ .action(async (productId, opts) => {
707
+ try {
708
+ await runRecipes(productId, {
709
+ scanId: opts.scanId,
710
+ guidance: opts.guidance,
711
+ verbose: opts.verbose,
712
+ });
713
+ }
714
+ catch (error) {
715
+ logError(error instanceof Error ? error.message : String(error));
716
+ process.exit(1);
717
+ }
718
+ });
719
+ // ============================================================
697
720
  // Subcommand: edsger pr-resolve <productId>
698
721
  // ============================================================
699
722
  program
@@ -5,11 +5,11 @@
5
5
  * user's session logs (only if the latest log is >1 hour old), and creates
6
6
  * product_user_feedbacks for any issues or improvement suggestions found.
7
7
  */
8
+ import { callMcpEndpoint } from '../../api/mcp-client.js';
8
9
  import { getProduct } from '../../api/products.js';
9
10
  import { getPendingLogsByUser, markLogsAnalyzed, } from '../../services/product-logs.js';
10
11
  import { getSupabase, hasSupabaseSession } from '../../supabase/client.js';
11
12
  import { logError, logInfo, logSuccess } from '../../utils/logger.js';
12
- import { callMcpEndpoint } from '../../api/mcp-client.js';
13
13
  import { executeLogAnalysis } from './agent.js';
14
14
  import { createAnalyzeLogsSystemPrompt, createAnalyzeLogsUserPrompt, } from './prompts.js';
15
15
  /**
@@ -41,8 +41,10 @@ export async function fetchBugFixingContext(issueId, verbose) {
41
41
  // Fall through to MCP
42
42
  }
43
43
  }
44
- if (raw == null) {
45
- const testReportResult = (await callMcpEndpoint('test_reports/latest', { issue_id: issueId }));
44
+ if (!raw) {
45
+ const testReportResult = (await callMcpEndpoint('test_reports/latest', {
46
+ issue_id: issueId,
47
+ }));
46
48
  raw = testReportResult.test_report ?? null;
47
49
  }
48
50
  if (raw) {
@@ -267,7 +267,7 @@ async function fetchIntelligenceReports(productId, focusReportId) {
267
267
  if (error) {
268
268
  throw new Error(error.message);
269
269
  }
270
- reports = (data || []);
270
+ reports = data || [];
271
271
  usedSdk = true;
272
272
  }
273
273
  catch {
@@ -0,0 +1,52 @@
1
+ /**
2
+ * product-techniques phase: clone the product's repo, ask Claude (via the
3
+ * `submit_techniques` MCP tool) to write a catalogue of the techniques the
4
+ * repo uses, and persist the result to product_techniques via the Supabase
5
+ * SDK.
6
+ *
7
+ * Mirrors the screen-flow pattern. Production-grade behaviours layered on
8
+ * top of the basic generate-and-write loop:
9
+ *
10
+ * - Heartbeat: `last_heartbeat_at` is refreshed on every assistant message
11
+ * so the reader can detect stalled / crashed runs (see services/db/
12
+ * product-techniques.ts for the lazy reaper).
13
+ * - Cancellation-safe writes: markRunning / markSuccess / markFailed only
14
+ * touch rows whose status is in {pending, running}. If the user clicked
15
+ * Stop and the row is now 'cancelled', the final write no-ops.
16
+ * - Tool-based submission: validated server-side via Zod + content checks
17
+ * (mcp-server.ts). Falls back to fenced JSON parsing for resilience.
18
+ */
19
+ import type { SupabaseClient } from '@supabase/supabase-js';
20
+ export interface ProductTechniquesOptions {
21
+ productId: string;
22
+ techniquesId: string;
23
+ guidance?: string;
24
+ verbose?: boolean;
25
+ }
26
+ export interface ProductTechniquesResult {
27
+ status: 'success' | 'error' | 'cancelled';
28
+ message: string;
29
+ summary?: string;
30
+ }
31
+ export declare function runProductTechniquesPhase(options: ProductTechniquesOptions): Promise<ProductTechniquesResult>;
32
+ /**
33
+ * Claim the row by flipping `pending` → `running`. Returns true on success
34
+ * (we won the claim) and false when the row has already moved on (e.g. user
35
+ * cancelled before the CLI started). Bounded by the status filter so we
36
+ * can't accidentally resurrect a 'cancelled' row.
37
+ */
38
+ export declare function markRunning(supabase: SupabaseClient, techniquesId: string): Promise<boolean>;
39
+ /**
40
+ * Touch the heartbeat. Best-effort — if it fails (network blip, RLS), the
41
+ * agent loop keeps running; the reader treats this row as stale and marks
42
+ * it failed on next read, which is the correct behaviour.
43
+ */
44
+ export declare function heartbeat(supabase: SupabaseClient, techniquesId: string): Promise<void>;
45
+ /**
46
+ * Write failure status iff the row is still in an active state. Returns
47
+ * true if the row was actually updated (so the caller knows whether the
48
+ * agent's verdict made it to the DB). Returns false when the row has
49
+ * already been cancelled or otherwise resolved by someone else.
50
+ */
51
+ export declare function markFailed(supabase: SupabaseClient, techniquesId: string, errorMessage: string): Promise<boolean>;
52
+ export declare function markSuccess(supabase: SupabaseClient, techniquesId: string, summary: string, content: string): Promise<boolean>;
@@ -0,0 +1,268 @@
1
+ /**
2
+ * product-techniques phase: clone the product's repo, ask Claude (via the
3
+ * `submit_techniques` MCP tool) to write a catalogue of the techniques the
4
+ * repo uses, and persist the result to product_techniques via the Supabase
5
+ * SDK.
6
+ *
7
+ * Mirrors the screen-flow pattern. Production-grade behaviours layered on
8
+ * top of the basic generate-and-write loop:
9
+ *
10
+ * - Heartbeat: `last_heartbeat_at` is refreshed on every assistant message
11
+ * so the reader can detect stalled / crashed runs (see services/db/
12
+ * product-techniques.ts for the lazy reaper).
13
+ * - Cancellation-safe writes: markRunning / markSuccess / markFailed only
14
+ * touch rows whose status is in {pending, running}. If the user clicked
15
+ * Stop and the row is now 'cancelled', the final write no-ops.
16
+ * - Tool-based submission: validated server-side via Zod + content checks
17
+ * (mcp-server.ts). Falls back to fenced JSON parsing for resilience.
18
+ */
19
+ import { query } from '@anthropic-ai/claude-agent-sdk';
20
+ import { getGitHubConfigByProduct } from '../../api/github.js';
21
+ import { DEFAULT_MODEL } from '../../constants.js';
22
+ import { getSupabase } from '../../supabase/client.js';
23
+ import { logError, logInfo, logSuccess, logWarning, } from '../../utils/logger.js';
24
+ import { cleanupIssueRepo, cloneIssueRepo, ensureWorkspaceDir, } from '../../workspace/workspace-manager.js';
25
+ import { fetchProductBasics } from '../find-shared/mcp.js';
26
+ import { createPromptGenerator, extractTextFromContent, tryExtractResult, } from '../pr-shared/agent-utils.js';
27
+ import { createTechniquesCaptureState, createTechniquesMcpServer, validateContent, } from './mcp-server.js';
28
+ import { createProductTechniquesSystemPrompt, createProductTechniquesUserPrompt, } from './prompts.js';
29
+ import { isTechniquesExtraction, TECHNIQUES_CONTENT_MAX, TECHNIQUES_SUMMARY_MAX, } from './types.js';
30
+ const WORKSPACE_KEY = 'product-techniques';
31
+ const MAX_TURNS = 120;
32
+ // Heartbeat cadence: at most one DB write per HEARTBEAT_MIN_INTERVAL_MS.
33
+ // Triggered on every assistant message so a stalled agent (no messages
34
+ // flowing) lets the row go stale and the reader can mark it failed.
35
+ const HEARTBEAT_MIN_INTERVAL_MS = 15_000;
36
+ export async function runProductTechniquesPhase(options) {
37
+ const { productId, techniquesId, guidance, verbose } = options;
38
+ logInfo(`Starting product-techniques generation for product ${productId}`);
39
+ const supabase = getSupabase();
40
+ const claimed = await markRunning(supabase, techniquesId);
41
+ if (!claimed) {
42
+ return {
43
+ status: 'cancelled',
44
+ message: 'Techniques row is no longer in a runnable state (likely cancelled before the CLI started)',
45
+ };
46
+ }
47
+ const githubConfig = await getGitHubConfigByProduct(productId, verbose);
48
+ if (!githubConfig.configured ||
49
+ !githubConfig.token ||
50
+ !githubConfig.owner ||
51
+ !githubConfig.repo) {
52
+ const msg = githubConfig.message ||
53
+ 'GitHub repository not configured for this product. Connect a repo first.';
54
+ await markFailed(supabase, techniquesId, msg);
55
+ return { status: 'error', message: msg };
56
+ }
57
+ let repoPath;
58
+ let succeeded = false;
59
+ try {
60
+ const workspaceRoot = ensureWorkspaceDir();
61
+ const repoKey = `${WORKSPACE_KEY}-${productId}`;
62
+ ({ repoPath } = cloneIssueRepo(workspaceRoot, repoKey, githubConfig.owner, githubConfig.repo, githubConfig.token));
63
+ const product = await fetchProductBasics(productId);
64
+ const systemPrompt = createProductTechniquesSystemPrompt();
65
+ const userPrompt = createProductTechniquesUserPrompt({
66
+ productName: product.name,
67
+ productDescription: product.description,
68
+ guidance,
69
+ });
70
+ logInfo('Running Claude agent to write techniques catalogue...');
71
+ const captureState = createTechniquesCaptureState();
72
+ const mcpServer = createTechniquesMcpServer(captureState);
73
+ let lastAssistantResponse = '';
74
+ let lastHeartbeatAt = 0;
75
+ for await (const message of query({
76
+ prompt: createPromptGenerator(userPrompt),
77
+ options: {
78
+ systemPrompt: {
79
+ type: 'preset',
80
+ preset: 'claude_code',
81
+ append: systemPrompt,
82
+ },
83
+ model: DEFAULT_MODEL,
84
+ maxTurns: MAX_TURNS,
85
+ permissionMode: 'bypassPermissions',
86
+ cwd: repoPath,
87
+ mcpServers: {
88
+ 'product-techniques': mcpServer,
89
+ },
90
+ },
91
+ })) {
92
+ if (message.type === 'assistant') {
93
+ lastAssistantResponse += extractTextFromContent(message.message?.content ?? [], verbose);
94
+ // Throttled heartbeat. Awaited (cheap UPDATE) so we don't pile up
95
+ // unresolved promises on a long run.
96
+ const now = Date.now();
97
+ if (now - lastHeartbeatAt >= HEARTBEAT_MIN_INTERVAL_MS) {
98
+ lastHeartbeatAt = now;
99
+ await heartbeat(supabase, techniquesId);
100
+ }
101
+ continue;
102
+ }
103
+ if (message.type !== 'result') {
104
+ continue;
105
+ }
106
+ // Prefer the MCP-captured extraction. Fall back to fenced JSON only
107
+ // if the agent ignored the tool — robustness, not a contract.
108
+ const fromTool = captureState.captured;
109
+ const fromFallback = fromTool
110
+ ? null
111
+ : tryFallbackParse(message, lastAssistantResponse);
112
+ const extraction = fromTool ?? fromFallback;
113
+ if (!extraction) {
114
+ const msg = message.subtype === 'success'
115
+ ? 'Techniques generation failed: agent did not call submit_techniques and no parseable fallback was found'
116
+ : `Techniques generation failed: agent ${message.subtype}`;
117
+ const written = await markFailed(supabase, techniquesId, msg);
118
+ return {
119
+ status: written ? 'error' : 'cancelled',
120
+ message: written
121
+ ? msg
122
+ : 'Generation was cancelled while the agent was running',
123
+ };
124
+ }
125
+ if (fromFallback) {
126
+ logWarning('Agent skipped submit_techniques; used fenced fallback JSON instead');
127
+ }
128
+ const written = await markSuccess(supabase, techniquesId, extraction.summary, extraction.content);
129
+ if (!written) {
130
+ return {
131
+ status: 'cancelled',
132
+ message: 'Generation was cancelled before the result could be written',
133
+ };
134
+ }
135
+ succeeded = true;
136
+ logSuccess(`Techniques catalogue generated (${extraction.content.length} chars of markdown)`);
137
+ return {
138
+ status: 'success',
139
+ message: 'Techniques catalogue generated',
140
+ summary: extraction.summary,
141
+ };
142
+ }
143
+ // Loop ended without a 'result' message — treat as failure.
144
+ const msg = 'Techniques generation ended without a result message';
145
+ await markFailed(supabase, techniquesId, msg);
146
+ return { status: 'error', message: msg };
147
+ }
148
+ catch (error) {
149
+ const errorMessage = error instanceof Error ? error.message : String(error);
150
+ logError(`Techniques generation failed: ${errorMessage}`);
151
+ await markFailed(supabase, techniquesId, errorMessage);
152
+ return { status: 'error', message: errorMessage };
153
+ }
154
+ finally {
155
+ if (succeeded) {
156
+ cleanupIssueRepo(repoPath);
157
+ }
158
+ }
159
+ }
160
+ // Fallback parser: extract a fenced `techniques` JSON block from the final
161
+ // assistant text if the agent skipped the submit_techniques tool. Backstop
162
+ // only — we expect the tool path to be the norm.
163
+ function tryFallbackParse(resultMessage, assistantText) {
164
+ const responseText = resultMessage.subtype === 'success'
165
+ ? resultMessage.result || assistantText
166
+ : assistantText;
167
+ const parsed = tryExtractResult(responseText, 'techniques');
168
+ if (!isTechniquesExtraction(parsed)) {
169
+ return null;
170
+ }
171
+ if (parsed.summary.length > TECHNIQUES_SUMMARY_MAX) {
172
+ return null;
173
+ }
174
+ if (parsed.content.length > TECHNIQUES_CONTENT_MAX) {
175
+ return null;
176
+ }
177
+ const { error } = validateContent(parsed.content);
178
+ if (error) {
179
+ return null;
180
+ }
181
+ return parsed;
182
+ }
183
+ // ============================================================================
184
+ // Persistence — exported for unit tests
185
+ // ============================================================================
186
+ /**
187
+ * Claim the row by flipping `pending` → `running`. Returns true on success
188
+ * (we won the claim) and false when the row has already moved on (e.g. user
189
+ * cancelled before the CLI started). Bounded by the status filter so we
190
+ * can't accidentally resurrect a 'cancelled' row.
191
+ */
192
+ export async function markRunning(supabase, techniquesId) {
193
+ const { data, error } = await supabase
194
+ .from('product_techniques')
195
+ .update({
196
+ status: 'running',
197
+ error: null,
198
+ last_heartbeat_at: new Date().toISOString(),
199
+ })
200
+ .eq('id', techniquesId)
201
+ .in('status', ['pending', 'running'])
202
+ .select('id')
203
+ .maybeSingle();
204
+ if (error) {
205
+ logWarning(`Could not mark techniques as running: ${error.message}`);
206
+ return false;
207
+ }
208
+ return data !== null;
209
+ }
210
+ /**
211
+ * Touch the heartbeat. Best-effort — if it fails (network blip, RLS), the
212
+ * agent loop keeps running; the reader treats this row as stale and marks
213
+ * it failed on next read, which is the correct behaviour.
214
+ */
215
+ export async function heartbeat(supabase, techniquesId) {
216
+ const { error } = await supabase
217
+ .from('product_techniques')
218
+ .update({ last_heartbeat_at: new Date().toISOString() })
219
+ .eq('id', techniquesId)
220
+ .eq('status', 'running');
221
+ if (error) {
222
+ logWarning(`Heartbeat failed: ${error.message}`);
223
+ }
224
+ }
225
+ /**
226
+ * Write failure status iff the row is still in an active state. Returns
227
+ * true if the row was actually updated (so the caller knows whether the
228
+ * agent's verdict made it to the DB). Returns false when the row has
229
+ * already been cancelled or otherwise resolved by someone else.
230
+ */
231
+ export async function markFailed(supabase, techniquesId, errorMessage) {
232
+ const { data, error } = await supabase
233
+ .from('product_techniques')
234
+ .update({
235
+ status: 'failed',
236
+ error: errorMessage,
237
+ completed_at: new Date().toISOString(),
238
+ })
239
+ .eq('id', techniquesId)
240
+ .in('status', ['pending', 'running'])
241
+ .select('id')
242
+ .maybeSingle();
243
+ if (error) {
244
+ logWarning(`Could not mark techniques as failed: ${error.message}`);
245
+ return false;
246
+ }
247
+ return data !== null;
248
+ }
249
+ export async function markSuccess(supabase, techniquesId, summary, content) {
250
+ const { data, error } = await supabase
251
+ .from('product_techniques')
252
+ .update({
253
+ status: 'success',
254
+ summary,
255
+ content,
256
+ error: null,
257
+ completed_at: new Date().toISOString(),
258
+ })
259
+ .eq('id', techniquesId)
260
+ .in('status', ['pending', 'running'])
261
+ .select('id')
262
+ .maybeSingle();
263
+ if (error) {
264
+ logWarning(`Could not mark techniques as success: ${error.message}`);
265
+ return false;
266
+ }
267
+ return data !== null;
268
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * In-process MCP server exposing a single tool — `submit_techniques` — that
3
+ * the Claude Agent SDK session calls to return the final markdown catalogue.
4
+ *
5
+ * Using a tool call instead of parsing a fenced JSON block lets the SDK
6
+ * enforce the schema (via Zod) and lets the agent self-correct when
7
+ * validation fails: the error message goes back as the tool result and the
8
+ * agent can re-call the tool with corrected data.
9
+ *
10
+ * The capture pattern: callers pass in a `TechniquesCaptureState`. The tool
11
+ * handler stores the validated args on `state.captured`. The orchestrator
12
+ * reads it after the SDK loop ends.
13
+ */
14
+ import { z } from 'zod';
15
+ import { type TechniquesExtraction } from './types.js';
16
+ export interface TechniquesCaptureState {
17
+ captured: TechniquesExtraction | null;
18
+ }
19
+ export declare function createTechniquesCaptureState(): TechniquesCaptureState;
20
+ /**
21
+ * Validation that goes beyond the basic Zod schema: enforce that the
22
+ * markdown actually has the H2 sections the prompt asks for. The agent gets
23
+ * an actionable error and can re-submit.
24
+ *
25
+ * We don't require ALL six sections — agents on tiny repos often legitimately
26
+ * collapse some. We require at least the first one ("Languages & Runtime")
27
+ * plus "Notable Techniques", which the prompt calls out as the whole point.
28
+ */
29
+ export declare function validateContent(content: string): {
30
+ error: string | null;
31
+ };
32
+ /**
33
+ * Build the `submit_techniques` tool. Exported separately from the server
34
+ * so tests can exercise the handler directly without going through MCP
35
+ * transport.
36
+ */
37
+ export declare function createSubmitTechniquesTool(state: TechniquesCaptureState): import("@anthropic-ai/claude-agent-sdk").SdkMcpToolDefinition<{
38
+ summary: z.ZodString;
39
+ content: z.ZodString;
40
+ }>;
41
+ export declare function createTechniquesMcpServer(state: TechniquesCaptureState): import("@anthropic-ai/claude-agent-sdk").McpSdkServerConfigWithInstance;