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(cos): continue bounded manual branch sweeps (#12002)
Adam Eivy95bbf46203fa
diff --git a/server/services/branchReconcile.js b/server/services/branchReconcile.jsindex 80ea66245f..359363b25b 100644--- a/server/services/branchReconcile.js+++ b/server/services/branchReconcile.js@@ -1058,6 +1058,12 @@ export function describeIdleReconcilePark(skipped = [], heldLive = [], { wip = [: heldBackMerged.length === entries.length ? 'merged-branches-held-back' : 'branches-blocked';return {reason, heldBackMerged, counts, skipCauses: entries.length ? skipCauses : null,+ remaining: entries.map(entry => ({+ ...(entry.branch && { branch: entry.branch }),+ ...((entry.path || entry.worktreePath) && { path: entry.path || entry.worktreePath }),+ ...(entry.issue && { issue: entry.issue }),+ reason: entry.cause,+ })),notLaterThan: lifts.length ? new Date(Math.min(...lifts)).toISOString() : null};}@@ -1145,7 +1151,7 @@ export async function cleanupMerged(repoPath, defaultBranch, merged, { activeAge});if (!retired.ok) {const { ok, ...failure } = retired;- skipped.push({ branch: b.branch, ...failure });+ skipped.push({ branch: b.branch, ...(b.worktreePath && { path: b.worktreePath }), ...failure });continue;}const markerHold = await retainedClaimMarkers(repoPath, b.branch, { origin });diff --git a/server/services/branchReconcile.test.js b/server/services/branchReconcile.test.jsindex 3664d0e7e7..1042c30a40 100644--- a/server/services/branchReconcile.test.js+++ b/server/services/branchReconcile.test.js@@ -371,7 +371,7 @@ describe('cleanupMerged', () => {execGit.mockResolvedValue({ stdout: '', exitCode: 0 });const res = await cleanupMerged('/repo', 'main', [{ branch: 'next/issue-2190', worktreePath }]);expect(res.cleaned).toEqual([]);- expect(res.skipped).toEqual([{ branch: 'next/issue-2190', reason: 'worktree-unmanaged-location' }]);+ expect(res.skipped).toEqual([{ branch: 'next/issue-2190', path: worktreePath, reason: 'worktree-unmanaged-location' }]);expect(wt.forceRemoveWorktreeDir).not.toHaveBeenCalled();expect(git.deleteBranch).not.toHaveBeenCalled();});@@ -398,7 +398,7 @@ describe('cleanupMerged', () => {execGit.mockResolvedValue({ stdout: ' M server/index.js', exitCode: 0 }); // dirtyconst res = await cleanupMerged('/repo', 'main', [{ branch: 'next/issue-2196', worktreePath: '/repo/.claude/worktrees/next-2196' }]);expect(res.cleaned).toEqual([]);- expect(res.skipped).toEqual([{ branch: 'next/issue-2196', reason: 'worktree-dirty' }]);+ expect(res.skipped).toEqual([{ branch: 'next/issue-2196', path: '/repo/.claude/worktrees/next-2196', reason: 'worktree-dirty' }]);expect(wt.forceRemoveWorktreeDir).not.toHaveBeenCalled();expect(git.deleteBranch).not.toHaveBeenCalled();});@@ -420,9 +420,9 @@ describe('cleanupMerged', () => {], { activeAgentIds, claimOwners: { agents: [] } });expect(res.cleaned).toEqual([]);expect(res.skipped).toEqual([- { branch: 'locked-b', reason: 'worktree-locked' },- { branch: 'claim-b', reason: 'worktree-human-claim' },- { branch: 'active-b', reason: 'worktree-active-agent' }+ { branch: 'locked-b', path: '/repo/data/cos/worktrees/locked', reason: 'worktree-locked' },+ { branch: 'claim-b', path: '/repo/data/cos/worktrees/claim-foo', reason: 'worktree-human-claim' },+ { branch: 'active-b', path: '/repo/data/cos/worktrees/agent-abc12345', reason: 'worktree-active-agent' }]);expect(wt.forceRemoveWorktreeDir).not.toHaveBeenCalled();expect(git.deleteBranch).not.toHaveBeenCalled();@@ -525,7 +525,7 @@ describe('cleanupMerged', () => {execGit.mockResolvedValue({ stdout: ' M server/index.js', exitCode: 0 }); // dirtyconst dirty = await cleanupMerged('/repo', 'main', [{ branch: 'claim/issue-1', ...shipped() }], { claimOwners: { agents: [] } });expect(dirty.cleaned).toEqual([]);- expect(dirty.skipped).toEqual([{ branch: 'claim/issue-1', reason: 'worktree-dirty' }]);+ expect(dirty.skipped).toEqual([{ branch: 'claim/issue-1', path: '/repo/data/cos/worktrees/claim-x', reason: 'worktree-dirty' }]);execGit.mockResolvedValue({ stdout: '', exitCode: 0 }); // clean// No completion proof — never pushed, remote ref still on origin, or the@@ -556,8 +556,8 @@ describe('cleanupMerged', () => {// A lock and a running agent both end at a time nothing here can predict —// inventing one would park the drain past a hold that may already be gone.expect(res.skipped).toEqual([- { branch: 'locked-b', reason: 'worktree-locked' },- { branch: 'active-b', reason: 'worktree-active-agent' }+ { branch: 'locked-b', path: '/repo/data/cos/worktrees/locked', reason: 'worktree-locked' },+ { branch: 'active-b', path: '/repo/data/cos/worktrees/agent-abc12345', reason: 'worktree-active-agent' }]);});});@@ -1409,7 +1409,7 @@ describe('reconcile', () => {const result = await cleanupMerged('/repo', 'main', [{branch: 'claim/issue-101', worktreePath: '/repo/data/cos/worktrees/claim-issue-101', worktreeAgeMs: 0,}], { activeAgentIds: new Set(), claimOwners: { agents: [], readAgents: async () => [] } });- expect(result.skipped).toEqual([{ branch: 'claim/issue-101', reason: 'worktree-dirty' }]);+ expect(result.skipped).toEqual([{ branch: 'claim/issue-101', path: '/repo/data/cos/worktrees/claim-issue-101', reason: 'worktree-dirty' }]);expect(wt.forceRemoveWorktreeDir).not.toHaveBeenCalled();expect(git.deleteBranch).not.toHaveBeenCalled();});@@ -1655,7 +1655,7 @@ describe('reconcile — cached SUPERSEDED verdicts (#3842)', () => {branch: 'next/issue-2190', worktreePath: '/private/tmp/portos-next-2190', worktreeAgeMs: 30 * 24 * 60 * 60 * 1000}]);expect(res.cleaned).toEqual([]);- expect(res.skipped).toEqual([{ branch: 'next/issue-2190', reason: 'worktree-unmanaged-location' }]);+ expect(res.skipped).toEqual([{ branch: 'next/issue-2190', path: '/private/tmp/portos-next-2190', reason: 'worktree-unmanaged-location' }]);expect(wt.forceRemoveWorktreeDir).not.toHaveBeenCalled();});@@ -2634,7 +2634,7 @@ describe('describeIdleReconcilePark', () => {it('reports a quiet repo when nothing is held', () => {expect(describeIdleReconcilePark([], [])).toEqual({- reason: 'no-in-flight-branches', heldBackMerged: [], counts: null, skipCauses: null, notLaterThan: null+ reason: 'no-in-flight-branches', heldBackMerged: [], counts: null, skipCauses: null, remaining: [], notLaterThan: null});expect(describeIdleReconcilePark()).toMatchObject({ reason: 'no-in-flight-branches' });});@@ -2668,7 +2668,7 @@ describe('describeIdleReconcilePark', () => {{ branch: 'b', reason: 'not-merged-on-recheck' },{ branch: 'c', reason: 'delete-failed: boom' }], []);- expect(out).toEqual({ reason: 'branches-blocked', heldBackMerged: [], counts: { heldBranches: 2, heldDetached: 0 }, skipCauses: { 'cleanup-disabled': 1, 'branch-delete-failed': 1 }, notLaterThan: null });+ expect(out).toEqual({ reason: 'branches-blocked', heldBackMerged: [], counts: { heldBranches: 2, heldDetached: 0 }, skipCauses: { 'cleanup-disabled': 1, 'branch-delete-failed': 1 }, remaining: [{ branch: 'a', reason: 'cleanup-disabled' }, { branch: 'c', reason: 'branch-delete-failed' }], notLaterThan: null });});it('ignores an unreadable retryAt rather than parking on NaN', () => {diff --git a/server/services/branchReconcileSweep.test.js b/server/services/branchReconcileSweep.test.jsnew file mode 100644index 0000000000..a8f11227ef--- /dev/null+++ b/server/services/branchReconcileSweep.test.js@@ -0,0 +1,97 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++// The schedule adapter serializes each write and returns fresh objects on read,+// matching persistence rather than sharing an in-memory execution record.+const persisted = vi.hoisted(() => ({ json: '' }));+vi.mock('./taskScheduleStore.js', () => ({+ loadSchedule: async () => JSON.parse(persisted.json),+ updateSchedule: async (mutate) => {+ const schedule = JSON.parse(persisted.json);+ const { result, changed } = await mutate(schedule);+ if (changed) persisted.json = JSON.stringify(schedule);+ return result;+ },+}));+vi.mock('./branchReconcile.js', async (importOriginal) => ({+ ...await importOriginal(),+ reconcile: vi.fn(),+ formatInFlightForPrompt: vi.fn(async (branches) => branches.map(b => b.branch).join(',')),+}));+vi.mock('./cosClaimOwnership.js', () => ({ readClaimOwnershipAgents: vi.fn(async () => []) }));+vi.mock('./agentState.js', async (importOriginal) => ({ ...await importOriginal(), getActiveAgentIds: () => new Set() }));+vi.mock('./userTimezone.js', () => ({ getUserTimezone: async () => 'UTC' }));++import { reconcile } from './branchReconcile.js';+import { readClaimOwnershipAgents } from './cosClaimOwnership.js';+import { applyPerpetualDrainCap, resolveBranchReconcileBlock } from './cosTaskPreStepBlocks.js';+import * as schedule from './taskSchedule.js';+import { perpetualRefillPlan } from './cos.js';++const app = { id: 'example-app', name: 'Example App', repoPath: '/example/repo' };+const branch = name => ({ branch: name, state: 'NEEDS_PR', head: name });+const scan = (inFlight = [], extra = {}) => ({ defaultBranch: 'main', inFlight, cleaned: [], skipped: [], wip: [], ...extra });+const outcome = () => JSON.parse(persisted.json).executions['task:branch-reconcile'].perApp[app.id].branchReconcileOutcome;+const run = () => resolveBranchReconcileBlock(app, 'branch-reconcile', { branchesPerAgent: 2 }, schedule);++beforeEach(() => {+ vi.clearAllMocks();+ readClaimOwnershipAgents.mockResolvedValue([]);+ persisted.json = JSON.stringify({ tasks: { 'branch-reconcile': { enabled: true, perpetual: false, type: 'on-demand', drainDispatchCap: 5 } }, executions: {} });+});++describe('manual branch reconciliation sweep', () => {+ it('processes multiple batches and performs the final scan with perpetual disabled', async () => {+ reconcile.mockResolvedValueOnce(scan(['a', 'b', 'c'].map(branch)))+ .mockResolvedValueOnce(scan([branch('c')], { cleaned: ['a', 'b'] }))+ .mockResolvedValueOnce(scan([], { cleaned: ['c'] }));+ expect(await run()).toMatchObject({ skip: false, block: 'a,b' });+ expect(perpetualRefillPlan({ metadata: { taskAnalysisType: 'branch-reconcile', taskOnDemand: true, taskApp: app.id } }, await schedule.loadSchedule()))+ .toEqual({ lane: 'onDemand', taskType: 'branch-reconcile', appId: app.id });+ expect(await run()).toMatchObject({ skip: false, block: 'c' });+ expect(await run()).toEqual({ skip: true });+ expect(outcome()).toMatchObject({ status: 'drained', remaining: [], nextAction: null });+ expect((await schedule.loadSchedule()).tasks['branch-reconcile'].perpetual).toBe(false);+ expect(await schedule.isPerpetualParkActive('branch-reconcile', app.id)).toBe(true);+ });++ it('attempts later batches before declaring unchanged branches incomplete', async () => {+ reconcile.mockResolvedValue(scan(['a', 'b', 'c'].map(branch)));+ expect(await run()).toMatchObject({ block: 'a,b' });+ expect(await run()).toMatchObject({ block: 'c' });+ expect(await run()).toEqual({ skip: true });+ expect(outcome()).toMatchObject({ status: 'incomplete', reason: 'no-progress', remaining: ['a', 'b', 'c'].map(branch => ({ branch, reason: 'actionable' })) });+ });++ it('applies the saved dispatch budget to a non-perpetual manual sweep and retains its last inventory', async () => {+ reconcile.mockResolvedValue(scan([branch('a')]));+ await run();+ const cap = await applyPerpetualDrainCap(app, 'branch-reconcile', { perpetual: false, drainDispatchCap: 1 }, schedule, { onDemand: true });+ expect(cap).toEqual({ skip: true });+ expect(outcome()).toMatchObject({ status: 'incomplete', reason: 'drain-cap', remaining: [{ branch: 'a', reason: 'actionable' }] });+ });++ it('retains checkout and gated-action holds instead of reporting a drained repository', async () => {+ reconcile.mockResolvedValue(scan([], {+ skipped: [{ branch: 'merged-held', path: '/example/held', reason: 'worktree-locked' }],+ detachedWorktrees: [{ path: '/example/detached', reason: 'worktree-unmanaged-location' }],+ wip: [{ branch: 'manual', liveOwnerReason: 'manual-handoff' }],+ }));+ expect(await run()).toEqual({ skip: true });+ expect(outcome()).toMatchObject({ status: 'incomplete', remaining: [+ { branch: 'merged-held', path: '/example/held', reason: 'worktree-locked' },+ { path: '/example/detached', reason: 'worktree-unmanaged-location' },+ { branch: 'manual', reason: 'manual-handoff' },+ ] });+ });++ it('preserves the last observation across a failed scan or unavailable ownership state', async () => {+ reconcile.mockResolvedValueOnce(scan([branch('a')])).mockRejectedValueOnce(new Error('git unavailable'));+ await run();+ const observedAt = outcome().observedAt;+ expect(await run()).toEqual({ skip: true });+ expect(outcome()).toMatchObject({ status: 'incomplete', reason: 'scan-unavailable', observedAt, remaining: [{ branch: 'a', reason: 'actionable' }] });+ readClaimOwnershipAgents.mockRejectedValueOnce(new Error('registry unavailable'));+ expect(await run()).toEqual({ skip: true });+ expect(outcome()).toMatchObject({ status: 'incomplete', reason: 'owner-state-unavailable' });+ });+});diff --git a/server/services/cos.js b/server/services/cos.jsindex 312f3eeb36..ff828a078f 100644--- a/server/services/cos.js+++ b/server/services/cos.js@@ -1372,10 +1372,10 @@ export function isPerpetualRefillCandidate(agent, schedule) {const analysisType = agentScheduledType(agent);if (!analysisType) return false;const taskDef = schedule?.tasks?.[analysisType];- // `perpetual` is the single drain signal, orthogonal to the cadence type: an- // on-demand or cron task carrying the flag re-queues on completion, and one- // without it stays a single-run action.- return Boolean(taskDef?.enabled) && taskDef?.perpetual === true;+ // A manual branch reconciliation owns a bounded sweep even when recurring+ // dispatch is off. Other task types retain their saved perpetual policy.+ const manualBranchSweep = analysisType === 'branch-reconcile' && agent?.metadata?.taskOnDemand;+ return Boolean(taskDef?.enabled) && (taskDef?.perpetual === true || Boolean(manualBranchSweep));}/**@@ -1437,7 +1437,15 @@ export function perpetualRefillPlan(agent, schedule) {* converges and can't fan out past the `dequeueNextTask` concurrency limits).*/async function refillPerpetualForCompletedAgent(agent) {- if (!isDaemonRunning()) return;+ const branchSweep = agentScheduledType(agent) === 'branch-reconcile';+ const interrupted = async (reason) => {+ if (!branchSweep) return;+ const { recordBranchReconcileOutcome } = await import('./taskSchedule.js');+ await recordBranchReconcileOutcome(agent?.metadata?.taskApp || null, {+ status: 'incomplete', reason, nextAction: 'Resolve the interruption, then run branch reconciliation again.',+ });+ };+ if (!isDaemonRunning()) return interrupted('daemon-stopped');// Only a SUCCESSFUL perpetual run drains to the next item immediately. A failed// run (provider/setup/test failure) must NOT refill back-to-back: perpetual// completions skip the per-app cooldown, and the work-detector will usually@@ -1445,13 +1453,13 @@ async function refillPerpetualForCompletedAgent(agent) {// the daemon through repeated failures. On failure, fall back to the task// retry/backoff and the improvement-check recheck cadence (the park IS the// throttle only once work genuinely runs out, not when a run errors).- if (!agent?.result?.success) return;- if (await isPaused()) return;+ if (!agent?.result?.success) return interrupted('agent-failed');+ if (await isPaused()) return interrupted('daemon-paused');const taskScheduleMod = await import('./taskSchedule.js');const schedule = await taskScheduleMod.loadSchedule();const plan = perpetualRefillPlan(agent, schedule);- if (plan.lane === 'skip') return;+ if (plan.lane === 'skip') return interrupted('continuation-disabled');const state = await loadState();@@ -1462,7 +1470,7 @@ async function refillPerpetualForCompletedAgent(agent) {// dequeueNextTask({ ignoreTaskId }) drains the re-issue dedup-safely against// the still-`in_progress` completing task; letting triggerOnDemandTask emit// its event would fire a redundant SECOND dequeue of the same request.- if (!isImprovementEnabled(state)) return;+ if (!isImprovementEnabled(state)) return interrupted('improvement-disabled');// A PARK STOPS A REFILL (#3848). Park elapse used to be read only by the// SCHEDULED lane, so a drain that parked itself (idle detector, no-progress,// drain cap) could still be re-issued here on the very next completion — the@@ -1479,12 +1487,16 @@ async function refillPerpetualForCompletedAgent(agent) {// the drain engines must leave the park, convergence signature, and dispatch// counter intact. Without that pair the refill wipes its own convergence// state on every hop and the drain never parks while one actionable item remains.- await taskScheduleMod.queuePerpetualRefill(plan.taskType, plan.appId);+ const refill = await taskScheduleMod.queuePerpetualRefill(plan.taskType, plan.appId).catch(async (err) => {+ await interrupted('refill-failed');+ throw err;+ });+ if (refill?.error) await interrupted('refill-rejected');return;}// Scheduled drain — autonomous queue path, gated on CoS auto-run.- if (!canQueueImprovementTasks(state)) return;+ if (!canQueueImprovementTasks(state)) return interrupted('automatic-dispatch-disabled');// `agent:completed` fires from `completeAgent` BEFORE the completion flow's// `updateTask` marks this agent's task done (agentLifecycle.js: completeAgentdiff --git a/server/services/cos.test.js b/server/services/cos.test.jsindex fc98dde6e9..08877a481b 100644--- a/server/services/cos.test.js+++ b/server/services/cos.test.js@@ -2280,6 +2280,21 @@ describe('perpetualRefillPlan — manual vs scheduled drain lane', () => {)).toEqual({ lane: 'onDemand', taskType: 'branch-reconcile', appId: 'app-1' });});+ it('continues a manual branch sweep with recurring dispatch disabled, preserving other one-shot boundaries', () => {+ const oneShot = { tasks: { 'branch-reconcile': { enabled: true, perpetual: false } } };+ const manual = { taskAnalysisType: 'branch-reconcile', taskOnDemand: true, taskApp: 'app-1' };+ for (const taskOnDemandOrigin of ['user', 'refill']) {+ expect(perpetualRefillPlan(agent({ ...manual, taskOnDemandOrigin }), oneShot))+ .toEqual({ lane: 'onDemand', taskType: 'branch-reconcile', appId: 'app-1' });+ }+ for (const taskOnDemandOrigin of ['quota-burn', 'unknown']) {+ expect(perpetualRefillPlan(agent({ ...manual, taskOnDemandOrigin }), oneShot)).toEqual({ lane: 'skip' });+ }+ expect(perpetualRefillPlan(agent({ ...manual, taskOnDemand: false }), oneShot)).toEqual({ lane: 'skip' });+ oneShot.tasks['branch-reconcile'].enabled = false;+ expect(perpetualRefillPlan(agent(manual), oneShot)).toEqual({ lane: 'skip' });+ });+it('a manual run with no app resolves appId to null (global on-demand re-issue)', () => {expect(perpetualRefillPlan(agent({ taskAnalysisType: 'claim-issue', taskOnDemand: true }),@@ -2369,7 +2384,7 @@ describe('cos.js source — agent:completed triggers perpetual refill', () => {// drain stalls. Pin the ignoreTaskId thread so a refactor can't reintroduce it.const fnIdx = COS_SRC.indexOf('async function refillPerpetualForCompletedAgent');expect(fnIdx, 'refillPerpetualForCompletedAgent must exist').toBeGreaterThan(-1);- const fnSlice = COS_SRC.slice(fnIdx, fnIdx + 4600);+ const fnSlice = COS_SRC.slice(fnIdx, COS_SRC.indexOf('\n}', fnIdx));expect(/queueEligibleImprovementTasks\(\s*state\s*,\s*cosTaskData\s*,\s*\{[\s\S]*?ignoreTaskId:\s*agent\?\.taskId[\s\S]*?\}\s*\)/.test(fnSlice),'refill must forward ignoreTaskId: agent?.taskId to queueEligibleImprovementTasks'@@ -2387,14 +2402,14 @@ describe('cos.js source — agent:completed triggers perpetual refill', () => {// absence of a keyword in a comment (fragile): Improve gate → re-issue →// return → (only then) the queue lane.const fnIdx = COS_SRC.indexOf('async function refillPerpetualForCompletedAgent');- const fnSlice = COS_SRC.slice(fnIdx, fnIdx + 4600);+ const fnSlice = COS_SRC.slice(fnIdx, COS_SRC.indexOf('\n}', fnIdx));expect(fnSlice.includes('const plan = perpetualRefillPlan(agent, schedule)'),'refill must resolve the lane via perpetualRefillPlan').toBe(true);expect(/plan\.lane === 'onDemand'/.test(fnSlice), 'refill must branch on the on-demand lane').toBe(true);- const improveGateIdx = fnSlice.indexOf('if (!isImprovementEnabled(state)) return;');+ const improveGateIdx = fnSlice.indexOf("if (!isImprovementEnabled(state)) return interrupted('improvement-disabled');");// queuePerpetualRefill owns the non-emitting REFILL pair so the on-demand// engines do NOT clear the drain's park / convergence signature / dispatch// counter on its behalf.@@ -2435,7 +2450,7 @@ describe('cos.js source — agent:completed triggers perpetual refill', () => {// unaffected: applyOnDemandRunResets clears the park for a USER-origin request// before this lane is ever reached.const fnIdx = COS_SRC.indexOf('async function refillPerpetualForCompletedAgent');- const fnSlice = COS_SRC.slice(fnIdx, fnIdx + 4600);+ const fnSlice = COS_SRC.slice(fnIdx, COS_SRC.indexOf('\n}', fnIdx));const parkIdx = fnSlice.indexOf('isPerpetualParkActive(plan.taskType, plan.appId)');const triggerIdx = fnSlice.indexOf('queuePerpetualRefill(plan.taskType, plan.appId');expect(parkIdx, 'refill must consult the type+app park before re-issuing').toBeGreaterThan(-1);@@ -2450,7 +2465,7 @@ describe('cos.js source — agent:completed triggers perpetual refill', () => {// sees the same issue as actionable). The refill must bail on a non-success// result and let task-retry/backoff + the recheck cadence handle failures.const fnIdx = COS_SRC.indexOf('async function refillPerpetualForCompletedAgent');- const fnSlice = COS_SRC.slice(fnIdx, fnIdx + 4600);+ const fnSlice = COS_SRC.slice(fnIdx, COS_SRC.indexOf('\n}', fnIdx));expect(/if\s*\(\s*!agent\?\.result\?\.success\s*\)\s*return/.test(fnSlice),'refill must early-return when the completed agent did not succeed'diff --git a/server/services/cosTaskGenerator.js b/server/services/cosTaskGenerator.jsindex b0ed39b577..82228b8445 100644--- a/server/services/cosTaskGenerator.js+++ b/server/services/cosTaskGenerator.js@@ -2021,7 +2021,7 @@ export async function drainProgrammaticOnDemandRequests({ taskScheduleMod, requeexport async function emitOnDemandEmpty({ taskScheduleMod, request, targetApp, taskConfig, preflightCardId = null, skip = null }) {const appId = targetApp?.id || null;const parkInfo = await taskScheduleMod.getPerpetualParkInfo(request.taskType, appId).catch(() => null);- const isDetectorDriven = taskConfig?.perpetual === true;+ const isDetectorDriven = taskConfig?.perpetual === true || request.taskType === 'branch-reconcile';const outcome = parkInfo ? 'parked' : (isDetectorDriven ? 'transient' : 'idle');// Layered Intelligence skips (e.g. a provider that can't drive an agent) record@@ -2631,7 +2631,7 @@ export async function prepareManagedAppImprovementTask(taskType, app, state, {// Consecutive-dispatch cap for EVERY perpetual drain (#3848). First, because a// capped drain must not pay for a work-detector probe or a reconcile git/gh scan// it is only going to discard.- const drainCap = await applyPerpetualDrainCap(app, taskType, interval, taskSchedule);+ const drainCap = await applyPerpetualDrainCap(app, taskType, interval, taskSchedule, { onDemand });if (drainCap.skip) return skipped('drain-cap');// Perpetual (drain-until-done) gate — probes for actionable work beforediff --git a/server/services/cosTaskGenerator.test.js b/server/services/cosTaskGenerator.test.jsindex 55f5f24db1..08bcdf7457 100644--- a/server/services/cosTaskGenerator.test.js+++ b/server/services/cosTaskGenerator.test.js@@ -1141,7 +1141,7 @@ describe('emitOnDemandEmpty', () => {ghHealth.mockResolvedValue({ status: 'ok', ok: true, detail: null, remedy: null });});- const emitTransient = async (taskType, skip = null) => {+ const emitTransient = async (taskType, skip = null, perpetual = true) => {const events = [];const handler = (d) => events.push(d);cosEvents.on('schedule:on-demand-empty', handler);@@ -1150,7 +1150,7 @@ describe('emitOnDemandEmpty', () => {taskScheduleMod: stubMod,request: { id: 'req-2', taskType },targetApp: { id: 'app-1', name: 'App One' },- taskConfig: { type: 'on-demand', perpetual: true },+ taskConfig: { type: 'on-demand', perpetual },skip});} finally {@@ -1167,7 +1167,7 @@ describe('emitOnDemandEmpty', () => {it('treats a perpetual on-demand drain as detector-driven for transient feedback', async () => {const skip = { gate: 'perpetual-work', cli: null, reason: 'probe-failed', remedy: null, detail: null };- expect(await emitTransient('branch-reconcile', skip)).toMatchObject({ taskType: 'branch-reconcile', outcome: 'transient', forge: null });+ expect(await emitTransient('branch-reconcile', skip, false)).toMatchObject({ taskType: 'branch-reconcile', outcome: 'transient', forge: null });});it('skips the gh probe for a non-gh transient verdict, so a glab/git fault never toasts a gh remedy', async () => {@@ -1701,7 +1701,7 @@ describe('the drain cap has exactly one implementation, at the choke point', ()it('prepareManagedAppImprovementTask applies it ahead of the work gate and the reconcile scans', () => {const start = GEN_SRC.indexOf('export async function prepareManagedAppImprovementTask');const body = GEN_SRC.slice(start, GEN_SRC.indexOf('return { task, pendingPerpetualDispatch, skip: null };', start));- const capIdx = body.indexOf('applyPerpetualDrainCap(app, taskType, interval, taskSchedule)');+ const capIdx = body.indexOf('applyPerpetualDrainCap(app, taskType, interval, taskSchedule, { onDemand })');expect(capIdx, 'the choke point must apply the per-type drain cap').toBeGreaterThan(-1);expect(capIdx).toBeLessThan(body.indexOf('applyPerpetualWorkGate('));expect(capIdx).toBeLessThan(body.indexOf('resolveBranchReconcileBlock('));diff --git a/server/services/cosTaskPreStepBlocks.js b/server/services/cosTaskPreStepBlocks.jsindex 7392a8181c..35be7a347a 100644--- a/server/services/cosTaskPreStepBlocks.js+++ b/server/services/cosTaskPreStepBlocks.js@@ -330,8 +330,8 @@ export function buildPlanConstraintBlock(planId) {** @returns {Promise<{skip:boolean}>}*/-export async function applyPerpetualDrainCap(app, taskType, interval, taskSchedule) {- if (interval.perpetual !== true) return { skip: false };+export async function applyPerpetualDrainCap(app, taskType, interval, taskSchedule, { onDemand = false } = {}) {+ if (interval.perpetual !== true && !(onDemand && taskType === 'branch-reconcile')) return { skip: false };// Coerce before validating: this key is not on the schedule route's allowlist, so// the only way it arrives non-numeric is a hand-edited schedule.json, where `"5"`// is the likeliest shape and reading it as "no cap" would silently unbound the@@ -460,11 +460,15 @@ export async function resolveBranchReconcileBlock(app, taskType, metadata, taskS});// A failed scan is treated as transient (git/gh blip) — skip WITHOUT parking// so the next tick retries instead of waiting out a full recheck cadence.- if (!result) return { skip: true };+ if (!result) {+ await taskSchedule.recordBranchReconcileOutcome(app.id, { status: 'incomplete', reason: 'scan-unavailable', nextAction: 'Restore repository access and run branch reconciliation again.' });+ return { skip: true };+ }// Same treatment for a cycle the reconciler skipped because `gh` was unreadable// (#3358): its empty in-flight set is "we could not ask", not "nothing to do",// so parking on it would sit out a full recheck cadence over a network blip.if (result.forgeUnavailable) {+ await taskSchedule.recordBranchReconcileOutcome(app.id, { status: 'incomplete', reason: 'forge-unavailable', nextAction: 'Restore forge access and run branch reconciliation again.' });emitLog('info', `🔀 branch-reconcile skipped for ${app.name}: forge unreachable (gh ${result.forgeStatus || 'error'})`, { appId: app.id, analysisType: taskType });return { skip: true };}@@ -474,6 +478,7 @@ export async function resolveBranchReconcileBlock(app, taskType, metadata, taskS// rather than parking. Merged branches were still cleaned (git truth), so log// that before bailing.if (result.prStateUnavailable) {+ await taskSchedule.recordBranchReconcileOutcome(app.id, { status: 'incomplete', reason: 'pr-state-unavailable', nextAction: 'Restore pull request access and run branch reconciliation again.' });emitLog('info', `🔀 branch-reconcile deferred for ${app.name}: PR state unreadable this cycle (cleaned ${result.cleaned.length} merged branch(es))`, { appId: app.id, analysisType: taskType });return { skip: true };}@@ -506,6 +511,17 @@ export async function resolveBranchReconcileBlock(app, taskType, metadata, taskSclaimMarkersHeld: result.claimMarkersHeld,gated: result.inFlight.filter(branch => !allActionable.includes(branch))});+ // This is a scan outcome, not the coordinator's success flag or a claim that+ // the repository contains only its default branch. Keep known holds visible.+ await taskSchedule.recordBranchReconcileOutcome(app.id, {+ status: 'running', reason: 'batch-pending',+ remaining: [+ ...idlePark.remaining,+ ...allActionable.map(b => ({ branch: b.branch, reason: 'actionable' })),+ ...(result.orphanRemotes?.reported || []).map(b => ({ branch: typeof b === 'string' ? b : b.branch, reason: 'remote-branch-held' })),+ ],+ nextAction: 'Continue the bounded sweep and rescan after the final batch.',+ });if (allActionable.length === 0) {// Definitive idle: nothing in-flight to drive. Park on the recheck cadence,// clearing the progress signature so a fresh set later dispatches and zeroing thediff --git a/server/services/onDemandDrain.js b/server/services/onDemandDrain.jsindex 184a4caad5..2d45919cba 100644--- a/server/services/onDemandDrain.js+++ b/server/services/onDemandDrain.js@@ -252,6 +252,7 @@ export async function drainOnDemandRequests(ctx, adapter) {await taskScheduleMod.recordExecution(`task:${request.taskType}`, targetApp.id);const prepared = await prepareManagedAppImprovementTask(request.taskType, targetApp, state, {skipPreconditions: true,+ onDemand: true,targetPullRequest: request.targetPullRequest ?? null,// The deterministic pre-agent work reports into the user's card as it// runs — this is the whole reason the card exists early.diff --git a/server/services/onDemandDrain.test.js b/server/services/onDemandDrain.test.jsindex b25a726f6f..c620752215 100644--- a/server/services/onDemandDrain.test.js+++ b/server/services/onDemandDrain.test.js@@ -381,6 +381,7 @@ describe.each(ENGINES)('%s — deferred perpetual-dispatch recording', (_name, mconst { adapter } = makeAdapter();await drainOnDemandRequests({ state: STATE }, adapter);+ expect(mocks.prepareManagedAppImprovementTask).toHaveBeenCalledWith('code-quality', APP, STATE, expect.objectContaining({ onDemand: true }));expect(mocks.recordDeferredPerpetualDispatch).toHaveBeenCalledWith({ taskType: 'code-quality', appId: 'acme', signature: 'sig-1' },expect.anything()diff --git a/server/services/taskSchedule.js b/server/services/taskSchedule.jsindex 96a0e152e9..0a01b74e95 100644--- a/server/services/taskSchedule.js+++ b/server/services/taskSchedule.js@@ -563,6 +563,17 @@ export async function parkPerpetual(taskType, appId = null, { reason = null, actconst record = ensureExecutionRecord(schedule, taskType, appId);record.parkedUntil = parkedUntil;record.parkReason = reason;+ if (taskType === 'branch-reconcile') {+ const remaining = record.branchReconcileOutcome?.remaining ?? null;+ const drained = reason === 'no-in-flight-branches' && remaining?.length === 0;+ record.branchReconcileOutcome = {+ ...record.branchReconcileOutcome,+ status: drained ? 'drained' : 'incomplete', reason, remaining,+ observedAt: record.branchReconcileOutcome?.observedAt ?? null,+ updatedAt: new Date().toISOString(),+ nextAction: drained ? null : 'Resolve the listed holds or dispatch limit, then run branch reconciliation again.',+ };+ }if (branchAttempts === null) delete record.branchReconcileAttempts;else if (branchAttempts !== undefined) record.branchReconcileAttempts = branchAttempts;record.parkActionableCount = actionableCount;@@ -708,6 +719,20 @@ export async function isPerpetualParkActive(taskType, appId = null) {return parkedUntilMs(record) > Date.now();}+/** Persist the latest deterministic sweep evidence independently of agent success. */+export async function recordBranchReconcileOutcome(appId, { status, reason, remaining, nextAction }) {+ return updateSchedule(async (schedule) => {+ const record = ensureExecutionRecord(schedule, 'branch-reconcile', appId);+ const now = new Date().toISOString();+ record.branchReconcileOutcome = {+ ...record.branchReconcileOutcome, status, reason, nextAction, updatedAt: now,+ remaining: remaining === undefined ? record.branchReconcileOutcome?.remaining ?? null : remaining,+ observedAt: remaining === undefined ? record.branchReconcileOutcome?.observedAt ?? null : now,+ };+ return { result: record.branchReconcileOutcome, changed: true };+ });+}+/*** A perpetual drain's convergence state, read in ONE pass: the actionable* signature its last dispatch was handed, and how many times it has dispatched@@ -1548,6 +1573,13 @@ export async function getScheduleStatus() {};}+ if (taskType === 'branch-reconcile') {+ taskStatus.reconcileOutcomes = Object.fromEntries([+ ...(execution.branchReconcileOutcome ? [[null, execution.branchReconcileOutcome]] : []),+ ...Object.entries(execution.perApp || {}).filter(([, record]) => record.branchReconcileOutcome)+ .map(([appId, record]) => [appId, record.branchReconcileOutcome]),+ ]);+ }status.tasks[taskType] = taskStatus;if (learningInfo.adjusted) {diff --git a/server/services/taskSchedule.test.js b/server/services/taskSchedule.test.jsindex e4c62f500a..1b275f81e5 100644--- a/server/services/taskSchedule.test.js+++ b/server/services/taskSchedule.test.js@@ -141,6 +141,7 @@ import {isPerpetualParkActive,getPerpetualDrainState,recordPerpetualDispatch,+ recordBranchReconcileOutcome,applyOnDemandRunResets,queuePerpetualRefill,isRefillRequest,@@ -3139,6 +3140,20 @@ describe('taskSchedule', () => {expect(await shouldRunTask('branch-reconcile', 'app-1')).toEqual({ shouldRun: false, reason: 'disabled' });});+ it('persists incomplete sweep evidence and exposes it when recurring dispatch is off', async () => {+ let saved = { version: 2, tasks: { 'branch-reconcile': { enabled: true, type: 'on-demand', perpetual: false } }, executions: {}, templates: [] };+ readJSONFile.mockImplementation(async () => structuredClone(saved));+ writeFile.mockImplementation(async (_path, contents) => { saved = JSON.parse(contents); });+ const remaining = [{ branch: 'fix/example', path: '/example/checkout', reason: 'worktree-locked' }];+ await recordBranchReconcileOutcome('app-1', { status: 'running', reason: 'batch-pending', remaining, nextAction: 'Continue.' });+ const observedAt = saved.executions['task:branch-reconcile'].perApp['app-1'].branchReconcileOutcome.observedAt;+ await recordBranchReconcileOutcome('app-1', { status: 'incomplete', reason: 'agent-failed', nextAction: 'Resolve the provider failure and run again.' });+ expect(saved.executions['task:branch-reconcile'].perApp['app-1'].branchReconcileOutcome)+ .toMatchObject({ status: 'incomplete', reason: 'agent-failed', remaining, observedAt });+ expect((await getScheduleStatus()).tasks['branch-reconcile'].reconcileOutcomes['app-1'])+ .toMatchObject({ status: 'incomplete', reason: 'agent-failed', remaining, observedAt });+ });+it('persists branch attempts through cap parks and manual resets, clearing only on sweep exhaustion', async () => {let saved = { version: 2, tasks: { 'branch-reconcile': { enabled: true, type: 'on-demand', perpetual: true } }, executions: {}, templates: [] };readJSONFile.mockImplementation(async () => JSON.parse(JSON.stringify(saved)));