-
Notifications
You must be signed in to change notification settings - Fork 2
fix: prevent resolver hang and add boss-supervised merge resolver #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tensor-ninja
merged 3 commits into
nightshiftco:main
from
dipeshbabu:fix/issue-50-resolver-no-hang
Feb 15, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,158 @@ | ||
| import type { createOpencodeClient } from "@opencode-ai/sdk/v2"; | ||
| import { runSession } from "../session"; | ||
| import { resolverPrompt } from "../../../lib/prompts/resolver"; | ||
| import { resolverBossPrompt } from "../../../lib/prompts/resolverBoss"; | ||
| import type { EventPublisher } from "../bus"; | ||
|
|
||
| export interface ResolverOptions { | ||
| client: ReturnType<typeof createOpencodeClient>; | ||
| workerClient: ReturnType<typeof createOpencodeClient>; | ||
| bossClient: ReturnType<typeof createOpencodeClient>; | ||
| worktreePath: string; | ||
| conflicts: string; | ||
| model: string; | ||
| evalModel: string; | ||
| maxIterations?: number; // default 4 | ||
| bus?: EventPublisher; | ||
| } | ||
|
|
||
| export interface ResolverResult { | ||
| output: string; | ||
| done: boolean; | ||
| } | ||
|
|
||
| export async function resolve(options: ResolverOptions): Promise<ResolverResult> { | ||
| const { client, conflicts, model, bus } = options; | ||
| async function execGit( | ||
| args: string[], | ||
| cwd: string, | ||
| ): Promise<{ code: number; stdout: string; stderr: string }> { | ||
| const proc = Bun.spawn(["git", ...args], { cwd, stdout: "pipe", stderr: "pipe" }); | ||
| const code = await proc.exited; | ||
| const stdout = (await new Response(proc.stdout).text()).trim(); | ||
| const stderr = (await new Response(proc.stderr).text()).trim(); | ||
| return { code, stdout, stderr }; | ||
| } | ||
|
|
||
| if (bus) { | ||
| bus.publish({ type: "resolver.start", timestamp: Date.now(), conflicts }); | ||
| } | ||
| async function getMergeState(worktreePath: string): Promise<{ | ||
| mergeInProgress: boolean; | ||
| statusPorcelain: string; | ||
| conflictMarkers: string; | ||
| conflictedFiles: string; | ||
| }> { | ||
| const mergeHead = await execGit(["rev-parse", "-q", "--verify", "MERGE_HEAD"], worktreePath); | ||
| const mergeInProgress = mergeHead.code === 0; | ||
|
|
||
| const status = await execGit(["status", "--porcelain"], worktreePath); | ||
|
|
||
| // 0 = found markers, 1 = none, 2 = error | ||
| const markers = await execGit(["grep", "-n", "-E", "^(<<<<<<<|=======|>>>>>>>)", "--", "."], worktreePath); | ||
| const conflictMarkers = markers.code === 0 ? markers.stdout : ""; | ||
|
|
||
| const prompt = resolverPrompt(conflicts); | ||
| const conflicts = await execGit(["diff", "--name-only", "--diff-filter=U"], worktreePath); | ||
|
|
||
| const { output } = await runSession({ | ||
| client, | ||
| prompt, | ||
| title: "merge-resolver", | ||
| return { | ||
| mergeInProgress, | ||
| statusPorcelain: status.stdout, | ||
| conflictMarkers, | ||
| conflictedFiles: conflicts.stdout.trim(), | ||
| }; | ||
| } | ||
|
|
||
| export async function resolve(options: ResolverOptions): Promise<ResolverResult> { | ||
| const { | ||
| workerClient, | ||
| bossClient, | ||
| worktreePath, | ||
| conflicts: originalConflicts, | ||
| model, | ||
| phase: "resolver", | ||
| evalModel, | ||
| maxIterations = 4, | ||
| bus, | ||
| }); | ||
| } = options; | ||
|
|
||
| if (bus) { | ||
| bus.publish({ type: "resolver.complete", timestamp: Date.now() }); | ||
| bus.publish({ type: "resolver.start", timestamp: Date.now(), conflicts: originalConflicts }); | ||
| } | ||
|
|
||
| let feedback = ""; | ||
| let lastOutput = ""; | ||
|
|
||
| for (let iter = 1; iter <= maxIterations; iter++) { | ||
| const stateBefore = await getMergeState(worktreePath); | ||
| const currentConflicts = stateBefore.conflictedFiles || originalConflicts; | ||
|
|
||
| const prompt = | ||
| resolverPrompt(currentConflicts) + | ||
| (feedback ? `\n\n## Boss feedback\n${feedback}` : ""); | ||
|
|
||
| const { output } = await runSession({ | ||
| client: workerClient, | ||
| prompt, | ||
| title: `merge-resolver-${iter}`, | ||
| model, | ||
| phase: "resolver", | ||
| bus, | ||
| }); | ||
|
|
||
| lastOutput = output; | ||
|
|
||
| const stateAfter = await getMergeState(worktreePath); | ||
|
|
||
| // Deterministic checks are the source of truth. | ||
| const done = | ||
| !stateAfter.mergeInProgress && | ||
| !stateAfter.conflictMarkers && | ||
| !stateAfter.statusPorcelain; | ||
|
|
||
| if (done) { | ||
| if (bus) bus.publish({ type: "resolver.complete", timestamp: Date.now() }); | ||
| return { output: lastOutput, done: true }; | ||
| } | ||
|
|
||
| // Boss provides targeted instructions (non-interactive). | ||
| const bossPrompt = resolverBossPrompt({ | ||
| originalConflicts, | ||
| currentConflicts: stateAfter.conflictedFiles, | ||
| mergeInProgress: stateAfter.mergeInProgress, | ||
| statusPorcelain: stateAfter.statusPorcelain, | ||
| conflictMarkers: stateAfter.conflictMarkers, | ||
| resolverOutput: lastOutput.slice(-8000), | ||
| }); | ||
|
|
||
| const boss = await runSession({ | ||
| client: bossClient, | ||
| prompt: bossPrompt, | ||
| title: `merge-resolver-boss-${iter}`, | ||
| model: evalModel, | ||
| phase: "validator", | ||
| bus, | ||
| timeoutMs: 10 * 60 * 1000, | ||
| }); | ||
|
|
||
| const bossSaysDone = boss.output.includes("VERDICT: DONE"); | ||
|
|
||
| if (bossSaysDone) { | ||
| // Boss can't change state, but re-checking here makes the logic explicit and robust. | ||
| const stateNow = await getMergeState(worktreePath); | ||
| const doneNow = | ||
| !stateNow.mergeInProgress && | ||
| !stateNow.conflictMarkers && | ||
| !stateNow.statusPorcelain; | ||
|
|
||
| if (doneNow) { | ||
| if (bus) bus.publish({ type: "resolver.complete", timestamp: Date.now() }); | ||
| return { output: lastOutput, done: true }; | ||
| } | ||
|
|
||
| feedback = | ||
| `Deterministic checks still failing.\n` + | ||
| `mergeInProgress=${stateNow.mergeInProgress}\n` + | ||
| `dirty=${Boolean(stateNow.statusPorcelain)}\n` + | ||
| `markers=${Boolean(stateNow.conflictMarkers)}\n` + | ||
| `Fix remaining conflicts, git add, and git commit.`; | ||
| } else { | ||
| feedback = boss.output; | ||
| } | ||
| } | ||
|
|
||
| return { output }; | ||
| if (bus) bus.publish({ type: "resolver.complete", timestamp: Date.now() }); | ||
| return { output: lastOutput, done: false }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| export function resolverBossPrompt(args: { | ||
| originalConflicts: string; | ||
| currentConflicts: string; | ||
| mergeInProgress: boolean; | ||
| statusPorcelain: string; | ||
| conflictMarkers: string; | ||
| resolverOutput: string; | ||
| }): string { | ||
| const { | ||
| originalConflicts, | ||
| currentConflicts, | ||
| mergeInProgress, | ||
| statusPorcelain, | ||
| conflictMarkers, | ||
| resolverOutput, | ||
| } = args; | ||
|
|
||
| return `You are the Merge Resolver Boss. | ||
|
|
||
| Your job is to decide whether the merge conflict resolution is COMPLETE. | ||
|
|
||
| Hard requirements (must all be true): | ||
| 1) No merge in progress (MERGE_HEAD absent) | ||
| 2) No conflict markers remain (<<<<<<<, =======, >>>>>>>) | ||
| 3) Working tree is clean (git status --porcelain is empty) | ||
|
|
||
| If any requirement fails: | ||
| - respond with VERDICT: NOT DONE | ||
| - provide specific actionable instructions | ||
| - NEVER ask questions | ||
|
|
||
| Inputs: | ||
| Original conflicted files: | ||
| ${originalConflicts || "(none)"} | ||
|
|
||
| Current conflicted files: | ||
| ${currentConflicts || "(none)"} | ||
|
|
||
| Deterministic checks: | ||
| mergeInProgress: ${mergeInProgress} | ||
| git status --porcelain: | ||
| ${statusPorcelain || "(clean)"} | ||
|
|
||
| conflict markers grep: | ||
| ${conflictMarkers || "(none)"} | ||
|
|
||
| Resolver output: | ||
| ${resolverOutput} | ||
|
|
||
| Return exactly one of: | ||
| VERDICT: DONE | ||
| VERDICT: NOT DONE | ||
| <instructions>`; | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.