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

address review (claude): protect dirty worktrees, honest dispatch reporting, validate cron

Adam Eivye68506a66cbd
diff --git a/client/src/components/settings/BranchReconcileTab.jsx b/client/src/components/settings/BranchReconcileTab.jsx
index 7b7b863dfe..8b155eb405 100644
--- a/client/src/components/settings/BranchReconcileTab.jsx
+++ b/client/src/components/settings/BranchReconcileTab.jsx
@@ -81,7 +81,7 @@ export function BranchReconcileTab() {
if (!summary) { toast.error('Reconcile run failed'); return; }
setLastRun(summary);
if (summary.skipped === 'disabled') toast.error('Reconciler is disabled — enable and save first');
- else toast.success(`Reconcile: cleaned ${summary.cleaned?.length ?? 0}, dispatched=${summary.dispatched ? 'yes' : 'no'}`);
+ else toast.success(`Reconcile: cleaned ${summary.cleaned?.length ?? 0}, coordinator ${summary.queued ? 'queued' : 'not queued'}`);
};
if (loading) return <BrailleSpinner />;
@@ -180,11 +180,18 @@ export function BranchReconcileTab() {
<dl className="grid grid-cols-2 sm:grid-cols-3 gap-3 text-sm">
<div><dt className="text-gray-500 text-xs uppercase">When</dt><dd className="text-gray-200">{lastRun.at ? new Date(lastRun.at).toLocaleString() : '—'}</dd></div>
<div><dt className="text-gray-500 text-xs uppercase">Cleaned</dt><dd className="text-gray-200">{lastRun.cleaned?.length ?? 0}</dd></div>
- <div><dt className="text-gray-500 text-xs uppercase">Dispatched agent</dt><dd className="text-gray-200">{lastRun.dispatched ? 'yes' : 'no'}</dd></div>
+ <div><dt className="text-gray-500 text-xs uppercase">Coordinator queued</dt><dd className="text-gray-200">{lastRun.queued ? 'yes' : 'no'}</dd></div>
<div><dt className="text-gray-500 text-xs uppercase">Actionable</dt><dd className="text-gray-200">{lastRun.actionable?.join(', ') || '—'}</dd></div>
<div><dt className="text-gray-500 text-xs uppercase">WIP (left)</dt><dd className="text-gray-200">{lastRun.wip?.join(', ') || '—'}</dd></div>
</dl>
)}
+ {/* The coordinator is queued but only RUNS when CoS auto-run is in Execute mode.
+ Warn honestly so the queued state isn't mistaken for "an agent is running". */}
+ {lastRun.queued && lastRun.coordinatorWillRun === false && (
+ <p className="text-xs text-port-warning mt-3">
+ Coordinator queued but CoS auto-run is <code>{lastRun.cosAutonomy || 'not execute'}</code> — it will spawn once you set CoS auto-run to <strong>Execute</strong>.
+ </p>
+ )}
</div>
)}
</div>
diff --git a/server/lib/validation.js b/server/lib/validation.js
index ad1bcfde3f..6c4adb5821 100644
--- a/server/lib/validation.js
+++ b/server/lib/validation.js
@@ -321,7 +321,11 @@ export const backupConfigSchema = z.object({
// PUT route (.partial()) and re-read on every scheduled run.
export const branchReconcileConfigSchema = z.object({
enabled: z.boolean().optional().default(false),
- cron: z.string().optional().default('0 3 * * *'),
+ // 5- or 6-field cron expression. Validated here (not just typed as a string)
+ // so a malformed save can't reach `startBranchReconcileScheduler` and throw
+ // out of the boot-time cron parse (which the boot `.catch` would swallow,
+ // leaving the scheduler silently unregistered).
+ cron: z.string().regex(/^\S+(\s+\S+){4,5}$/, 'cron must have 5 or 6 whitespace-separated fields').optional().default('0 3 * * *'),
actions: z.object(optionalBooleanMap([
'cleanupMerged', 'openPr', 'resolveConflicts', 'autoMerge'
])).optional().default({})
diff --git a/server/lib/validation.test.js b/server/lib/validation.test.js
index e643c071a3..af93e3aa6e 100644
--- a/server/lib/validation.test.js
+++ b/server/lib/validation.test.js
@@ -52,6 +52,13 @@ describe('validation.js', () => {
expect(branchReconcileConfigSchema.safeParse({ enabled: 'yes' }).success).toBe(false);
});
+ it('rejects a malformed cron expression but accepts 5- and 6-field forms', () => {
+ expect(branchReconcileConfigSchema.safeParse({ cron: 'not a cron' }).success).toBe(false);
+ expect(branchReconcileConfigSchema.safeParse({ cron: '0 3 * *' }).success).toBe(false); // only 4 fields
+ expect(branchReconcileConfigSchema.safeParse({ cron: '0 3 * * *' }).success).toBe(true);
+ expect(branchReconcileConfigSchema.safeParse({ cron: '0 0 3 * * *' }).success).toBe(true); // 6-field
+ });
+
it('.partial() allows a single-field toggle patch', () => {
expect(branchReconcileConfigSchema.partial().safeParse({ enabled: true }).success).toBe(true);
});
diff --git a/server/services/branchReconcile.js b/server/services/branchReconcile.js
index 1863536462..0b9611ab0a 100644
--- a/server/services/branchReconcile.js
+++ b/server/services/branchReconcile.js
@@ -44,8 +44,14 @@ const PR_LIST_LIMIT = 200;
*/
export function classifyBranch({ hasUpstream, isMerged, worktreeDirty, openPr }) {
if (isMerged) return 'MERGED';
+ // A worktree with real uncommitted changes is NEVER handed to the coordinator
+ // agent — even for a branch with an open PR. The agent's per-state actions
+ // (rebase/resolve/merge) run git operations that could stash/reset/checkout
+ // and silently discard the user's in-progress work. Skip it as WIP regardless
+ // of PR state; the `cleanupMerged` path applies the same guard for MERGED.
+ if (worktreeDirty) return 'WIP';
if (openPr) return openPr.mergeable === 'CONFLICTING' ? 'CONFLICTED' : 'IN_REVIEW';
- if (hasUpstream && !worktreeDirty) return 'NEEDS_PR';
+ if (hasUpstream) return 'NEEDS_PR';
return 'WIP';
}
diff --git a/server/services/branchReconcile.test.js b/server/services/branchReconcile.test.js
index 76cf4b150d..0b8eed80b6 100644
--- a/server/services/branchReconcile.test.js
+++ b/server/services/branchReconcile.test.js
@@ -66,6 +66,10 @@ describe('classifyBranch', () => {
it('pushed but dirty worktree → WIP', () => {
expect(classifyBranch({ isMerged: false, openPr: null, hasUpstream: true, worktreeDirty: true })).toBe('WIP');
});
+ it('dirty worktree wins over an open PR → WIP (never hand a dirty tree to the agent)', () => {
+ expect(classifyBranch({ isMerged: false, openPr: { mergeable: 'MERGEABLE' }, hasUpstream: true, worktreeDirty: true })).toBe('WIP');
+ expect(classifyBranch({ isMerged: false, openPr: { mergeable: 'CONFLICTING' }, hasUpstream: true, worktreeDirty: true })).toBe('WIP');
+ });
it('local-only (no upstream), no PR → WIP', () => {
expect(classifyBranch({ isMerged: false, openPr: null, hasUpstream: false, worktreeDirty: false })).toBe('WIP');
});
diff --git a/server/services/branchReconcileScheduler.js b/server/services/branchReconcileScheduler.js
index 152b1e7c1e..f4f12dc58c 100644
--- a/server/services/branchReconcileScheduler.js
+++ b/server/services/branchReconcileScheduler.js
@@ -19,6 +19,7 @@ import { getUserTimezone } from '../lib/timezone.js';
import { PATHS } from '../lib/fileUtils.js';
import { PRIORITY_VALUES, addTask } from './cosTaskStore.js';
import { reconcile } from './branchReconcile.js';
+import { getDomainMode } from '../lib/domainAutonomy.js';
const CRON_ID = 'branch-reconcile';
@@ -145,16 +146,25 @@ export async function runBranchReconcile({ force = false, now = Date.now() } = {
const result = await reconcile(PATHS.root, { cleanup: actionOn(actions, 'cleanupMerged') });
const actionable = filterActionable(result.inFlight, actions);
- let dispatched = false;
+ let queued = false;
+ // The coordinator is an auto-approved INTERNAL task, which the CoS runner
+ // only actually spawns when its autonomy mode for the `cos` domain is
+ // `execute` (off/dry-run leave it queued). Surface the mode so the summary
+ // can't claim an agent will run when it won't — the reconciler's enable
+ // toggle is independent of the CoS auto-run setting.
+ let cosAutonomy = 'unknown';
if (actionable.length > 0) {
const task = buildCoordinatorTask(actionable, {
defaultBranch: result.defaultBranch, actions, now
});
const added = await addTask(task, 'internal', { raw: true });
- dispatched = !added?.duplicate;
+ queued = !added?.duplicate;
if (added?.duplicate) {
console.log('🔀 branch-reconcile: a coordinator is already in flight — skipping dispatch');
}
+ const { getConfig } = await import('./cos.js');
+ const config = await getConfig().catch(() => null);
+ cosAutonomy = config ? getDomainMode(config, 'cos') : 'unknown';
}
const summary = {
@@ -164,10 +174,13 @@ export async function runBranchReconcile({ force = false, now = Date.now() } = {
actionable: actionable.map((b) => b.branch),
wip: result.wip.map((b) => b.branch),
skipped: result.skipped,
- dispatched
+ queued,
+ // true only when the coordinator was queued AND the CoS runner will spawn it.
+ coordinatorWillRun: queued && cosAutonomy === 'execute',
+ cosAutonomy
};
lastRun = summary;
- console.log(`🔀 branch-reconcile: cleaned ${result.cleaned.length}, ${actionable.length} in-flight actionable, dispatched=${dispatched}`);
+ console.log(`🔀 branch-reconcile: cleaned ${result.cleaned.length}, ${actionable.length} actionable, queued=${queued}, cosAutonomy=${cosAutonomy}`);
return summary;
} catch (err) {
console.error(`❌ branch-reconcile run failed: ${err.message}`);
diff --git a/server/services/branchReconcileScheduler.test.js b/server/services/branchReconcileScheduler.test.js
index c133c35fab..407861b289 100644
--- a/server/services/branchReconcileScheduler.test.js
+++ b/server/services/branchReconcileScheduler.test.js
@@ -19,6 +19,8 @@ vi.mock('./cosTaskStore.js', () => ({
addTask: vi.fn(async () => ({ id: 'sys-branch-reconcile-x' }))
}));
vi.mock('./branchReconcile.js', () => ({ reconcile: vi.fn() }));
+vi.mock('./cos.js', () => ({ getConfig: vi.fn(async () => ({ domainAutonomy: { cos: 'execute' } })) }));
+vi.mock('../lib/domainAutonomy.js', () => ({ getDomainMode: vi.fn((config) => config?.domainAutonomy?.cos || 'off') }));
import {
filterActionable, formatInFlightForPrompt, buildCoordinatorTask, runBranchReconcile, getLastRun
@@ -26,10 +28,12 @@ import {
import { getSettings } from './settings.js';
import { addTask } from './cosTaskStore.js';
import { reconcile } from './branchReconcile.js';
+import { getConfig } from './cos.js';
beforeEach(() => {
vi.clearAllMocks();
addTask.mockResolvedValue({ id: 'sys-branch-reconcile-x' });
+ getConfig.mockResolvedValue({ domainAutonomy: { cos: 'execute' } });
});
const IN_FLIGHT = [
@@ -114,24 +118,36 @@ describe('runBranchReconcile', () => {
const res = await runBranchReconcile({ now: 0 });
expect(addTask).toHaveBeenCalledTimes(1);
expect(addTask.mock.calls[0][1]).toBe('internal');
- expect(res.dispatched).toBe(true);
+ expect(res.queued).toBe(true);
+ expect(res.coordinatorWillRun).toBe(true); // cos autonomy mocked to 'execute'
+ expect(res.cosAutonomy).toBe('execute');
expect(res.actionable).toEqual(['needspr', 'conflicted', 'inreview']);
});
- it('does not dispatch when nothing is actionable', async () => {
+ it('reports coordinatorWillRun=false when CoS autonomy is not execute', async () => {
+ getSettings.mockResolvedValue({ branchReconcile: { enabled: true, actions: {} } });
+ reconcile.mockResolvedValue({ defaultBranch: 'main', cleaned: [], inFlight: IN_FLIGHT, wip: [], skipped: [] });
+ getConfig.mockResolvedValue({ domainAutonomy: { cos: 'off' } });
+ const res = await runBranchReconcile({ now: 0 });
+ expect(res.queued).toBe(true);
+ expect(res.coordinatorWillRun).toBe(false);
+ expect(res.cosAutonomy).toBe('off');
+ });
+
+ it('does not queue when nothing is actionable', async () => {
getSettings.mockResolvedValue({ branchReconcile: { enabled: true, actions: {} } });
reconcile.mockResolvedValue({ defaultBranch: 'main', cleaned: [], inFlight: [], wip: ['x'], skipped: [] });
const res = await runBranchReconcile({ now: 0 });
expect(addTask).not.toHaveBeenCalled();
- expect(res.dispatched).toBe(false);
+ expect(res.queued).toBe(false);
});
- it('reports dispatched=false when addTask says duplicate', async () => {
+ it('reports queued=false when addTask says duplicate', async () => {
getSettings.mockResolvedValue({ branchReconcile: { enabled: true, actions: {} } });
reconcile.mockResolvedValue({ defaultBranch: 'main', cleaned: [], inFlight: IN_FLIGHT, wip: [], skipped: [] });
addTask.mockResolvedValue({ id: 'existing', duplicate: true });
const res = await runBranchReconcile({ now: 0 });
- expect(res.dispatched).toBe(false);
+ expect(res.queued).toBe(false);
expect(getLastRun()).toBe(res);
});
});