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

compare a superseded verdict's dirty paths through one helper on both sides

[._.]/ Adam Eivy055d6f521ad7
diff --git a/server/services/branchReconcile.js b/server/services/branchReconcile.js
index 89a91377a4..36f7866bed 100644
--- a/server/services/branchReconcile.js
+++ b/server/services/branchReconcile.js
@@ -32,7 +32,7 @@ import { getOriginInfo } from '../lib/gitRemote.js';
import { githubRepoSpec, githubApiHost } from '../lib/workTracker.js';
import { safeJSONParse, PATHS } from '../lib/fileUtils.js';
import { PROTECTED_BRANCHES } from '../lib/gitArgs.js';
-import { readVerdictLedger, partitionSuperseded, recordVerdictInstruction, recordVerdict, sameSet } from './supersededLedger.js';
+import { readVerdictLedger, partitionSuperseded, recordVerdictInstruction, recordVerdict, sameDirtyPaths } from './supersededLedger.js';
import { backupSupersededBranch } from './supersededBackup.js';
// Never reconciled — the canonical long-lived-branch set (`main`/`master`/`dev`/
@@ -953,7 +953,9 @@ export async function reapSupersededBranches(repoPath, defaultBranch, superseded
for (const b of superseded || []) {
const verdict = b.verdict || {};
- if (!b.tip || b.tip !== verdict.tip || !sameSet(b.dirtyPaths || [], verdict.dirtyPaths || [])) {
+ // Same comparison isVerdictFresh makes, via the same helper — a raw compare
+ // here would hold a pre-upgrade entry that the partition just accepted.
+ if (!b.tip || b.tip !== verdict.tip || !sameDirtyPaths(verdict.dirtyPaths, b.dirtyPaths)) {
hold(b, { reason: 'verdict-does-not-match-branch' });
continue;
}
diff --git a/server/services/branchReconcile.test.js b/server/services/branchReconcile.test.js
index bcf105d31e..7f4015583f 100644
--- a/server/services/branchReconcile.test.js
+++ b/server/services/branchReconcile.test.js
@@ -1205,6 +1205,38 @@ describe('reconcile — cached SUPERSEDED verdicts (#3842)', () => {
expect(res.superseded.map((b) => b.branch)).toEqual([BRANCH]);
});
+ // Cross-version: this install's ledger predates classifyWorktreeDirt subtracting
+ // PortOS's own runtime scratch, so its recorded dirtyPaths list it while the live
+ // side no longer does. Both the partition and the reap must subtract it — a raw
+ // compare in either one strands the branch it was written to retire.
+ it('reaps a verdict recorded before the scratch subtraction shipped', async () => {
+ setupAbandoned();
+ tryReadFileMock.mockResolvedValue(ledger({
+ dirtyPaths: ['server/services/thing.js', 'PORTOS_PUBLIC_REVIEW_INPUT.json']
+ }));
+
+ const res = await reconcile('/repo', { activeAgentIds: new Set() });
+ expect(res.reapedSuperseded).toEqual([BRANCH]);
+ expect(res.skipped).not.toContainEqual(
+ expect.objectContaining({ branch: BRANCH, reason: 'verdict-does-not-match-branch' })
+ );
+ });
+
+ it('holds a branch whose verdict names a genuinely different change set', async () => {
+ setupAbandoned();
+ tryReadFileMock.mockResolvedValue(ledger());
+ // Freshness passed on the gathered entry, but the entry handed to the reap
+ // disagrees with its own verdict — the contract check a direct caller needs.
+ const { reapSupersededBranches } = await import('./branchReconcile.js');
+ const out = await reapSupersededBranches('/repo', 'main', [{
+ branch: BRANCH, tip: 'aaaaaaa', worktreePath: WORKTREE, dirtyPaths: ['other.js'],
+ verdict: { tip: 'aaaaaaa', dirtyPaths: ['server/services/thing.js'] }
+ }], { activeAgentIds: new Set() });
+ expect(out.reaped).toEqual([]);
+ expect(out.skipped).toEqual([{ branch: BRANCH, reason: 'verdict-does-not-match-branch' }]);
+ expect(backupSupersededBranchMock).not.toHaveBeenCalled();
+ });
+
it('re-analyzes when the branch tip moved', async () => {
setupAbandoned({ tip: 'bbbbbbb' });
tryReadFileMock.mockResolvedValue(ledger());
diff --git a/server/services/supersededBackup.js b/server/services/supersededBackup.js
index 7df33dad2a..ff272dbc3b 100644
--- a/server/services/supersededBackup.js
+++ b/server/services/supersededBackup.js
@@ -23,6 +23,7 @@
* survives binary content and needs no patch tooling to read back.
*/
+import { createHash } from 'node:crypto';
import { cp, stat, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { atomicWrite, ensureDir, PATHS } from '../lib/fileUtils.js';
@@ -32,8 +33,18 @@ import { execGitSafe } from './git.js';
/** Where an install keeps its pre-reap backups. */
export const backupRoot = (cosDir = PATHS.cos) => join(cosDir, 'abandoned-worktree-backups');
-/** Branch name → a filesystem-safe directory name. Pure. */
-export const backupSlug = (branch) => kebabCase(String(branch || '')) || 'branch';
+/**
+ * Branch name → a filesystem-safe, collision-free directory name. Pure.
+ *
+ * The hash suffix is not decoration: `kebabCase` maps `feature/foo-bar` and
+ * `feature/foo/bar` to the same slug, and reaping the second would overwrite the
+ * first's backup — which by then is the only remaining copy of that branch.
+ */
+export const backupSlug = (branch) => {
+ const name = String(branch || '');
+ const digest = createHash('sha256').update(name).digest('hex').slice(0, 8);
+ return `${kebabCase(name) || 'branch'}-${digest}`;
+};
// A single untracked file above this is almost certainly build output or a model
// artifact rather than authored work; it is named in the manifest instead of
diff --git a/server/services/supersededBackup.test.js b/server/services/supersededBackup.test.js
index 139c297f6c..b9bbda08d2 100644
--- a/server/services/supersededBackup.test.js
+++ b/server/services/supersededBackup.test.js
@@ -54,6 +54,9 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('backupSupersededBranch', () => {
);
expect(result.dir).toBe(join(backupRoot(cosDir), backupSlug(BRANCH)));
+ // Two branch names that kebab-case identically must not share a backup dir —
+ // the second reap would overwrite the only surviving copy of the first.
+ expect(backupSlug('feature/foo-bar')).not.toBe(backupSlug('feature/foo/bar'));
// The patches' CONTENT is proven by the replay test below, which applies
// them; here it is enough that both were written.
diff --git a/server/services/supersededLedger.js b/server/services/supersededLedger.js
index be4b20c3f1..1e1a8b21ae 100644
--- a/server/services/supersededLedger.js
+++ b/server/services/supersededLedger.js
@@ -124,17 +124,32 @@ export function isVerdictFresh(entry, branch, { replacedByPresent = true, repoPa
if (!replacedByPresent) return false;
if (!entry.tip || entry.tip !== branch.tip) return false;
if (!sameSet(entry.collisionPaths, branch.collisionPaths)) return false;
- // A verdict recorded before `classifyWorktreeDirt` learned to subtract PortOS's
- // own runtime scratch lists that scratch among its dirty paths, while the live
- // side no longer does. Comparing raw would expire every such entry and re-pay
- // the coordinator analysis that the ledger exists to avoid — so subtract on
- // both sides. Nothing else changes: a live list never contains scratch.
- return sameSet(
- (entry.dirtyPaths || []).filter((path) => !isAgentScratchPath(path)),
- (branch.dirtyPaths || []).filter((path) => !isAgentScratchPath(path))
- );
+ return sameDirtyPaths(entry.dirtyPaths, branch.dirtyPaths);
}
+/**
+ * Do a ledger entry's recorded dirty paths still describe the branch's live ones?
+ *
+ * Subtracts PortOS's own runtime scratch from BOTH sides. A verdict recorded
+ * before `classifyWorktreeDirt` learned to subtract that scratch lists it among
+ * its dirty paths while the live side no longer does; comparing raw would expire
+ * every such entry and re-pay the coordinator analysis the ledger exists to
+ * avoid. Nothing else changes — a live list never contains scratch.
+ *
+ * Exported because `reapSupersededBranches` re-checks the same pairing before it
+ * deletes anything: comparing raw there would pass the partition and then hold
+ * the branch forever as "verdict-does-not-match-branch", which is the accumulation
+ * this whole path exists to end.
+ *
+ * @param {string[]} recorded
+ * @param {string[]} live
+ * @returns {boolean}
+ */
+export const sameDirtyPaths = (recorded = [], live = []) => sameSet(
+ (recorded || []).filter((path) => !isAgentScratchPath(path)),
+ (live || []).filter((path) => !isAgentScratchPath(path))
+);
+
/**
* Split the in-flight set into branches still worth analyzing and branches whose
* SUPERSEDED verdict is cached and still fresh. Pure — reachability of each