@almightygpt/core 0.9.2 → 0.10.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.
- package/dist/adapters/factory.d.ts +12 -0
- package/dist/adapters/factory.d.ts.map +1 -0
- package/dist/adapters/factory.js +40 -0
- package/dist/adapters/factory.js.map +1 -0
- package/dist/auth/__tests__/keychain.test.d.ts +18 -0
- package/dist/auth/__tests__/keychain.test.d.ts.map +1 -0
- package/dist/auth/__tests__/keychain.test.js +155 -0
- package/dist/auth/__tests__/keychain.test.js.map +1 -0
- package/dist/auth/__tests__/resolver.test.d.ts +13 -0
- package/dist/auth/__tests__/resolver.test.d.ts.map +1 -0
- package/dist/auth/__tests__/resolver.test.js +182 -0
- package/dist/auth/__tests__/resolver.test.js.map +1 -0
- package/dist/auth/__tests__/validator.test.d.ts +15 -0
- package/dist/auth/__tests__/validator.test.d.ts.map +1 -0
- package/dist/auth/__tests__/validator.test.js +197 -0
- package/dist/auth/__tests__/validator.test.js.map +1 -0
- package/dist/auth/validator.js +19 -14
- package/dist/auth/validator.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/plan/run-plan-review.d.ts +40 -0
- package/dist/plan/run-plan-review.d.ts.map +1 -0
- package/dist/plan/run-plan-review.js +224 -0
- package/dist/plan/run-plan-review.js.map +1 -0
- package/dist/plan/run-plan.d.ts +42 -0
- package/dist/plan/run-plan.d.ts.map +1 -0
- package/dist/plan/run-plan.js +193 -0
- package/dist/plan/run-plan.js.map +1 -0
- package/dist/runs/types.d.ts +1 -1
- package/dist/runs/types.d.ts.map +1 -1
- package/package.json +4 -2
- package/src/adapters/factory.ts +45 -0
- package/src/auth/__tests__/keychain.test.ts +171 -0
- package/src/auth/__tests__/resolver.test.ts +231 -0
- package/src/auth/__tests__/validator.test.ts +241 -0
- package/src/auth/validator.ts +27 -14
- package/src/index.ts +13 -1
- package/src/plan/run-plan-review.ts +302 -0
- package/src/plan/run-plan.ts +247 -0
- package/src/runs/types.ts +3 -1
package/src/auth/validator.ts
CHANGED
|
@@ -107,7 +107,7 @@ async function validateOpenAI(key: string): Promise<ValidationResult> {
|
|
|
107
107
|
return {
|
|
108
108
|
ok: false,
|
|
109
109
|
statusCode: res.status,
|
|
110
|
-
error: normalizeOpenAIError(res.status, rawBody),
|
|
110
|
+
error: normalizeOpenAIError(res.status, rawBody, key),
|
|
111
111
|
rawBody,
|
|
112
112
|
};
|
|
113
113
|
}
|
|
@@ -142,7 +142,7 @@ async function validateAnthropic(key: string): Promise<ValidationResult> {
|
|
|
142
142
|
return {
|
|
143
143
|
ok: false,
|
|
144
144
|
statusCode: res.status,
|
|
145
|
-
error: normalizeAnthropicError(res.status, rawBody),
|
|
145
|
+
error: normalizeAnthropicError(res.status, rawBody, key),
|
|
146
146
|
rawBody,
|
|
147
147
|
};
|
|
148
148
|
}
|
|
@@ -193,28 +193,36 @@ async function validateGoogle(key: string): Promise<ValidationResult> {
|
|
|
193
193
|
// Never echo the raw key back even by accident (defense in depth: we
|
|
194
194
|
// also redact anything that looks like the submitted key).
|
|
195
195
|
|
|
196
|
-
function normalizeOpenAIError(
|
|
196
|
+
function normalizeOpenAIError(
|
|
197
|
+
status: number,
|
|
198
|
+
rawBody: string,
|
|
199
|
+
submittedKey: string,
|
|
200
|
+
): string {
|
|
197
201
|
// OpenAI shape: { "error": { "message": "...", "type": "...", "code": "..." } }
|
|
198
202
|
try {
|
|
199
203
|
const parsed = JSON.parse(rawBody) as {
|
|
200
204
|
error?: { message?: string; type?: string; code?: string };
|
|
201
205
|
};
|
|
202
206
|
const msg = parsed.error?.message;
|
|
203
|
-
if (msg) return `[${status}] OpenAI: ${truncate(msg, 200)}`;
|
|
207
|
+
if (msg) return `[${status}] OpenAI: ${truncate(redactKey(msg, submittedKey), 200)}`;
|
|
204
208
|
} catch {
|
|
205
209
|
/* fall through */
|
|
206
210
|
}
|
|
207
211
|
return statusOnlyMessage("OpenAI", status);
|
|
208
212
|
}
|
|
209
213
|
|
|
210
|
-
function normalizeAnthropicError(
|
|
214
|
+
function normalizeAnthropicError(
|
|
215
|
+
status: number,
|
|
216
|
+
rawBody: string,
|
|
217
|
+
submittedKey: string,
|
|
218
|
+
): string {
|
|
211
219
|
// Anthropic shape: { "type": "error", "error": { "type": "...", "message": "..." } }
|
|
212
220
|
try {
|
|
213
221
|
const parsed = JSON.parse(rawBody) as {
|
|
214
222
|
error?: { type?: string; message?: string };
|
|
215
223
|
};
|
|
216
224
|
const msg = parsed.error?.message;
|
|
217
|
-
if (msg) return `[${status}] Anthropic: ${truncate(msg, 200)}`;
|
|
225
|
+
if (msg) return `[${status}] Anthropic: ${truncate(redactKey(msg, submittedKey), 200)}`;
|
|
218
226
|
} catch {
|
|
219
227
|
/* fall through */
|
|
220
228
|
}
|
|
@@ -231,20 +239,25 @@ function normalizeGoogleError(
|
|
|
231
239
|
const parsed = JSON.parse(rawBody) as {
|
|
232
240
|
error?: { code?: number; message?: string; status?: string };
|
|
233
241
|
};
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
// error messages (e.g. "API key not valid. Pass a valid API key.")
|
|
237
|
-
// — we don't ship the actual key value if it ever ends up here.
|
|
238
|
-
if (submittedKey && msg.includes(submittedKey)) {
|
|
239
|
-
msg = msg.replace(submittedKey, "<redacted-key>");
|
|
240
|
-
}
|
|
241
|
-
if (msg) return `[${status}] Google: ${truncate(msg, 200)}`;
|
|
242
|
+
const msg = parsed.error?.message ?? "";
|
|
243
|
+
if (msg) return `[${status}] Google: ${truncate(redactKey(msg, submittedKey), 200)}`;
|
|
242
244
|
} catch {
|
|
243
245
|
/* fall through */
|
|
244
246
|
}
|
|
245
247
|
return statusOnlyMessage("Google", status);
|
|
246
248
|
}
|
|
247
249
|
|
|
250
|
+
/**
|
|
251
|
+
* Belt-and-braces: if a provider echoes the submitted key in its
|
|
252
|
+
* error body, redact before surfacing to the user. Codex's v0.8 P2 #6
|
|
253
|
+
* found this gap (originally Google-only); now applied to all three
|
|
254
|
+
* providers via this shared helper.
|
|
255
|
+
*/
|
|
256
|
+
function redactKey(msg: string, key: string): string {
|
|
257
|
+
if (!key || !msg.includes(key)) return msg;
|
|
258
|
+
return msg.split(key).join("<redacted-key>");
|
|
259
|
+
}
|
|
260
|
+
|
|
248
261
|
function statusOnlyMessage(provider: string, status: number): string {
|
|
249
262
|
if (status === 401 || status === 403) {
|
|
250
263
|
return `[${status}] ${provider} rejected the key (unauthorized).`;
|
package/src/index.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* - budget/ ✅ task #14 BudgetTracker + BudgetExceededError
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
-
export const VERSION = "0.
|
|
16
|
+
export const VERSION = "0.10.1";
|
|
17
17
|
|
|
18
18
|
// MCP server (v0.9.0+) — exposes AlmightyGPT's review surface as MCP tools.
|
|
19
19
|
export { startMcpServer } from "./mcp/server.js";
|
|
@@ -143,6 +143,18 @@ export {
|
|
|
143
143
|
type KeychainAdapter,
|
|
144
144
|
} from "./auth/keychain.js";
|
|
145
145
|
export { validateKey, type ValidationResult } from "./auth/validator.js";
|
|
146
|
+
// Plan subsystem (v0.10.0+) — Worker plan + Reviewer plan-review
|
|
147
|
+
export {
|
|
148
|
+
runWorkerPlan,
|
|
149
|
+
type PlanOptions,
|
|
150
|
+
type PlanResult,
|
|
151
|
+
} from "./plan/run-plan.js";
|
|
152
|
+
export {
|
|
153
|
+
runPlanReview,
|
|
154
|
+
type PlanReviewOptions,
|
|
155
|
+
type PlanReviewResult,
|
|
156
|
+
} from "./plan/run-plan-review.js";
|
|
157
|
+
|
|
146
158
|
export {
|
|
147
159
|
AuthMissingError,
|
|
148
160
|
PROVIDER_ENV_VARS,
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `almightygpt review --plan <file>` — Reviewer AI critiques a PLAN
|
|
3
|
+
* doc (not a git diff). Same review primitives as runDiffReview but
|
|
4
|
+
* the input is the plan markdown and the framing tells the Reviewer
|
|
5
|
+
* to critique the plan's structure, completeness, and risks rather
|
|
6
|
+
* than line-by-line code.
|
|
7
|
+
*
|
|
8
|
+
* Output lands at `<reviewsDir>/plan-<topic>.md` (prefix distinguishes
|
|
9
|
+
* plan reviews from diff reviews when they share the same topic name).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { readFile } from "node:fs/promises";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { loadConfig } from "../config/load.js";
|
|
15
|
+
import { makeAdapter } from "../adapters/factory.js";
|
|
16
|
+
import { AdapterError } from "../adapters/types.js";
|
|
17
|
+
import { assembleMemory } from "../review/memory.js";
|
|
18
|
+
import {
|
|
19
|
+
createRunFolder,
|
|
20
|
+
writeRunMetadata,
|
|
21
|
+
writeRunInput,
|
|
22
|
+
writeAgentOutput,
|
|
23
|
+
collectGitContext,
|
|
24
|
+
} from "../runs/folder.js";
|
|
25
|
+
import {
|
|
26
|
+
writeHumanReviewFile,
|
|
27
|
+
preflightReviewFileCollision,
|
|
28
|
+
} from "../review/write.js";
|
|
29
|
+
|
|
30
|
+
export interface PlanReviewOptions {
|
|
31
|
+
repoRoot: string;
|
|
32
|
+
topic: string;
|
|
33
|
+
/** Path (relative to repoRoot) to the plan markdown to review. */
|
|
34
|
+
planPath: string;
|
|
35
|
+
reviewer?: string;
|
|
36
|
+
force?: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface PlanReviewResult {
|
|
40
|
+
reviewPath: string;
|
|
41
|
+
reviewBytes: number;
|
|
42
|
+
reviewer: string;
|
|
43
|
+
provider: string;
|
|
44
|
+
modelUsed: string;
|
|
45
|
+
tokensIn: number;
|
|
46
|
+
cachedTokensIn: number;
|
|
47
|
+
tokensOut: number;
|
|
48
|
+
costUsd: number;
|
|
49
|
+
latencyMs: number;
|
|
50
|
+
memorySources: { path: string; bytes: number }[];
|
|
51
|
+
memoryMissing: string[];
|
|
52
|
+
runId: string;
|
|
53
|
+
runFolder: string;
|
|
54
|
+
shallowWarning?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const PLAN_REVIEW_SYSTEM_FRAMING = [
|
|
58
|
+
"You are the Reviewer AI in an AlmightyGPT Plan-review stage.",
|
|
59
|
+
"",
|
|
60
|
+
"WHAT YOU ARE REVIEWING: the PLAN markdown supplied below. NOT a git",
|
|
61
|
+
"diff and NOT code — a plan that a Worker AI drafted in response to a",
|
|
62
|
+
"human's requirement. The plan has not been implemented yet. Your job",
|
|
63
|
+
"is to find what's WRONG with the plan: missing steps, hidden",
|
|
64
|
+
"dependencies, risks the Worker didn't surface, ambiguous decisions,",
|
|
65
|
+
"things that will break in production, edge cases the test plan misses.",
|
|
66
|
+
"",
|
|
67
|
+
"WHAT THE ORCHESTRATOR OWNS (do NOT include these in your response):",
|
|
68
|
+
" - The H1 title (orchestrator prepends `# Plan Review: <topic>`).",
|
|
69
|
+
" - The header block with model / tokens / cost.",
|
|
70
|
+
" - The `## Cost and Latency` and `## Appendix: Raw Outputs` sections.",
|
|
71
|
+
"",
|
|
72
|
+
"WHAT YOU MUST PRODUCE — start with `## Decision Required` and emit",
|
|
73
|
+
"ONLY these sections in this order:",
|
|
74
|
+
" ## Decision Required",
|
|
75
|
+
" ## Highest-Risk Findings",
|
|
76
|
+
" ## Concrete Weaknesses",
|
|
77
|
+
" ## Worker Plan Summary",
|
|
78
|
+
" ## Test Plan",
|
|
79
|
+
" ## Human Decision",
|
|
80
|
+
"",
|
|
81
|
+
"ANTI-SYCOPHANCY (non-negotiable):",
|
|
82
|
+
" - Find at least 3 concrete weaknesses with specific file / step / line",
|
|
83
|
+
" references from the plan.",
|
|
84
|
+
" - A finding without a specific anchor is too vague.",
|
|
85
|
+
" - 'Looks good, minor suggestions' is a FAILED review — recalibrate.",
|
|
86
|
+
"",
|
|
87
|
+
"REVIEW LENS — focus on what plans commonly get wrong:",
|
|
88
|
+
" - Steps assume capabilities that don't exist yet",
|
|
89
|
+
" - Risks section underweights production blast radius",
|
|
90
|
+
" - Test plan is generic ('add tests') instead of named cases",
|
|
91
|
+
" - Open questions are missing things the human will actually need to",
|
|
92
|
+
" decide before implementation",
|
|
93
|
+
" - Affected modules list is incomplete (the plan touches more surfaces",
|
|
94
|
+
" than it admits)",
|
|
95
|
+
" - Migration / rollback story is missing or hand-wavy",
|
|
96
|
+
].join("\n");
|
|
97
|
+
|
|
98
|
+
function buildPlanReviewUserMessage(
|
|
99
|
+
topic: string,
|
|
100
|
+
planContent: string,
|
|
101
|
+
): string {
|
|
102
|
+
return [
|
|
103
|
+
`# Plan-review request — topic: \`${topic}\``,
|
|
104
|
+
"",
|
|
105
|
+
"## The plan to review",
|
|
106
|
+
"",
|
|
107
|
+
"```markdown",
|
|
108
|
+
planContent.trim(),
|
|
109
|
+
"```",
|
|
110
|
+
"",
|
|
111
|
+
"## Your task",
|
|
112
|
+
"",
|
|
113
|
+
"Critique the plan above using the structure in your system prompt.",
|
|
114
|
+
"Start your response with `## Decision Required` (no H1, no preamble).",
|
|
115
|
+
].join("\n");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export async function runPlanReview(
|
|
119
|
+
opts: PlanReviewOptions,
|
|
120
|
+
): Promise<PlanReviewResult> {
|
|
121
|
+
const config = await loadConfig(opts.repoRoot);
|
|
122
|
+
|
|
123
|
+
const reviewerName = opts.reviewer ?? config.defaults.reviewer;
|
|
124
|
+
if (!reviewerName) {
|
|
125
|
+
throw new Error(
|
|
126
|
+
"No reviewer specified. Pass --reviewer <name> or set defaults.reviewer in .almightygpt/config.yaml.",
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
const agentConfig = config.agents[reviewerName];
|
|
130
|
+
if (!agentConfig) {
|
|
131
|
+
throw new Error(
|
|
132
|
+
`Reviewer "${reviewerName}" not found in .almightygpt/config.yaml agents map.`,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
if (!agentConfig.enabled) {
|
|
136
|
+
throw new Error(`Reviewer "${reviewerName}" is disabled in .almightygpt/config.yaml.`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const adapter = makeAdapter(reviewerName, agentConfig.provider);
|
|
140
|
+
if (!(await adapter.isAvailable())) {
|
|
141
|
+
throw new AdapterError(
|
|
142
|
+
`Adapter "${reviewerName}" (${agentConfig.provider}) is not available. Set the provider's API key.`,
|
|
143
|
+
reviewerName,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Plan reviews land at <reviewsDir>/plan-<topic>.md so they don't
|
|
148
|
+
// collide with diff reviews on the same topic.
|
|
149
|
+
const planReviewTopic = `plan-${opts.topic}`;
|
|
150
|
+
await preflightReviewFileCollision(
|
|
151
|
+
opts.repoRoot,
|
|
152
|
+
config.reviewsDir,
|
|
153
|
+
planReviewTopic,
|
|
154
|
+
opts.force ?? false,
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
// Load the plan to review.
|
|
158
|
+
let planContent: string;
|
|
159
|
+
try {
|
|
160
|
+
planContent = await readFile(join(opts.repoRoot, opts.planPath), "utf8");
|
|
161
|
+
} catch (err) {
|
|
162
|
+
throw new Error(
|
|
163
|
+
`Could not read plan file at ${opts.planPath}: ${err instanceof Error ? err.message : String(err)}`,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const runFolder = await createRunFolder({
|
|
168
|
+
repoRoot: opts.repoRoot,
|
|
169
|
+
runsDir: config.runsDir,
|
|
170
|
+
topic: planReviewTopic,
|
|
171
|
+
type: "review-plan",
|
|
172
|
+
});
|
|
173
|
+
const createdAt = new Date().toISOString();
|
|
174
|
+
|
|
175
|
+
const memory = await assembleMemory(opts.repoRoot, agentConfig.memoryFile);
|
|
176
|
+
const systemPrompt = PLAN_REVIEW_SYSTEM_FRAMING + "\n\n" + memory.text;
|
|
177
|
+
const userMessage = buildPlanReviewUserMessage(opts.topic, planContent);
|
|
178
|
+
|
|
179
|
+
await writeRunInput(runFolder.absPath, userMessage);
|
|
180
|
+
|
|
181
|
+
const adapterOut = await adapter.execute({
|
|
182
|
+
role: "reviewer",
|
|
183
|
+
systemPrompt,
|
|
184
|
+
userMessage,
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
await writeAgentOutput(runFolder.absPath, "reviewer", adapterOut.content);
|
|
188
|
+
|
|
189
|
+
// Shallow detection: same rule as diff reviews — need N concrete
|
|
190
|
+
// weaknesses with anchors.
|
|
191
|
+
const shallowWarning = detectShallowPlanReview(
|
|
192
|
+
adapterOut.content,
|
|
193
|
+
config.review.requireConcreteWeaknesses,
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
const writeOpts: Parameters<typeof writeHumanReviewFile>[0] = {
|
|
197
|
+
repoRoot: opts.repoRoot,
|
|
198
|
+
reviewsDir: config.reviewsDir,
|
|
199
|
+
topic: planReviewTopic,
|
|
200
|
+
reviewerName,
|
|
201
|
+
reviewerProvider: adapter.provider,
|
|
202
|
+
modelUsed: adapterOut.modelUsed,
|
|
203
|
+
body: adapterOut.content,
|
|
204
|
+
metrics: {
|
|
205
|
+
tokensIn: adapterOut.tokensIn,
|
|
206
|
+
tokensOut: adapterOut.tokensOut,
|
|
207
|
+
costUsd: adapterOut.costUsd,
|
|
208
|
+
latencyMs: adapterOut.latencyMs,
|
|
209
|
+
},
|
|
210
|
+
runFolder: runFolder.relPath,
|
|
211
|
+
};
|
|
212
|
+
if (shallowWarning) writeOpts.shallowWarning = shallowWarning;
|
|
213
|
+
if (opts.force) writeOpts.force = opts.force;
|
|
214
|
+
const written = await writeHumanReviewFile(writeOpts);
|
|
215
|
+
|
|
216
|
+
const git = await collectGitContext(opts.repoRoot);
|
|
217
|
+
await writeRunMetadata(runFolder.absPath, {
|
|
218
|
+
id: runFolder.id,
|
|
219
|
+
type: "review-plan",
|
|
220
|
+
createdAt,
|
|
221
|
+
finishedAt: new Date().toISOString(),
|
|
222
|
+
workspacePath: opts.repoRoot,
|
|
223
|
+
topic: planReviewTopic,
|
|
224
|
+
git,
|
|
225
|
+
input: { source: "requirement-file", path: opts.planPath },
|
|
226
|
+
agents: [
|
|
227
|
+
{
|
|
228
|
+
name: reviewerName,
|
|
229
|
+
role: "reviewer",
|
|
230
|
+
provider: agentConfig.provider,
|
|
231
|
+
enabled: true,
|
|
232
|
+
},
|
|
233
|
+
],
|
|
234
|
+
adapterVersions: [],
|
|
235
|
+
status: "completed",
|
|
236
|
+
metrics: [
|
|
237
|
+
{
|
|
238
|
+
agent: reviewerName,
|
|
239
|
+
role: "reviewer",
|
|
240
|
+
provider: adapter.provider,
|
|
241
|
+
model: adapterOut.modelUsed,
|
|
242
|
+
tokensIn: adapterOut.tokensIn,
|
|
243
|
+
cachedTokensIn: adapterOut.cachedTokensIn ?? 0,
|
|
244
|
+
tokensOut: adapterOut.tokensOut,
|
|
245
|
+
costUsd: adapterOut.costUsd,
|
|
246
|
+
latencyMs: adapterOut.latencyMs,
|
|
247
|
+
},
|
|
248
|
+
],
|
|
249
|
+
totals: {
|
|
250
|
+
tokensIn: adapterOut.tokensIn,
|
|
251
|
+
tokensOut: adapterOut.tokensOut,
|
|
252
|
+
costUsd: adapterOut.costUsd,
|
|
253
|
+
latencyMs: adapterOut.latencyMs,
|
|
254
|
+
},
|
|
255
|
+
reviewPath: written.path,
|
|
256
|
+
budget: {
|
|
257
|
+
maxCostPerRunUsd: config.budget.maxCostPerRunUsd,
|
|
258
|
+
maxTokensPerRun: config.budget.maxTokensPerRun,
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
const result: PlanReviewResult = {
|
|
263
|
+
reviewPath: written.path,
|
|
264
|
+
reviewBytes: written.bytes,
|
|
265
|
+
reviewer: reviewerName,
|
|
266
|
+
provider: adapter.provider,
|
|
267
|
+
modelUsed: adapterOut.modelUsed,
|
|
268
|
+
tokensIn: adapterOut.tokensIn,
|
|
269
|
+
cachedTokensIn: adapterOut.cachedTokensIn ?? 0,
|
|
270
|
+
tokensOut: adapterOut.tokensOut,
|
|
271
|
+
costUsd: adapterOut.costUsd,
|
|
272
|
+
latencyMs: adapterOut.latencyMs,
|
|
273
|
+
memorySources: memory.sources,
|
|
274
|
+
memoryMissing: memory.missing,
|
|
275
|
+
runId: runFolder.id,
|
|
276
|
+
runFolder: runFolder.relPath,
|
|
277
|
+
};
|
|
278
|
+
if (shallowWarning) result.shallowWarning = shallowWarning;
|
|
279
|
+
return result;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Same shallow heuristic as diff reviews — count file/step/line anchors. */
|
|
283
|
+
function detectShallowPlanReview(
|
|
284
|
+
content: string,
|
|
285
|
+
requireConcreteWeaknesses: number,
|
|
286
|
+
): string | undefined {
|
|
287
|
+
const anchorPattern = /\b(?:file|step|line|section)[:\s][\w\-./]+/gi;
|
|
288
|
+
const anchors = content.match(anchorPattern) ?? [];
|
|
289
|
+
const weaknessesSection = content.match(
|
|
290
|
+
/## Concrete Weaknesses([\s\S]*?)(?=##|$)/i,
|
|
291
|
+
);
|
|
292
|
+
const weaknessBullets =
|
|
293
|
+
weaknessesSection?.[1]?.match(/^\s*[-*\d.]/gm)?.length ?? 0;
|
|
294
|
+
|
|
295
|
+
if (anchors.length === 0) {
|
|
296
|
+
return "Plan review has zero anchored references (file / step / line). The Reviewer may not have engaged with specifics — consider re-running.";
|
|
297
|
+
}
|
|
298
|
+
if (weaknessBullets < requireConcreteWeaknesses) {
|
|
299
|
+
return `Plan review listed ${weaknessBullets} concrete weaknesses, fewer than the configured minimum of ${requireConcreteWeaknesses}. Output may be shallow — consider re-running with a more rigorous Reviewer.`;
|
|
300
|
+
}
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `almightygpt plan` — Worker AI reads a free-text requirement +
|
|
3
|
+
* project memory, produces a structured plan markdown.
|
|
4
|
+
*
|
|
5
|
+
* Distinct from the review pipeline because the INPUT is a
|
|
6
|
+
* requirement (a sentence or paragraph from the user describing
|
|
7
|
+
* what they want), not a git diff. Output is a plan doc with
|
|
8
|
+
* required sections — same shape on every run so the Reviewer
|
|
9
|
+
* downstream knows what to expect.
|
|
10
|
+
*
|
|
11
|
+
* Plans land at `docs/<worker>-plans/<topic>.md`. The Reviewer step
|
|
12
|
+
* then runs against this plan via the `review --plan <file>` mode
|
|
13
|
+
* (see run-plan-review.ts).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
17
|
+
import { dirname, join } from "node:path";
|
|
18
|
+
import { loadConfig } from "../config/load.js";
|
|
19
|
+
import { makeAdapter } from "../adapters/factory.js";
|
|
20
|
+
import { AdapterError } from "../adapters/types.js";
|
|
21
|
+
import { assembleMemory } from "../review/memory.js";
|
|
22
|
+
import {
|
|
23
|
+
createRunFolder,
|
|
24
|
+
writeRunMetadata,
|
|
25
|
+
writeRunInput,
|
|
26
|
+
writeAgentOutput,
|
|
27
|
+
collectGitContext,
|
|
28
|
+
} from "../runs/folder.js";
|
|
29
|
+
import { assertSafeToWrite } from "../git/status.js";
|
|
30
|
+
|
|
31
|
+
export interface PlanOptions {
|
|
32
|
+
repoRoot: string;
|
|
33
|
+
topic: string;
|
|
34
|
+
requirement: string;
|
|
35
|
+
worker?: string;
|
|
36
|
+
force?: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface PlanResult {
|
|
40
|
+
planPath: string;
|
|
41
|
+
planBytes: number;
|
|
42
|
+
worker: string;
|
|
43
|
+
provider: string;
|
|
44
|
+
modelUsed: string;
|
|
45
|
+
tokensIn: number;
|
|
46
|
+
cachedTokensIn: number;
|
|
47
|
+
tokensOut: number;
|
|
48
|
+
costUsd: number;
|
|
49
|
+
latencyMs: number;
|
|
50
|
+
memorySources: { path: string; bytes: number }[];
|
|
51
|
+
memoryMissing: string[];
|
|
52
|
+
runId: string;
|
|
53
|
+
runFolder: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const PLAN_SYSTEM_FRAMING = [
|
|
57
|
+
"You are the Worker AI in an AlmightyGPT Plan stage.",
|
|
58
|
+
"",
|
|
59
|
+
"WHAT YOU ARE DOING: turning a free-text requirement (the user message",
|
|
60
|
+
"below) into a structured plan markdown that a Reviewer AI will then",
|
|
61
|
+
"critique. You are NOT writing code — only the plan. The human will",
|
|
62
|
+
"approve / reject / refine the plan, and only then will implementation",
|
|
63
|
+
"happen (in a separate tool).",
|
|
64
|
+
"",
|
|
65
|
+
"WHAT THE ORCHESTRATOR OWNS (do NOT include these in your response):",
|
|
66
|
+
" - The H1 title (the orchestrator prepends `# Plan: <topic>`).",
|
|
67
|
+
" - The header block with model / tokens / cost.",
|
|
68
|
+
"",
|
|
69
|
+
"WHAT YOU MUST PRODUCE — emit exactly these sections in this order:",
|
|
70
|
+
" ## Goal",
|
|
71
|
+
" ## Affected modules / surfaces",
|
|
72
|
+
" ## Step-by-step approach",
|
|
73
|
+
" ## Risks",
|
|
74
|
+
" ## Test plan",
|
|
75
|
+
" ## Open questions",
|
|
76
|
+
"",
|
|
77
|
+
"STYLE:",
|
|
78
|
+
" - Be concrete. Name specific files, classes, functions, env vars.",
|
|
79
|
+
" - Write so the Reviewer can find concrete things to critique.",
|
|
80
|
+
" - Open questions are not a weakness; they're the things you'd ask",
|
|
81
|
+
" the human if you could. Surface them.",
|
|
82
|
+
].join("\n");
|
|
83
|
+
|
|
84
|
+
function buildPlanUserMessage(topic: string, requirement: string): string {
|
|
85
|
+
return [
|
|
86
|
+
`# Plan request — topic: \`${topic}\``,
|
|
87
|
+
"",
|
|
88
|
+
"## Requirement (from human)",
|
|
89
|
+
"",
|
|
90
|
+
requirement.trim(),
|
|
91
|
+
"",
|
|
92
|
+
"## Your task",
|
|
93
|
+
"",
|
|
94
|
+
"Produce the plan markdown using the structure in your system prompt.",
|
|
95
|
+
"Start your response with `## Goal` (no H1, no preamble).",
|
|
96
|
+
].join("\n");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export async function runWorkerPlan(opts: PlanOptions): Promise<PlanResult> {
|
|
100
|
+
const config = await loadConfig(opts.repoRoot);
|
|
101
|
+
|
|
102
|
+
const workerName = opts.worker ?? config.defaults.worker;
|
|
103
|
+
if (!workerName) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
"No worker specified. Pass --worker <name> or set defaults.worker in .almightygpt/config.yaml.",
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
const agentConfig = config.agents[workerName];
|
|
109
|
+
if (!agentConfig) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
`Worker "${workerName}" not found in .almightygpt/config.yaml agents map.`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
if (!agentConfig.enabled) {
|
|
115
|
+
throw new Error(`Worker "${workerName}" is disabled in .almightygpt/config.yaml.`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const adapter = makeAdapter(workerName, agentConfig.provider);
|
|
119
|
+
if (!(await adapter.isAvailable())) {
|
|
120
|
+
throw new AdapterError(
|
|
121
|
+
`Adapter "${workerName}" (${agentConfig.provider}) is not available. Set the provider's API key.`,
|
|
122
|
+
workerName,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Plans land at docs/<worker>-plans/<topic>.md — symmetric with reviews.
|
|
127
|
+
const plansDir = `docs/${workerName}-plans`;
|
|
128
|
+
const planRel = `${plansDir}/${opts.topic}.md`;
|
|
129
|
+
await assertSafeToWrite(opts.repoRoot, planRel, opts.force ?? false);
|
|
130
|
+
|
|
131
|
+
const runFolder = await createRunFolder({
|
|
132
|
+
repoRoot: opts.repoRoot,
|
|
133
|
+
runsDir: config.runsDir,
|
|
134
|
+
topic: opts.topic,
|
|
135
|
+
type: "plan",
|
|
136
|
+
});
|
|
137
|
+
const createdAt = new Date().toISOString();
|
|
138
|
+
|
|
139
|
+
const memory = await assembleMemory(opts.repoRoot, agentConfig.memoryFile);
|
|
140
|
+
const systemPrompt = PLAN_SYSTEM_FRAMING + "\n\n" + memory.text;
|
|
141
|
+
const userMessage = buildPlanUserMessage(opts.topic, opts.requirement);
|
|
142
|
+
|
|
143
|
+
await writeRunInput(runFolder.absPath, userMessage);
|
|
144
|
+
|
|
145
|
+
const adapterOut = await adapter.execute({
|
|
146
|
+
role: "worker",
|
|
147
|
+
systemPrompt,
|
|
148
|
+
userMessage,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
await writeAgentOutput(runFolder.absPath, "worker", adapterOut.content);
|
|
152
|
+
|
|
153
|
+
// Compose the final plan file: orchestrator-owned header + worker body.
|
|
154
|
+
const header = [
|
|
155
|
+
`# Plan: ${opts.topic}`,
|
|
156
|
+
"",
|
|
157
|
+
`> Worker: \`${workerName}\` (${adapter.provider}, ${adapterOut.modelUsed})`,
|
|
158
|
+
`> Generated: ${new Date().toISOString()}`,
|
|
159
|
+
`> Tokens: ${adapterOut.tokensIn} in / ${adapterOut.tokensOut} out`,
|
|
160
|
+
`> Cost: $${adapterOut.costUsd.toFixed(4)} USD`,
|
|
161
|
+
`> Run folder: \`${runFolder.relPath}\``,
|
|
162
|
+
"",
|
|
163
|
+
].join("\n");
|
|
164
|
+
|
|
165
|
+
const footer = [
|
|
166
|
+
"",
|
|
167
|
+
"---",
|
|
168
|
+
"",
|
|
169
|
+
"## How this plan was produced",
|
|
170
|
+
"",
|
|
171
|
+
`The AlmightyGPT Plan stage produced this. Worker (\`${workerName}\`)`,
|
|
172
|
+
`read the requirement above + the project's memory files.`,
|
|
173
|
+
"",
|
|
174
|
+
"**Next stage:** cross-AI review with `almightygpt review --plan",
|
|
175
|
+
`${planRel} --reviewer codex --topic ${opts.topic}\` (Reviewer critiques`,
|
|
176
|
+
"this plan), then human approval, then implementation in your",
|
|
177
|
+
"preferred coding tool.",
|
|
178
|
+
].join("\n");
|
|
179
|
+
|
|
180
|
+
const planContent = header + adapterOut.content.trim() + footer;
|
|
181
|
+
const planAbs = join(opts.repoRoot, planRel);
|
|
182
|
+
await mkdir(dirname(planAbs), { recursive: true });
|
|
183
|
+
await writeFile(planAbs, planContent, "utf8");
|
|
184
|
+
|
|
185
|
+
const gitContext = await collectGitContext(opts.repoRoot);
|
|
186
|
+
await writeRunMetadata(runFolder.absPath, {
|
|
187
|
+
id: runFolder.id,
|
|
188
|
+
type: "plan",
|
|
189
|
+
createdAt,
|
|
190
|
+
finishedAt: new Date().toISOString(),
|
|
191
|
+
workspacePath: opts.repoRoot,
|
|
192
|
+
topic: opts.topic,
|
|
193
|
+
git: gitContext,
|
|
194
|
+
input: { source: "requirement-file" },
|
|
195
|
+
agents: [
|
|
196
|
+
{
|
|
197
|
+
name: workerName,
|
|
198
|
+
role: "worker",
|
|
199
|
+
provider: agentConfig.provider,
|
|
200
|
+
enabled: true,
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
adapterVersions: [],
|
|
204
|
+
status: "completed",
|
|
205
|
+
metrics: [
|
|
206
|
+
{
|
|
207
|
+
agent: workerName,
|
|
208
|
+
role: "worker",
|
|
209
|
+
provider: adapter.provider,
|
|
210
|
+
model: adapterOut.modelUsed,
|
|
211
|
+
tokensIn: adapterOut.tokensIn,
|
|
212
|
+
cachedTokensIn: adapterOut.cachedTokensIn ?? 0,
|
|
213
|
+
tokensOut: adapterOut.tokensOut,
|
|
214
|
+
costUsd: adapterOut.costUsd,
|
|
215
|
+
latencyMs: adapterOut.latencyMs,
|
|
216
|
+
},
|
|
217
|
+
],
|
|
218
|
+
totals: {
|
|
219
|
+
tokensIn: adapterOut.tokensIn,
|
|
220
|
+
tokensOut: adapterOut.tokensOut,
|
|
221
|
+
costUsd: adapterOut.costUsd,
|
|
222
|
+
latencyMs: adapterOut.latencyMs,
|
|
223
|
+
},
|
|
224
|
+
reviewPath: planRel,
|
|
225
|
+
budget: {
|
|
226
|
+
maxCostPerRunUsd: config.budget.maxCostPerRunUsd,
|
|
227
|
+
maxTokensPerRun: config.budget.maxTokensPerRun,
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
planPath: planRel,
|
|
233
|
+
planBytes: planContent.length,
|
|
234
|
+
worker: workerName,
|
|
235
|
+
provider: adapter.provider,
|
|
236
|
+
modelUsed: adapterOut.modelUsed,
|
|
237
|
+
tokensIn: adapterOut.tokensIn,
|
|
238
|
+
cachedTokensIn: adapterOut.cachedTokensIn ?? 0,
|
|
239
|
+
tokensOut: adapterOut.tokensOut,
|
|
240
|
+
costUsd: adapterOut.costUsd,
|
|
241
|
+
latencyMs: adapterOut.latencyMs,
|
|
242
|
+
memorySources: memory.sources,
|
|
243
|
+
memoryMissing: memory.missing,
|
|
244
|
+
runId: runFolder.id,
|
|
245
|
+
runFolder: runFolder.relPath,
|
|
246
|
+
};
|
|
247
|
+
}
|
package/src/runs/types.ts
CHANGED
|
@@ -14,7 +14,9 @@ export type RunType =
|
|
|
14
14
|
| "review-diff"
|
|
15
15
|
| "review-path"
|
|
16
16
|
| "review-requirement"
|
|
17
|
-
| "review-worker-reviewer"
|
|
17
|
+
| "review-worker-reviewer"
|
|
18
|
+
| "plan"
|
|
19
|
+
| "review-plan";
|
|
18
20
|
|
|
19
21
|
export type RunStatus = "running" | "completed" | "failed" | "aborted_budget";
|
|
20
22
|
|