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

fail closed in repo-sync: never drop a stash or mutate a repo on unknown state

[._.]/ Adam Eivyfb4ef1cab089
diff --git a/server/services/cosTaskGenerator.js b/server/services/cosTaskGenerator.js
index 9c037c9595..2130d76a2b 100644
--- a/server/services/cosTaskGenerator.js
+++ b/server/services/cosTaskGenerator.js
@@ -2448,7 +2448,7 @@ async function resolveBranchReconcileBlock(app, taskType, metadata, taskSchedule
if (taskType !== 'branch-reconcile') return { skip: false, block: '' };
const { reconcile, filterActionable, limitBranchesForAgent, formatInFlightForPrompt, actionableSignature, describeIdleReconcilePark } = await import('./branchReconcile.js');
const { formatSupersededForPrompt } = await import('./supersededLedger.js');
- const { getActiveAgentIds } = await import('./agentState.js');
+ const { getActiveAgentIds, isTruthyMeta } = await import('./agentState.js');
// Action toggles were merged (global → per-app override) + value-constrained
// by sanitizeTaskMetadata into `metadata`; each is ON unless explicitly false.
const actions = {
@@ -2569,7 +2569,7 @@ async function resolveRepoSyncBlock(app, taskType, metadata) {
if (taskType !== 'repo-sync') return { skip: false, block: '' };
const {
REPO_SYNC_ACTION_KEYS, syncRepos, resolveSyncTargets, summarizeSync,
- shouldDispatchVerifier, formatRepoSyncReport
+ shouldDispatchVerifier, formatRepoSyncReport, formatWithheldSweepReport
} = await import('./repoSync.js');
const { getActiveAgentIds } = await import('./agentState.js');
@@ -2598,6 +2598,17 @@ async function resolveRepoSyncBlock(app, taskType, metadata) {
return { skip: true };
}
+ // `requireApproval` means "no unattended action until a human says go" — and
+ // this sweep IS action: it pushes, checks out, fast-forwards, drops stashes,
+ // and deletes worktrees. Running it here to build the agent's report would
+ // perform every one of those BEFORE the approval gate downstream ever sees the
+ // task. So withhold it and hand the agent the job instead; it runs only once
+ // the task has been approved and dispatched.
+ if (isTruthyMeta(metadata.requireApproval)) {
+ emitLog('info', `🔄 repo-sync: deterministic sweep withheld — this task requires approval`, { analysisType: taskType });
+ return { skip: false, block: formatWithheldSweepReport(targets) };
+ }
+
const results = await syncRepos(targets, { activeAgentIds: new Set(getActiveAgentIds()) })
.catch((err) => {
emitLog('warn', `repo-sync sweep failed: ${err.message}`, { analysisType: taskType });
diff --git a/server/services/repoSync.js b/server/services/repoSync.js
index 8df4d89161..1235ba48d5 100644
--- a/server/services/repoSync.js
+++ b/server/services/repoSync.js
@@ -89,6 +89,7 @@ export const ESCALATION_KINDS = Object.freeze({
IN_FLIGHT_BRANCH: 'in-flight-branch',
ORPHAN_REMOTE: 'orphan-remote',
ACTION_FAILED: 'action-failed',
+ AGENT_AT_WORK: 'agent-at-work',
SCAN_FAILED: 'scan-failed'
});
@@ -201,24 +202,34 @@ export function parseStashList(stdout) {
}
/**
- * Every path a stash commit carries — its tracked diff against its own base
- * plus, when the stash was taken with `-u`, the untracked snapshot in its third
- * parent. An unreadable read yields nothing from that half, which is also the
- * safe direction: a path we cannot see cannot be proven redundant.
+ * The paths a stash commit carries, kept in their two classes because each lives
+ * in a DIFFERENT tree: the tracked changes are in the stash commit itself, while
+ * the untracked snapshot a `git stash -u` captured is in its third parent. A
+ * caller that flattens them and compares everything against the stash commit
+ * would find an untracked path missing from BOTH sides and conclude the stash is
+ * redundant — dropping work that only ever existed in `^3`.
+ *
+ * Fails CLOSED: an unreadable read yields `failed: true` rather than an empty
+ * list, because "no paths" is the one answer that makes a stash look droppable.
+ *
* @param {string} repoPath
* @param {string} sha - the stash commit
* @param {number} parentCount - from `parseStashList`; 3 ⇒ carries untracked files
- * @returns {Promise<string[]>}
+ * @returns {Promise<{tracked:string[], untracked:string[], failed:boolean}>}
*/
async function stashPaths(repoPath, sha, parentCount) {
+ const hasUntracked = parentCount >= 3;
const [tracked, untracked] = await Promise.all([
execGitSafe(['diff', '--name-only', `${sha}^1`, sha], repoPath, { ignoreExitCode: true }),
- parentCount >= 3
+ hasUntracked
? execGitSafe(['ls-tree', '-r', '--name-only', `${sha}^3`], repoPath, { ignoreExitCode: true })
- : Promise.resolve({ exitCode: 1, stdout: '' })
+ : Promise.resolve({ exitCode: 0, stdout: '' })
]);
- const lines = (out) => (out.exitCode === 0 ? out.stdout : '').split('\n').map((l) => l.trim()).filter(Boolean);
- return [...new Set([...lines(tracked), ...lines(untracked)])];
+ if (tracked.exitCode !== 0 || untracked.exitCode !== 0) {
+ return { tracked: [], untracked: [], failed: true };
+ }
+ const lines = (out) => out.stdout.split('\n').map((l) => l.trim()).filter(Boolean);
+ return { tracked: lines(tracked), untracked: lines(untracked), failed: false };
}
/**
@@ -231,6 +242,12 @@ async function stashPaths(repoPath, sha, parentCount) {
* "SUPERSEDED" classification the `stash-cleanup` prompt asks a model to make by
* hand — everything this can't prove is left for that judgment call.
*
+ * Each path class is compared against the tree that HOLDS it — tracked changes
+ * against the stash commit, the untracked snapshot against its third parent.
+ * Comparing an untracked path against the stash commit would find it missing on
+ * both sides and call the stash redundant, which is how this check would come to
+ * delete the very work it exists to protect.
+ *
* Fails CLOSED in every direction: an unreadable path list, a too-large stash,
* or a git error all yield `superseded: false`.
*
@@ -242,15 +259,23 @@ async function stashPaths(repoPath, sha, parentCount) {
*/
export async function classifyStash(repoPath, sha, target, parentCount = 2) {
const paths = await stashPaths(repoPath, sha, parentCount);
- if (paths.length === 0) return { superseded: true, reason: 'empty stash (no changes recorded)' };
- if (paths.length > MAX_STASH_PATHS_FOR_AUTO_CLASSIFY) {
- return { superseded: false, reason: `touches ${paths.length} paths — too large to auto-classify` };
+ if (paths.failed) return { superseded: false, reason: 'could not read the stash contents' };
+ const total = paths.tracked.length + paths.untracked.length;
+ if (total === 0) return { superseded: true, reason: 'empty stash (no changes recorded)' };
+ if (total > MAX_STASH_PATHS_FOR_AUTO_CLASSIFY) {
+ return { superseded: false, reason: `touches ${total} paths — too large to auto-classify` };
}
- const diff = await execGitSafe(['diff', '--quiet', sha, target, '--', ...paths], repoPath, { ignoreExitCode: true });
- if (diff.exitCode === 0) {
- return { superseded: true, reason: `content identical to ${target} on all ${paths.length} path(s)` };
+ // Two probes, one per tree. A non-zero exit is "differs OR unreadable"; both
+ // must keep the stash, so they share an answer.
+ if (paths.tracked.length) {
+ const diff = await execGitSafe(['diff', '--quiet', sha, target, '--', ...paths.tracked], repoPath, { ignoreExitCode: true });
+ if (diff.exitCode !== 0) return { superseded: false, reason: `differs from ${target}` };
}
- return { superseded: false, reason: `differs from ${target}` };
+ if (paths.untracked.length) {
+ const diff = await execGitSafe(['diff', '--quiet', `${sha}^3`, target, '--', ...paths.untracked], repoPath, { ignoreExitCode: true });
+ if (diff.exitCode !== 0) return { superseded: false, reason: `untracked content differs from ${target}` };
+ }
+ return { superseded: true, reason: `content identical to ${target} on all ${total} path(s)` };
}
/**
@@ -288,24 +313,44 @@ async function collectRepoState(repoPath) {
fetchError = await fetchOrigin(repoPath, { prune: true }).then(() => null, (err) => err.message);
}
+ // EVERY read below feeds a safety decision, so a failure is recorded as such
+ // rather than degrading into the value that happens to look safe. Left to
+ // `.catch(() => '')`, an unreadable `git status` reads as a CLEAN tree, an
+ // unreadable branch list as NO branches, and a failed stash list as NO stashes
+ // — each of which unlocks a mutation the real state would have refused.
+ const readFailures = [];
+ const required = async (label, promise, fallback) => promise.catch((err) => {
+ readFailures.push(`${label}: ${err.message}`);
+ return fallback;
+ });
const [defaultBranch, headBranch, porcelain, branches, rawStashes, activeAgentId] = await Promise.all([
getDefaultBranch(repoPath).then((b) => b || 'main', () => 'main'),
- getBranch(repoPath).catch(() => ''),
- getStatusPorcelain(repoPath).catch(() => ''),
- getBranches(repoPath).catch(() => []),
+ required('current branch', getBranch(repoPath), ''),
+ required('working-tree status', getStatusPorcelain(repoPath), ''),
+ required('branch list', getBranches(repoPath), []),
// The planner refuses to act on a repo mid-operation, so classifying its
// stashes (2–3 git spawns each) buys nothing.
operationInProgress
? Promise.resolve([])
: execGitSafe(['stash', 'list', '--format=%gd%x1f%H%x1f%P%x1f%gs'], repoPath, { ignoreExitCode: true })
- .then((r) => parseStashList(r.stdout)),
- findActiveAgentInWorkspace(repoPath).catch(() => null)
+ .then((r) => {
+ if (r.exitCode !== 0) {
+ readFailures.push(`stash list: ${(r.stderr || '').trim().split('\n').slice(-1)[0] || 'git exited non-zero'}`);
+ return [];
+ }
+ return parseStashList(r.stdout);
+ }),
+ // A lookup failure here must NOT read as "nobody is working in this
+ // checkout" — that is the fact every mutating step below is gated on.
+ required('active-agent lookup', findActiveAgentInWorkspace(repoPath), null)
]);
const detached = headBranch === 'HEAD' || headBranch === '';
const currentBranch = detached ? null : headBranch;
const dirtyTracked = dirtyTrackedPaths(porcelain);
const remoteDefault = hasOrigin ? `origin/${defaultBranch}` : null;
+ const remotes = await execGitSafe(['remote'], repoPath, { ignoreExitCode: true })
+ .then((r) => (r.exitCode === 0 ? r.stdout.split('\n').map((l) => l.trim()).filter(Boolean) : []));
// How the LOCAL default branch stands against origin's copy — the number that
// decides fast-forward vs "diverged, hand it to the agent". `getBranches`
@@ -324,6 +369,20 @@ async function collectRepoState(repoPath) {
// otherwise, so an offline machine still gets a correct — just staler — answer.
const stashTarget = remoteDefault && defaultDivergence ? remoteDefault : defaultBranch;
+ // `getBranches` derives ahead/behind from `%(upstream:track)`, which is EMPTY
+ // for a branch that was never pushed — so its `ahead` is 0 and the
+ // unpushed-work escalation below could never fire for exactly the branches it
+ // exists to catch. Measure those against the default branch instead. Bounded,
+ // and only for the upstream-less ones, so a normal repo pays nothing.
+ const localOnly = branches.filter((b) => !b.tracking && b.name !== defaultBranch);
+ const localAheadByBranch = new Map(await mapWithConcurrency(localOnly, STASH_CLASSIFY_CONCURRENCY, async (b) => [
+ b.name,
+ (await countAheadBehind(repoPath, b.name, defaultBranch))?.ahead ?? 0
+ ]));
+ const branchesWithLocalAhead = branches.map((b) => (
+ localAheadByBranch.has(b.name) ? { ...b, localAhead: localAheadByBranch.get(b.name) } : b
+ ));
+
const [stashes, currentBranchMerged] = await Promise.all([
mapWithConcurrency(rawStashes, STASH_CLASSIFY_CONCURRENCY, async (entry) => ({
...entry,
@@ -347,12 +406,14 @@ async function collectRepoState(repoPath) {
repoPath,
isRepo: true,
fetchError,
+ readFailures,
+ remotes,
defaultBranch,
remoteDefault,
currentBranch,
operationInProgress,
dirtyTracked,
- branches,
+ branches: branchesWithLocalAhead,
defaultDivergence,
stashes,
activeAgentId,
@@ -395,16 +456,44 @@ export function planRepoSync(state, actions = {}) {
if (!state?.isRepo) return { steps, escalations };
- // A settled HEAD is the precondition for every safety property in this
- // module, so a repo mid-operation gets no steps at all.
+ // Everything below decides what to MUTATE from what the snapshot observed, so
+ // each of these three says "the snapshot is not a sound basis for that" and
+ // returns no steps at all. Reporting without acting is always available; acting
+ // on state we could not establish is what loses work.
+
+ // A settled HEAD is the precondition for every safety property in this module.
if (state.operationInProgress) {
escalate(ESCALATION_KINDS.OPERATION_IN_PROGRESS,
`a ${state.operationInProgress} is in progress — resolve it before this checkout can be synced`);
return { steps, escalations };
}
+ // A read that failed leaves a fact missing, and every missing fact defaults to
+ // the value that looks safe (clean tree, no branches, no stashes, no agent) —
+ // precisely the values that unlock mutations. Refuse until the snapshot is whole.
+ if (state.readFailures?.length) {
+ for (const failure of state.readFailures) {
+ escalate(ESCALATION_KINDS.SCAN_FAILED, `could not read ${failure} — no automatic action taken on this repo`);
+ }
+ return { steps, escalations };
+ }
+
+ // Somebody's agent is working in this checkout right now. Pushing its branch,
+ // fast-forwarding under it, dropping its stashes, or reaping its worktrees all
+ // race live work — the switch-back gate below is NOT enough on its own.
+ if (state.activeAgentId) {
+ escalate(ESCALATION_KINDS.AGENT_AT_WORK,
+ `agent ${state.activeAgentId} is running in this checkout — left untouched`);
+ return { steps, escalations };
+ }
+
+ // Without a successful fetch every remote-derived number is stale, so the
+ // remote-dependent steps (push, fast-forward) would act on a guess. Report and
+ // stop; the next run re-fetches.
if (state.fetchError) {
- escalate(ESCALATION_KINDS.SCAN_FAILED, `could not fetch from origin: ${state.fetchError}`);
+ escalate(ESCALATION_KINDS.SCAN_FAILED,
+ `could not fetch from origin (${state.fetchError}) — origin state is unknown, so no automatic action was taken`);
+ return { steps, escalations };
}
const clean = state.dirtyTracked.length === 0;
@@ -423,12 +512,13 @@ export function planRepoSync(state, actions = {}) {
// judgment call, not a sync step.
for (const branch of state.branches) {
if (!branch.tracking) {
- // No upstream at all. Unpushed commits are unreviewed by definition; a
- // bare pointer at the default branch's tip is just cruft the merged-branch
- // cleanup handles, so only a branch with its own commits escalates.
- if (branch.ahead > 0 && !branch.isDefault) {
+ // No upstream at all, so `ahead` is 0 by construction — the snapshot
+ // measured these against the default branch instead (`localAhead`). A bare
+ // pointer at the default tip is just cruft the merged-branch cleanup
+ // handles; a branch with commits of its own is unreviewed work.
+ if (branch.localAhead > 0 && !branch.isDefault) {
escalate(ESCALATION_KINDS.UNPUSHED_BRANCH,
- `${branch.name} has ${branch.ahead} local commit(s) and no upstream — never pushed, no PR`);
+ `${branch.name} has ${branch.localAhead} commit(s) not on ${state.defaultBranch} and no upstream — never pushed, no PR`);
}
continue;
}
@@ -438,7 +528,18 @@ export function planRepoSync(state, actions = {}) {
continue;
}
if (branch.ahead > 0 && actionOn(actions, 'syncPush')) {
- steps.push({ kind: 'push', branch: branch.name, ahead: branch.ahead });
+ // The ahead count was measured against THIS branch's configured upstream,
+ // so the push has to go there. `git push origin <local name>` would publish
+ // to a different ref whenever the upstream is on another remote or carries
+ // another name — landing commits somewhere nobody is watching while the
+ // real upstream stays behind.
+ const upstream = parseUpstream(branch.tracking, state.remotes);
+ if (!upstream) {
+ escalate(ESCALATION_KINDS.UNPUSHED_BRANCH,
+ `${branch.name} is ${branch.ahead} ahead of ${branch.tracking}, whose remote is not one of this repo's (${state.remotes.join(', ') || 'none'}) — push it by hand`);
+ continue;
+ }
+ steps.push({ kind: 'push', branch: branch.name, ahead: branch.ahead, remote: upstream.remote, remoteRef: upstream.ref });
}
}
@@ -504,6 +605,26 @@ export function planRepoSync(state, actions = {}) {
return { steps, escalations };
}
+/**
+ * Split a `%(upstream:short)` value (`origin/feature/x`) into the remote and the
+ * branch name on it, or null when the leading segment is not one of this repo's
+ * remotes. Remote names cannot contain `/`, so the first segment is the remote
+ * and everything after it is the ref — which is what keeps a branch named
+ * `feature/x` from being mistaken for a remote called `feature`. Pure.
+ * @param {string} tracking
+ * @param {string[]} remotes
+ * @returns {{remote:string, ref:string}|null}
+ */
+export function parseUpstream(tracking, remotes = []) {
+ const value = String(tracking || '');
+ const slash = value.indexOf('/');
+ if (slash <= 0) return null;
+ const remote = value.slice(0, slash);
+ const ref = value.slice(slash + 1);
+ if (!ref || !remotes.includes(remote)) return null;
+ return { remote, ref };
+}
+
/** Numeric index of a `stash@{N}` ref; -1 when unparseable. Pure. */
export function stashIndex(ref) {
const m = /^stash@\{(\d+)\}$/.exec(String(ref || '').trim());
@@ -532,7 +653,7 @@ async function runStep(repoPath, step) {
return r.exitCode === 0 ? { ok: true, step } : fail((r.stderr + r.stdout).trim().split('\n').slice(-3).join(' '));
};
- if (step.kind === 'push') return attempt(['push', 'origin', step.branch]);
+ if (step.kind === 'push') return attempt(['push', step.remote, `${step.branch}:${step.remoteRef}`]);
if (step.kind === 'switch-default') return attempt(['checkout', step.to]);
if (step.kind === 'ff-default') {
@@ -576,7 +697,7 @@ async function runStep(repoPath, step) {
*/
export function describeStep(step) {
switch (step.kind) {
- case 'push': return `pushed ${step.branch} (${step.ahead} commit(s)) to origin`;
+ case 'push': return `pushed ${step.branch} (${step.ahead} commit(s)) to ${step.remote}/${step.remoteRef}`;
case 'switch-default': return `switched checkout from ${step.from} to ${step.to}`;
case 'ff-default': return `fast-forwarded ${step.branch} (${step.behind} behind origin)`;
case 'drop-stash': return `dropped ${step.ref} — ${step.reason}`;
@@ -627,7 +748,14 @@ export async function syncRepo(repo, { activeAgentIds = new Set() } = {}) {
// classified `inFlight` set is REPORTED, not driven: see the scope boundary in
// the module header for why finishing those branches belongs to
// branch-reconcile and not here.
- if (actionOn(actions, 'cleanupMerged') && !state.operationInProgress) {
+ // Gated on the same facts the planner refuses to act without: reconcile
+ // DELETES branches and worktrees, so an incomplete snapshot, a live agent, or
+ // an unfetched origin must keep it out exactly as they keep out the steps above.
+ const reconcileSafe = !state.operationInProgress
+ && !state.readFailures?.length
+ && !state.activeAgentId
+ && !state.fetchError;
+ if (actionOn(actions, 'cleanupMerged') && reconcileSafe) {
const { reconcile } = await import('./branchReconcile.js');
const result = await reconcile(repoPath, {
cleanup: true,
@@ -768,6 +896,34 @@ export function formatRepoSyncReport(results, { verifyReason = '' } = {}) {
return lines.join('\n');
}
+/**
+ * The `{repoSyncReport}` block for a run whose deterministic sweep was WITHHELD
+ * because the task requires approval. The sweep mutates checkouts, so running it
+ * to build a report would be the very mutation the approval gate exists to hold —
+ * the agent is told to do the whole job itself instead, once it is approved. Pure.
+ * @param {{repoPath:string, appId?:string, name?:string}[]} targets
+ * @returns {string}
+ */
+export function formatWithheldSweepReport(targets) {
+ return [
+ '## Programmatic sync pass — WITHHELD (this task requires approval)',
+ '',
+ 'No automatic pass ran: it mutates checkouts, and this task is configured to',
+ 'require approval before that happens. **You are doing the whole sync yourself**,',
+ 'so treat the list below as your job, not as a verification pass.',
+ '',
+ 'For each repository: fetch and prune, push any branch strictly ahead of its own',
+ 'configured upstream (never `--force`, never one that has diverged), fast-forward',
+ 'the default branch (`--ff-only` only), return the checkout to the default branch',
+ 'when the branch it is on is clean and already merged, delete branches and',
+ 'worktrees already merged into the default branch, and drop only stash entries',
+ 'whose content is already identical to the default branch. Skip — and report —',
+ 'any repository mid-merge/rebase or with an agent running in it.',
+ '',
+ ...(targets || []).map((t) => `- **${t.name}**${t.appId ? ` (${t.appId})` : ''} — \`${t.repoPath}\``)
+ ].join('\n');
+}
+
/**
* Resolve the repos a sweep covers from the managed-app registry.
*
diff --git a/server/services/repoSync.test.js b/server/services/repoSync.test.js
index 7efd285971..8cc886300c 100644
--- a/server/services/repoSync.test.js
+++ b/server/services/repoSync.test.js
@@ -63,6 +63,8 @@ const cleanState = (over = {}) => ({
remoteDefault: 'origin/main',
currentBranch: 'main',
operationInProgress: null,
+ readFailures: [],
+ remotes: ['origin'],
dirtyTracked: [],
branches: [{ name: 'main', current: true, tracking: 'origin/main', ahead: 0, behind: 0, isDefault: true, merged: false, worktree: false }],
defaultDivergence: { ahead: 0, behind: 0 },
@@ -75,6 +77,10 @@ const cleanState = (over = {}) => ({
const branch = (name, over = {}) => ({
name, current: false, tracking: `origin/${name}`, ahead: 0, behind: 0, isDefault: false, merged: false, worktree: false, ...over
});
+// A branch that was never pushed: git reports ahead:0 for it (the count comes
+// from %(upstream:track), which is empty), so the snapshot measures it against
+// the default branch and stores the result as localAhead.
+const localBranch = (name, localAhead) => branch(name, { tracking: null, ahead: 0, localAhead });
const kinds = (escalations) => escalations.map((e) => e.kind);
const stepKinds = (steps) => steps.map((s) => s.kind);
@@ -146,6 +152,31 @@ describe('planRepoSync — refuses to act on an unsettled repo', () => {
expect(kinds(escalations)).toContain(ESCALATION_KINDS.SCAN_FAILED);
});
+ it('plans nothing when a snapshot read failed', () => {
+ // An unreadable status/branch/stash read defaults to the value that LOOKS
+ // safe (clean tree, no branches, no stashes) — exactly the values that unlock
+ // mutations. The gate refuses until the snapshot is whole.
+ const { steps, escalations } = planRepoSync(cleanState({
+ readFailures: ['working-tree status: git died'],
+ branches: [branch('feature/x', { ahead: 2 })],
+ defaultDivergence: { ahead: 0, behind: 3 }
+ }));
+ expect(steps).toEqual([]);
+ expect(kinds(escalations)).toEqual([ESCALATION_KINDS.SCAN_FAILED]);
+ });
+
+ it('plans no remote-dependent action when the fetch failed', () => {
+ // Every remote-derived number is stale without a successful fetch, so a push
+ // or fast-forward would act on a guess.
+ const { steps, escalations } = planRepoSync(cleanState({
+ fetchError: 'could not read from remote',
+ branches: [branch('feature/x', { ahead: 2 })],
+ defaultDivergence: { ahead: 0, behind: 3 }
+ }));
+ expect(steps).toEqual([]);
+ expect(kinds(escalations)).toEqual([ESCALATION_KINDS.SCAN_FAILED]);
+ });
+
it('does nothing to a repo that is already in the target state', () => {
const { steps, escalations } = planRepoSync(cleanState());
expect(steps).toEqual([]);
@@ -156,7 +187,7 @@ describe('planRepoSync — refuses to act on an unsettled repo', () => {
describe('planRepoSync — publishing local commits', () => {
it('pushes a branch that is strictly ahead of its upstream', () => {
const { steps } = planRepoSync(cleanState({ branches: [branch('feature/x', { ahead: 2 })] }));
- expect(steps).toEqual([{ kind: 'push', branch: 'feature/x', ahead: 2 }]);
+ expect(steps).toEqual([{ kind: 'push', branch: 'feature/x', ahead: 2, remote: 'origin', remoteRef: 'feature/x' }]);
});
it('never pushes a branch that has diverged — it escalates instead', () => {
@@ -166,18 +197,41 @@ describe('planRepoSync — publishing local commits', () => {
});
it('escalates local commits on a branch with no upstream', () => {
- const { steps, escalations } = planRepoSync(cleanState({ branches: [branch('feature/x', { tracking: null, ahead: 4 })] }));
+ // Keyed on localAhead, NOT ahead: git reports ahead:0 for every branch
+ // without an upstream, so an `ahead`-keyed check could never fire for
+ // exactly the never-pushed branches this escalation exists to catch.
+ const { steps, escalations } = planRepoSync(cleanState({ branches: [localBranch('feature/x', 4)] }));
expect(steps).toEqual([]);
expect(kinds(escalations)).toEqual([ESCALATION_KINDS.UNPUSHED_BRANCH]);
- expect(escalations[0].detail).toContain('4 local commit(s)');
+ expect(escalations[0].detail).toContain('4 commit(s) not on main');
});
it('ignores an upstream-less branch carrying no commits of its own', () => {
- const { steps, escalations } = planRepoSync(cleanState({ branches: [branch('scratch', { tracking: null, ahead: 0 })] }));
+ const { steps, escalations } = planRepoSync(cleanState({ branches: [localBranch('scratch', 0)] }));
expect(steps).toEqual([]);
expect(escalations).toEqual([]);
});
+ it('pushes to the branch\'s CONFIGURED upstream, not origin/<local name>', () => {
+ // A branch tracking a differently-named ref (or another remote) has its ahead
+ // count measured against THAT upstream — pushing to origin/<local name>
+ // publishes somewhere nobody watches and leaves the real upstream behind.
+ const { steps } = planRepoSync(cleanState({
+ remotes: ['origin', 'upstream'],
+ branches: [branch('feature/x', { tracking: 'upstream/renamed-x', ahead: 1 })]
+ }));
+ expect(steps).toEqual([{ kind: 'push', branch: 'feature/x', ahead: 1, remote: 'upstream', remoteRef: 'renamed-x' }]);
+ });
+
+ it('refuses to guess when the upstream names a remote this repo does not have', () => {
+ const { steps, escalations } = planRepoSync(cleanState({
+ remotes: ['origin'],
+ branches: [branch('feature/x', { tracking: 'gone/feature/x', ahead: 1 })]
+ }));
+ expect(steps).toEqual([]);
+ expect(kinds(escalations)).toEqual([ESCALATION_KINDS.UNPUSHED_BRANCH]);
+ });
+
it('honours syncPush: false', () => {
const { steps } = planRepoSync(cleanState({ branches: [branch('feature/x', { ahead: 2 })] }), { syncPush: false });
expect(steps).toEqual([]);
@@ -243,10 +297,19 @@ describe('planRepoSync — returning to the default branch', () => {
expect(kinds(escalations)).toContain(ESCALATION_KINDS.OFF_DEFAULT_BRANCH);
});
- it('will not switch a checkout an agent is running in', () => {
- const { steps, escalations } = planRepoSync(offDefault({ currentBranchMerged: true, activeAgentId: 'agent-7' }));
- expect(stepKinds(steps)).not.toContain('switch-default');
- expect(escalations.find((e) => e.kind === ESCALATION_KINDS.OFF_DEFAULT_BRANCH).detail).toContain('agent-7');
+ it('will not touch a checkout an agent is running in — at all', () => {
+ // Not merely "won't switch": pushing its branch, fast-forwarding under it, or
+ // dropping its stashes all race live work just as badly.
+ const { steps, escalations } = planRepoSync(offDefault({
+ currentBranchMerged: true,
+ activeAgentId: 'agent-7',
+ branches: [branch('feature/x', { current: true, ahead: 3 })],
+ defaultDivergence: { ahead: 0, behind: 2 },
+ stashes: [{ ref: 'stash@{0}', sha: 's0', message: 'wip', superseded: true, reason: 'identical' }]
+ }));
+ expect(steps).toEqual([]);
+ expect(kinds(escalations)).toEqual([ESCALATION_KINDS.AGENT_AT_WORK]);
+ expect(escalations[0].detail).toContain('agent-7');
});
it('will not switch off a branch whose work has not landed', () => {
@@ -309,7 +372,7 @@ describe('classifyStash', () => {
};
it('treats a stash that records no change as redundant', async () => {
- scriptGit([['diff --name-only', { stdout: '' }], ['ls-tree', { exitCode: 128 }]]);
+ scriptGit([['diff --name-only', { stdout: '' }]]);
const verdict = await classifyStash('/repo', 'sha1', 'origin/main');
expect(verdict).toEqual({ superseded: true, reason: 'empty stash (no changes recorded)' });
});
@@ -317,7 +380,6 @@ describe('classifyStash', () => {
it('is redundant when every touched path is identical on the target', async () => {
scriptGit([
['diff --name-only', { stdout: 'a.js\nb.js\n' }],
- ['ls-tree', { exitCode: 128 }],
['diff --quiet', { exitCode: 0 }]
]);
const verdict = await classifyStash('/repo', 'sha1', 'origin/main');
@@ -332,7 +394,6 @@ describe('classifyStash', () => {
it('is NOT redundant when the target differs', async () => {
scriptGit([
['diff --name-only', { stdout: 'a.js\n' }],
- ['ls-tree', { exitCode: 128 }],
['diff --quiet', { exitCode: 1 }]
]);
expect((await classifyStash('/repo', 'sha1', 'origin/main')).superseded).toBe(false);
@@ -342,24 +403,58 @@ describe('classifyStash', () => {
scriptGit([
['diff --name-only', { stdout: 'a.js\n' }],
['ls-tree', { stdout: 'scratch/new.txt\n' }],
- ['diff --quiet', { exitCode: 1 }]
+ ['diff --quiet', { exitCode: 0 }]
]);
await classifyStash('/repo', 'sha1', 'origin/main', 3);
- const diffCall = execGitMock.mock.calls.map(([args]) => args).find((args) => args[0] === 'diff' && args.includes('--quiet'));
- expect(diffCall).toContain('scratch/new.txt');
+ const quiet = execGitMock.mock.calls.map(([args]) => args).filter((args) => args[0] === 'diff' && args.includes('--quiet'));
+ // The untracked path lives ONLY in the stash's third parent. Comparing it
+ // against the stash COMMIT would find it missing on both sides and call the
+ // stash redundant — dropping the untracked work outright.
+ const untrackedProbe = quiet.find((args) => args.includes('scratch/new.txt'));
+ expect(untrackedProbe).toEqual(['diff', '--quiet', 'sha1^3', 'origin/main', '--', 'scratch/new.txt']);
+ const trackedProbe = quiet.find((args) => args.includes('a.js'));
+ expect(trackedProbe).toEqual(['diff', '--quiet', 'sha1', 'origin/main', '--', 'a.js']);
+ });
+
+ it('keeps a stash whose untracked content is NOT on the target', async () => {
+ scriptGit([
+ ['diff --name-only', { stdout: '' }],
+ ['ls-tree', { stdout: 'scratch/new.txt\n' }],
+ ['diff --quiet', { exitCode: 1 }]
+ ]);
+ const verdict = await classifyStash('/repo', 'sha1', 'origin/main', 3);
+ expect(verdict.superseded).toBe(false);
+ expect(verdict.reason).toContain('untracked content differs');
+ });
+
+ it('keeps a stash whose path list could not be read', async () => {
+ // An unreadable path list used to collapse to "no paths", which reads as an
+ // empty stash — and an empty stash is dropped.
+ scriptGit([['diff --name-only', { exitCode: 128 }]]);
+ const verdict = await classifyStash('/repo', 'sha1', 'origin/main');
+ expect(verdict.superseded).toBe(false);
+ expect(verdict.reason).toContain('could not read');
+ });
+
+ it('keeps a -u stash whose third-parent read failed', async () => {
+ scriptGit([['diff --name-only', { stdout: 'a.js\n' }], ['ls-tree', { exitCode: 128 }]]);
+ const verdict = await classifyStash('/repo', 'sha1', 'origin/main', 3);
+ expect(verdict.superseded).toBe(false);
+ expect(verdict.reason).toContain('could not read');
});
it('skips the third-parent read for a stash that has no untracked half', async () => {
// Every stash NOT taken with `-u` has two parents, so `<sha>^3` cannot
// resolve — spawning it is a guaranteed failure per ordinary stash.
scriptGit([['diff --name-only', { stdout: 'a.js\n' }], ['diff --quiet', { exitCode: 0 }]]);
- await classifyStash('/repo', 'sha1', 'origin/main', 2);
+ const verdict = await classifyStash('/repo', 'sha1', 'origin/main', 2);
+ expect(verdict.superseded).toBe(true);
expect(execGitMock.mock.calls.some(([args]) => args[0] === 'ls-tree')).toBe(false);
});
it('refuses to auto-classify a stash too large to compare in one spawn', async () => {
const many = Array.from({ length: MAX_STASH_PATHS_FOR_AUTO_CLASSIFY + 1 }, (_, i) => `f${i}.js`).join('\n');
- scriptGit([['diff --name-only', { stdout: many }], ['ls-tree', { exitCode: 128 }]]);
+ scriptGit([['diff --name-only', { stdout: many }]]);
const verdict = await classifyStash('/repo', 'sha1', 'origin/main');
expect(verdict.superseded).toBe(false);
expect(verdict.reason).toContain('too large');
@@ -367,7 +462,7 @@ describe('classifyStash', () => {
});
it('fails closed when git errors out', async () => {
- scriptGit([['diff --name-only', { stdout: 'a.js\n' }], ['ls-tree', { exitCode: 128 }], ['diff --quiet', { exitCode: 129 }]]);
+ scriptGit([['diff --name-only', { stdout: 'a.js\n' }], ['diff --quiet', { exitCode: 129 }]]);
expect((await classifyStash('/repo', 'sha1', 'origin/main')).superseded).toBe(false);
});
});
@@ -384,6 +479,7 @@ describe('syncRepo — the argv it issues is the non-destructive form', () => {
execGitMock.mockImplementation(async (args) => {
const line = args.join(' ');
if (line.startsWith('rev-parse --git-dir')) return { stdout: '.git', stderr: '', exitCode: 0 };
+ if (line === 'remote') return { stdout: 'origin\n', stderr: '', exitCode: 0 };
if (line.startsWith('rev-list')) return { stdout: divergence, stderr: '', exitCode: 0 };
if (line.startsWith('stash list')) return { stdout: stashList, stderr: '', exitCode: 0 };
return { stdout: '', stderr: '', exitCode: 0 };
@@ -458,6 +554,9 @@ describe('syncRepo — the argv it issues is the non-destructive form', () => {
expect(result.performed).toEqual([]);
expect(result.escalations.map((e) => e.kind)).toEqual([ESCALATION_KINDS.ACTION_FAILED]);
expect(result.escalations[0].detail).toContain('rejected');
+ // Pushed to the resolved upstream, as an explicit <local>:<remote-ref> refspec.
+ const pushCall = execGitMock.mock.calls.map(([a]) => a).find((a) => a[0] === 'push');
+ expect(pushCall).toEqual(['push', 'origin', 'feature/x:feature/x']);
});
it('re-verifies a stash sha before dropping it and skips a shifted ref', async () => {