Skip to content

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

fix: give the pending-merge drain its own 30-minute CoS timer instead of a per-restart cadence (#3630)

[._.]/ Adam Eivy33be32d9dd17
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 24ef5da31d..c63127a011 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -13,3 +13,5 @@
- A Layered Intelligence run that ends badly now says what actually went wrong. Its whole job is to return JSON that a deterministic step files as one tracker issue — its prompt *forbids* touching the repo — yet the idle reaper judged it by the same "is there a file change or a commit?" question it asks a coding agent, and reported `idle-no-changes`: "the agent did not modify files", on a task told not to modify files. The real miss was one question away and is now the one asked: the `.agent-done` payload never landed, so nothing was filed. Failures on this task type read `idle-no-deliverable` and point at the missing payload (a smaller model answering in the terminal instead of writing the file — see #3640), and a run that did write its payload just before the reaper is now correctly rescued rather than failed for a clean tree. Code-editing task types are unchanged.
- CoS agents whose deliverable is a **commit** are no longer scored as failures. The idle reaper's "did this agent do anything?" gate read only *uncommitted* changes, so a `/do:release` or `/do:pr` run that committed, pushed, and opened its PR — leaving a clean tree *because it succeeded* — was recorded as `idle-no-changes` and retried. Two consecutive release runs on 2026-08-08 each did their whole job (cut the release commit, opened the release PR, then repaired it against review findings) and were both reported failed, holding the release for two days. The gate now counts commits made inside the run window as evidence of work, alongside a dirty tree. The report-shaped half of the same problem (`/do:review`, `/do:scan`, `/do:plan-task`, `/do:replan`, whose deliverable lands outside the repo) is tracked in #3636, and the unsatisfiable `[task-<id>]` success criterion one layer up in #3637.
+
+- Green merge-only PRs are drained on a real 30-minute cadence again. The pending-merge sweep had moved onto the CoS evaluation pass, but evaluation is event-driven — there is no evaluation timer — so on a quiet install it ran roughly once per PortOS restart, and the `MAX_PENDING_MERGE_TICKS` escape hatch counted restarts instead of the six hours it documents. The sweep now owns a `cos-pending-merge-sweep` interval job registered and cancelled with the other CoS timers, gated on the same "CoS auto-run is `execute` and not paused" switch. It stays decoupled from the `pr-watcher` task type, so leaving that task disabled still can't strand a queued merge, and `POST /api/cos/evaluate` still drains opportunistically.
diff --git a/server/services/cos.js b/server/services/cos.js
index 50e94ad83d..4155c8d1ac 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -374,6 +374,35 @@ export async function start() {
metadata: { description: 'CoS rehabilitation check for skipped tasks' }
});
+ // Pending-merge drain (30 min). `evaluateTasks` also sweeps opportunistically,
+ // but evaluation is event-driven — with no periodic evaluation timer that drain
+ // fires roughly once per restart, so a green merge-only PR opened while PortOS
+ // runs would sit at `ticks: 0` forever (#3630). This timer is the cadence
+ // `MAX_PENDING_MERGE_TICKS` is documented against, and it is deliberately NOT
+ // coupled to the `pr-watcher` task type — a disabled watcher must not strand
+ // queued merges. Same autonomy gate as the evaluateTasks call site: merging
+ // writes to a default branch, so it only runs when the user has CoS auto-run
+ // set to `execute` and the daemon is not paused.
+ const { PENDING_MERGE_SWEEP_INTERVAL_MS } = await import('./prWatcher.js');
+ scheduleEvent({
+ id: 'cos-pending-merge-sweep',
+ type: 'interval',
+ intervalMs: PENDING_MERGE_SWEEP_INTERVAL_MS,
+ handler: async () => {
+ const s = await loadState();
+ if (s.paused || getDomainMode(s.config, 'cos') !== 'execute') return;
+ const prWatcher = await import('./prWatcher.js');
+ const sweep = await prWatcher.sweepPendingMergePrs().catch((err) => {
+ console.warn(`⚠️ sweepPendingMergePrs failed: ${err?.message || err}`);
+ return null;
+ });
+ if (sweep && (sweep.merged || sweep.escalated || sweep.timedOut)) {
+ emitLog('info', `🤖 Pending merges: ${sweep.merged} merged, ${sweep.escalated} escalated, ${sweep.timedOut} timed out`);
+ }
+ },
+ metadata: { description: 'CoS pending merge-only PR drain' }
+ });
+
// Register autonomous job schedules (individual timers per job)
await registerJobSchedules();
@@ -420,6 +449,7 @@ export async function stop() {
cancelEvent('cos-learning-insights');
cancelEvent('cos-rehabilitation-check');
cancelEvent('cos-improvement-check');
+ cancelEvent('cos-pending-merge-sweep');
await unregisterJobSchedules();
await withStateLock(async () => {
diff --git a/server/services/cos.test.js b/server/services/cos.test.js
index debabf142f..898ef2d1e7 100644
--- a/server/services/cos.test.js
+++ b/server/services/cos.test.js
@@ -37,6 +37,7 @@ import { dirname, join } from 'path';
import { firstLine, isPerpetualRefillCandidate, perpetualRefillPlan } from './cos.js';
import { canQueueImprovementTasks } from './cosState.js';
import { createDequeueCapacity, isMissionTierEligible, isIdleTierEligible } from './cosDequeue.js';
+import { PENDING_MERGE_SWEEP_INTERVAL_MS, MAX_PENDING_MERGE_TICKS } from './prWatcher.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const COS_SRC = readFileSync(join(__dirname, 'cos.js'), 'utf-8');
@@ -1500,3 +1501,51 @@ describe('canQueueImprovementTasks — autonomous queuing gate', () => {
expect(canQueueImprovementTasks(cfg(undefined, 'execute'))).toBe(false);
});
});
+
+describe('pending-merge sweep — own timer, not the evaluation cadence (#3630)', () => {
+ // Evaluation is event-driven (cosState.js records that the periodic
+ // evaluateTasks timer was removed), so the drain inside evaluateTasks fires
+ // roughly once per restart. The cadence-bearing drain is a CoS interval job.
+ const sweepBlock = COS_SRC.slice(
+ COS_SRC.indexOf("id: 'cos-pending-merge-sweep'"),
+ COS_SRC.indexOf("id: 'cos-pending-merge-sweep'") + 1400
+ );
+
+ it('registers a cos-pending-merge-sweep interval job alongside the other CoS timers', () => {
+ expect(COS_SRC).toMatch(/id:\s*'cos-pending-merge-sweep'/);
+ expect(sweepBlock).toMatch(/type:\s*'interval'/);
+ expect(sweepBlock).toMatch(/sweepPendingMergePrs\(\)/);
+ });
+
+ it('drives the interval off PENDING_MERGE_SWEEP_INTERVAL_MS, not a local literal', () => {
+ expect(sweepBlock).toMatch(/intervalMs:\s*PENDING_MERGE_SWEEP_INTERVAL_MS/);
+ expect(COS_SRC).toMatch(/PENDING_MERGE_SWEEP_INTERVAL_MS\s*\}\s*=\s*await import\('\.\/prWatcher\.js'\)/);
+ });
+
+ it('gates the sweep on !paused AND cos auto-run === execute', () => {
+ expect(sweepBlock).toMatch(/s\.paused\s*\|\|\s*getDomainMode\(s\.config,\s*'cos'\)\s*!==\s*'execute'/);
+ });
+
+ it('cancels the timer with the other CoS jobs on stop()', () => {
+ const stopFn = extractFnBody(COS_SRC, COS_SRC.indexOf('export async function stop'));
+ expect(stopFn).toMatch(/cancelEvent\('cos-health-check'\)/);
+ expect(stopFn).toMatch(/cancelEvent\('cos-pending-merge-sweep'\)/);
+ });
+
+ it('keeps the opportunistic drain in evaluateTasks so the call site cannot be deleted silently', () => {
+ const evalFn = extractFnBody(GEN_SRC, GEN_SRC.indexOf('export async function evaluateTasks'));
+ expect(evalFn).toMatch(/sweepPendingMergePrs\(\)/);
+ expect(evalFn).toMatch(/!paused && getDomainMode\(state\.config, 'cos'\) === 'execute'/);
+ });
+
+ it('does NOT re-couple the drain to the pr-watcher task type', () => {
+ const watcherFn = extractFnBody(GEN_SRC, GEN_SRC.indexOf('async function resolvePrWatcherBlock'));
+ expect(watcherFn).not.toMatch(/sweepPendingMergePrs\(/);
+ });
+
+ it('maps MAX_PENDING_MERGE_TICKS back to wall-clock hours', () => {
+ const hours = (PENDING_MERGE_SWEEP_INTERVAL_MS * MAX_PENDING_MERGE_TICKS) / (60 * 60 * 1000);
+ expect(PENDING_MERGE_SWEEP_INTERVAL_MS).toBe(30 * 60 * 1000);
+ expect(hours).toBe(6);
+ });
+});
diff --git a/server/services/cosTaskGenerator.js b/server/services/cosTaskGenerator.js
index f82333f272..64b16016b3 100644
--- a/server/services/cosTaskGenerator.js
+++ b/server/services/cosTaskGenerator.js
@@ -1187,9 +1187,13 @@ export async function evaluateTasks(options) {
return s;
});
- // Drain merge-only PRs on the evaluation cadence rather than the `pr-watcher`
- // task's, which most installs leave disabled — that coupling stranded every
- // green PortOS-opened PR at `ticks: 0` forever. Runs BEFORE the agent-slot
+ // Drain merge-only PRs here rather than on the `pr-watcher` task's schedule,
+ // which most installs leave disabled — that coupling stranded every green
+ // PortOS-opened PR at `ticks: 0` forever. This is the OPPORTUNISTIC drain:
+ // evaluation is event-driven, so the cadence-bearing one is the
+ // `cos-pending-merge-sweep` timer in cos.js (#3630). Keeping it here costs
+ // nothing and makes `POST /api/cos/evaluate` do the obvious thing.
+ // Runs BEFORE the agent-slot
// gate below because a deterministic merge claims no lane, so a full agent
// roster must not also wedge the merge queue.
//
diff --git a/server/services/prWatcher.js b/server/services/prWatcher.js
index 515e579914..8f250191e4 100644
--- a/server/services/prWatcher.js
+++ b/server/services/prWatcher.js
@@ -46,9 +46,13 @@ import { PR_COMPLETIONS } from '../lib/prDisposition.js';
// the cap. 200 matches the limit github.js#syncRepos already uses.
const PR_LIST_LIMIT = 200;
-// A watcher tick normally runs every 30 minutes. Six hours is long enough for
-// an ordinary CI queue, while still surfacing a wedged provider or branch
-// protection rule before a merge-only PR silently leaks forever.
+// The pending-merge sweep runs on its own CoS timer (`cos-pending-merge-sweep`
+// in cos.js) every 30 minutes. Six hours is long enough for an ordinary CI
+// queue, while still surfacing a wedged provider or branch protection rule
+// before a merge-only PR silently leaks forever. cos.js drives its interval off
+// PENDING_MERGE_SWEEP_INTERVAL_MS so `MAX_PENDING_MERGE_TICKS` keeps mapping to
+// wall-clock hours rather than to restarts (#3630).
+export const PENDING_MERGE_SWEEP_INTERVAL_MS = 30 * 60 * 1000;
export const MAX_PENDING_MERGE_TICKS = 12;
const GREEN_CHECK_VERDICTS = new Set(['SUCCESS', 'NEUTRAL', 'SKIPPED']);