@sentry/warden 0.0.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/.agents/skills/find-bugs/SKILL.md +75 -0
- package/.agents/skills/vercel-react-best-practices/AGENTS.md +2934 -0
- package/.agents/skills/vercel-react-best-practices/SKILL.md +136 -0
- package/.agents/skills/vercel-react-best-practices/rules/advanced-event-handler-refs.md +55 -0
- package/.agents/skills/vercel-react-best-practices/rules/advanced-init-once.md +42 -0
- package/.agents/skills/vercel-react-best-practices/rules/advanced-use-latest.md +39 -0
- package/.agents/skills/vercel-react-best-practices/rules/async-api-routes.md +38 -0
- package/.agents/skills/vercel-react-best-practices/rules/async-defer-await.md +80 -0
- package/.agents/skills/vercel-react-best-practices/rules/async-dependencies.md +51 -0
- package/.agents/skills/vercel-react-best-practices/rules/async-parallel.md +28 -0
- package/.agents/skills/vercel-react-best-practices/rules/async-suspense-boundaries.md +99 -0
- package/.agents/skills/vercel-react-best-practices/rules/bundle-barrel-imports.md +59 -0
- package/.agents/skills/vercel-react-best-practices/rules/bundle-conditional.md +31 -0
- package/.agents/skills/vercel-react-best-practices/rules/bundle-defer-third-party.md +49 -0
- package/.agents/skills/vercel-react-best-practices/rules/bundle-dynamic-imports.md +35 -0
- package/.agents/skills/vercel-react-best-practices/rules/bundle-preload.md +50 -0
- package/.agents/skills/vercel-react-best-practices/rules/client-event-listeners.md +74 -0
- package/.agents/skills/vercel-react-best-practices/rules/client-localstorage-schema.md +71 -0
- package/.agents/skills/vercel-react-best-practices/rules/client-passive-event-listeners.md +48 -0
- package/.agents/skills/vercel-react-best-practices/rules/client-swr-dedup.md +56 -0
- package/.agents/skills/vercel-react-best-practices/rules/js-batch-dom-css.md +107 -0
- package/.agents/skills/vercel-react-best-practices/rules/js-cache-function-results.md +80 -0
- package/.agents/skills/vercel-react-best-practices/rules/js-cache-property-access.md +28 -0
- package/.agents/skills/vercel-react-best-practices/rules/js-cache-storage.md +70 -0
- package/.agents/skills/vercel-react-best-practices/rules/js-combine-iterations.md +32 -0
- package/.agents/skills/vercel-react-best-practices/rules/js-early-exit.md +50 -0
- package/.agents/skills/vercel-react-best-practices/rules/js-hoist-regexp.md +45 -0
- package/.agents/skills/vercel-react-best-practices/rules/js-index-maps.md +37 -0
- package/.agents/skills/vercel-react-best-practices/rules/js-length-check-first.md +49 -0
- package/.agents/skills/vercel-react-best-practices/rules/js-min-max-loop.md +82 -0
- package/.agents/skills/vercel-react-best-practices/rules/js-set-map-lookups.md +24 -0
- package/.agents/skills/vercel-react-best-practices/rules/js-tosorted-immutable.md +57 -0
- package/.agents/skills/vercel-react-best-practices/rules/rendering-activity.md +26 -0
- package/.agents/skills/vercel-react-best-practices/rules/rendering-animate-svg-wrapper.md +47 -0
- package/.agents/skills/vercel-react-best-practices/rules/rendering-conditional-render.md +40 -0
- package/.agents/skills/vercel-react-best-practices/rules/rendering-content-visibility.md +38 -0
- package/.agents/skills/vercel-react-best-practices/rules/rendering-hoist-jsx.md +46 -0
- package/.agents/skills/vercel-react-best-practices/rules/rendering-hydration-no-flicker.md +82 -0
- package/.agents/skills/vercel-react-best-practices/rules/rendering-hydration-suppress-warning.md +30 -0
- package/.agents/skills/vercel-react-best-practices/rules/rendering-svg-precision.md +28 -0
- package/.agents/skills/vercel-react-best-practices/rules/rendering-usetransition-loading.md +75 -0
- package/.agents/skills/vercel-react-best-practices/rules/rerender-defer-reads.md +39 -0
- package/.agents/skills/vercel-react-best-practices/rules/rerender-dependencies.md +45 -0
- package/.agents/skills/vercel-react-best-practices/rules/rerender-derived-state-no-effect.md +40 -0
- package/.agents/skills/vercel-react-best-practices/rules/rerender-derived-state.md +29 -0
- package/.agents/skills/vercel-react-best-practices/rules/rerender-functional-setstate.md +74 -0
- package/.agents/skills/vercel-react-best-practices/rules/rerender-lazy-state-init.md +58 -0
- package/.agents/skills/vercel-react-best-practices/rules/rerender-memo-with-default-value.md +38 -0
- package/.agents/skills/vercel-react-best-practices/rules/rerender-memo.md +44 -0
- package/.agents/skills/vercel-react-best-practices/rules/rerender-move-effect-to-event.md +45 -0
- package/.agents/skills/vercel-react-best-practices/rules/rerender-simple-expression-in-memo.md +35 -0
- package/.agents/skills/vercel-react-best-practices/rules/rerender-transitions.md +40 -0
- package/.agents/skills/vercel-react-best-practices/rules/rerender-use-ref-transient-values.md +73 -0
- package/.agents/skills/vercel-react-best-practices/rules/server-after-nonblocking.md +73 -0
- package/.agents/skills/vercel-react-best-practices/rules/server-auth-actions.md +96 -0
- package/.agents/skills/vercel-react-best-practices/rules/server-cache-lru.md +41 -0
- package/.agents/skills/vercel-react-best-practices/rules/server-cache-react.md +76 -0
- package/.agents/skills/vercel-react-best-practices/rules/server-dedup-props.md +65 -0
- package/.agents/skills/vercel-react-best-practices/rules/server-parallel-fetching.md +83 -0
- package/.agents/skills/vercel-react-best-practices/rules/server-serialization.md +38 -0
- package/.claude/settings.json +57 -0
- package/.claude/settings.local.json +88 -0
- package/.claude/skills/agent-prompt/SKILL.md +54 -0
- package/.claude/skills/agent-prompt/references/agentic-patterns.md +94 -0
- package/.claude/skills/agent-prompt/references/anti-patterns.md +140 -0
- package/.claude/skills/agent-prompt/references/context-design.md +124 -0
- package/.claude/skills/agent-prompt/references/core-principles.md +75 -0
- package/.claude/skills/agent-prompt/references/model-guidance.md +118 -0
- package/.claude/skills/agent-prompt/references/output-formats.md +98 -0
- package/.claude/skills/agent-prompt/references/skill-structure.md +115 -0
- package/.claude/skills/agent-prompt/references/system-prompts.md +115 -0
- package/.claude/skills/notseer/SKILL.md +131 -0
- package/.claude/skills/skill-writer/SKILL.md +140 -0
- package/.claude/skills/testing-guidelines/SKILL.md +132 -0
- package/.claude/skills/warden-skill/SKILL.md +250 -0
- package/.claude/skills/warden-skill/references/config-schema.md +133 -0
- package/.dex/config.toml +2 -0
- package/.github/workflows/ci.yml +33 -0
- package/.github/workflows/release.yml +54 -0
- package/.github/workflows/warden.yml +40 -0
- package/AGENTS.md +89 -0
- package/CONTRIBUTING.md +60 -0
- package/LICENSE +105 -0
- package/README.md +43 -0
- package/SPEC.md +263 -0
- package/action.yml +87 -0
- package/assets/favicon.png +0 -0
- package/assets/warden-icon-bw.svg +5 -0
- package/assets/warden-icon-purple.png +0 -0
- package/assets/warden-icon-purple.svg +5 -0
- package/docs/.claude/settings.local.json +11 -0
- package/docs/astro.config.mjs +43 -0
- package/docs/package.json +19 -0
- package/docs/pnpm-lock.yaml +4000 -0
- package/docs/public/favicon.svg +5 -0
- package/docs/src/components/Code.astro +141 -0
- package/docs/src/components/PackageManagerTabs.astro +183 -0
- package/docs/src/components/Terminal.astro +212 -0
- package/docs/src/layouts/Base.astro +380 -0
- package/docs/src/pages/cli.astro +167 -0
- package/docs/src/pages/config.astro +394 -0
- package/docs/src/pages/guide.astro +449 -0
- package/docs/src/pages/index.astro +490 -0
- package/docs/src/styles/global.css +551 -0
- package/docs/tsconfig.json +3 -0
- package/docs/vercel.json +5 -0
- package/eslint.config.js +33 -0
- package/package.json +73 -0
- package/src/action/index.ts +1 -0
- package/src/action/main.ts +868 -0
- package/src/cli/args.test.ts +477 -0
- package/src/cli/args.ts +415 -0
- package/src/cli/commands/add.ts +447 -0
- package/src/cli/commands/init.test.ts +136 -0
- package/src/cli/commands/init.ts +132 -0
- package/src/cli/commands/setup-app/browser.ts +38 -0
- package/src/cli/commands/setup-app/credentials.ts +45 -0
- package/src/cli/commands/setup-app/manifest.ts +48 -0
- package/src/cli/commands/setup-app/server.ts +172 -0
- package/src/cli/commands/setup-app.ts +156 -0
- package/src/cli/commands/sync.ts +114 -0
- package/src/cli/context.ts +131 -0
- package/src/cli/files.test.ts +155 -0
- package/src/cli/files.ts +89 -0
- package/src/cli/fix.test.ts +310 -0
- package/src/cli/fix.ts +387 -0
- package/src/cli/git.test.ts +119 -0
- package/src/cli/git.ts +318 -0
- package/src/cli/index.ts +14 -0
- package/src/cli/main.ts +672 -0
- package/src/cli/output/box.ts +235 -0
- package/src/cli/output/formatters.test.ts +187 -0
- package/src/cli/output/formatters.ts +269 -0
- package/src/cli/output/icons.ts +13 -0
- package/src/cli/output/index.ts +44 -0
- package/src/cli/output/ink-runner.tsx +337 -0
- package/src/cli/output/jsonl.test.ts +347 -0
- package/src/cli/output/jsonl.ts +126 -0
- package/src/cli/output/reporter.ts +435 -0
- package/src/cli/output/tasks.ts +374 -0
- package/src/cli/output/tty.test.ts +117 -0
- package/src/cli/output/tty.ts +60 -0
- package/src/cli/output/verbosity.test.ts +40 -0
- package/src/cli/output/verbosity.ts +31 -0
- package/src/cli/terminal.test.ts +148 -0
- package/src/cli/terminal.ts +301 -0
- package/src/config/index.ts +3 -0
- package/src/config/loader.test.ts +313 -0
- package/src/config/loader.ts +103 -0
- package/src/config/schema.ts +168 -0
- package/src/config/writer.test.ts +119 -0
- package/src/config/writer.ts +84 -0
- package/src/diff/classify.test.ts +162 -0
- package/src/diff/classify.ts +92 -0
- package/src/diff/coalesce.test.ts +208 -0
- package/src/diff/coalesce.ts +133 -0
- package/src/diff/context.test.ts +226 -0
- package/src/diff/context.ts +201 -0
- package/src/diff/index.ts +4 -0
- package/src/diff/parser.test.ts +212 -0
- package/src/diff/parser.ts +149 -0
- package/src/event/context.ts +132 -0
- package/src/event/index.ts +2 -0
- package/src/event/schedule-context.ts +101 -0
- package/src/examples/examples.integration.test.ts +66 -0
- package/src/examples/index.test.ts +101 -0
- package/src/examples/index.ts +122 -0
- package/src/examples/setup.ts +25 -0
- package/src/index.ts +115 -0
- package/src/output/dedup.test.ts +419 -0
- package/src/output/dedup.ts +607 -0
- package/src/output/github-checks.test.ts +300 -0
- package/src/output/github-checks.ts +476 -0
- package/src/output/github-issues.ts +329 -0
- package/src/output/index.ts +5 -0
- package/src/output/issue-renderer.ts +197 -0
- package/src/output/renderer.test.ts +727 -0
- package/src/output/renderer.ts +217 -0
- package/src/output/stale.test.ts +375 -0
- package/src/output/stale.ts +155 -0
- package/src/output/types.ts +34 -0
- package/src/sdk/index.ts +1 -0
- package/src/sdk/runner.test.ts +806 -0
- package/src/sdk/runner.ts +1232 -0
- package/src/skills/index.ts +36 -0
- package/src/skills/loader.test.ts +300 -0
- package/src/skills/loader.ts +423 -0
- package/src/skills/remote.test.ts +704 -0
- package/src/skills/remote.ts +604 -0
- package/src/triggers/matcher.test.ts +277 -0
- package/src/triggers/matcher.ts +152 -0
- package/src/types/index.ts +194 -0
- package/src/utils/async.ts +18 -0
- package/src/utils/index.test.ts +84 -0
- package/src/utils/index.ts +50 -0
- package/tsconfig.json +25 -0
- package/vitest.config.ts +8 -0
- package/vitest.integration.config.ts +11 -0
- package/warden.toml +19 -0
|
@@ -0,0 +1,1232 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { query, type SDKResultMessage } from '@anthropic-ai/claude-agent-sdk';
|
|
4
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
5
|
+
import type { SkillDefinition, ChunkingConfig } from '../config/schema.js';
|
|
6
|
+
import { FindingSchema } from '../types/index.js';
|
|
7
|
+
import type { EventContext, SkillReport, Finding, UsageStats, SkippedFile, RetryConfig } from '../types/index.js';
|
|
8
|
+
import {
|
|
9
|
+
APIError,
|
|
10
|
+
RateLimitError,
|
|
11
|
+
InternalServerError,
|
|
12
|
+
APIConnectionError,
|
|
13
|
+
APIConnectionTimeoutError,
|
|
14
|
+
} from '@anthropic-ai/sdk';
|
|
15
|
+
import {
|
|
16
|
+
parseFileDiff,
|
|
17
|
+
expandDiffContext,
|
|
18
|
+
formatHunkForAnalysis,
|
|
19
|
+
classifyFile,
|
|
20
|
+
coalesceHunks,
|
|
21
|
+
type HunkWithContext,
|
|
22
|
+
} from '../diff/index.js';
|
|
23
|
+
|
|
24
|
+
export class SkillRunnerError extends Error {
|
|
25
|
+
constructor(message: string, options?: { cause?: unknown }) {
|
|
26
|
+
super(message, options);
|
|
27
|
+
this.name = 'SkillRunnerError';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Default concurrency for file-level parallel processing */
|
|
32
|
+
const DEFAULT_FILE_CONCURRENCY = 5;
|
|
33
|
+
|
|
34
|
+
/** Pattern to match the start of findings JSON (allows whitespace after brace) */
|
|
35
|
+
const FINDINGS_JSON_START = /\{\s*"findings"/;
|
|
36
|
+
|
|
37
|
+
/** Threshold in characters above which to warn about large prompts (~25k tokens) */
|
|
38
|
+
const LARGE_PROMPT_THRESHOLD_CHARS = 100000;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Estimate token count from character count.
|
|
42
|
+
* Uses chars/4 as a rough approximation for English text.
|
|
43
|
+
*/
|
|
44
|
+
export function estimateTokens(chars: number): number {
|
|
45
|
+
return Math.ceil(chars / 4);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Result from analyzing a single hunk */
|
|
49
|
+
interface HunkAnalysisResult {
|
|
50
|
+
findings: Finding[];
|
|
51
|
+
usage: UsageStats;
|
|
52
|
+
/** Whether the hunk analysis failed (SDK error, API error, etc.) */
|
|
53
|
+
failed: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Extract usage stats from an SDK result message.
|
|
58
|
+
*/
|
|
59
|
+
function extractUsage(result: SDKResultMessage): UsageStats {
|
|
60
|
+
return {
|
|
61
|
+
inputTokens: result.usage['input_tokens'],
|
|
62
|
+
outputTokens: result.usage['output_tokens'],
|
|
63
|
+
cacheReadInputTokens: result.usage['cache_read_input_tokens'] ?? 0,
|
|
64
|
+
cacheCreationInputTokens: result.usage['cache_creation_input_tokens'] ?? 0,
|
|
65
|
+
costUSD: result.total_cost_usd,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Create empty usage stats.
|
|
71
|
+
*/
|
|
72
|
+
function emptyUsage(): UsageStats {
|
|
73
|
+
return {
|
|
74
|
+
inputTokens: 0,
|
|
75
|
+
outputTokens: 0,
|
|
76
|
+
cacheReadInputTokens: 0,
|
|
77
|
+
cacheCreationInputTokens: 0,
|
|
78
|
+
costUSD: 0,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Aggregate multiple usage stats into one.
|
|
84
|
+
*/
|
|
85
|
+
export function aggregateUsage(usages: UsageStats[]): UsageStats {
|
|
86
|
+
return usages.reduce(
|
|
87
|
+
(acc, u) => ({
|
|
88
|
+
inputTokens: acc.inputTokens + u.inputTokens,
|
|
89
|
+
outputTokens: acc.outputTokens + u.outputTokens,
|
|
90
|
+
cacheReadInputTokens: (acc.cacheReadInputTokens ?? 0) + (u.cacheReadInputTokens ?? 0),
|
|
91
|
+
cacheCreationInputTokens: (acc.cacheCreationInputTokens ?? 0) + (u.cacheCreationInputTokens ?? 0),
|
|
92
|
+
costUSD: acc.costUSD + u.costUSD,
|
|
93
|
+
}),
|
|
94
|
+
emptyUsage()
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Default retry configuration */
|
|
99
|
+
const DEFAULT_RETRY_CONFIG: Required<RetryConfig> = {
|
|
100
|
+
maxRetries: 3,
|
|
101
|
+
initialDelayMs: 1000,
|
|
102
|
+
backoffMultiplier: 2,
|
|
103
|
+
maxDelayMs: 30000,
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Check if an error is retryable.
|
|
108
|
+
* Retries on: rate limits (429), server errors (5xx), connection errors, timeouts.
|
|
109
|
+
*/
|
|
110
|
+
export function isRetryableError(error: unknown): boolean {
|
|
111
|
+
if (error instanceof RateLimitError) return true;
|
|
112
|
+
if (error instanceof InternalServerError) return true;
|
|
113
|
+
if (error instanceof APIConnectionError) return true;
|
|
114
|
+
if (error instanceof APIConnectionTimeoutError) return true;
|
|
115
|
+
|
|
116
|
+
// Check for generic APIError with retryable status codes
|
|
117
|
+
if (error instanceof APIError) {
|
|
118
|
+
const status = error.status;
|
|
119
|
+
if (status === 429) return true;
|
|
120
|
+
if (status !== undefined && status >= 500 && status < 600) return true;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Check if an error is an authentication failure.
|
|
128
|
+
* These require user action (login or API key) and should not be retried.
|
|
129
|
+
*/
|
|
130
|
+
export function isAuthenticationError(error: unknown): boolean {
|
|
131
|
+
if (error instanceof APIError && error.status === 401) {
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Check error message for common auth failure patterns
|
|
136
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
137
|
+
const authPatterns = [
|
|
138
|
+
'authentication',
|
|
139
|
+
'unauthorized',
|
|
140
|
+
'invalid.*api.*key',
|
|
141
|
+
'not.*logged.*in',
|
|
142
|
+
'login.*required',
|
|
143
|
+
];
|
|
144
|
+
return authPatterns.some((pattern) => new RegExp(pattern, 'i').test(message));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** User-friendly error message for authentication failures */
|
|
148
|
+
const AUTH_ERROR_MESSAGE = `Authentication required.
|
|
149
|
+
|
|
150
|
+
claude login # Use Claude Code subscription
|
|
151
|
+
export WARDEN_ANTHROPIC_API_KEY=sk-... # Or use API key
|
|
152
|
+
|
|
153
|
+
https://console.anthropic.com/ for API keys`;
|
|
154
|
+
|
|
155
|
+
export class WardenAuthenticationError extends Error {
|
|
156
|
+
constructor() {
|
|
157
|
+
super(AUTH_ERROR_MESSAGE);
|
|
158
|
+
this.name = 'WardenAuthenticationError';
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Calculate delay for a retry attempt using exponential backoff.
|
|
164
|
+
*/
|
|
165
|
+
export function calculateRetryDelay(
|
|
166
|
+
attempt: number,
|
|
167
|
+
config: Required<RetryConfig>
|
|
168
|
+
): number {
|
|
169
|
+
const delay = config.initialDelayMs * Math.pow(config.backoffMultiplier, attempt);
|
|
170
|
+
return Math.min(delay, config.maxDelayMs);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Sleep for a specified duration, respecting abort signal.
|
|
175
|
+
*/
|
|
176
|
+
async function sleep(ms: number, abortSignal?: AbortSignal): Promise<void> {
|
|
177
|
+
return new Promise((resolve, reject) => {
|
|
178
|
+
if (abortSignal?.aborted) {
|
|
179
|
+
reject(new Error('Aborted'));
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const timeout = setTimeout(resolve, ms);
|
|
184
|
+
|
|
185
|
+
abortSignal?.addEventListener('abort', () => {
|
|
186
|
+
clearTimeout(timeout);
|
|
187
|
+
reject(new Error('Aborted'));
|
|
188
|
+
}, { once: true });
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Callbacks for progress reporting during skill execution.
|
|
194
|
+
*/
|
|
195
|
+
export interface SkillRunnerCallbacks {
|
|
196
|
+
/** Start time of the skill execution (for elapsed time calculations) */
|
|
197
|
+
skillStartTime?: number;
|
|
198
|
+
onFileStart?: (file: string, index: number, total: number) => void;
|
|
199
|
+
onHunkStart?: (file: string, hunkNum: number, totalHunks: number, lineRange: string) => void;
|
|
200
|
+
onHunkComplete?: (file: string, hunkNum: number, findings: Finding[]) => void;
|
|
201
|
+
onFileComplete?: (file: string, index: number, total: number) => void;
|
|
202
|
+
/** Called when a prompt exceeds the large prompt threshold */
|
|
203
|
+
onLargePrompt?: (file: string, lineRange: string, chars: number, estimatedTokens: number) => void;
|
|
204
|
+
/** Called with prompt size info in debug mode */
|
|
205
|
+
onPromptSize?: (file: string, lineRange: string, systemChars: number, userChars: number, totalChars: number, estimatedTokens: number) => void;
|
|
206
|
+
/** Called when a retry attempt is made (verbose mode) */
|
|
207
|
+
onRetry?: (file: string, lineRange: string, attempt: number, maxRetries: number, error: string, delayMs: number) => void;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export interface SkillRunnerOptions {
|
|
211
|
+
apiKey?: string;
|
|
212
|
+
maxTurns?: number;
|
|
213
|
+
/** Lines of context to include around each hunk */
|
|
214
|
+
contextLines?: number;
|
|
215
|
+
/** Process files in parallel (default: true) */
|
|
216
|
+
parallel?: boolean;
|
|
217
|
+
/** Max concurrent file analyses when parallel=true (default: 5) */
|
|
218
|
+
concurrency?: number;
|
|
219
|
+
/** Delay in milliseconds between batch starts when parallel=true (default: 0) */
|
|
220
|
+
batchDelayMs?: number;
|
|
221
|
+
/** Model to use for analysis (e.g., 'claude-sonnet-4-20250514'). Uses SDK default if not specified. */
|
|
222
|
+
model?: string;
|
|
223
|
+
/** Progress callbacks */
|
|
224
|
+
callbacks?: SkillRunnerCallbacks;
|
|
225
|
+
/** Abort controller for cancellation on SIGINT */
|
|
226
|
+
abortController?: AbortController;
|
|
227
|
+
/** Path to Claude Code CLI executable. Required in CI environments. */
|
|
228
|
+
pathToClaudeCodeExecutable?: string;
|
|
229
|
+
/** Retry configuration for transient API failures */
|
|
230
|
+
retry?: RetryConfig;
|
|
231
|
+
/** Enable verbose logging for retry attempts */
|
|
232
|
+
verbose?: boolean;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Builds the system prompt for hunk-based analysis.
|
|
237
|
+
*
|
|
238
|
+
* Future enhancement: Could have the agent output a structured `contextAssessment`
|
|
239
|
+
* (applicationType, trustBoundaries, filesChecked) to cache across hunks, allow
|
|
240
|
+
* user overrides, or build analytics. Not implemented since we don't consume it yet.
|
|
241
|
+
*/
|
|
242
|
+
function buildHunkSystemPrompt(skill: SkillDefinition): string {
|
|
243
|
+
const sections = [
|
|
244
|
+
`<role>
|
|
245
|
+
You are a code analysis agent for Warden. You evaluate code changes against specific skill criteria and report findings ONLY when the code violates or conflicts with those criteria. You do not perform general code review or report issues outside the skill's scope.
|
|
246
|
+
</role>`,
|
|
247
|
+
|
|
248
|
+
`<tools>
|
|
249
|
+
You have access to these tools to gather context:
|
|
250
|
+
- **Read**: Check related files to understand context
|
|
251
|
+
- **Grep**: Search for patterns to trace data flow or find related code
|
|
252
|
+
</tools>`,
|
|
253
|
+
|
|
254
|
+
`<skill_instructions>
|
|
255
|
+
The following defines the ONLY criteria you should evaluate. Do not report findings outside this scope:
|
|
256
|
+
|
|
257
|
+
${skill.prompt}
|
|
258
|
+
</skill_instructions>`,
|
|
259
|
+
|
|
260
|
+
`<output_format>
|
|
261
|
+
IMPORTANT: Your response must be ONLY a valid JSON object. No markdown, no explanation, no code fences.
|
|
262
|
+
|
|
263
|
+
Example response format:
|
|
264
|
+
{"findings": [{"id": "example-1", "severity": "medium", "confidence": "high", "title": "Issue title", "description": "Description", "location": {"path": "file.ts", "startLine": 10}}]}
|
|
265
|
+
|
|
266
|
+
Full schema:
|
|
267
|
+
{
|
|
268
|
+
"findings": [
|
|
269
|
+
{
|
|
270
|
+
"id": "unique-identifier",
|
|
271
|
+
"severity": "critical|high|medium|low|info",
|
|
272
|
+
"confidence": "high|medium|low",
|
|
273
|
+
"title": "Short descriptive title",
|
|
274
|
+
"description": "Detailed explanation of the issue",
|
|
275
|
+
"location": {
|
|
276
|
+
"path": "path/to/file.ts",
|
|
277
|
+
"startLine": 10,
|
|
278
|
+
"endLine": 15
|
|
279
|
+
},
|
|
280
|
+
"suggestedFix": {
|
|
281
|
+
"description": "How to fix this issue",
|
|
282
|
+
"diff": "unified diff format"
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
]
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
Requirements:
|
|
289
|
+
- Return ONLY valid JSON starting with {"findings":
|
|
290
|
+
- "findings" array can be empty if no issues found
|
|
291
|
+
- "location.path" is auto-filled from context - just provide startLine (and optionally endLine). Omit location entirely for general findings not about a specific line.
|
|
292
|
+
- "confidence" reflects how certain you are this is a real issue given the codebase context
|
|
293
|
+
- "suggestedFix" is optional - only include when you can provide a complete, correct fix **to the file being analyzed**. Omit suggestedFix if:
|
|
294
|
+
- The fix would be incomplete or you're uncertain about the correct solution
|
|
295
|
+
- The fix requires changes to a different file or a new file (describe the fix in the description field instead)
|
|
296
|
+
- Keep descriptions SHORT (1-2 sentences max) - avoid lengthy explanations
|
|
297
|
+
- Be concise - focus only on the changes shown
|
|
298
|
+
</output_format>`,
|
|
299
|
+
];
|
|
300
|
+
|
|
301
|
+
const { rootDir } = skill;
|
|
302
|
+
if (rootDir) {
|
|
303
|
+
const resourceDirs = ['scripts', 'references', 'assets'].filter((dir) =>
|
|
304
|
+
existsSync(join(rootDir, dir))
|
|
305
|
+
);
|
|
306
|
+
if (resourceDirs.length > 0) {
|
|
307
|
+
const dirList = resourceDirs.map((d) => `${d}/`).join(', ');
|
|
308
|
+
sections.push(`<skill_resources>
|
|
309
|
+
This skill is located at: ${rootDir}
|
|
310
|
+
You can read files from ${dirList} subdirectories using the Read tool with the full path.
|
|
311
|
+
</skill_resources>`);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return sections.join('\n\n');
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Context about the PR being analyzed, for inclusion in prompts.
|
|
320
|
+
*
|
|
321
|
+
* The title and body (like a commit message) help explain the _intent_ of the
|
|
322
|
+
* changes to the agent, enabling it to better understand what the author was
|
|
323
|
+
* trying to accomplish and identify issues that conflict with that intent.
|
|
324
|
+
*/
|
|
325
|
+
export interface PRPromptContext {
|
|
326
|
+
/** All files being changed in the PR */
|
|
327
|
+
changedFiles: string[];
|
|
328
|
+
/** PR title - explains what the change does */
|
|
329
|
+
title?: string;
|
|
330
|
+
/** PR description/body - explains why and provides additional context */
|
|
331
|
+
body?: string | null;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Builds the user prompt for a single hunk.
|
|
336
|
+
*/
|
|
337
|
+
function buildHunkUserPrompt(
|
|
338
|
+
skill: SkillDefinition,
|
|
339
|
+
hunkCtx: HunkWithContext,
|
|
340
|
+
prContext?: PRPromptContext
|
|
341
|
+
): string {
|
|
342
|
+
const sections: string[] = [];
|
|
343
|
+
|
|
344
|
+
sections.push(`Analyze this code change according to the "${skill.name}" skill criteria.`);
|
|
345
|
+
|
|
346
|
+
// Include PR title and description for context on intent
|
|
347
|
+
if (prContext?.title) {
|
|
348
|
+
let prSection = `## Pull Request Context\n**Title:** ${prContext.title}`;
|
|
349
|
+
if (prContext.body) {
|
|
350
|
+
// Truncate very long PR descriptions to avoid bloating prompts
|
|
351
|
+
const maxBodyLength = 1000;
|
|
352
|
+
const body = prContext.body.length > maxBodyLength
|
|
353
|
+
? prContext.body.slice(0, maxBodyLength) + '...'
|
|
354
|
+
: prContext.body;
|
|
355
|
+
prSection += `\n\n**Description:**\n${body}`;
|
|
356
|
+
}
|
|
357
|
+
sections.push(prSection);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Include list of other files being changed in the PR for context
|
|
361
|
+
const otherFiles = prContext?.changedFiles.filter((f) => f !== hunkCtx.filename) ?? [];
|
|
362
|
+
if (otherFiles.length > 0) {
|
|
363
|
+
sections.push(`## Other Files in This PR
|
|
364
|
+
The following files are also being changed in this PR (may provide useful context):
|
|
365
|
+
${otherFiles.map((f) => `- ${f}`).join('\n')}`);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
sections.push(formatHunkForAnalysis(hunkCtx));
|
|
369
|
+
|
|
370
|
+
sections.push(
|
|
371
|
+
`IMPORTANT: Only report findings that are explicitly covered by the skill instructions. Do not report general code quality issues, bugs, or improvements unless the skill specifically asks for them. Return an empty findings array if no issues match the skill's criteria.`
|
|
372
|
+
);
|
|
373
|
+
|
|
374
|
+
return sections.join('\n\n');
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Result from extracting findings JSON from text.
|
|
379
|
+
*/
|
|
380
|
+
export type ExtractFindingsResult =
|
|
381
|
+
| { success: true; findings: unknown[] }
|
|
382
|
+
| { success: false; error: string; preview: string };
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Extract JSON object from text, handling nested braces correctly.
|
|
386
|
+
* Starts from the given position and returns the balanced JSON object.
|
|
387
|
+
*/
|
|
388
|
+
export function extractBalancedJson(text: string, startIndex: number): string | null {
|
|
389
|
+
let depth = 0;
|
|
390
|
+
let inString = false;
|
|
391
|
+
let escape = false;
|
|
392
|
+
|
|
393
|
+
for (let i = startIndex; i < text.length; i++) {
|
|
394
|
+
const char = text[i];
|
|
395
|
+
|
|
396
|
+
if (escape) {
|
|
397
|
+
escape = false;
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (char === '\\' && inString) {
|
|
402
|
+
escape = true;
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (char === '"') {
|
|
407
|
+
inString = !inString;
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
if (inString) continue;
|
|
412
|
+
|
|
413
|
+
if (char === '{') depth++;
|
|
414
|
+
if (char === '}') {
|
|
415
|
+
depth--;
|
|
416
|
+
if (depth === 0) {
|
|
417
|
+
return text.slice(startIndex, i + 1);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return null;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Extract findings JSON from model output text.
|
|
427
|
+
* Handles markdown code fences, prose before JSON, and nested objects.
|
|
428
|
+
*/
|
|
429
|
+
export function extractFindingsJson(rawText: string): ExtractFindingsResult {
|
|
430
|
+
let text = rawText.trim();
|
|
431
|
+
|
|
432
|
+
// Strip markdown code fences if present (handles any language tag: ```json, ```typescript, ```c++, etc.)
|
|
433
|
+
const codeBlockMatch = text.match(/```[\w+#-]*\s*([\s\S]*?)```/);
|
|
434
|
+
if (codeBlockMatch?.[1]) {
|
|
435
|
+
text = codeBlockMatch[1].trim();
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// Find the start of the findings JSON object
|
|
439
|
+
const findingsMatch = text.match(FINDINGS_JSON_START);
|
|
440
|
+
if (!findingsMatch || findingsMatch.index === undefined) {
|
|
441
|
+
return {
|
|
442
|
+
success: false,
|
|
443
|
+
error: 'no_findings_json',
|
|
444
|
+
preview: text.slice(0, 200),
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
const findingsStart = findingsMatch.index;
|
|
448
|
+
|
|
449
|
+
// Extract the balanced JSON object
|
|
450
|
+
const jsonStr = extractBalancedJson(text, findingsStart);
|
|
451
|
+
if (!jsonStr) {
|
|
452
|
+
return {
|
|
453
|
+
success: false,
|
|
454
|
+
error: 'unbalanced_json',
|
|
455
|
+
preview: text.slice(findingsStart, findingsStart + 200),
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// Parse the JSON
|
|
460
|
+
let parsed: unknown;
|
|
461
|
+
try {
|
|
462
|
+
parsed = JSON.parse(jsonStr);
|
|
463
|
+
} catch {
|
|
464
|
+
return {
|
|
465
|
+
success: false,
|
|
466
|
+
error: 'invalid_json',
|
|
467
|
+
preview: jsonStr.slice(0, 200),
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// Validate structure
|
|
472
|
+
if (typeof parsed !== 'object' || parsed === null || !('findings' in parsed)) {
|
|
473
|
+
return {
|
|
474
|
+
success: false,
|
|
475
|
+
error: 'missing_findings_key',
|
|
476
|
+
preview: jsonStr.slice(0, 200),
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
const findings = (parsed as { findings: unknown }).findings;
|
|
481
|
+
if (!Array.isArray(findings)) {
|
|
482
|
+
return {
|
|
483
|
+
success: false,
|
|
484
|
+
error: 'findings_not_array',
|
|
485
|
+
preview: jsonStr.slice(0, 200),
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
return { success: true, findings };
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** Max characters to send to LLM fallback (roughly ~8k tokens) */
|
|
493
|
+
const LLM_FALLBACK_MAX_CHARS = 32000;
|
|
494
|
+
|
|
495
|
+
/** Timeout for LLM fallback API calls in milliseconds */
|
|
496
|
+
const LLM_FALLBACK_TIMEOUT_MS = 30000;
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Truncate text for LLM fallback while preserving the findings JSON.
|
|
500
|
+
*
|
|
501
|
+
* Caller must ensure findings JSON exists in the text before calling.
|
|
502
|
+
*/
|
|
503
|
+
export function truncateForLLMFallback(rawText: string, maxChars: number): string {
|
|
504
|
+
if (rawText.length <= maxChars) {
|
|
505
|
+
return rawText;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
const findingsIndex = rawText.match(FINDINGS_JSON_START)?.index ?? -1;
|
|
509
|
+
|
|
510
|
+
// If findings starts within our budget, simple truncation from start preserves it
|
|
511
|
+
if (findingsIndex < maxChars - 20) {
|
|
512
|
+
return rawText.slice(0, maxChars) + '\n[... truncated]';
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// Findings is beyond our budget - skip to just before it
|
|
516
|
+
// Keep minimal context (10% of budget or 200 chars, whichever is smaller)
|
|
517
|
+
const markerOverhead = 40;
|
|
518
|
+
const usableBudget = maxChars - markerOverhead;
|
|
519
|
+
const contextBefore = Math.min(200, Math.floor(usableBudget * 0.1), findingsIndex);
|
|
520
|
+
const startIndex = findingsIndex - contextBefore;
|
|
521
|
+
const endIndex = startIndex + usableBudget;
|
|
522
|
+
|
|
523
|
+
const truncatedContent = rawText.slice(startIndex, endIndex);
|
|
524
|
+
const suffix = endIndex < rawText.length ? '\n[... truncated]' : '';
|
|
525
|
+
|
|
526
|
+
return '[... truncated ...]\n' + truncatedContent + suffix;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Extract findings from malformed output using LLM as a fallback.
|
|
531
|
+
* Uses claude-haiku-4-5 for lightweight, fast extraction.
|
|
532
|
+
*/
|
|
533
|
+
export async function extractFindingsWithLLM(
|
|
534
|
+
rawText: string,
|
|
535
|
+
apiKey?: string
|
|
536
|
+
): Promise<ExtractFindingsResult> {
|
|
537
|
+
if (!apiKey) {
|
|
538
|
+
return {
|
|
539
|
+
success: false,
|
|
540
|
+
error: 'no_api_key_for_fallback',
|
|
541
|
+
preview: rawText.slice(0, 200),
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// If no findings anchor exists, there's nothing to extract
|
|
546
|
+
if (!FINDINGS_JSON_START.test(rawText)) {
|
|
547
|
+
return {
|
|
548
|
+
success: false,
|
|
549
|
+
error: 'no_findings_to_extract',
|
|
550
|
+
preview: rawText.slice(0, 200),
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// Truncate input while preserving JSON boundaries
|
|
555
|
+
const truncatedText = truncateForLLMFallback(rawText, LLM_FALLBACK_MAX_CHARS);
|
|
556
|
+
|
|
557
|
+
try {
|
|
558
|
+
const client = new Anthropic({ apiKey, timeout: LLM_FALLBACK_TIMEOUT_MS });
|
|
559
|
+
const response = await client.messages.create({
|
|
560
|
+
model: 'claude-haiku-4-5',
|
|
561
|
+
max_tokens: 4096,
|
|
562
|
+
messages: [
|
|
563
|
+
{
|
|
564
|
+
role: 'user',
|
|
565
|
+
content: `Extract the findings JSON from this model output.
|
|
566
|
+
Return ONLY valid JSON in format: {"findings": [...]}
|
|
567
|
+
If no findings exist, return: {"findings": []}
|
|
568
|
+
|
|
569
|
+
Model output:
|
|
570
|
+
${truncatedText}`,
|
|
571
|
+
},
|
|
572
|
+
],
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
const content = response.content[0];
|
|
576
|
+
if (!content || content.type !== 'text') {
|
|
577
|
+
return {
|
|
578
|
+
success: false,
|
|
579
|
+
error: 'llm_unexpected_response',
|
|
580
|
+
preview: rawText.slice(0, 200),
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// Parse the LLM response as JSON
|
|
585
|
+
return extractFindingsJson(content.text);
|
|
586
|
+
} catch (error) {
|
|
587
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
588
|
+
return {
|
|
589
|
+
success: false,
|
|
590
|
+
error: `llm_extraction_failed: ${errorMessage}`,
|
|
591
|
+
preview: rawText.slice(0, 200),
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* Validate and normalize findings from extracted JSON.
|
|
598
|
+
*/
|
|
599
|
+
function validateFindings(findings: unknown[], filename: string): Finding[] {
|
|
600
|
+
const validated: Finding[] = [];
|
|
601
|
+
|
|
602
|
+
for (const f of findings) {
|
|
603
|
+
// Normalize location path before validation
|
|
604
|
+
if (typeof f === 'object' && f !== null && 'location' in f) {
|
|
605
|
+
const loc = (f as Record<string, unknown>)['location'];
|
|
606
|
+
if (loc && typeof loc === 'object') {
|
|
607
|
+
(loc as Record<string, unknown>)['path'] = filename;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const result = FindingSchema.safeParse(f);
|
|
612
|
+
if (result.success) {
|
|
613
|
+
validated.push({
|
|
614
|
+
...result.data,
|
|
615
|
+
location: result.data.location ? { ...result.data.location, path: filename } : undefined,
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
return validated;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* Parse findings from a hunk analysis result.
|
|
625
|
+
* Uses a two-tier extraction strategy:
|
|
626
|
+
* 1. Regex-based extraction (fast, handles well-formed output)
|
|
627
|
+
* 2. LLM fallback using haiku (handles malformed output gracefully)
|
|
628
|
+
*/
|
|
629
|
+
async function parseHunkOutput(
|
|
630
|
+
result: SDKResultMessage,
|
|
631
|
+
filename: string,
|
|
632
|
+
apiKey?: string
|
|
633
|
+
): Promise<Finding[]> {
|
|
634
|
+
if (result.subtype !== 'success') {
|
|
635
|
+
// Silently return empty - the SDK already handles error reporting
|
|
636
|
+
return [];
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// Tier 1: Try regex-based extraction first (fast)
|
|
640
|
+
const extracted = extractFindingsJson(result.result);
|
|
641
|
+
|
|
642
|
+
if (extracted.success) {
|
|
643
|
+
return validateFindings(extracted.findings, filename);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// Tier 2: Try LLM fallback for malformed output
|
|
647
|
+
const fallback = await extractFindingsWithLLM(result.result, apiKey);
|
|
648
|
+
|
|
649
|
+
if (fallback.success) {
|
|
650
|
+
return validateFindings(fallback.findings, filename);
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// Both tiers failed - return empty findings silently
|
|
654
|
+
return [];
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Callbacks for prompt size reporting during hunk analysis.
|
|
659
|
+
*/
|
|
660
|
+
interface HunkAnalysisCallbacks {
|
|
661
|
+
lineRange: string;
|
|
662
|
+
onLargePrompt?: (lineRange: string, chars: number, estimatedTokens: number) => void;
|
|
663
|
+
onPromptSize?: (lineRange: string, systemChars: number, userChars: number, totalChars: number, estimatedTokens: number) => void;
|
|
664
|
+
onRetry?: (lineRange: string, attempt: number, maxRetries: number, error: string, delayMs: number) => void;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* Execute a single SDK query attempt.
|
|
669
|
+
*/
|
|
670
|
+
async function executeQuery(
|
|
671
|
+
systemPrompt: string,
|
|
672
|
+
userPrompt: string,
|
|
673
|
+
repoPath: string,
|
|
674
|
+
options: SkillRunnerOptions
|
|
675
|
+
): Promise<SDKResultMessage | undefined> {
|
|
676
|
+
const { maxTurns = 50, model, abortController, pathToClaudeCodeExecutable } = options;
|
|
677
|
+
|
|
678
|
+
const stream = query({
|
|
679
|
+
prompt: userPrompt,
|
|
680
|
+
options: {
|
|
681
|
+
maxTurns,
|
|
682
|
+
cwd: repoPath,
|
|
683
|
+
systemPrompt,
|
|
684
|
+
// Only allow read-only tools - context is already provided in the prompt
|
|
685
|
+
allowedTools: ['Read', 'Grep'],
|
|
686
|
+
// Explicitly block modification/side-effect tools as defense-in-depth
|
|
687
|
+
disallowedTools: ['Write', 'Edit', 'Bash', 'WebFetch', 'WebSearch', 'Task', 'TodoWrite'],
|
|
688
|
+
permissionMode: 'bypassPermissions',
|
|
689
|
+
model,
|
|
690
|
+
abortController,
|
|
691
|
+
pathToClaudeCodeExecutable,
|
|
692
|
+
},
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
let resultMessage: SDKResultMessage | undefined;
|
|
696
|
+
|
|
697
|
+
for await (const message of stream) {
|
|
698
|
+
if (message.type === 'result') {
|
|
699
|
+
resultMessage = message;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
return resultMessage;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Analyze a single hunk with retry logic for transient failures.
|
|
708
|
+
*/
|
|
709
|
+
async function analyzeHunk(
|
|
710
|
+
skill: SkillDefinition,
|
|
711
|
+
hunkCtx: HunkWithContext,
|
|
712
|
+
repoPath: string,
|
|
713
|
+
options: SkillRunnerOptions,
|
|
714
|
+
callbacks?: HunkAnalysisCallbacks,
|
|
715
|
+
prContext?: PRPromptContext
|
|
716
|
+
): Promise<HunkAnalysisResult> {
|
|
717
|
+
const { apiKey, abortController, retry } = options;
|
|
718
|
+
|
|
719
|
+
const systemPrompt = buildHunkSystemPrompt(skill);
|
|
720
|
+
const userPrompt = buildHunkUserPrompt(skill, hunkCtx, prContext);
|
|
721
|
+
|
|
722
|
+
// Report prompt size information
|
|
723
|
+
const systemChars = systemPrompt.length;
|
|
724
|
+
const userChars = userPrompt.length;
|
|
725
|
+
const totalChars = systemChars + userChars;
|
|
726
|
+
const estimatedTokens = estimateTokens(totalChars);
|
|
727
|
+
|
|
728
|
+
// Always call onPromptSize if provided (for debug mode)
|
|
729
|
+
callbacks?.onPromptSize?.(callbacks.lineRange, systemChars, userChars, totalChars, estimatedTokens);
|
|
730
|
+
|
|
731
|
+
// Warn about large prompts
|
|
732
|
+
if (totalChars > LARGE_PROMPT_THRESHOLD_CHARS) {
|
|
733
|
+
callbacks?.onLargePrompt?.(callbacks.lineRange, totalChars, estimatedTokens);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// Merge retry config with defaults
|
|
737
|
+
const retryConfig: Required<RetryConfig> = {
|
|
738
|
+
...DEFAULT_RETRY_CONFIG,
|
|
739
|
+
...retry,
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
let lastError: unknown;
|
|
743
|
+
// Track accumulated usage across retry attempts for accurate cost reporting
|
|
744
|
+
const accumulatedUsage: UsageStats[] = [];
|
|
745
|
+
|
|
746
|
+
for (let attempt = 0; attempt <= retryConfig.maxRetries; attempt++) {
|
|
747
|
+
// Check for abort before each attempt
|
|
748
|
+
if (abortController?.signal.aborted) {
|
|
749
|
+
return { findings: [], usage: aggregateUsage(accumulatedUsage), failed: true };
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
try {
|
|
753
|
+
const resultMessage = await executeQuery(systemPrompt, userPrompt, repoPath, options);
|
|
754
|
+
|
|
755
|
+
if (!resultMessage) {
|
|
756
|
+
return { findings: [], usage: aggregateUsage(accumulatedUsage), failed: true };
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
// Extract usage from the result, regardless of success/error status
|
|
760
|
+
const usage = extractUsage(resultMessage);
|
|
761
|
+
accumulatedUsage.push(usage);
|
|
762
|
+
|
|
763
|
+
// Check if the SDK returned an error result (e.g., max turns, budget exceeded)
|
|
764
|
+
const isError = resultMessage.is_error || resultMessage.subtype !== 'success';
|
|
765
|
+
|
|
766
|
+
if (isError) {
|
|
767
|
+
// SDK error - we have usage but no valid findings
|
|
768
|
+
return {
|
|
769
|
+
findings: [],
|
|
770
|
+
usage: aggregateUsage(accumulatedUsage),
|
|
771
|
+
failed: true,
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
return {
|
|
776
|
+
findings: await parseHunkOutput(resultMessage, hunkCtx.filename, apiKey),
|
|
777
|
+
usage: aggregateUsage(accumulatedUsage),
|
|
778
|
+
failed: false,
|
|
779
|
+
};
|
|
780
|
+
} catch (error) {
|
|
781
|
+
lastError = error;
|
|
782
|
+
|
|
783
|
+
// Authentication errors should surface immediately with helpful guidance
|
|
784
|
+
if (isAuthenticationError(error)) {
|
|
785
|
+
throw new WardenAuthenticationError();
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
// Don't retry if not a retryable error or we've exhausted retries
|
|
789
|
+
if (!isRetryableError(error) || attempt >= retryConfig.maxRetries) {
|
|
790
|
+
break;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
// Calculate delay and wait before retry
|
|
794
|
+
const delayMs = calculateRetryDelay(attempt, retryConfig);
|
|
795
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
796
|
+
|
|
797
|
+
// Notify about retry in verbose mode
|
|
798
|
+
callbacks?.onRetry?.(
|
|
799
|
+
callbacks.lineRange,
|
|
800
|
+
attempt + 1,
|
|
801
|
+
retryConfig.maxRetries,
|
|
802
|
+
errorMessage,
|
|
803
|
+
delayMs
|
|
804
|
+
);
|
|
805
|
+
|
|
806
|
+
try {
|
|
807
|
+
await sleep(delayMs, abortController?.signal);
|
|
808
|
+
} catch {
|
|
809
|
+
// Aborted during sleep
|
|
810
|
+
return { findings: [], usage: aggregateUsage(accumulatedUsage), failed: true };
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// All attempts failed - return failure with any accumulated usage
|
|
816
|
+
// Log the final error for debugging if verbose
|
|
817
|
+
if (options.verbose && lastError) {
|
|
818
|
+
const errorMessage = lastError instanceof Error ? lastError.message : String(lastError);
|
|
819
|
+
callbacks?.onRetry?.(
|
|
820
|
+
callbacks.lineRange,
|
|
821
|
+
retryConfig.maxRetries + 1,
|
|
822
|
+
retryConfig.maxRetries,
|
|
823
|
+
`Final failure: ${errorMessage}`,
|
|
824
|
+
0
|
|
825
|
+
);
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
return { findings: [], usage: aggregateUsage(accumulatedUsage), failed: true };
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
/**
|
|
832
|
+
* Deduplicate findings by id and location.
|
|
833
|
+
*/
|
|
834
|
+
export function deduplicateFindings(findings: Finding[]): Finding[] {
|
|
835
|
+
const seen = new Set<string>();
|
|
836
|
+
return findings.filter((f) => {
|
|
837
|
+
const key = `${f.id}:${f.location?.path}:${f.location?.startLine}`;
|
|
838
|
+
if (seen.has(key)) return false;
|
|
839
|
+
seen.add(key);
|
|
840
|
+
return true;
|
|
841
|
+
});
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* A file prepared for analysis with its hunks.
|
|
846
|
+
*/
|
|
847
|
+
export interface PreparedFile {
|
|
848
|
+
filename: string;
|
|
849
|
+
hunks: HunkWithContext[];
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
function groupHunksByFile(hunks: HunkWithContext[]): PreparedFile[] {
|
|
853
|
+
const fileMap = new Map<string, HunkWithContext[]>();
|
|
854
|
+
|
|
855
|
+
for (const hunk of hunks) {
|
|
856
|
+
const existing = fileMap.get(hunk.filename);
|
|
857
|
+
if (existing) {
|
|
858
|
+
existing.push(hunk);
|
|
859
|
+
} else {
|
|
860
|
+
fileMap.set(hunk.filename, [hunk]);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
return Array.from(fileMap, ([filename, fileHunks]) => ({ filename, hunks: fileHunks }));
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
/**
|
|
868
|
+
* Get line range string for a hunk.
|
|
869
|
+
*/
|
|
870
|
+
function getHunkLineRange(hunk: HunkWithContext): string {
|
|
871
|
+
const start = hunk.hunk.newStart;
|
|
872
|
+
const end = start + hunk.hunk.newCount - 1;
|
|
873
|
+
return start === end ? `${start}` : `${start}-${end}`;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/**
|
|
877
|
+
* Attach elapsed time to findings if skill start time is available.
|
|
878
|
+
*/
|
|
879
|
+
function attachElapsedTime(findings: Finding[], skillStartTime: number | undefined): void {
|
|
880
|
+
if (skillStartTime === undefined) return;
|
|
881
|
+
const elapsedMs = Date.now() - skillStartTime;
|
|
882
|
+
for (const finding of findings) {
|
|
883
|
+
finding.elapsedMs = elapsedMs;
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
/**
|
|
888
|
+
* Options for preparing files for analysis.
|
|
889
|
+
*/
|
|
890
|
+
export interface PrepareFilesOptions {
|
|
891
|
+
/** Lines of context to include around each hunk */
|
|
892
|
+
contextLines?: number;
|
|
893
|
+
/** Chunking configuration for file patterns and coalescing */
|
|
894
|
+
chunking?: ChunkingConfig;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
/**
|
|
898
|
+
* Result from preparing files for analysis.
|
|
899
|
+
*/
|
|
900
|
+
export interface PrepareFilesResult {
|
|
901
|
+
/** Files prepared for analysis */
|
|
902
|
+
files: PreparedFile[];
|
|
903
|
+
/** Files that were skipped due to chunking patterns */
|
|
904
|
+
skippedFiles: SkippedFile[];
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
/**
|
|
908
|
+
* Prepare files for analysis by parsing patches into hunks with context.
|
|
909
|
+
* Returns files that have changes to analyze and files that were skipped.
|
|
910
|
+
*/
|
|
911
|
+
export function prepareFiles(
|
|
912
|
+
context: EventContext,
|
|
913
|
+
options: PrepareFilesOptions = {}
|
|
914
|
+
): PrepareFilesResult {
|
|
915
|
+
const { contextLines = 20, chunking } = options;
|
|
916
|
+
|
|
917
|
+
if (!context.pullRequest) {
|
|
918
|
+
return { files: [], skippedFiles: [] };
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
const pr = context.pullRequest;
|
|
922
|
+
const allHunks: HunkWithContext[] = [];
|
|
923
|
+
const skippedFiles: SkippedFile[] = [];
|
|
924
|
+
|
|
925
|
+
for (const file of pr.files) {
|
|
926
|
+
if (!file.patch) continue;
|
|
927
|
+
|
|
928
|
+
// Check if this file should be skipped based on chunking patterns
|
|
929
|
+
const mode = classifyFile(file.filename, chunking?.filePatterns);
|
|
930
|
+
if (mode === 'skip') {
|
|
931
|
+
skippedFiles.push({
|
|
932
|
+
filename: file.filename,
|
|
933
|
+
reason: 'builtin', // Could be enhanced to track which pattern matched
|
|
934
|
+
});
|
|
935
|
+
continue;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
const statusMap: Record<string, 'added' | 'removed' | 'modified' | 'renamed'> = {
|
|
939
|
+
added: 'added',
|
|
940
|
+
removed: 'removed',
|
|
941
|
+
modified: 'modified',
|
|
942
|
+
renamed: 'renamed',
|
|
943
|
+
copied: 'added',
|
|
944
|
+
changed: 'modified',
|
|
945
|
+
unchanged: 'modified',
|
|
946
|
+
};
|
|
947
|
+
const status = statusMap[file.status] ?? 'modified';
|
|
948
|
+
|
|
949
|
+
const diff = parseFileDiff(file.filename, file.patch, status);
|
|
950
|
+
|
|
951
|
+
// Apply hunk coalescing if enabled (default: enabled)
|
|
952
|
+
const coalesceEnabled = chunking?.coalesce?.enabled !== false;
|
|
953
|
+
const hunks = coalesceEnabled
|
|
954
|
+
? coalesceHunks(diff.hunks, {
|
|
955
|
+
maxGapLines: chunking?.coalesce?.maxGapLines,
|
|
956
|
+
maxChunkSize: chunking?.coalesce?.maxChunkSize,
|
|
957
|
+
})
|
|
958
|
+
: diff.hunks;
|
|
959
|
+
|
|
960
|
+
const hunksWithContext = expandDiffContext(context.repoPath, { ...diff, hunks }, contextLines);
|
|
961
|
+
allHunks.push(...hunksWithContext);
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
return {
|
|
965
|
+
files: groupHunksByFile(allHunks),
|
|
966
|
+
skippedFiles,
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
/**
|
|
971
|
+
* Callbacks for per-file analysis progress.
|
|
972
|
+
*/
|
|
973
|
+
export interface FileAnalysisCallbacks {
|
|
974
|
+
skillStartTime?: number;
|
|
975
|
+
onHunkStart?: (hunkNum: number, totalHunks: number, lineRange: string) => void;
|
|
976
|
+
onHunkComplete?: (hunkNum: number, findings: Finding[]) => void;
|
|
977
|
+
/** Called when a prompt exceeds the large prompt threshold */
|
|
978
|
+
onLargePrompt?: (lineRange: string, chars: number, estimatedTokens: number) => void;
|
|
979
|
+
/** Called with prompt size info in debug mode */
|
|
980
|
+
onPromptSize?: (lineRange: string, systemChars: number, userChars: number, totalChars: number, estimatedTokens: number) => void;
|
|
981
|
+
/** Called when a retry attempt is made (verbose mode) */
|
|
982
|
+
onRetry?: (lineRange: string, attempt: number, maxRetries: number, error: string, delayMs: number) => void;
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
/**
|
|
986
|
+
* Result from analyzing a single file.
|
|
987
|
+
*/
|
|
988
|
+
export interface FileAnalysisResult {
|
|
989
|
+
filename: string;
|
|
990
|
+
findings: Finding[];
|
|
991
|
+
usage: UsageStats;
|
|
992
|
+
/** Number of hunks that failed to analyze */
|
|
993
|
+
failedHunks: number;
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
/**
|
|
997
|
+
* Analyze a single prepared file's hunks.
|
|
998
|
+
*/
|
|
999
|
+
export async function analyzeFile(
|
|
1000
|
+
skill: SkillDefinition,
|
|
1001
|
+
file: PreparedFile,
|
|
1002
|
+
repoPath: string,
|
|
1003
|
+
options: SkillRunnerOptions = {},
|
|
1004
|
+
callbacks?: FileAnalysisCallbacks,
|
|
1005
|
+
prContext?: PRPromptContext
|
|
1006
|
+
): Promise<FileAnalysisResult> {
|
|
1007
|
+
const { abortController } = options;
|
|
1008
|
+
const fileFindings: Finding[] = [];
|
|
1009
|
+
const fileUsage: UsageStats[] = [];
|
|
1010
|
+
let failedHunks = 0;
|
|
1011
|
+
|
|
1012
|
+
for (const [hunkIndex, hunk] of file.hunks.entries()) {
|
|
1013
|
+
if (abortController?.signal.aborted) break;
|
|
1014
|
+
|
|
1015
|
+
const lineRange = getHunkLineRange(hunk);
|
|
1016
|
+
callbacks?.onHunkStart?.(hunkIndex + 1, file.hunks.length, lineRange);
|
|
1017
|
+
|
|
1018
|
+
const hunkCallbacks: HunkAnalysisCallbacks | undefined = callbacks
|
|
1019
|
+
? {
|
|
1020
|
+
lineRange,
|
|
1021
|
+
onLargePrompt: callbacks.onLargePrompt,
|
|
1022
|
+
onPromptSize: callbacks.onPromptSize,
|
|
1023
|
+
onRetry: callbacks.onRetry,
|
|
1024
|
+
}
|
|
1025
|
+
: undefined;
|
|
1026
|
+
|
|
1027
|
+
const result = await analyzeHunk(skill, hunk, repoPath, options, hunkCallbacks, prContext);
|
|
1028
|
+
|
|
1029
|
+
if (result.failed) {
|
|
1030
|
+
failedHunks++;
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
attachElapsedTime(result.findings, callbacks?.skillStartTime);
|
|
1034
|
+
callbacks?.onHunkComplete?.(hunkIndex + 1, result.findings);
|
|
1035
|
+
|
|
1036
|
+
fileFindings.push(...result.findings);
|
|
1037
|
+
fileUsage.push(result.usage);
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
return {
|
|
1041
|
+
filename: file.filename,
|
|
1042
|
+
findings: fileFindings,
|
|
1043
|
+
usage: aggregateUsage(fileUsage),
|
|
1044
|
+
failedHunks,
|
|
1045
|
+
};
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
/**
|
|
1049
|
+
* Run a skill on a PR, analyzing each hunk separately.
|
|
1050
|
+
*/
|
|
1051
|
+
export async function runSkill(
|
|
1052
|
+
skill: SkillDefinition,
|
|
1053
|
+
context: EventContext,
|
|
1054
|
+
options: SkillRunnerOptions = {}
|
|
1055
|
+
): Promise<SkillReport> {
|
|
1056
|
+
const { parallel = true, callbacks, abortController } = options;
|
|
1057
|
+
const startTime = Date.now();
|
|
1058
|
+
|
|
1059
|
+
if (!context.pullRequest) {
|
|
1060
|
+
throw new SkillRunnerError('Pull request context required for skill execution');
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
const { files: fileHunks, skippedFiles } = prepareFiles(context, {
|
|
1064
|
+
contextLines: options.contextLines,
|
|
1065
|
+
// Note: chunking config should come from the caller (e.g., from warden.toml defaults)
|
|
1066
|
+
// For now, we use built-in defaults. The caller can pass explicit chunking config.
|
|
1067
|
+
});
|
|
1068
|
+
|
|
1069
|
+
if (fileHunks.length === 0) {
|
|
1070
|
+
const report: SkillReport = {
|
|
1071
|
+
skill: skill.name,
|
|
1072
|
+
summary: 'No code changes to analyze',
|
|
1073
|
+
findings: [],
|
|
1074
|
+
usage: emptyUsage(),
|
|
1075
|
+
durationMs: Date.now() - startTime,
|
|
1076
|
+
};
|
|
1077
|
+
if (skippedFiles.length > 0) {
|
|
1078
|
+
report.skippedFiles = skippedFiles;
|
|
1079
|
+
}
|
|
1080
|
+
return report;
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
const totalFiles = fileHunks.length;
|
|
1084
|
+
const allFindings: Finding[] = [];
|
|
1085
|
+
|
|
1086
|
+
// Track all usage stats for aggregation
|
|
1087
|
+
const allUsage: UsageStats[] = [];
|
|
1088
|
+
|
|
1089
|
+
// Track failed hunks across all files
|
|
1090
|
+
let totalFailedHunks = 0;
|
|
1091
|
+
|
|
1092
|
+
// Build PR context for inclusion in prompts (helps LLM understand the full scope of changes)
|
|
1093
|
+
const prContext: PRPromptContext = {
|
|
1094
|
+
changedFiles: context.pullRequest.files.map((f) => f.filename),
|
|
1095
|
+
title: context.pullRequest.title,
|
|
1096
|
+
body: context.pullRequest.body,
|
|
1097
|
+
};
|
|
1098
|
+
|
|
1099
|
+
/**
|
|
1100
|
+
* Process all hunks for a single file sequentially.
|
|
1101
|
+
* Wraps analyzeFile with progress callbacks.
|
|
1102
|
+
*/
|
|
1103
|
+
async function processFile(
|
|
1104
|
+
fileHunkEntry: PreparedFile,
|
|
1105
|
+
fileIndex: number
|
|
1106
|
+
): Promise<FileAnalysisResult> {
|
|
1107
|
+
const { filename } = fileHunkEntry;
|
|
1108
|
+
|
|
1109
|
+
callbacks?.onFileStart?.(filename, fileIndex, totalFiles);
|
|
1110
|
+
|
|
1111
|
+
const fileCallbacks: FileAnalysisCallbacks = {
|
|
1112
|
+
skillStartTime: callbacks?.skillStartTime,
|
|
1113
|
+
onHunkStart: (hunkNum, totalHunks, lineRange) => {
|
|
1114
|
+
callbacks?.onHunkStart?.(filename, hunkNum, totalHunks, lineRange);
|
|
1115
|
+
},
|
|
1116
|
+
onHunkComplete: (hunkNum, findings) => {
|
|
1117
|
+
callbacks?.onHunkComplete?.(filename, hunkNum, findings);
|
|
1118
|
+
},
|
|
1119
|
+
onLargePrompt: callbacks?.onLargePrompt
|
|
1120
|
+
? (lineRange, chars, estimatedTokens) => {
|
|
1121
|
+
callbacks.onLargePrompt?.(filename, lineRange, chars, estimatedTokens);
|
|
1122
|
+
}
|
|
1123
|
+
: undefined,
|
|
1124
|
+
onPromptSize: callbacks?.onPromptSize
|
|
1125
|
+
? (lineRange, systemChars, userChars, totalChars, estimatedTokens) => {
|
|
1126
|
+
callbacks.onPromptSize?.(filename, lineRange, systemChars, userChars, totalChars, estimatedTokens);
|
|
1127
|
+
}
|
|
1128
|
+
: undefined,
|
|
1129
|
+
onRetry: callbacks?.onRetry
|
|
1130
|
+
? (lineRange, attempt, maxRetries, error, delayMs) => {
|
|
1131
|
+
callbacks.onRetry?.(filename, lineRange, attempt, maxRetries, error, delayMs);
|
|
1132
|
+
}
|
|
1133
|
+
: undefined,
|
|
1134
|
+
};
|
|
1135
|
+
|
|
1136
|
+
const result = await analyzeFile(skill, fileHunkEntry, context.repoPath, options, fileCallbacks, prContext);
|
|
1137
|
+
|
|
1138
|
+
callbacks?.onFileComplete?.(filename, fileIndex, totalFiles);
|
|
1139
|
+
|
|
1140
|
+
return result;
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
// Process files - parallel or sequential based on options
|
|
1144
|
+
if (parallel) {
|
|
1145
|
+
// Process files in parallel with concurrency limit
|
|
1146
|
+
const fileConcurrency = options.concurrency ?? DEFAULT_FILE_CONCURRENCY;
|
|
1147
|
+
const batchDelayMs = options.batchDelayMs ?? 0;
|
|
1148
|
+
|
|
1149
|
+
for (let i = 0; i < fileHunks.length; i += fileConcurrency) {
|
|
1150
|
+
// Check for abort before starting new batch
|
|
1151
|
+
if (abortController?.signal.aborted) break;
|
|
1152
|
+
|
|
1153
|
+
// Apply rate limiting delay between batches (not before the first batch)
|
|
1154
|
+
if (i > 0 && batchDelayMs > 0) {
|
|
1155
|
+
await new Promise((resolve) => setTimeout(resolve, batchDelayMs));
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
const batch = fileHunks.slice(i, i + fileConcurrency);
|
|
1159
|
+
const batchPromises = batch.map((fileHunkEntry, batchIndex) =>
|
|
1160
|
+
processFile(fileHunkEntry, i + batchIndex)
|
|
1161
|
+
);
|
|
1162
|
+
|
|
1163
|
+
const batchResults = await Promise.all(batchPromises);
|
|
1164
|
+
for (const result of batchResults) {
|
|
1165
|
+
allFindings.push(...result.findings);
|
|
1166
|
+
allUsage.push(result.usage);
|
|
1167
|
+
totalFailedHunks += result.failedHunks;
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
} else {
|
|
1171
|
+
// Process files sequentially
|
|
1172
|
+
for (const [fileIndex, fileHunkEntry] of fileHunks.entries()) {
|
|
1173
|
+
// Check for abort before starting new file
|
|
1174
|
+
if (abortController?.signal.aborted) break;
|
|
1175
|
+
|
|
1176
|
+
const result = await processFile(fileHunkEntry, fileIndex);
|
|
1177
|
+
allFindings.push(...result.findings);
|
|
1178
|
+
allUsage.push(result.usage);
|
|
1179
|
+
totalFailedHunks += result.failedHunks;
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
// Deduplicate findings
|
|
1184
|
+
const uniqueFindings = deduplicateFindings(allFindings);
|
|
1185
|
+
|
|
1186
|
+
// Generate summary
|
|
1187
|
+
const summary = generateSummary(skill.name, uniqueFindings);
|
|
1188
|
+
|
|
1189
|
+
// Aggregate usage across all hunks
|
|
1190
|
+
const totalUsage = aggregateUsage(allUsage);
|
|
1191
|
+
|
|
1192
|
+
const report: SkillReport = {
|
|
1193
|
+
skill: skill.name,
|
|
1194
|
+
summary,
|
|
1195
|
+
findings: uniqueFindings,
|
|
1196
|
+
usage: totalUsage,
|
|
1197
|
+
durationMs: Date.now() - startTime,
|
|
1198
|
+
};
|
|
1199
|
+
if (skippedFiles.length > 0) {
|
|
1200
|
+
report.skippedFiles = skippedFiles;
|
|
1201
|
+
}
|
|
1202
|
+
if (totalFailedHunks > 0) {
|
|
1203
|
+
report.failedHunks = totalFailedHunks;
|
|
1204
|
+
}
|
|
1205
|
+
return report;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
/**
|
|
1209
|
+
* Generate a summary of findings.
|
|
1210
|
+
*/
|
|
1211
|
+
export function generateSummary(skillName: string, findings: Finding[]): string {
|
|
1212
|
+
if (findings.length === 0) {
|
|
1213
|
+
return `${skillName}: No issues found`;
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
const counts: Record<string, number> = {};
|
|
1217
|
+
for (const f of findings) {
|
|
1218
|
+
counts[f.severity] = (counts[f.severity] ?? 0) + 1;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
const parts: string[] = [];
|
|
1222
|
+
if (counts['critical']) parts.push(`${counts['critical']} critical`);
|
|
1223
|
+
if (counts['high']) parts.push(`${counts['high']} high`);
|
|
1224
|
+
if (counts['medium']) parts.push(`${counts['medium']} medium`);
|
|
1225
|
+
if (counts['low']) parts.push(`${counts['low']} low`);
|
|
1226
|
+
if (counts['info']) parts.push(`${counts['info']} info`);
|
|
1227
|
+
|
|
1228
|
+
return `${skillName}: Found ${findings.length} issue${findings.length === 1 ? '' : 's'} (${parts.join(', ')})`;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
// Legacy export for backwards compatibility
|
|
1232
|
+
export { buildHunkSystemPrompt as buildSystemPrompt };
|