diff --git a/apps/server/agent/file-io.test.ts b/apps/server/agent/file-io.test.ts index 20aec4d..396453f 100644 --- a/apps/server/agent/file-io.test.ts +++ b/apps/server/agent/file-io.test.ts @@ -68,6 +68,7 @@ describe('writeInputFiles', () => { name: 'Phase One', content: 'First phase', status: 'pending', + mergeBase: null, createdAt: new Date(), updatedAt: new Date(), } as Phase; diff --git a/apps/server/db/schema.ts b/apps/server/db/schema.ts index 61ae1e2..1248f7f 100644 --- a/apps/server/db/schema.ts +++ b/apps/server/db/schema.ts @@ -55,6 +55,7 @@ export const phases = sqliteTable('phases', { status: text('status', { enum: ['pending', 'approved', 'in_progress', 'completed', 'blocked', 'pending_review'] }) .notNull() .default('pending'), + mergeBase: text('merge_base'), createdAt: integer('created_at', { mode: 'timestamp' }).notNull(), updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(), }); diff --git a/apps/server/drizzle/0031_add_phase_merge_base.sql b/apps/server/drizzle/0031_add_phase_merge_base.sql new file mode 100644 index 0000000..7771d38 --- /dev/null +++ b/apps/server/drizzle/0031_add_phase_merge_base.sql @@ -0,0 +1 @@ +ALTER TABLE phases ADD COLUMN merge_base TEXT; diff --git a/apps/server/drizzle/meta/_journal.json b/apps/server/drizzle/meta/_journal.json index ac6687d..1c5870a 100644 --- a/apps/server/drizzle/meta/_journal.json +++ b/apps/server/drizzle/meta/_journal.json @@ -218,6 +218,13 @@ "when": 1772150400000, "tag": "0030_remove_task_approval", "breakpoints": true + }, + { + "idx": 31, + "version": "6", + "when": 1772236800000, + "tag": "0031_add_phase_merge_base", + "breakpoints": true } ] } \ No newline at end of file diff --git a/apps/server/execution/orchestrator.ts b/apps/server/execution/orchestrator.ts index a08e2f1..55de53a 100644 --- a/apps/server/execution/orchestrator.ts +++ b/apps/server/execution/orchestrator.ts @@ -278,6 +278,18 @@ export class ExecutionOrchestrator { const projects = await this.projectRepository.findProjectsByInitiativeId(phase.initiativeId); + // Store merge base before merging so we can reconstruct diffs for completed phases + for (const project of projects) { + const clonePath = await ensureProjectClone(project, this.workspaceRoot); + try { + const mergeBase = await this.branchManager.getMergeBase(clonePath, initBranch, phBranch); + await this.phaseRepository.update(phaseId, { mergeBase }); + break; // Only need one merge base (first project) + } catch { + // Phase branch may not exist in this project clone + } + } + for (const project of projects) { const clonePath = await ensureProjectClone(project, this.workspaceRoot); const result = await this.branchManager.mergeBranch(clonePath, phBranch, initBranch); diff --git a/apps/server/git/branch-manager.ts b/apps/server/git/branch-manager.ts index f5d9b54..113ac7b 100644 --- a/apps/server/git/branch-manager.ts +++ b/apps/server/git/branch-manager.ts @@ -57,6 +57,12 @@ export interface BranchManager { */ diffCommit(repoPath: string, commitHash: string): Promise; + /** + * Get the merge base (common ancestor) of two branches. + * Returns the commit hash of the merge base. + */ + getMergeBase(repoPath: string, branch1: string, branch2: string): Promise; + /** * Push a branch to a remote. * Defaults to 'origin' if no remote specified. diff --git a/apps/server/git/simple-git-branch-manager.ts b/apps/server/git/simple-git-branch-manager.ts index bee747a..0c73ce7 100644 --- a/apps/server/git/simple-git-branch-manager.ts +++ b/apps/server/git/simple-git-branch-manager.ts @@ -141,6 +141,12 @@ export class SimpleGitBranchManager implements BranchManager { return git.diff([`${commitHash}~1`, commitHash]); } + async getMergeBase(repoPath: string, branch1: string, branch2: string): Promise { + const git = simpleGit(repoPath); + const result = await git.raw(['merge-base', branch1, branch2]); + return result.trim(); + } + async pushBranch(repoPath: string, branch: string, remote = 'origin'): Promise { const git = simpleGit(repoPath); await git.push(remote, branch); diff --git a/apps/server/trpc/routers/phase.ts b/apps/server/trpc/routers/phase.ts index 604b45a..4762232 100644 --- a/apps/server/trpc/routers/phase.ts +++ b/apps/server/trpc/routers/phase.ts @@ -219,8 +219,8 @@ export function phaseProcedures(publicProcedure: ProcedureBuilder) { if (!phase) { throw new TRPCError({ code: 'NOT_FOUND', message: `Phase '${input.phaseId}' not found` }); } - if (phase.status !== 'pending_review') { - throw new TRPCError({ code: 'BAD_REQUEST', message: `Phase is not pending review (status: ${phase.status})` }); + if (phase.status !== 'pending_review' && phase.status !== 'completed') { + throw new TRPCError({ code: 'BAD_REQUEST', message: `Phase is not reviewable (status: ${phase.status})` }); } const initiative = await initiativeRepo.findById(phase.initiativeId); @@ -230,13 +230,15 @@ export function phaseProcedures(publicProcedure: ProcedureBuilder) { const initBranch = initiative.branch; const phBranch = phaseBranchName(initBranch, phase.name); + // For completed phases, use stored merge base; for pending_review, use initiative branch + const diffBase = (phase.status === 'completed' && phase.mergeBase) ? phase.mergeBase : initBranch; const projects = await projectRepo.findProjectsByInitiativeId(phase.initiativeId); let rawDiff = ''; for (const project of projects) { const clonePath = await ensureProjectClone(project, ctx.workspaceRoot!); - const diff = await branchManager.diffBranches(clonePath, initBranch, phBranch); + const diff = await branchManager.diffBranches(clonePath, diffBase, phBranch); if (diff) { rawDiff += diff + '\n'; } @@ -270,8 +272,8 @@ export function phaseProcedures(publicProcedure: ProcedureBuilder) { if (!phase) { throw new TRPCError({ code: 'NOT_FOUND', message: `Phase '${input.phaseId}' not found` }); } - if (phase.status !== 'pending_review') { - throw new TRPCError({ code: 'BAD_REQUEST', message: `Phase is not pending review (status: ${phase.status})` }); + if (phase.status !== 'pending_review' && phase.status !== 'completed') { + throw new TRPCError({ code: 'BAD_REQUEST', message: `Phase is not reviewable (status: ${phase.status})` }); } const initiative = await initiativeRepo.findById(phase.initiativeId); @@ -281,13 +283,14 @@ export function phaseProcedures(publicProcedure: ProcedureBuilder) { const initBranch = initiative.branch; const phBranch = phaseBranchName(initBranch, phase.name); + const diffBase = (phase.status === 'completed' && phase.mergeBase) ? phase.mergeBase : initBranch; const projects = await projectRepo.findProjectsByInitiativeId(phase.initiativeId); const allCommits: Array<{ hash: string; shortHash: string; message: string; author: string; date: string; filesChanged: number; insertions: number; deletions: number }> = []; for (const project of projects) { const clonePath = await ensureProjectClone(project, ctx.workspaceRoot!); - const commits = await branchManager.listCommits(clonePath, initBranch, phBranch); + const commits = await branchManager.listCommits(clonePath, diffBase, phBranch); allCommits.push(...commits); } diff --git a/apps/web/src/components/review/ReviewHeader.tsx b/apps/web/src/components/review/ReviewHeader.tsx index d12a5bd..0b10a96 100644 --- a/apps/web/src/components/review/ReviewHeader.tsx +++ b/apps/web/src/components/review/ReviewHeader.tsx @@ -21,11 +21,13 @@ import type { FileDiff, ReviewStatus } from "./types"; interface PhaseOption { id: string; name: string; + status: string; } interface ReviewHeaderProps { phases: PhaseOption[]; activePhaseId: string | null; + isReadOnly?: boolean; onPhaseSelect: (id: string) => void; phaseName: string; sourceBranch: string; @@ -44,6 +46,7 @@ interface ReviewHeaderProps { export function ReviewHeader({ phases, activePhaseId, + isReadOnly, onPhaseSelect, phaseName, sourceBranch, @@ -92,6 +95,12 @@ export function ReviewHeader({
{phases.map((phase) => { const isActive = phase.id === activePhaseId; + const isCompleted = phase.status === "completed"; + const dotColor = isActive + ? "bg-primary" + : isCompleted + ? "bg-status-success-dot" + : "bg-status-warning-dot"; return ( @@ -171,102 +178,111 @@ export function ReviewHeader({ {preview && } {/* Review status / actions */} - {status === "pending" && ( - <> - -
- - - {/* Merge confirmation dropdown */} - {showConfirmation && ( -
-

- Ready to merge? -

-
-
- - - 0 unresolved comments - -
-
- - - {viewed}/{total} files viewed - -
-
-
- - -
-
- )} -
- - )} - {status === "approved" && ( + {isReadOnly ? ( - Approved - - )} - {status === "changes_requested" && ( - - - Changes Requested + Merged + ) : ( + <> + {status === "pending" && ( + <> + +
+ + + {/* Merge confirmation dropdown */} + {showConfirmation && ( +
+

+ Ready to merge? +

+
+
+ + + 0 unresolved comments + +
+
+ + + {viewed}/{total} files viewed + +
+
+
+ + +
+
+ )} +
+ + )} + {status === "approved" && ( + + + Approved + + )} + {status === "changes_requested" && ( + + + Changes Requested + + )} + )}
diff --git a/docs/database.md b/docs/database.md index c4bfc59..6afb841 100644 --- a/docs/database.md +++ b/docs/database.md @@ -29,7 +29,8 @@ All adapters use nanoid() for IDs, auto-manage timestamps, and use Drizzle's `.r | initiativeId | text FK → initiatives (cascade) | | | name | text NOT NULL | | | content | text nullable | Tiptap JSON | -| status | text enum | 'pending' \| 'approved' \| 'in_progress' \| 'completed' \| 'blocked' | +| status | text enum | 'pending' \| 'approved' \| 'in_progress' \| 'completed' \| 'blocked' \| 'pending_review' | +| mergeBase | text nullable | git merge-base hash stored before phase merge, enables diff reconstruction for completed phases | | createdAt, updatedAt | integer/timestamp | | ### phase_dependencies diff --git a/docs/git-process-logging.md b/docs/git-process-logging.md index 817dbfc..b35efa2 100644 --- a/docs/git-process-logging.md +++ b/docs/git-process-logging.md @@ -45,6 +45,7 @@ Worktrees stored in `.cw-worktrees/` subdirectory of the repo. Each agent gets a | `remoteBranchExists(repoPath, branch)` | Check remote tracking branches (`origin/`) | | `listCommits(repoPath, base, head)` | List commits head has that base doesn't (with stats) | | `diffCommit(repoPath, commitHash)` | Get unified diff for a single commit | +| `getMergeBase(repoPath, branch1, branch2)` | Get common ancestor commit hash | `remoteBranchExists` is used by `registerProject` and `updateProject` to validate that a project's default branch actually exists in the cloned repository before saving.