@callumvass/forgeflow-dev 0.1.0 → 0.3.2

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.
@@ -1,33 +0,0 @@
1
- import { type AnyCtx, emptyStage, runAgent, TOOLS_ALL, TOOLS_NO_EDIT } from "@callumvass/forgeflow-shared";
2
- import { AGENTS_DIR } from "../resolve.js";
3
-
4
- export async function runDiscoverSkills(
5
- cwd: string,
6
- query: string,
7
- signal: AbortSignal,
8
- onUpdate: AnyCtx,
9
- _ctx: AnyCtx,
10
- ) {
11
- // If query looks like specific skill names (contains commas or known skill identifiers),
12
- // treat as install mode. Otherwise, discover mode.
13
- const isInstall = query.includes(",") || query.includes("/");
14
-
15
- const stages = [emptyStage("skill-discoverer")];
16
- const opts = { agentsDir: AGENTS_DIR, cwd, signal, stages, pipeline: "discover-skills", onUpdate };
17
-
18
- const task = isInstall
19
- ? `Install these skills as forgeflow plugins: ${query}`
20
- : query
21
- ? `Discover skills related to "${query}" — recommend only, do NOT install.`
22
- : "Analyze the project tech stack and discover relevant skills — recommend only, do NOT install.";
23
-
24
- // Install mode needs write access, discover mode is read-only
25
- const tools = isInstall ? TOOLS_ALL : TOOLS_NO_EDIT;
26
-
27
- const result = await runAgent("skill-discoverer", task, { ...opts, tools });
28
-
29
- return {
30
- content: [{ type: "text" as const, text: result.output || "No skills found." }],
31
- details: { pipeline: "discover-skills", stages },
32
- };
33
- }
@@ -1,181 +0,0 @@
1
- import { type AnyCtx, emptyStage, type StageResult, sumUsage } from "@callumvass/forgeflow-shared";
2
- import { exec } from "../utils/exec.js";
3
- import { setForgeflowStatus, updateProgressWidget } from "../utils/ui.js";
4
- import { runImplement } from "./implement.js";
5
-
6
- interface IssueInfo {
7
- number: number;
8
- title: string;
9
- body: string;
10
- }
11
-
12
- /**
13
- * Get issue numbers whose dependencies (referenced as #N in ## Dependencies section) are satisfied.
14
- */
15
- function getReadyIssues(issues: IssueInfo[], completed: Set<number>): number[] {
16
- return issues
17
- .filter((issue) => {
18
- if (completed.has(issue.number)) return false;
19
- const parts = issue.body.split("## Dependencies");
20
- if (parts.length < 2) return true;
21
- const depSection = parts[1]?.split("\n## ")[0] ?? "";
22
- const deps = [...depSection.matchAll(/#(\d+)/g)].map((m) => parseInt(m[1] ?? "0", 10));
23
- return deps.every((d) => completed.has(d));
24
- })
25
- .map((i) => i.number);
26
- }
27
-
28
- export async function runImplementAll(
29
- cwd: string,
30
- signal: AbortSignal,
31
- onUpdate: AnyCtx,
32
- ctx: AnyCtx,
33
- flags: { skipPlan: boolean; skipReview: boolean },
34
- ) {
35
- const allStages: StageResult[] = [];
36
- const issueProgress = new Map<number, { title: string; status: "pending" | "running" | "done" | "failed" }>();
37
-
38
- // Seed completed set with already-closed issues
39
- const closedJson = await exec(
40
- `gh issue list --state closed --label "auto-generated" --json number --jq '.[].number'`,
41
- cwd,
42
- );
43
- const completed = new Set<number>(closedJson ? closedJson.split("\n").filter(Boolean).map(Number) : []);
44
-
45
- let iteration = 0;
46
- const maxIterations = 50;
47
-
48
- while (iteration++ < maxIterations) {
49
- if (signal.aborted) break;
50
-
51
- // Return to main and pull
52
- await exec("git checkout main && git pull --rebase", cwd);
53
-
54
- // Fetch open issues
55
- const issuesJson = await exec(
56
- `gh issue list --state open --label "auto-generated" --json number,title,body --jq 'sort_by(.number)'`,
57
- cwd,
58
- );
59
- let issues: IssueInfo[];
60
- try {
61
- issues = JSON.parse(issuesJson || "[]");
62
- } catch {
63
- issues = [];
64
- }
65
-
66
- if (issues.length === 0) {
67
- return {
68
- content: [{ type: "text" as const, text: "All issues implemented." }],
69
- details: { pipeline: "implement-all", stages: allStages },
70
- };
71
- }
72
-
73
- // Track all known issues in progress widget
74
- for (const issue of issues) {
75
- if (!issueProgress.has(issue.number)) {
76
- issueProgress.set(issue.number, { title: issue.title, status: "pending" });
77
- }
78
- }
79
-
80
- // Find ready issues (deps satisfied)
81
- const ready = getReadyIssues(issues, completed);
82
- if (ready.length === 0) {
83
- return {
84
- content: [
85
- { type: "text" as const, text: `${issues.length} issues remain but all have unresolved dependencies.` },
86
- ],
87
- details: { pipeline: "implement-all", stages: allStages },
88
- isError: true,
89
- };
90
- }
91
-
92
- // biome-ignore lint/style/noNonNullAssertion: ready is non-empty (checked above)
93
- const issueNum = ready[0]!;
94
- const issueTitle = issues.find((i) => i.number === issueNum)?.title ?? `#${issueNum}`;
95
-
96
- // Update status + widget
97
- issueProgress.set(issueNum, { title: issueTitle, status: "running" });
98
- setForgeflowStatus(
99
- ctx,
100
- `implement-all · ${completed.size}/${completed.size + issues.length} · #${issueNum} ${issueTitle}`,
101
- );
102
- updateProgressWidget(ctx, issueProgress, sumUsage(allStages).cost);
103
-
104
- // Run implement for this issue
105
- allStages.push(emptyStage(`implement-${issueNum}`));
106
- const implResult = await runImplement(cwd, String(issueNum), signal, onUpdate, ctx, {
107
- ...flags,
108
- autonomous: true,
109
- });
110
-
111
- // Accumulate usage from detailed stages into the container stage
112
- const implStage = allStages.find((s) => s.name === `implement-${issueNum}`);
113
- if (implStage) {
114
- implStage.status = implResult.isError ? "failed" : "done";
115
- implStage.output = implResult.content[0]?.type === "text" ? implResult.content[0].text : "";
116
- const detailedStages = (implResult as AnyCtx).details?.stages as StageResult[] | undefined;
117
- if (detailedStages) implStage.usage = sumUsage(detailedStages);
118
- }
119
-
120
- if (implResult.isError) {
121
- issueProgress.set(issueNum, { title: issueTitle, status: "failed" });
122
- updateProgressWidget(ctx, issueProgress, sumUsage(allStages).cost);
123
- return {
124
- content: [
125
- {
126
- type: "text" as const,
127
- text: `Failed on issue #${issueNum}: ${implResult.content[0]?.type === "text" ? implResult.content[0].text : "unknown error"}`,
128
- },
129
- ],
130
- details: { pipeline: "implement-all", stages: allStages },
131
- isError: true,
132
- };
133
- }
134
-
135
- // Check for PR and merge
136
- const branch = `feat/issue-${issueNum}`;
137
- await exec("git checkout main && git pull --rebase", cwd);
138
- const prNum = await exec(`gh pr list --head "${branch}" --json number --jq '.[0].number'`, cwd);
139
-
140
- if (prNum && prNum !== "null") {
141
- const mergeResult = await exec(`gh pr merge ${prNum} --squash --delete-branch`, cwd);
142
- if (mergeResult.includes("Merged") || mergeResult === "") {
143
- completed.add(issueNum);
144
- } else {
145
- const prState = await exec(`gh pr view ${prNum} --json state --jq '.state'`, cwd);
146
- if (prState === "MERGED") {
147
- completed.add(issueNum);
148
- } else {
149
- issueProgress.set(issueNum, { title: issueTitle, status: "failed" });
150
- updateProgressWidget(ctx, issueProgress, sumUsage(allStages).cost);
151
- return {
152
- content: [{ type: "text" as const, text: `Failed to merge PR #${prNum} for issue #${issueNum}.` }],
153
- details: { pipeline: "implement-all", stages: allStages },
154
- isError: true,
155
- };
156
- }
157
- }
158
- } else {
159
- issueProgress.set(issueNum, { title: issueTitle, status: "failed" });
160
- updateProgressWidget(ctx, issueProgress, sumUsage(allStages).cost);
161
- return {
162
- content: [{ type: "text" as const, text: `No PR found for issue #${issueNum} after implementation.` }],
163
- details: { pipeline: "implement-all", stages: allStages },
164
- isError: true,
165
- };
166
- }
167
-
168
- // Mark done and update widget
169
- issueProgress.set(issueNum, { title: issueTitle, status: "done" });
170
- setForgeflowStatus(
171
- ctx,
172
- `implement-all · ${completed.size}/${completed.size + issues.length - 1} · $${sumUsage(allStages).cost.toFixed(2)}`,
173
- );
174
- updateProgressWidget(ctx, issueProgress, sumUsage(allStages).cost);
175
- }
176
-
177
- return {
178
- content: [{ type: "text" as const, text: `Reached max iterations (${maxIterations}).` }],
179
- details: { pipeline: "implement-all", stages: allStages },
180
- };
181
- }
@@ -1,305 +0,0 @@
1
- import {
2
- type AnyCtx,
3
- cleanSignal,
4
- emptyStage,
5
- readSignal,
6
- runAgent,
7
- type StageResult,
8
- signalExists,
9
- TOOLS_ALL,
10
- TOOLS_READONLY,
11
- } from "@callumvass/forgeflow-shared";
12
- import { AGENTS_DIR } from "../resolve.js";
13
- import { exec } from "../utils/exec.js";
14
- import { ensureBranch, resolveIssue } from "../utils/git.js";
15
- import { setForgeflowStatus } from "../utils/ui.js";
16
- import { runReviewInline } from "./review.js";
17
-
18
- /**
19
- * Run review and fix any findings via implementor.
20
- */
21
- async function reviewAndFix(
22
- cwd: string,
23
- signal: AbortSignal,
24
- onUpdate: AnyCtx,
25
- ctx: AnyCtx,
26
- stages: StageResult[],
27
- pipeline = "implement",
28
- ): Promise<void> {
29
- const reviewResult = await runReviewInline(cwd, signal, onUpdate, ctx, stages);
30
- if (reviewResult.isError) {
31
- const findings = reviewResult.content[0]?.type === "text" ? reviewResult.content[0].text : "";
32
- stages.push(emptyStage("fix-findings"));
33
- await runAgent(
34
- "implementor",
35
- `Fix the following code review findings:\n\n${findings}\n\nRULES:\n- Fix only the cited issues. Do not refactor or improve unrelated code.\n- Run the check command after fixes.\n- Commit and push the fixes.`,
36
- { agentsDir: AGENTS_DIR, cwd, signal, stages, pipeline, onUpdate, tools: TOOLS_ALL, stageName: "fix-findings" },
37
- );
38
- cleanSignal(cwd, "findings");
39
- }
40
- }
41
-
42
- /**
43
- * Run refactorer then review+fix. Shared by fresh implementation and resume-from-branch paths.
44
- */
45
- async function refactorAndReview(
46
- cwd: string,
47
- signal: AbortSignal,
48
- onUpdate: AnyCtx,
49
- ctx: AnyCtx,
50
- stages: StageResult[],
51
- skipReview: boolean,
52
- pipeline = "implement",
53
- ): Promise<void> {
54
- if (!stages.some((s) => s.name === "refactorer")) stages.push(emptyStage("refactorer"));
55
- await runAgent(
56
- "refactorer",
57
- "Review code added in this branch (git diff main...HEAD). Refactor if clear wins exist. Run checks after changes. Commit and push if changed.",
58
- { agentsDir: AGENTS_DIR, cwd, signal, stages, pipeline, onUpdate, tools: TOOLS_ALL },
59
- );
60
-
61
- if (!skipReview) {
62
- await reviewAndFix(cwd, signal, onUpdate, ctx, stages, pipeline);
63
- }
64
- }
65
-
66
- /**
67
- * Parse unresolved questions from the plan and prompt the user for answers.
68
- * Returns the plan with answers injected inline.
69
- */
70
- async function resolveQuestions(plan: string, ctx: AnyCtx): Promise<string> {
71
- const sectionMatch = plan.match(/### Unresolved Questions\n([\s\S]*?)(?=\n###|$)/);
72
- if (!sectionMatch) return plan;
73
-
74
- const section = sectionMatch[1] ?? "";
75
- const questions: string[] = [];
76
- for (const m of section.matchAll(/^- (.+)$/gm)) {
77
- if (m[1]) questions.push(m[1]);
78
- }
79
-
80
- if (questions.length === 0) return plan;
81
-
82
- let updatedSection = section;
83
- for (const q of questions) {
84
- const answer = await ctx.ui.input(`${q}`, "Skip to use defaults");
85
- if (answer != null && answer.trim() !== "") {
86
- updatedSection = updatedSection.replace(`- ${q}`, `- ${q}\n **Answer:** ${answer.trim()}`);
87
- }
88
- }
89
-
90
- return plan.replace(`### Unresolved Questions\n${section}`, `### Unresolved Questions\n${updatedSection}`);
91
- }
92
-
93
- export async function runImplement(
94
- cwd: string,
95
- issueArg: string,
96
- signal: AbortSignal,
97
- onUpdate: AnyCtx,
98
- ctx: AnyCtx,
99
- flags: { skipPlan: boolean; skipReview: boolean; autonomous?: boolean; customPrompt?: string } = {
100
- skipPlan: false,
101
- skipReview: false,
102
- },
103
- ) {
104
- const interactive = ctx.hasUI && !flags.autonomous;
105
- const resolved = await resolveIssue(cwd, issueArg || undefined);
106
- if (typeof resolved === "string") {
107
- return { content: [{ type: "text" as const, text: resolved }], details: { pipeline: "implement", stages: [] } };
108
- }
109
-
110
- const isGitHub = resolved.source === "github" && resolved.number > 0;
111
- const issueLabel = isGitHub ? `#${resolved.number}: ${resolved.title}` : `${resolved.key}: ${resolved.title}`;
112
-
113
- // Status line for standalone /implement (implement-all manages its own)
114
- if (!flags.autonomous && (resolved.number || resolved.key)) {
115
- const tag = isGitHub ? `#${resolved.number}` : resolved.key;
116
- setForgeflowStatus(ctx, `${tag} ${resolved.title} · ${resolved.branch}`);
117
- }
118
-
119
- const issueContext = isGitHub
120
- ? `Issue #${resolved.number}: ${resolved.title}\n\n${resolved.body}`
121
- : `Jira ${resolved.key}: ${resolved.title}\n\n${resolved.body}`;
122
-
123
- const customPromptSection = flags.customPrompt ? `\n\nADDITIONAL INSTRUCTIONS FROM USER:\n${flags.customPrompt}` : "";
124
-
125
- // --- Resumability: skip to review if work already exists ---
126
- if (resolved.existingPR) {
127
- const stages: StageResult[] = [];
128
- if (!flags.skipReview) {
129
- await reviewAndFix(cwd, signal, onUpdate, ctx, stages);
130
- }
131
- return {
132
- content: [{ type: "text" as const, text: `Resumed ${issueLabel} — PR #${resolved.existingPR} already exists.` }],
133
- details: { pipeline: "implement", stages },
134
- };
135
- }
136
-
137
- if (resolved.branch) {
138
- const branchExists = await exec(
139
- `git rev-parse --verify ${resolved.branch} 2>/dev/null && echo yes || echo no`,
140
- cwd,
141
- );
142
- if (branchExists === "yes") {
143
- await ensureBranch(cwd, resolved.branch);
144
- const ahead = await exec(`git rev-list main..${resolved.branch} --count`, cwd);
145
- if (parseInt(ahead, 10) > 0) {
146
- await exec(`git push -u origin ${resolved.branch}`, cwd);
147
- const prBody = isGitHub ? `Closes #${resolved.number}` : `Jira: ${resolved.key}`;
148
- await exec(`gh pr create --title "${resolved.title}" --body "${prBody}" --head ${resolved.branch}`, cwd);
149
-
150
- const stages: StageResult[] = [];
151
- await refactorAndReview(cwd, signal, onUpdate, ctx, stages, flags.skipReview);
152
- return {
153
- content: [{ type: "text" as const, text: `Resumed ${issueLabel} — pushed existing commits and created PR.` }],
154
- details: { pipeline: "implement", stages },
155
- };
156
- }
157
- }
158
- }
159
-
160
- // --- Fresh implementation ---
161
- const stageList: StageResult[] = [];
162
- if (!flags.skipPlan) stageList.push(emptyStage("planner"));
163
- stageList.push(emptyStage("implementor"));
164
- stageList.push(emptyStage("refactorer"));
165
- const stages = stageList;
166
- const opts = { agentsDir: AGENTS_DIR, cwd, signal, stages, pipeline: "implement", onUpdate };
167
-
168
- let plan = "";
169
-
170
- if (!flags.skipPlan) {
171
- const planResult = await runAgent(
172
- "planner",
173
- `Plan the implementation for this issue by producing a sequenced list of test cases.\n\n${issueContext}${customPromptSection}`,
174
- { ...opts, tools: TOOLS_READONLY },
175
- );
176
-
177
- if (planResult.status === "failed") {
178
- return {
179
- content: [{ type: "text" as const, text: `Planner failed: ${planResult.output}` }],
180
- details: { pipeline: "implement", stages },
181
- isError: true,
182
- };
183
- }
184
- plan = planResult.output;
185
-
186
- // Interactive mode: let user review/edit the plan before proceeding
187
- if (interactive && plan) {
188
- const edited = await ctx.ui.editor(`Review implementation plan for ${issueLabel}`, plan);
189
- if (edited != null && edited !== plan) {
190
- plan = edited;
191
- }
192
-
193
- // Surface unresolved questions one-by-one for user answers
194
- plan = await resolveQuestions(plan, ctx);
195
-
196
- const action = await ctx.ui.select("Plan ready. What next?", ["Approve and implement", "Cancel"]);
197
- if (action === "Cancel" || action == null) {
198
- return {
199
- content: [{ type: "text" as const, text: "Implementation cancelled." }],
200
- details: { pipeline: "implement", stages },
201
- };
202
- }
203
- }
204
- }
205
-
206
- // Create/checkout feature branch if on main
207
- if (resolved.branch) {
208
- const currentBranch = await exec("git branch --show-current", cwd);
209
- if (currentBranch === "main" || currentBranch === "master") {
210
- const dirty = await exec("git status --porcelain", cwd);
211
- if (dirty) {
212
- return {
213
- content: [
214
- {
215
- type: "text" as const,
216
- text: `Cannot switch to ${resolved.branch} — working tree is dirty. Please commit or stash your changes first.`,
217
- },
218
- ],
219
- details: { pipeline: "implement", stages: [] },
220
- isError: true,
221
- };
222
- }
223
- await ensureBranch(cwd, resolved.branch);
224
- const afterBranch = await exec("git branch --show-current", cwd);
225
- if (afterBranch !== resolved.branch) {
226
- return {
227
- content: [
228
- {
229
- type: "text" as const,
230
- text: `Failed to switch to ${resolved.branch} (still on ${afterBranch}). Check git state and retry.`,
231
- },
232
- ],
233
- details: { pipeline: "implement", stages: [] },
234
- isError: true,
235
- };
236
- }
237
- }
238
- }
239
-
240
- // Clean up stale blockers
241
- cleanSignal(cwd, "blocked");
242
-
243
- // Implementor
244
- const planSection = plan ? `\n\nIMPLEMENTATION PLAN:\n${plan}` : "";
245
- const branchNote = resolved.branch
246
- ? `\n- You should be on branch: ${resolved.branch} — do NOT create or switch branches.`
247
- : "\n- Do NOT create or switch branches.";
248
- const prNote = resolved.existingPR ? `\n- PR #${resolved.existingPR} already exists for this branch.` : "";
249
- const closeNote = isGitHub
250
- ? `\n- The PR body MUST include 'Closes #${resolved.number}' so the issue auto-closes on merge.`
251
- : `\n- The PR body should reference Jira issue ${resolved.key}.`;
252
- const unresolvedNote = flags.autonomous
253
- ? `\n- If the plan has unresolved questions, resolve them yourself using sensible defaults. Do NOT stop and wait.`
254
- : "";
255
-
256
- await runAgent(
257
- "implementor",
258
- `Implement the following issue using strict TDD (red-green-refactor).\n\n${issueContext}${planSection}${customPromptSection}\n\nWORKFLOW:\n1. Read the codebase.\n2. TDD${plan ? " following the plan" : ""}.\n3. Refactor after all tests pass.\n4. Run check command, fix failures.\n5. Commit, push, and create a PR.\n\nCONSTRAINTS:${branchNote}${prNote}${closeNote}${unresolvedNote}\n- If blocked, write BLOCKED.md with the reason and stop.`,
259
- { ...opts, tools: TOOLS_ALL },
260
- );
261
-
262
- // Check for blocker
263
- if (signalExists(cwd, "blocked")) {
264
- const reason = readSignal(cwd, "blocked") ?? "";
265
- return {
266
- content: [{ type: "text" as const, text: `Implementor blocked:\n${reason}` }],
267
- details: { pipeline: "implement", stages },
268
- isError: true,
269
- };
270
- }
271
-
272
- // Refactor + review
273
- await refactorAndReview(cwd, signal, onUpdate, ctx, stages, flags.skipReview);
274
-
275
- // Ensure PR exists — agent may have skipped or failed `gh pr create`
276
- let prNumber = "";
277
- if (resolved.branch) {
278
- await exec(`git push -u origin ${resolved.branch}`, cwd);
279
- prNumber = await exec(`gh pr list --head "${resolved.branch}" --json number --jq '.[0].number'`, cwd);
280
- if (!prNumber || prNumber === "null") {
281
- const prBody = isGitHub ? `Closes #${resolved.number}` : `Jira: ${resolved.key}`;
282
- await exec(`gh pr create --title "${resolved.title}" --body "${prBody}" --head ${resolved.branch}`, cwd);
283
- prNumber = await exec(`gh pr list --head "${resolved.branch}" --json number --jq '.[0].number'`, cwd);
284
- }
285
- }
286
-
287
- // Squash-merge, delete branch, update local main (skip when called from implement-all)
288
- if (!flags.autonomous && prNumber && prNumber !== "null") {
289
- const mergeStage = emptyStage("merge");
290
- stages.push(mergeStage);
291
- await exec(`gh pr merge ${prNumber} --squash --delete-branch`, cwd);
292
- await exec("git checkout main && git pull", cwd);
293
- mergeStage.status = "done";
294
- mergeStage.output = `Merged PR #${prNumber}`;
295
- onUpdate?.({
296
- content: [{ type: "text", text: "Pipeline complete" }],
297
- details: { pipeline: "implement", stages },
298
- });
299
- }
300
-
301
- return {
302
- content: [{ type: "text" as const, text: `Implementation of ${issueLabel} complete.` }],
303
- details: { pipeline: "implement", stages },
304
- };
305
- }