Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix(branch-reconcile): drive a reconciled branch to merged instead of stopping at "PR opened"
[._.]/ Adam Eivy805208392ddd
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex 4e3bc9c3c3..8853fdf8fe 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -30,3 +30,9 @@- The workflow is invoked correctly no matter which agent runs it. Because the provider dropdown defaults to Auto, the template stores the workflow's name rather than a literal `/do:` string — at launch, a Claude Code agent gets `/do:plan-task`, OpenCode gets `/do-plan-task`, and Codex/Grok/Antigravity agents get it named as an Agent Skill. The workflow's full procedure travels with the task in every case, so it also works when the agent is running against one of your other apps rather than PortOS itself, where those slash commands aren't installed.- Applying a template no longer clears the target application you'd already selected — which had also silently reset that app's worktree and PR defaults.- `/do:plan-task` and `/do:config` are now available as project slash commands in this repo; they were the only two bundled commands missing a link.++## CoS branch reconciliation++- **A branch the reconciler picks up is now driven all the way to merged, not just to "PR opened."** When the task verified a branch was ready and opened its pull request, it reported success and exited within a minute — while CI was still running. The pull request then sat green and mergeable until the next scheduled run noticed it again. The agent now waits out the check run in-session, fixes what goes red, and merges once every required check passes and the pull request is mergeable.+- The reconciler's blanket "never merge unreviewed work" rule read as a veto on the merge step it was supposed to perform, so it has been replaced by the explicit gate each branch already carries (required CI green, mergeable, and the review it names). Merging still only ever happens through `gh pr merge` on a branch the task was handed.+- Waiting for a Copilot review on an in-review pull request is now time-boxed to 10 minutes — on a repository without Copilot review enabled, the old unbounded wait could strand a green pull request open indefinitely.diff --git a/server/services/branchReconcile.js b/server/services/branchReconcile.jsindex 4542a02421..b9c5d471aa 100644--- a/server/services/branchReconcile.js+++ b/server/services/branchReconcile.js@@ -343,18 +343,56 @@ export function filterActionable(inFlight, actions) {});}-/** Per-state one-line instruction to the coordinator/sub-agent. */-export function desiredEndState(state, actions) {+// The terminal state of an auto-mergeable branch is MERGED — not "PR opened",+// not "PR green and waiting". Every drive-to-merge instruction ends with this+// tail so the agent waits CI out IN-SESSION instead of handing back an open PR+// (the miss that left a green, MERGEABLE PR sitting open after a NEEDS_PR run:+// the agent opened it and signaled done 51s later). GitHub-native `--auto` is+// deliberately not the answer on its own — the agent exits the moment it+// finishes, so a queued auto-merge that later goes red has nobody left to fix+// it, and the branch silently stays in-flight until the next recheck.+//+// `pr` is the PR's number when the branch already has one, else the `<num>`+// placeholder the agent fills in after opening it.+const driveToMerge = (pr) => [+ 'Opening (or approving) the PR is NOT the end state — a green PR left open is still an unfinished branch that this task will simply re-drive on its next run.',+ `Wait for CI in-session: \`gh pr checks ${pr} --required --watch --fail-fast\`, or re-poll \`gh pr checks ${pr}\` every 30s — allow up to 20 minutes for the run to finish.`,+ 'If a required check FAILS, fix it on the branch, push, and re-poll (max 3 rounds); if it is still red after that, leave the PR open and report exactly which check failed.',+ `Once every required check is green AND the PR is MERGEABLE, merge it: \`gh pr merge ${pr} --merge --delete-branch\`.`,+ 'After the merge, delete the local branch and remove its worktree.'+].join(' ');++/**+ * Per-state instruction to the coordinator/sub-agent.+ * @param {string} state+ * @param {object} actions - per-app action toggles+ * @param {{ prNumber?: number }} [ctx] - the branch's open PR number, when it has one+ */+export function desiredEndState(state, actions, { prNumber } = {}) {+ const pr = prNumber ? String(prNumber) : '<num>';if (state === 'NEEDS_PR') {- return 'Verify the branch\'s work is complete and ready (tests pass, no stubs/TODO markers, changelog present). If ready, run `/do:pr`. If NOT ready, report it as incomplete and leave the branch untouched — do not open a half-baked PR.';+ const verify = 'Verify the branch\'s work is complete and ready (tests pass, no stubs/TODO markers, changelog present). If NOT ready, report it as incomplete and leave the branch untouched — do not open a half-baked PR.';+ // `--merge` / `--no-merge` are passed explicitly rather than inherited from+ // this machine's saved slashdo defaults, so the task's own autoMerge toggle+ // decides the outcome on every install. A spawned sub-agent can't always+ // expand a slash command, hence the by-hand equivalent.+ const byHand = 'by hand, if slash commands aren\'t available in your context: self-review the diff, `git push -u origin <branch>`, then `gh pr create` with a Summary + Test plan';+ if (!actionOn(actions, 'autoMerge')) {+ return `${verify} If ready, ship it with \`/do:pr --no-merge\` (${byHand}). Do NOT merge (auto-merge is disabled) — stop once the PR is open and report its URL.`;+ }+ return `${verify} If ready, ship it end-to-end with \`/do:pr --merge\` (${byHand}). ${driveToMerge(pr)}`;}if (state === 'CONFLICTED') {return 'Rebase the branch onto the default branch, resolve all conflicts, run the tests, and push.';}// IN_REVIEWconst canMerge = actionOn(actions, 'autoMerge');+ // The Copilot gate is time-boxed for the same reason the merge waits on CI+ // in-session: a repo without Copilot review enabled never produces one, and an+ // unbounded "await the review" leaves a green PR open forever. 10 minutes+ // mirrors the claim-issue flow's Copilot poll.return `Drive the open PR toward green: request/await the Copilot review and address feedback.${canMerge- ? ' Then MERGE it (`gh pr merge --merge --delete-branch`) ONLY when it is MERGEABLE, CI is fully green, and the LATEST Copilot review reports "0 comments" (pre-resolved threads do NOT count; a PR over 20k lines is exempt from the Copilot check and needs only CI-green + mergeable). After merging, delete the local branch and remove its worktree.'+ ? ` Merge ONLY when the LATEST Copilot review reports "0 comments" (pre-resolved threads do NOT count; a PR over 20k lines is exempt from the Copilot check and needs only CI-green + mergeable). If no Copilot review lands within 10 minutes of requesting it, the Copilot gate is satisfied — this repo may not have Copilot review enabled — and CI-green + MERGEABLE is the whole gate. ${driveToMerge(pr)}`: ' Do NOT merge (auto-merge is disabled) — stop once the PR is green and ready for the user to merge.'}`;}@@ -390,7 +428,7 @@ export function formatInFlightForPrompt(inFlight, { defaultBranch, actions }) {const pr = b.openPr ? ` — PR #${b.openPr.number} (${b.openPr.mergeable})${b.openPr.url ? ` ${b.openPr.url}` : ''}` : ' — no PR';lines.push(`### \`${b.branch}\` [${b.state}]${pr}`);if (b.worktreePath) lines.push(`- Worktree: \`${b.worktreePath}\``);- lines.push(`- Do: ${desiredEndState(b.state, actions)}`);+ lines.push(`- Do: ${desiredEndState(b.state, actions, { prNumber: b.openPr?.number })}`);lines.push('');}return lines.join('\n');diff --git a/server/services/branchReconcile.test.js b/server/services/branchReconcile.test.jsindex 7adc1695a8..0f62352346 100644--- a/server/services/branchReconcile.test.js+++ b/server/services/branchReconcile.test.js@@ -315,7 +315,7 @@ describe('filterActionable', () => {describe('desiredEndState', () => {it('tells IN_REVIEW to merge only when autoMerge is on', () => {- expect(desiredEndState('IN_REVIEW', {})).toContain('gh pr merge --merge --delete-branch');+ expect(desiredEndState('IN_REVIEW', {})).toContain('gh pr merge <num> --merge --delete-branch');expect(desiredEndState('IN_REVIEW', { autoMerge: false })).toContain('Do NOT merge');});@@ -323,6 +323,45 @@ describe('desiredEndState', () => {expect(desiredEndState('NEEDS_PR', {})).toContain('/do:pr');expect(desiredEndState('CONFLICTED', {})).toContain('Rebase');});++ // The miss this pins: a NEEDS_PR run opened a PR, reported "left open for+ // review", and exited 51s later — while CI was still running on a branch the+ // task was supposed to finish. "PR opened" is a step, not the end state.+ it('drives NEEDS_PR past the PR to a merge when autoMerge is on', () => {+ const instruction = desiredEndState('NEEDS_PR', {});+ expect(instruction).toContain('/do:pr --merge');+ expect(instruction).toContain('gh pr checks <num>');+ expect(instruction).toContain('gh pr merge <num> --merge --delete-branch');+ expect(instruction).toContain('NOT the end state');+ });++ it('stops NEEDS_PR at an open PR when autoMerge is off', () => {+ const instruction = desiredEndState('NEEDS_PR', { autoMerge: false });+ expect(instruction).toContain('/do:pr --no-merge');+ expect(instruction).toContain('Do NOT merge');+ expect(instruction).not.toContain('gh pr merge');+ });++ it('makes both merge-bound states wait for CI in-session rather than queueing --auto', () => {+ for (const state of ['NEEDS_PR', 'IN_REVIEW']) {+ const instruction = desiredEndState(state, {});+ expect(instruction).toContain('--required --watch --fail-fast');+ expect(instruction).not.toContain('--auto');+ }+ });++ it('names the real PR number when the branch already has one', () => {+ const instruction = desiredEndState('IN_REVIEW', {}, { prNumber: 42 });+ expect(instruction).toContain('gh pr checks 42 --required --watch --fail-fast');+ expect(instruction).toContain('gh pr merge 42 --merge --delete-branch');+ expect(instruction).not.toContain('<num>');+ });++ // A repo without Copilot review enabled never produces one — an unbounded+ // "await the review" would strand a green PR open forever.+ it('time-boxes the IN_REVIEW Copilot gate', () => {+ expect(desiredEndState('IN_REVIEW', {})).toContain('within 10 minutes');+ });});describe('actionableSignature', () => {diff --git a/server/services/taskPromptDefaults.test.js b/server/services/taskPromptDefaults.test.jsindex 5aa87c2c92..a5863e9302 100644--- a/server/services/taskPromptDefaults.test.js+++ b/server/services/taskPromptDefaults.test.js@@ -150,4 +150,23 @@ describe('taskPromptDefaults integrity snapshot', () => {expect(outgoing).not.toContain('{worktreesRoot}');expect(outgoing).not.toBe(current);});++ // branch-reconcile v2: "PR opened" is a completed STEP, not a completed+ // branch. v1's blanket "never merge unreviewed work" rule read as a veto on+ // the per-branch merge instruction, so a coordinator opened a PR and exited+ // while CI was still running — leaving a green, MERGEABLE PR sitting open.+ it('branch-reconcile v2 makes merged (not PR-opened) the end state, preserving the v1 default', () => {+ const current = DEFAULT_TASK_PROMPTS['branch-reconcile'];+ expect(current).toContain('not finished until it IS merged');+ expect(current).not.toContain('never merge unreviewed work');+ expect(PROMPT_VERSIONS['branch-reconcile']).toBe(2);++ // The outgoing v1 default carried the blanket ban and no CI-wait rule; it is+ // preserved verbatim so installs holding it are recognized and upgraded.+ const previous = PREVIOUS_DEFAULT_PROMPTS['branch-reconcile'];+ const v1 = previous[previous.length - 1];+ expect(v1).toContain('never merge unreviewed work');+ expect(v1).not.toContain('not finished until it IS merged');+ expect(v1).not.toBe(current);+ });});diff --git a/server/services/taskPromptDefaults/integrity.snapshot.json b/server/services/taskPromptDefaults/integrity.snapshot.jsonindex de11491adc..1410a83a58 100644--- a/server/services/taskPromptDefaults/integrity.snapshot.json+++ b/server/services/taskPromptDefaults/integrity.snapshot.json@@ -24,7 +24,7 @@"do-replan": "59a57f1bb445138c30ab4fa23630c6f8","jira-status-report": "9d374a9d8ccb92c5c8fd0aa9899e79a5","branch-cleanup": "8fdeb4acc2749816a47ca318b6602721",- "branch-reconcile": "7ef2b5f7d8f05937d4912b26ad45f42e",+ "branch-reconcile": "b4502b930deb020fa3a31c082c44da9e","issue-reconcile": "a30f5d76b980bc9016f576047c9d5bb1","pr-reviewer": "add27b67daa6aa2c75717ac96a6bd625","pr-reviewer-security": "2050297d1cdb081a8576a781828ee44f",@@ -44,7 +44,7 @@"code-reviewer-b": 1,"reference-watch": 3,"pr-watcher": 1,- "branch-reconcile": 1,+ "branch-reconcile": 2,"issue-reconcile": 3,"refresh-local-llm-catalog": 1,"security": 2,@@ -148,6 +148,9 @@"609167c41331e6bbfc6d5f5edc452d4c","9c7508598ee772d1cee8f609f9ed7372"],+ "branch-reconcile": [+ "7ef2b5f7d8f05937d4912b26ad45f42e"+ ],"issue-reconcile": ["c57a39aaa118173a496f030f5878bfc1","c87d7adb57de208c069964760c9c6276"diff --git a/server/services/taskPromptDefaults/previousDefaults.js b/server/services/taskPromptDefaults/previousDefaults.jsindex 6a3177d782..cfcb8b235c 100644--- a/server/services/taskPromptDefaults/previousDefaults.js+++ b/server/services/taskPromptDefaults/previousDefaults.js@@ -4828,6 +4828,30 @@ Summarize:IMPORTANT: Always use \`git pull --rebase --autostash\` before pushing (dev branch gets auto-bumped by CI). Never use \`git push\` alone.`,],+ 'branch-reconcile': [+ // v1 default — superseded by v2, whose Rules make "merged" (not "PR opened")+ // the terminal state for an auto-mergeable branch. v1's blanket "never merge+ // unreviewed work" rule read as a veto on the per-branch merge instruction,+ // so a NEEDS_PR branch ended with a green PR left sitting open.+ `[Improvement: {appName}] Branch & PR Reconciliation++You are the coordinator for finishing {appName}'s unfinished local git work. The scheduler has already run the deterministic pass (removed fully-merged, orphaned local branches + their worktrees) and handed you ONLY the branches that need judgment.++Repository: {repoPath}++Each branch listed below is a LOCAL branch in THIS clone of {appName}. On a machine that is a federated sync peer, branches created on OTHER machines exist here only as remote-tracking refs (\`origin/*\`) and are deliberately NOT listed — never open, rebase, or merge anything that is not in the list below.++{inFlightBranches}++Spawn ONE sub-agent per branch (they are independent — run them in parallel) to carry out that branch's "Do:" instruction, each working in the branch's existing worktree when it has one.++## Rules+- Work ONLY on the branches listed above. Never touch a branch that is not listed.+- Never force-push the default branch and never merge unreviewed work.+- If a sub-agent reports a branch is incomplete or blocked, leave it as-is and note it in your summary.+- Summarize what each branch ended up doing (PR opened / conflicts resolved / merged / left incomplete).`,+ ],+'issue-reconcile': [// v1 default (GitHub-only) — superseded by the forge-aware v2 body (gh/glab).`[Improvement: {appName}] Zombie Issue Reconciliationdiff --git a/server/services/taskPromptDefaults/prompts.js b/server/services/taskPromptDefaults/prompts.jsindex 1c7dfbe053..4f4c979708 100644--- a/server/services/taskPromptDefaults/prompts.js+++ b/server/services/taskPromptDefaults/prompts.js@@ -1272,9 +1272,11 @@ Spawn ONE sub-agent per branch (they are independent — run them in parallel) t## Rules- Work ONLY on the branches listed above. Never touch a branch that is not listed.-- Never force-push the default branch and never merge unreviewed work.+- Never force-push the default branch.+- **A branch whose "Do:" line ends in a merge is not finished until it IS merged.** Its sub-agent stays alive through CI — waiting out the check run, fixing what goes red, then merging — and reports back only when the PR is merged or a specific check/review is blocking it. "PR opened, left open for review" is a completed STEP, not a completed branch: the PR just sits green until the next run re-drives it. Do not end your own run while a sub-agent is still waiting on CI.+- Merging is gated by the "Do:" line itself (required CI green + MERGEABLE + the review it names) — that gate, not a blanket ban, is what keeps unreviewed work out of the default branch. Merge only via \`gh pr merge\`, never a local \`git merge\` into the default branch.- If a sub-agent reports a branch is incomplete or blocked, leave it as-is and note it in your summary.-- Summarize what each branch ended up doing (PR opened / conflicts resolved / merged / left incomplete).`,+- Summarize what each branch ended up doing (merged / PR opened but blocked on <what> / conflicts resolved / left incomplete). When a PR is left open, name the check or review that blocked it.`,'issue-reconcile': `[Improvement: {appName}] Zombie Issue Reconciliationdiff --git a/server/services/taskPromptDefaults/versions.js b/server/services/taskPromptDefaults/versions.jsindex f1b6ef27d0..aa60b80099 100644--- a/server/services/taskPromptDefaults/versions.js+++ b/server/services/taskPromptDefaults/versions.js@@ -17,7 +17,7 @@ export const PROMPT_VERSIONS = {'code-reviewer-b': 1, // v1: 2-stage pipeline (codebase review → triage & implement)'reference-watch': 3, // v3: record proposals in the app's RESOLVED work tracker (PLAN.md / GitHub / GitLab / JIRA) via the {trackerInstructions} block — no longer hardcodes PLAN.md, so an app configured for GitHub issues gets `gh issue create` proposals. v2: append slug-tagged checklist items to PLAN.md (Adopt + Maybe) instead of writing REFERENCE_REVIEW.md; security-flagged commits get no PLAN entry (mentioned only in final summary)'pr-watcher': 1, // v1: review-and-comment default for newly-opened PRs on the app's default branch- 'branch-reconcile': 1, // v1: per-app coordinator that finishes in-flight LOCAL branches (open PR / resolve conflicts / drive review / auto-merge) after the deterministic merged-branch cleanup pass. Peer-safe (local refs only). Replaced the PortOS-only branchReconcileScheduler.+ 'branch-reconcile': 2, // v2: a branch whose "Do:" line ends in a merge isn't finished until it IS merged — the sub-agent waits CI out in-session instead of handing back a green-but-open PR, and the old blanket "never merge unreviewed work" rule (which vetoed the per-branch merge instruction) is replaced by the explicit CI-green + MERGEABLE + review gate. v1: per-app coordinator that finishes in-flight LOCAL branches (open PR / resolve conflicts / drive review / auto-merge) after the deterministic merged-branch cleanup pass. Peer-safe (local refs only). Replaced the PortOS-only branchReconcileScheduler.'issue-reconcile': 3, // v3: adds a JIRA arm — status-based zombies (a ticket left In Review with remaining scope + no live claim; JIRA has no `in-progress` label) detected via the PortOS JIRA API and healed through ticket transitions + `POST tickets`, routed in via the app's resolved workTracker ('jira') rather than the git host. v2: forge-aware — the scan + coordinator now cover GitLab (`glab` issues + MRs) as well as GitHub, resolved from the app's origin host; every heal command is shown as gh/glab and the injected header names the forge. v1: per-app coordinator that heals ZOMBIE issues (open + in-progress but their PR merged with no live claim) after the deterministic gh/git scan. Applies the partial-ship hybrid — close + file a scoped follow-up when the remainder is separable, else comment "done/remaining" + release the claim so the queue re-picks it.'refresh-local-llm-catalog': 1, // v1: research current local models, refresh LOCAL_LLM_CATALOG + EDITORIAL_FAMILY_RANK, PR (PortOS repo only)