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
Branch & PR Reconciler Implementation Plan
For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (
- [ ]) syntax for tracking.
Goal: A daily, opt-in CoS automation that reconciles this machine's unfinished git work — deterministically cleaning up merged/orphaned local branches + worktrees, and dispatching one coordinator CoS agent (which orchestrates one sub-agent per branch) to finish in-flight PRs — while never touching a federated peer's branches.
Architecture: Two tiers. Tier 1 is a pure classifier + deterministic cleanup in branchReconcile.js (no LLM). Tier 2 is a scheduler (branchReconcileScheduler.js, eventScheduler cron, backupScheduler.js pattern) that runs Tier 1 then, if in-flight branches remain, enqueues a single coordinator CoS task via cosTaskStore.addTask. Ownership is scoped by local-branch existence (refs/heads/), so peer branches (origin/* only) are structurally invisible.
Tech Stack: Node/Express, existing server/services/git.js + github.js (execGh) + eventScheduler.js + cosTaskStore.js, Zod validation, Vitest.
Global Constraints
- No try/catch except at non-request boundaries (scheduler handlers, child-process callbacks) — those wrap + log emoji-prefixed
console.error. - Functional style, no classes. DRY/YAGNI.
- Single-line emoji logging with interpolated values.
- Ownership discriminator: local branch must exist in
refs/heads/; never author-filter. Always excludemain,master,release, and the resolved default branch. - Cleanup deletes a branch ONLY when
isBranchMergedInto(default)is true AND its worktree is clean; usegit branch -d(never-D). - New settings key ships a default in
data.reference/settings.json; Zod schema tolerant of absence;enabled: falseby default (no cold-bootstrap LLM calls). - New public modules re-exported from the same-dir
index.jsbarrel + one README row.
Task 1: Pure branch classifier
Files:
- Create:
server/services/branchReconcile.js - Test:
server/services/branchReconcile.test.js
Interfaces:
- Produces:
PROTECTED_BRANCHES: string[]classifyBranch(input) => statewhereinput = { branch, hasUpstream, isMerged, hasWorktree, worktreeDirty, openPr }(openPr={ number, mergeable, isDraft }| null) andstate ∈ 'MERGED' | 'CONFLICTED' | 'IN_REVIEW' | 'NEEDS_PR' | 'WIP'.classifyBranches(inputs[]) => { branch, state, openPr }[]
Classification order (first match wins):
isMerged→MERGEDopenPr && openPr.mergeable === 'CONFLICTING'→CONFLICTEDopenPr(any other mergeable value) →IN_REVIEWhasUpstream && !worktreeDirty(pushed, not merged, no PR, clean) →NEEDS_PR- else (
!hasUpstreamorworktreeDirty) →WIP
- Step 1: Write failing tests covering each state + precedence (merged-with-open-PR still
MERGED; dirty pushed branch →WIP; conflicting PR →CONFLICTED). - Step 2: Run
cd server && npx vitest run services/branchReconcile.test.js→ FAIL (module missing). - Step 3: Implement
PROTECTED_BRANCHES,classifyBranch,classifyBranchesper the table. - Step 4: Run tests → PASS.
- Step 5: Commit
feat: add branch reconcile classifier.
Task 2: Branch-state gathering + deterministic cleanup
Files:
- Modify:
server/services/branchReconcile.js - Test:
server/services/branchReconcile.test.js
Interfaces:
Consumes:
git.js(getDefaultBranch,isBranchMergedInto,deleteBranch),github.jsexecGh,worktreeManager.js(listWorktrees,forceRemoveWorktreeDir).Produces:
gatherBranchState(repoPath, { defaultBranch }) => inputs[]— enumeratesrefs/heads/(viagit for-each-refporcelain through git.js helper or a localrunGit), worktrees, merged status, open PRs (execGh pr list --json number,headRefName,mergeable,isDraft), and per-worktree dirtiness. Excludes protected branches.cleanupMerged(repoPath, merged[]) => { cleaned: string[], skipped: {branch,reason}[] }— for eachMERGEDbranch: re-verifyisBranchMergedInto; if a worktree exists and is clean,git worktree remove --force+ prune, thendeleteBranch(local:true); skip (with reason) if unmerged-on-recheck or dirty.reconcile(repoPath) => { cleaned, inFlight, wip, skipped }— gather → classify → cleanupMerged(MERGED) → returns inFlight (CONFLICTED|IN_REVIEW|NEEDS_PRwith theiropenPr/state) and wip lists.
Step 1: Write failing tests for
cleanupMergedsafety gates with mocked git/worktree: (a) merged+clean → cleaned; (b) merged-but-recheck-unmerged → skippednot-merged; (c) merged+dirty-worktree → skippeddirty. Andreconcileend-to-end with a mockedgatherBranchState.Step 2: Run vitest → FAIL.
Step 3: Implement
gatherBranchState,cleanupMerged,reconcile.Step 4: Run tests → PASS.
Step 5: Commit
feat: add branch reconcile gather + deterministic cleanup.
Task 3: Settings schema + defaults
Files:
- Modify:
server/lib/validation.js(addbranchReconcileConfigSchema, wire into settings PUT slice) - Modify:
data.reference/settings.json(add defaultbranchReconcileblock) - Test:
server/lib/validation.test.js(or nearest settings-schema test)
Interfaces:
- Produces:
branchReconcileConfigSchema = z.object({ enabled: z.boolean(), cron: z.string(), actions: optionalBooleanMap(['cleanupMerged','openPr','resolveConflicts','autoMerge']) }).partial().
Default block:
"branchReconcile": {
"enabled": false,
"cron": "0 3 * * *",
"actions": { "cleanupMerged": true, "openPr": true, "resolveConflicts": true, "autoMerge": true }
}
- Step 1: Write failing test asserting the schema accepts the default block and rejects a non-boolean
enabled. - Step 2: Run vitest → FAIL.
- Step 3: Implement schema +
settings.jsonslice validation (mirrorbackupConfigSchema.partial()inroutes/settings.js) + seed default. - Step 4: Run → PASS.
- Step 5: Commit
feat: add branchReconcile settings schema + default.
Task 4: Coordinator prompt template + prompt builder
Files:
- Create:
data.reference/prompts/branch-reconcile-coordinator.md - Modify:
server/services/branchReconcileScheduler.js(addformatInFlightForPrompt,buildCoordinatorTask) - Test:
server/services/branchReconcileScheduler.test.js
Interfaces:
- Produces:
formatInFlightForPrompt(inFlight, { defaultBranch }) => string— one block per branch: name, worktree path, state, PR number/url, desired end-state.buildCoordinatorTask(inFlight, { repoPath, defaultBranch, actions }) => taskObject— reads the template, injects{inFlightData}, returns{ id: 'sys-branch-reconcile-<base36>', description, priority:'LOW', priorityValue, metadata:{ taskApp:'_self', context, useWorktree:false } }(multi-line body inmetadata.context, first line indescription, per the cosTaskStore round-trip rule).
Prompt template instructs: for each branch, per state — NEEDS_PR: verify complete/ready then /do:pr, else report incomplete; CONFLICTED: rebase onto {defaultBranch}, resolve, push; IN_REVIEW: run review loop; auto-merge only when MERGEABLE + CI green + latest Copilot review "0 comments" (pre-resolved threads don't count; ≥20k-line PRs exempt from Copilot gate). Respect each action toggle. Spawn one sub-agent per branch.
- Step 1: Write failing tests for
formatInFlightForPrompt(renders each state) andbuildCoordinatorTask(multi-line body lands inmetadata.context, id prefix,useWorktree:false). - Step 2: Run vitest → FAIL.
- Step 3: Implement template + both functions.
- Step 4: Run → PASS.
- Step 5: Commit
feat: add branch reconcile coordinator prompt + task builder.
Task 5: Scheduler + run handler + dispatch
Files:
- Modify:
server/services/branchReconcileScheduler.js - Test:
server/services/branchReconcileScheduler.test.js
Interfaces:
Consumes:
eventScheduler(schedule,cancel),settings.jsgetSettings,branchReconcile.reconcile,cosTaskStore.addTask,cosState/cos.jsrunning check,timezone.Produces:
runBranchReconcile() => summary— re-reads settings; if!enabledreturn{ skipped:'disabled' }; resolverepoPath(PortOS root) +defaultBranch; callreconcile(); gate cleanup onactions.cleanupMerged; ifinFlight.lengthand any agent-action enabled and CoS is running,addTask(buildCoordinatorTask(...), 'internal', { raw:true }); return{ cleaned, inFlight, wip, skipped, dispatched }. Logs single-line emoji summary. Wrapped in try/catch (non-request boundary).startBranchReconcileScheduler()/stopBranchReconcileScheduler()— register/cancel cron idbranch-reconcile, handler =runBranchReconcile.
Step 1: Write failing tests: disabled →
{skipped:'disabled'}and no reconcile call; enabled + inFlight + CoS running →addTaskcalled once;actions.cleanupMerged:false→ cleanup skipped. Mockreconcile,getSettings,addTask, CoS-running.Step 2: Run vitest → FAIL.
Step 3: Implement the handler + start/stop.
Step 4: Run → PASS.
Step 5: Commit
feat: add branch reconcile scheduler + dispatch.
Task 6: API route + boot registration + barrel/README
Files:
- Create:
server/routes/branchReconcile.js(POST /run,GET /status) - Modify:
server/index.js(mount route +startBranchReconcileScheduler().catch(...)) - Modify: route registry (wherever routes are mounted, e.g.
server/routes/index.jsorserver/index.js) - Modify:
server/services/README.md(rows for the two new services) — no barrel forservices/(it uses README only per convention); confirm. - Test:
server/routes/branchReconcile.test.js
Interfaces:
POST /api/branch-reconcile/run→runBranchReconcile(); returns the summary JSON.GET /api/branch-reconcile/status→ last-run summary (module-levellastRun).Step 1: Write failing test hitting
POST /run(mockrunBranchReconcile) asserting it returns the summary.Step 2: Run vitest → FAIL.
Step 3: Implement route, mount it, add boot registration, add README rows.
Step 4: Run full server suite
cd server && npm test→ PASS.Step 5: Commit
feat: add branch reconcile API + boot registration.
Task 7: Settings UI toggle + Run Now
Files:
- Modify: existing CoS scheduling settings component (locate via nav; add a "Branch & PR Reconciler" section)
- Create:
client/src/services/wrapper method or reuseapi.jsforPOST /api/branch-reconcile/run - Test: client vitest for the new section (toggle renders, Run Now gated on saved-enabled)
Interfaces: toggle (enabled), cron input, four action checkboxes, "Run now" button gated on saved enabled (per "Run Now gates on saved state" rule) + disabled while dirty/saving. Shows last-run summary.
- Step 1: Write failing client test (Run Now disabled when form dirty).
- Step 2: Run
cd client && npx vitest run <file>→ FAIL. - Step 3: Implement section + api wrapper.
- Step 4: Run client tests → PASS. Build check
cd client && npm run build. - Step 5: Commit
feat: add branch reconciler settings UI.
Self-Review
- Spec coverage: ownership (Task 1–2 local-branch enumeration), deterministic cleanup (Task 2), classifier states (Task 1), coordinator+sub-agents (Task 4–5), auto-merge gate (Task 4 prompt), settings/opt-in/default (Task 3), manual run (Task 6), daily cron (Task 3 default + Task 5), UI (Task 7), distribution seed (Task 3). ✓
- Placeholders: none — signatures + classification table + default JSON are concrete.
- Type consistency:
reconcilereturns{ cleaned, inFlight, wip, skipped }used identically by Task 5;buildCoordinatorTaskconsumesinFlightshape from Task 2;classifyBranchstates reused verbatim in Task 4 prompt. - Deferred (append to PLAN.md if skipped): per-branch in-flight re-entrancy marker across daily runs (rely on CoS
taskConflictworkspace collision initially);GET /statuspersistence across restart (module-level only).