@delegance/claude-autopilot 2.4.0 → 5.0.0-alpha.1

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 (129) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/README.md +164 -106
  3. package/bin/_launcher.js +77 -0
  4. package/bin/claude-autopilot.js +3 -0
  5. package/bin/guardrail.js +3 -0
  6. package/package.json +15 -9
  7. package/presets/generic/guardrail.config.yaml +35 -0
  8. package/presets/generic/stack.md +40 -0
  9. package/presets/nextjs-supabase/{autopilot.config.yaml → guardrail.config.yaml} +7 -0
  10. package/scripts/autoregress.ts +27 -11
  11. package/skills/autopilot/SKILL.md +170 -0
  12. package/skills/claude-autopilot.md +80 -0
  13. package/skills/guardrail.md +39 -0
  14. package/skills/migrate/SKILL.md +83 -0
  15. package/src/adapters/council/claude.ts +41 -0
  16. package/src/adapters/council/openai.ts +40 -0
  17. package/src/adapters/council/types.ts +7 -0
  18. package/src/adapters/loader.ts +7 -7
  19. package/src/adapters/review-engine/auto.ts +2 -2
  20. package/src/adapters/review-engine/claude.ts +9 -11
  21. package/src/adapters/review-engine/codex.ts +9 -11
  22. package/src/adapters/review-engine/gemini.ts +9 -11
  23. package/src/adapters/review-engine/openai-compatible.ts +10 -12
  24. package/src/adapters/review-engine/parse-output.ts +32 -6
  25. package/src/adapters/review-engine/prompt-builder.ts +19 -0
  26. package/src/adapters/review-engine/types.ts +1 -1
  27. package/src/adapters/vcs-host/commit-status.ts +39 -0
  28. package/src/adapters/vcs-host/github.ts +2 -2
  29. package/src/cli/baseline.ts +125 -0
  30. package/src/cli/ci.ts +11 -8
  31. package/src/cli/costs.ts +80 -0
  32. package/src/cli/council.ts +96 -0
  33. package/src/cli/detector.ts +21 -5
  34. package/src/cli/explain.ts +197 -0
  35. package/src/cli/fix.ts +249 -0
  36. package/src/cli/hook.ts +72 -27
  37. package/src/cli/ignore-helper.ts +116 -0
  38. package/src/cli/index.ts +302 -28
  39. package/src/cli/init.ts +12 -12
  40. package/src/cli/lsp.ts +200 -0
  41. package/src/cli/mcp.ts +206 -0
  42. package/src/cli/pr-comment.ts +5 -5
  43. package/src/cli/pr-desc.ts +168 -0
  44. package/src/cli/pr-review-comments.ts +3 -3
  45. package/src/cli/pr.ts +76 -0
  46. package/src/cli/preflight.ts +15 -32
  47. package/src/cli/report.ts +186 -0
  48. package/src/cli/run.ts +140 -36
  49. package/src/cli/scan.ts +233 -0
  50. package/src/cli/setup.ts +121 -15
  51. package/src/cli/test-gen.ts +125 -0
  52. package/src/cli/triage.ts +137 -0
  53. package/src/cli/watch.ts +52 -31
  54. package/src/cli/worker.ts +109 -0
  55. package/src/core/cache/review-cache.ts +2 -2
  56. package/src/core/chunking/index.ts +2 -2
  57. package/src/core/config/loader.ts +24 -12
  58. package/src/core/config/preset-resolver.ts +6 -6
  59. package/src/core/config/schema.ts +121 -3
  60. package/src/core/config/types.ts +57 -2
  61. package/src/core/council/config.ts +71 -0
  62. package/src/core/council/context.ts +17 -0
  63. package/src/core/council/runner.ts +83 -0
  64. package/src/core/council/types.ts +45 -0
  65. package/src/core/detect/llm-key.ts +89 -0
  66. package/src/core/detect/workspaces.ts +103 -0
  67. package/src/core/errors.ts +4 -4
  68. package/src/core/fix/generator.ts +149 -0
  69. package/src/core/ignore/index.ts +4 -4
  70. package/src/core/mcp/concurrency.ts +16 -0
  71. package/src/core/mcp/handlers/fix-finding.ts +126 -0
  72. package/src/core/mcp/handlers/get-capabilities.ts +62 -0
  73. package/src/core/mcp/handlers/get-findings.ts +36 -0
  74. package/src/core/mcp/handlers/review-diff.ts +65 -0
  75. package/src/core/mcp/handlers/scan-files.ts +65 -0
  76. package/src/core/mcp/handlers/validate-fix.ts +41 -0
  77. package/src/core/mcp/run-store.ts +85 -0
  78. package/src/core/mcp/workspace.ts +35 -0
  79. package/src/core/persist/baseline.ts +112 -0
  80. package/src/core/persist/cost-log.ts +1 -1
  81. package/src/core/persist/findings-cache.ts +1 -1
  82. package/src/core/persist/triage.ts +112 -0
  83. package/src/core/phases/static-rules.ts +18 -5
  84. package/src/core/pipeline/review-phase.ts +65 -26
  85. package/src/core/pipeline/run.ts +42 -10
  86. package/src/core/runtime/lock.ts +2 -2
  87. package/src/core/runtime/state.ts +2 -2
  88. package/src/core/schema-alignment/detector.ts +59 -0
  89. package/src/core/schema-alignment/extractor/index.ts +24 -0
  90. package/src/core/schema-alignment/extractor/prisma.ts +21 -0
  91. package/src/core/schema-alignment/extractor/sql.ts +99 -0
  92. package/src/core/schema-alignment/llm-check.ts +91 -0
  93. package/src/core/schema-alignment/scanner.ts +107 -0
  94. package/src/core/schema-alignment/types.ts +43 -0
  95. package/src/core/shell.ts +3 -3
  96. package/src/core/static-rules/registry.ts +17 -8
  97. package/src/core/static-rules/rules/brand-tokens.ts +145 -0
  98. package/src/core/static-rules/rules/hardcoded-secrets.ts +27 -1
  99. package/src/core/static-rules/rules/insecure-redirect.ts +67 -0
  100. package/src/core/static-rules/rules/missing-auth.ts +70 -0
  101. package/src/core/static-rules/rules/schema-alignment.ts +132 -0
  102. package/src/core/static-rules/rules/sql-injection.ts +71 -0
  103. package/src/core/static-rules/rules/ssrf.ts +63 -0
  104. package/src/core/static-rules/tailwind-extractor.ts +38 -0
  105. package/src/core/test-gen/coverage-analyzer.ts +93 -0
  106. package/src/core/test-gen/framework-detector.ts +21 -0
  107. package/src/core/test-gen/test-writer.ts +33 -0
  108. package/src/core/ui/design-context-loader.ts +87 -0
  109. package/src/core/worker/client.ts +46 -0
  110. package/src/core/worker/lockfile.ts +38 -0
  111. package/src/core/worker/server.ts +81 -0
  112. package/src/formatters/junit.ts +52 -0
  113. package/src/formatters/sarif.ts +2 -2
  114. package/src/index.ts +1 -2
  115. package/tests/snapshots/baselines/src-formatters-sarif.json +4 -4
  116. package/tests/snapshots/index.json +3 -3
  117. package/tests/snapshots/src-formatters-sarif.snap.ts +1 -1
  118. package/tests/snapshots/src-snapshots-impact-selector.snap.ts +3 -3
  119. package/tests/snapshots/src-snapshots-import-scanner.snap.ts +3 -3
  120. package/tests/snapshots/src-snapshots-serializer.snap.ts +2 -2
  121. package/bin/autopilot.js +0 -20
  122. package/skills/autopilot.md +0 -157
  123. /package/presets/go/{autopilot.config.yaml → guardrail.config.yaml} +0 -0
  124. /package/presets/python-fastapi/{autopilot.config.yaml → guardrail.config.yaml} +0 -0
  125. /package/presets/rails-postgres/{autopilot.config.yaml → guardrail.config.yaml} +0 -0
  126. /package/presets/t3/{autopilot.config.yaml → guardrail.config.yaml} +0 -0
  127. /package/{src → scripts}/snapshots/impact-selector.ts +0 -0
  128. /package/{src → scripts}/snapshots/import-scanner.ts +0 -0
  129. /package/{src → scripts}/snapshots/serializer.ts +0 -0
@@ -1,4 +1,4 @@
1
- export const AUTOPILOT_CONFIG_SCHEMA = {
1
+ export const GUARDRAIL_CONFIG_SCHEMA = {
2
2
  $schema: 'http://json-schema.org/draft-07/schema#',
3
3
  type: 'object',
4
4
  required: ['configVersion'],
@@ -35,7 +35,24 @@ export const AUTOPILOT_CONFIG_SCHEMA = {
35
35
  },
36
36
  additionalProperties: false,
37
37
  },
38
- reviewStrategy: { enum: ['auto', 'single-pass', 'file-level'] },
38
+ ignore: {
39
+ type: 'array',
40
+ items: {
41
+ oneOf: [
42
+ { type: 'string' },
43
+ {
44
+ type: 'object',
45
+ required: ['path'],
46
+ properties: {
47
+ rule: { type: 'string' },
48
+ path: { type: 'string' },
49
+ },
50
+ additionalProperties: false,
51
+ },
52
+ ],
53
+ },
54
+ },
55
+ reviewStrategy: { enum: ['auto', 'single-pass', 'file-level', 'diff', 'auto-diff'] },
39
56
  chunking: {
40
57
  type: 'object',
41
58
  properties: {
@@ -47,10 +64,111 @@ export const AUTOPILOT_CONFIG_SCHEMA = {
47
64
  },
48
65
  additionalProperties: false,
49
66
  },
50
- cost: { type: 'object' },
67
+ policy: {
68
+ type: 'object',
69
+ properties: {
70
+ failOn: { enum: ['critical', 'warning', 'note', 'none'] },
71
+ newOnly: { type: 'boolean' },
72
+ baselinePath: { type: 'string' },
73
+ },
74
+ additionalProperties: false,
75
+ },
76
+ pipeline: {
77
+ type: 'object',
78
+ properties: {
79
+ runReviewOnStaticFail: { type: 'boolean' },
80
+ runReviewOnTestFail: { type: 'boolean' },
81
+ },
82
+ additionalProperties: false,
83
+ },
84
+ cost: {
85
+ type: 'object',
86
+ properties: {
87
+ maxPerRun: { type: 'number' },
88
+ estimateBeforeRun: { type: 'boolean' },
89
+ pricing: { type: 'object' },
90
+ },
91
+ additionalProperties: false,
92
+ },
93
+ brand: {
94
+ type: 'object',
95
+ properties: {
96
+ colorsFrom: { type: 'string' },
97
+ colors: { type: 'array', items: { type: 'string' } },
98
+ fonts: { type: 'array', items: { type: 'string' } },
99
+ componentLibrary: {
100
+ oneOf: [
101
+ { type: 'string' },
102
+ {
103
+ type: 'object',
104
+ properties: {
105
+ tokens: { type: 'string' },
106
+ guide: { type: 'string' },
107
+ },
108
+ additionalProperties: false,
109
+ },
110
+ ],
111
+ },
112
+ },
113
+ additionalProperties: false,
114
+ },
115
+ 'schema-alignment': {
116
+ type: 'object',
117
+ properties: {
118
+ enabled: { type: 'boolean' },
119
+ migrationGlobs: { type: 'array', items: { type: 'string', minLength: 1 } },
120
+ layerRoots: {
121
+ type: 'object',
122
+ properties: {
123
+ types: { type: 'array', items: { type: 'string' }, minItems: 1 },
124
+ api: { type: 'array', items: { type: 'string' }, minItems: 1 },
125
+ ui: { type: 'array', items: { type: 'string' }, minItems: 1 },
126
+ },
127
+ additionalProperties: false,
128
+ },
129
+ llmCheck: { type: 'boolean' },
130
+ severity: { enum: ['warning', 'error'] },
131
+ },
132
+ additionalProperties: false,
133
+ },
51
134
  cache: { type: 'object' },
52
135
  persistence: { type: 'object' },
53
136
  concurrency: { type: 'object' },
137
+ council: {
138
+ type: 'object',
139
+ required: ['models', 'synthesizer'],
140
+ additionalProperties: false,
141
+ properties: {
142
+ models: {
143
+ type: 'array',
144
+ minItems: 2,
145
+ items: {
146
+ type: 'object',
147
+ required: ['adapter', 'model', 'label'],
148
+ additionalProperties: false,
149
+ properties: {
150
+ adapter: { type: 'string' },
151
+ model: { type: 'string' },
152
+ label: { type: 'string' },
153
+ },
154
+ },
155
+ },
156
+ synthesizer: {
157
+ type: 'object',
158
+ required: ['adapter', 'model', 'label'],
159
+ additionalProperties: false,
160
+ properties: {
161
+ adapter: { type: 'string' },
162
+ model: { type: 'string' },
163
+ label: { type: 'string' },
164
+ },
165
+ },
166
+ timeout_ms: { type: 'number' },
167
+ min_successful_responses: { type: 'number' },
168
+ parallel_input_max_tokens: { type: 'number' },
169
+ synthesis_input_max_tokens: { type: 'number' },
170
+ },
171
+ },
54
172
  },
55
173
  definitions: {
56
174
  adapterRef: {
@@ -1,3 +1,5 @@
1
+ import type { SchemaAlignmentConfig } from '../schema-alignment/types.ts';
2
+
1
3
  export interface AdapterReference {
2
4
  adapter: string;
3
5
  options?: Record<string, unknown>;
@@ -7,7 +9,7 @@ export type AdapterRef = string | AdapterReference;
7
9
 
8
10
  export type StaticRuleReference = string | { adapter: string; options?: Record<string, unknown> };
9
11
 
10
- export interface AutopilotConfig {
12
+ export interface GuardrailConfig {
11
13
  configVersion: 1;
12
14
  preset?: string;
13
15
  reviewEngine?: AdapterRef;
@@ -36,8 +38,61 @@ export interface AutopilotConfig {
36
38
  parallelism?: number;
37
39
  rateLimitBackoff?: 'exp' | 'linear' | 'none';
38
40
  };
39
- cost?: Record<string, unknown>;
41
+ policy?: {
42
+ /** Severity threshold for exit code 1. Default: 'critical'. Use 'none' to always pass. */
43
+ failOn?: 'critical' | 'warning' | 'note' | 'none';
44
+ /** Only report findings not present in the committed baseline. Default: false. */
45
+ newOnly?: boolean;
46
+ /** Path to baseline file relative to cwd. Default: .guardrail-baseline.json */
47
+ baselinePath?: string;
48
+ };
49
+ pipeline?: {
50
+ /**
51
+ * When true, run the LLM review phase even if the static-rules phase reports `fail`
52
+ * (i.e. finds a critical). Default: true. Set to false to skip only the review
53
+ * phase on static-fail — the tests phase still runs regardless.
54
+ *
55
+ * Users that explicitly configure a review engine typically expect it to run — the
56
+ * bugs the LLM is best at (IDOR, TOCTOU, CORS, off-by-one, rate limits) often sit
57
+ * in the same commit as something a static rule already flagged. This flag only
58
+ * gates the review phase, mirroring `runReviewOnTestFail`.
59
+ */
60
+ runReviewOnStaticFail?: boolean;
61
+ /**
62
+ * When true, run the LLM review phase even if the tests phase reports `fail`.
63
+ * Default: false — failing tests usually indicate broken code, not code to review.
64
+ * This flag only gates the review phase; the tests phase itself always runs.
65
+ */
66
+ runReviewOnTestFail?: boolean;
67
+ };
68
+ cost?: {
69
+ /** Abort review phase if estimated spend exceeds this amount (USD). */
70
+ maxPerRun?: number;
71
+ /** Print token estimate before starting LLM review. Default: false. */
72
+ estimateBeforeRun?: boolean;
73
+ /** Per-model token price overrides (input/output per 1M tokens). */
74
+ pricing?: Record<string, { inputPer1M: number; outputPer1M: number }>;
75
+ };
76
+ brand?: {
77
+ /** Path to tailwind.config.{ts,js} — auto-extracts theme.colors as canonical palette */
78
+ colorsFrom?: string;
79
+ /** Explicit canonical color values (hex/rgb/hsl). Merged with colorsFrom. */
80
+ colors?: string[];
81
+ /** Canonical font family names */
82
+ fonts?: string[];
83
+ /** Path to design system component library (informational, for future LLM review) */
84
+ componentLibrary?: string | { tokens?: string; guide?: string };
85
+ };
86
+ 'schema-alignment'?: SchemaAlignmentConfig;
40
87
  cache?: Record<string, unknown>;
41
88
  persistence?: Record<string, unknown>;
42
89
  concurrency?: Record<string, unknown>;
90
+ council?: {
91
+ models: Array<{ adapter: string; model: string; label: string }>;
92
+ synthesizer: { adapter: string; model: string; label: string };
93
+ timeout_ms?: number;
94
+ min_successful_responses?: number;
95
+ parallel_input_max_tokens?: number;
96
+ synthesis_input_max_tokens?: number;
97
+ };
43
98
  }
@@ -0,0 +1,71 @@
1
+ // src/core/council/config.ts
2
+ import { GuardrailError } from '../errors.ts';
3
+ import type { CouncilConfig, CouncilModelEntry } from './types.ts';
4
+
5
+ const SUPPORTED_ADAPTERS = new Set(['claude', 'openai']);
6
+
7
+ export function parseCouncilConfig(raw: Record<string, unknown>): CouncilConfig {
8
+ const models = raw['models'] as Array<Record<string, string>> | undefined;
9
+ const synthRaw = raw['synthesizer'] as Record<string, string> | undefined;
10
+ const timeoutMs = (raw['timeout_ms'] as number | undefined) ?? 30000;
11
+ const minSuccessful = (raw['min_successful_responses'] as number | undefined) ?? 1;
12
+ const parallelInputMaxTokens = (raw['parallel_input_max_tokens'] as number | undefined) ?? 8000;
13
+ const synthesisInputMaxTokens = (raw['synthesis_input_max_tokens'] as number | undefined) ?? 12000;
14
+
15
+ if (!Array.isArray(models) || models.length < 2) {
16
+ throw new GuardrailError('council.models must have at least 2 entries', { code: 'invalid_config' });
17
+ }
18
+
19
+ if (!synthRaw?.['adapter'] || !synthRaw['model'] || !synthRaw['label']) {
20
+ throw new GuardrailError('council.synthesizer requires adapter, model, and label', { code: 'invalid_config' });
21
+ }
22
+
23
+ if (timeoutMs < 5000) {
24
+ throw new GuardrailError(`council.timeout_ms must be >= 5000, got ${timeoutMs}`, { code: 'invalid_config' });
25
+ }
26
+
27
+ if (minSuccessful < 1 || minSuccessful > models.length) {
28
+ throw new GuardrailError(
29
+ `council.min_successful_responses must be 1–${models.length}, got ${minSuccessful}`,
30
+ { code: 'invalid_config' },
31
+ );
32
+ }
33
+
34
+ for (const entry of [...models, synthRaw]) {
35
+ if (!SUPPORTED_ADAPTERS.has(entry['adapter']!)) {
36
+ throw new GuardrailError(
37
+ `council: unknown adapter "${entry['adapter']}" — supported: ${[...SUPPORTED_ADAPTERS].join(', ')}`,
38
+ { code: 'invalid_config' },
39
+ );
40
+ }
41
+ }
42
+
43
+ const seen = new Set<string>();
44
+ for (const m of models) {
45
+ if (seen.has(m['label']!)) {
46
+ throw new GuardrailError(`council.models: duplicate label "${m['label']}"`, { code: 'invalid_config' });
47
+ }
48
+ seen.add(m['label']!);
49
+ }
50
+
51
+ const parsedModels: CouncilModelEntry[] = models.map(m => ({
52
+ adapter: m['adapter'] as 'claude' | 'openai',
53
+ model: m['model']!,
54
+ label: m['label']!,
55
+ }));
56
+
57
+ const synthesizer: CouncilModelEntry = {
58
+ adapter: synthRaw['adapter'] as 'claude' | 'openai',
59
+ model: synthRaw['model']!,
60
+ label: synthRaw['label']!,
61
+ };
62
+
63
+ return {
64
+ models: parsedModels,
65
+ synthesizer,
66
+ timeoutMs,
67
+ minSuccessfulResponses: minSuccessful,
68
+ parallelInputMaxTokens,
69
+ synthesisInputMaxTokens,
70
+ };
71
+ }
@@ -0,0 +1,17 @@
1
+ const CHARS_PER_TOKEN = 4;
2
+
3
+ export function windowContext(text: string, maxTokens: number): string {
4
+ const estimated = Math.ceil(text.length / CHARS_PER_TOKEN);
5
+ if (estimated <= maxTokens) return text;
6
+
7
+ const maxChars = maxTokens * CHARS_PER_TOKEN;
8
+ // Reserve budget for the marker so the final output stays within maxTokens.
9
+ // Use a conservative upper bound of the formatted marker (the digit count of
10
+ // charsDropped is computed from text length to avoid circular dependency).
11
+ const markerOverhead = `<!-- [council: truncated ${text.length} chars] -->\n`.length;
12
+ const effectiveMaxChars = Math.max(0, maxChars - markerOverhead);
13
+ const charsDropped = text.length - effectiveMaxChars;
14
+ const marker = `<!-- [council: truncated ${charsDropped} chars] -->\n`;
15
+ process.stderr.write(`[council] context truncated: dropped ${charsDropped} chars to fit ${maxTokens} token budget\n`);
16
+ return marker + text.slice(charsDropped);
17
+ }
@@ -0,0 +1,83 @@
1
+ import * as crypto from 'node:crypto';
2
+ import { windowContext } from './context.ts';
3
+ import type { CouncilConfig, CouncilResult, ModelResponse } from './types.ts';
4
+ import type { CouncilAdapter } from '../../adapters/council/types.ts';
5
+
6
+ async function consultWithTimeout(
7
+ adapter: CouncilAdapter,
8
+ prompt: string,
9
+ context: string,
10
+ timeoutMs: number,
11
+ ): Promise<ModelResponse> {
12
+ const start = Date.now();
13
+ let timer: NodeJS.Timeout | undefined;
14
+ try {
15
+ const text = await Promise.race([
16
+ adapter.consult(prompt, context),
17
+ new Promise<never>((_, reject) => {
18
+ timer = setTimeout(() => reject(new Error('timeout')), timeoutMs);
19
+ }),
20
+ ]);
21
+ return { label: adapter.label, status: 'ok', text, latencyMs: Date.now() - start };
22
+ } catch (err) {
23
+ const message = err instanceof Error ? err.message : String(err);
24
+ return message === 'timeout'
25
+ ? { label: adapter.label, status: 'timeout', error: 'timed out', latencyMs: Date.now() - start }
26
+ : { label: adapter.label, status: 'error', error: message, latencyMs: Date.now() - start };
27
+ } finally {
28
+ // Always clear the timer to avoid keeping the event loop alive after the
29
+ // adapter resolves/rejects. Long-running hosts (MCP server) would accumulate
30
+ // dangling timers for the full timeoutMs otherwise.
31
+ if (timer) clearTimeout(timer);
32
+ }
33
+ }
34
+
35
+ export async function runCouncil(
36
+ config: CouncilConfig,
37
+ adapters: CouncilAdapter[],
38
+ synthesizer: CouncilAdapter,
39
+ prompt: string,
40
+ contextDoc: string,
41
+ ): Promise<CouncilResult> {
42
+ const run_id = crypto.randomUUID();
43
+ const context = windowContext(contextDoc, config.parallelInputMaxTokens);
44
+
45
+ const responses = await Promise.all(
46
+ adapters.map(a => consultWithTimeout(a, prompt, context, config.timeoutMs))
47
+ );
48
+
49
+ const successful = responses.filter(r => r.status === 'ok');
50
+
51
+ if (successful.length < config.minSuccessfulResponses) {
52
+ return { schema_version: 1, run_id, status: 'failed', prompt, responses };
53
+ }
54
+
55
+ const responseSections = successful
56
+ .map(r => `### ${r.label}\n${r.text}`)
57
+ .join('\n\n');
58
+
59
+ const synthesisDoc = `${contextDoc}\n\n---\n\n${responseSections}`;
60
+ const synthesisCtx = windowContext(synthesisDoc, config.synthesisInputMaxTokens);
61
+ const synthesisPrompt = [
62
+ `You have received responses from multiple technical advisors on the following question:\n\n## Original Question\n\n${prompt}`,
63
+ `## Advisor Responses\n\n${responseSections}`,
64
+ 'Based on these responses, provide a synthesis: areas of agreement, key disagreements, and your final recommendation.',
65
+ ].join('\n\n');
66
+
67
+ // Synthesizer shares the same per-call timeout as model calls so a hung
68
+ // synthesizer API doesn't block the whole command indefinitely.
69
+ const synthResponse = await consultWithTimeout(
70
+ synthesizer,
71
+ synthesisPrompt,
72
+ synthesisCtx,
73
+ config.timeoutMs,
74
+ );
75
+ // status:'ok' means the synthesizer call itself completed without error.
76
+ // Empty text is valid (e.g. the --no-synthesize stub that intentionally
77
+ // returns ''); only treat actual failures/timeouts as partial.
78
+ if (synthResponse.status === 'ok') {
79
+ const synthesis = { label: synthesizer.label, text: synthResponse.text ?? '', latencyMs: synthResponse.latencyMs };
80
+ return { schema_version: 1, run_id, status: 'success', prompt, responses, synthesis };
81
+ }
82
+ return { schema_version: 1, run_id, status: 'partial', prompt, responses };
83
+ }
@@ -0,0 +1,45 @@
1
+ // adapter is a closed union — extending to a new provider requires an intentional
2
+ // code change in config.ts and cli/council.ts
3
+ export interface CouncilModelEntry {
4
+ adapter: 'claude' | 'openai';
5
+ model: string;
6
+ label: string;
7
+ }
8
+
9
+ export interface CouncilConfig {
10
+ models: CouncilModelEntry[];
11
+ synthesizer: CouncilModelEntry;
12
+ timeoutMs: number;
13
+ minSuccessfulResponses: number;
14
+ parallelInputMaxTokens: number;
15
+ synthesisInputMaxTokens: number;
16
+ }
17
+
18
+ export type ModelResponseStatus = 'ok' | 'timeout' | 'error';
19
+
20
+ export interface ModelResponse {
21
+ label: string;
22
+ status: ModelResponseStatus;
23
+ text?: string;
24
+ error?: string;
25
+ latencyMs: number;
26
+ }
27
+
28
+ export interface SynthesisResponse {
29
+ label: string;
30
+ text: string;
31
+ latencyMs: number;
32
+ }
33
+
34
+ export type CouncilStatus = 'success' | 'partial' | 'failed';
35
+
36
+ export interface CouncilResult {
37
+ // snake_case: wire-format field, consistent with MCP handler schema_version convention
38
+ schema_version: 1;
39
+ // snake_case: wire-format field
40
+ run_id: string;
41
+ status: CouncilStatus;
42
+ prompt: string;
43
+ responses: ModelResponse[];
44
+ synthesis?: SynthesisResponse;
45
+ }
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Shared LLM API key detection. Used by setup, doctor/preflight, scan, and run so
3
+ * every surface agrees on which env vars count as "have a key."
4
+ *
5
+ * Before this unified helper, doctor only checked ANTHROPIC_API_KEY + OPENAI_API_KEY
6
+ * while setup/scan/run checked all 5 providers — producing contradictory messages
7
+ * ("LLM API key: detected" from setup, "No LLM API key" from doctor moments later).
8
+ */
9
+
10
+ import * as fs from 'node:fs';
11
+
12
+ /** All env var names guardrail recognizes as LLM API keys, ordered by preference. */
13
+ export const LLM_KEY_NAMES = [
14
+ 'ANTHROPIC_API_KEY',
15
+ 'OPENAI_API_KEY',
16
+ 'GEMINI_API_KEY',
17
+ 'GOOGLE_API_KEY',
18
+ 'GROQ_API_KEY',
19
+ ] as const;
20
+
21
+ export type LLMKeyName = typeof LLM_KEY_NAMES[number];
22
+
23
+ export interface KeyDetectionOptions {
24
+ /** Additional key→value map to check alongside process.env (e.g. parsed .env.local). */
25
+ extraEnv?: Record<string, string | undefined>;
26
+ }
27
+
28
+ export interface KeyDetectionResult {
29
+ /** True if any recognized LLM key is set to a non-empty value. */
30
+ hasKey: boolean;
31
+ /** Preferred key that was detected, or null. Follows LLM_KEY_NAMES order. */
32
+ preferred: LLMKeyName | null;
33
+ /** All keys that were detected, in LLM_KEY_NAMES order. */
34
+ detected: LLMKeyName[];
35
+ }
36
+
37
+ function readEnvFileSync(filePath: string): Record<string, string> {
38
+ const vars: Record<string, string> = {};
39
+ try {
40
+ const content = fs.readFileSync(filePath, 'utf-8');
41
+ for (const line of content.split('\n')) {
42
+ const trimmed = line.trim();
43
+ if (!trimmed || trimmed.startsWith('#')) continue;
44
+ const eq = trimmed.indexOf('=');
45
+ if (eq < 0) continue;
46
+ vars[trimmed.slice(0, eq).trim()] = trimmed.slice(eq + 1).trim().replace(/^['"]|['"]$/g, '');
47
+ }
48
+ } catch {
49
+ /* ignore */
50
+ }
51
+ return vars;
52
+ }
53
+
54
+ /** Load an env file into a plain object without mutating process.env. */
55
+ export function loadEnvFile(filePath: string): Record<string, string> {
56
+ return readEnvFileSync(filePath);
57
+ }
58
+
59
+ /** Detect whether any recognized LLM API key is set. */
60
+ export function detectLLMKey(options: KeyDetectionOptions = {}): KeyDetectionResult {
61
+ const extra = options.extraEnv ?? {};
62
+ const detected: LLMKeyName[] = [];
63
+ for (const name of LLM_KEY_NAMES) {
64
+ // Treat empty string as "not set" so an env file value can supply the key when
65
+ // the shell has `FOO=` exported. `??` would shadow the env file here because it
66
+ // only falls through on null/undefined — matching the old `!! || !!` semantics.
67
+ const fromProcess = process.env[name];
68
+ const value = (fromProcess && fromProcess.length > 0) ? fromProcess : extra[name];
69
+ if (value && value.length > 0) detected.push(name);
70
+ }
71
+ return {
72
+ hasKey: detected.length > 0,
73
+ preferred: detected[0] ?? null,
74
+ detected,
75
+ };
76
+ }
77
+
78
+ /**
79
+ * Human-readable list of providers and signup URLs, used by every "no key" message.
80
+ * Must cover every entry in LLM_KEY_NAMES so users see the same set of options across
81
+ * preflight, setup, scan, and run.
82
+ */
83
+ export const LLM_KEY_HINTS: Array<{ name: LLMKeyName; url: string; note?: string }> = [
84
+ { name: 'ANTHROPIC_API_KEY', url: 'https://console.anthropic.com/' },
85
+ { name: 'OPENAI_API_KEY', url: 'https://platform.openai.com/api-keys' },
86
+ { name: 'GEMINI_API_KEY', url: 'https://aistudio.google.com/app/apikey' },
87
+ { name: 'GOOGLE_API_KEY', url: 'https://aistudio.google.com/app/apikey', note: 'legacy alias for GEMINI_API_KEY' },
88
+ { name: 'GROQ_API_KEY', url: 'https://console.groq.com/keys', note: 'fast free tier' },
89
+ ];
@@ -0,0 +1,103 @@
1
+ import * as fs from 'node:fs';
2
+ import * as path from 'node:path';
3
+
4
+ export interface Workspace {
5
+ name: string;
6
+ dir: string; // absolute path
7
+ rel: string; // relative to root
8
+ testCommand?: string;
9
+ }
10
+
11
+ function readJson(p: string): Record<string, unknown> | null {
12
+ try { return JSON.parse(fs.readFileSync(p, 'utf8')) as Record<string, unknown>; } catch { return null; }
13
+ }
14
+
15
+ function globDirs(root: string, patterns: string[]): string[] {
16
+ const results: string[] = [];
17
+ for (const pattern of patterns) {
18
+ // Support "packages/*" and "apps/*" style globs (one level deep only)
19
+ const parts = pattern.split('/');
20
+ if (parts.length === 2 && parts[1] === '*') {
21
+ const base = path.join(root, parts[0]!);
22
+ if (!fs.existsSync(base)) continue;
23
+ for (const entry of fs.readdirSync(base, { withFileTypes: true })) {
24
+ if (entry.isDirectory()) results.push(path.join(base, entry.name));
25
+ }
26
+ } else {
27
+ const abs = path.join(root, pattern);
28
+ if (fs.existsSync(abs) && fs.statSync(abs).isDirectory()) results.push(abs);
29
+ }
30
+ }
31
+ return results;
32
+ }
33
+
34
+ function detectTestCommand(dir: string): string | undefined {
35
+ const pkg = readJson(path.join(dir, 'package.json'));
36
+ if (pkg?.scripts && typeof (pkg.scripts as Record<string, unknown>).test === 'string') {
37
+ return `npm test --prefix ${dir}`;
38
+ }
39
+ if (fs.existsSync(path.join(dir, 'go.mod'))) return `go test ./... -C ${dir}`;
40
+ if (fs.existsSync(path.join(dir, 'Cargo.toml'))) return `cargo test --manifest-path ${path.join(dir, 'Cargo.toml')}`;
41
+ return undefined;
42
+ }
43
+
44
+ /** Detect npm/yarn/pnpm workspaces, Turborepo, Nx, Go multi-module. */
45
+ export function detectWorkspaces(cwd: string): Workspace[] | null {
46
+ const pkg = readJson(path.join(cwd, 'package.json')) as { workspaces?: string[] | { packages?: string[] }; name?: string } | null;
47
+
48
+ // npm/yarn workspaces
49
+ let wsDirs: string[] = [];
50
+ if (pkg?.workspaces) {
51
+ const patterns = Array.isArray(pkg.workspaces) ? pkg.workspaces : (pkg.workspaces.packages ?? []);
52
+ wsDirs = globDirs(cwd, patterns);
53
+ }
54
+
55
+ // Turborepo — pnpm-workspace.yaml or turbo.json
56
+ if (wsDirs.length === 0 && fs.existsSync(path.join(cwd, 'pnpm-workspace.yaml'))) {
57
+ try {
58
+ const raw = fs.readFileSync(path.join(cwd, 'pnpm-workspace.yaml'), 'utf8');
59
+ const matches = raw.match(/^\s*-\s*['"]?([^'"#\n]+)['"]?/gm) ?? [];
60
+ const patterns = matches.map(m => m.replace(/^\s*-\s*['"]?/, '').replace(/['"]?\s*$/, '').trim());
61
+ wsDirs = globDirs(cwd, patterns);
62
+ } catch { /* ignore */ }
63
+ }
64
+
65
+ // Turborepo fallback: packages/ and apps/ dirs
66
+ if (wsDirs.length === 0 && fs.existsSync(path.join(cwd, 'turbo.json'))) {
67
+ wsDirs = globDirs(cwd, ['packages/*', 'apps/*']);
68
+ }
69
+
70
+ // Nx: check nx.json + libs/ + apps/
71
+ if (wsDirs.length === 0 && fs.existsSync(path.join(cwd, 'nx.json'))) {
72
+ wsDirs = globDirs(cwd, ['libs/*', 'apps/*', 'packages/*']);
73
+ }
74
+
75
+ if (wsDirs.length === 0) return null;
76
+
77
+ return wsDirs
78
+ .filter(d => fs.existsSync(d))
79
+ .map(d => {
80
+ const rel = path.relative(cwd, d);
81
+ const pkgJson = readJson(path.join(d, 'package.json')) as { name?: string } | null;
82
+ return {
83
+ name: pkgJson?.name ?? rel,
84
+ dir: d,
85
+ rel,
86
+ testCommand: detectTestCommand(d),
87
+ };
88
+ });
89
+ }
90
+
91
+ /** Given a list of touched files, return which workspaces they belong to. */
92
+ export function mapFilesToWorkspaces(files: string[], workspaces: Workspace[], cwd: string): Map<Workspace, string[]> {
93
+ const result = new Map<Workspace, string[]>();
94
+ for (const file of files) {
95
+ const abs = path.isAbsolute(file) ? file : path.resolve(cwd, file);
96
+ const ws = workspaces.find(w => abs.startsWith(w.dir + path.sep) || abs === w.dir);
97
+ if (ws) {
98
+ if (!result.has(ws)) result.set(ws, []);
99
+ result.get(ws)!.push(file);
100
+ }
101
+ }
102
+ return result;
103
+ }
@@ -4,7 +4,7 @@ export type ErrorCode =
4
4
  | 'auth' | 'rate_limit' | 'transient_network' | 'invalid_config'
5
5
  | 'adapter_bug' | 'user_input' | 'budget_exceeded' | 'concurrency_lock' | 'superseded';
6
6
 
7
- export interface AutopilotErrorOptions {
7
+ export interface GuardrailErrorOptions {
8
8
  code: ErrorCode;
9
9
  retryable?: boolean;
10
10
  provider?: string;
@@ -18,16 +18,16 @@ const DEFAULT_RETRYABLE: Record<ErrorCode, boolean> = {
18
18
  concurrency_lock: false, superseded: false,
19
19
  };
20
20
 
21
- export class AutopilotError extends Error {
21
+ export class GuardrailError extends Error {
22
22
  code: ErrorCode;
23
23
  retryable: boolean;
24
24
  provider?: string;
25
25
  step?: string;
26
26
  details: Record<string, unknown>;
27
27
 
28
- constructor(message: string, options: AutopilotErrorOptions) {
28
+ constructor(message: string, options: GuardrailErrorOptions) {
29
29
  super(message);
30
- this.name = 'AutopilotError';
30
+ this.name = 'GuardrailError';
31
31
  this.code = options.code;
32
32
  this.retryable = options.retryable ?? DEFAULT_RETRYABLE[options.code];
33
33
  this.provider = options.provider;