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: stop orphan fallback when merged inventory is unreadable (#13405)

[._.]/ Adam Eivyc3bfb767b297
diff --git a/server/services/autonomousJobs.test.js b/server/services/autonomousJobs.test.js
index ba0ce542e4..6b45d54a6f 100644
--- a/server/services/autonomousJobs.test.js
+++ b/server/services/autonomousJobs.test.js
@@ -1254,13 +1254,6 @@ describe('autonomousJobs', () => {
expect(opts.activeAgentIds().has('live-agent-99')).toBe(true)
})
- it('does not fall through to orphan cleanup after a failed merged sweep', async () => {
- reapMergedWorktrees.mockRejectedValueOnce(new Error('synthetic sweep failure'))
- await expect(agentDataCleanup()).resolves.toMatchObject({ mergedReaped: 0, worktreesReaped: 0,
- worktreeHolds: [{ reason: 'merged-worktree-reap-failed' }] })
- expect(cleanupOrphanedWorktrees).not.toHaveBeenCalled()
- })
-
it('skips every reap when the stored statuses cannot be read (a paused agent could look orphaned)', async () => {
listStoredAgentStatuses.mockRejectedValueOnce(new Error('database unavailable'))
diff --git a/server/services/retirementAppScope.test.js b/server/services/retirementAppScope.test.js
index 686158e70f..11d6f6e9a8 100644
--- a/server/services/retirementAppScope.test.js
+++ b/server/services/retirementAppScope.test.js
@@ -10,7 +10,7 @@ import { materializeGitRepo, SKIP_HEAVY_INTEGRATION } from '../lib/gitTestRepo.j
import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../lib/mockPathsDataRoot.js';
import { PATHS } from '../lib/paths.js';
-const injected = vi.hoisted(() => ({ app: null, apps: null, schedule: null, scheduleWrites: 0, failScheduleWrite: null, failOrphanInventory: false, inventoryFailureArmed: false, inventoryFailures: 0 }));
+const injected = vi.hoisted(() => ({ app: null, apps: null, schedule: null, scheduleWrites: 0, failScheduleWrite: null, failOrphanInventory: false, failMergedInventory: false, inventoryFailureArmed: false, inventoryFailures: 0 }));
vi.mock('../lib/paths.js', async importOriginal => makePathsProxy(await importOriginal(), {
dataRoot: () => realpathSync.native(lazyTempDataRoot('portos-cleanup-receipts-')),
extraOverrides: () => ({ root: injected.app?.repoPath || lazyTempDataRoot('portos-cleanup-receipts-') }),
@@ -19,6 +19,10 @@ afterAll(cleanupTempDataRoots);
vi.mock('../lib/execGit.js', async importOriginal => {
const actual = await importOriginal();
return { ...actual, execGit: async (args, cwd, options) => {
+ if (injected.failMergedInventory && args[0] === 'fetch') {
+ injected.failMergedInventory = false;
+ injected.inventoryFailureArmed = true;
+ }
if (injected.inventoryFailureArmed && args[0] === 'worktree' && args[1] === 'list') {
injected.inventoryFailureArmed = false;
injected.inventoryFailures++;
@@ -52,7 +56,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('outside reconciliation retirement recei
let scratch;
let repo;
beforeEach(async () => {
- Object.assign(injected, { app: null, apps: null, schedule: { tasks: {}, executions: {} }, scheduleWrites: 0, failScheduleWrite: null, failOrphanInventory: false, inventoryFailureArmed: false, inventoryFailures: 0 });
+ Object.assign(injected, { app: null, apps: null, schedule: { tasks: {}, executions: {} }, scheduleWrites: 0, failScheduleWrite: null, failOrphanInventory: false, failMergedInventory: false, inventoryFailureArmed: false, inventoryFailures: 0 });
scratch = realpathSync.native(await mkdtemp(join(tmpdir(), 'portos-cleanup-repo-')));
await mkdir(join(scratch, 'repo'));
await materializeGitRepo(join(scratch, 'repo'), { identity: { email: '[email protected]', name: 'Test' } });
@@ -119,6 +123,20 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('outside reconciliation retirement recei
expect(outcome.retirementObligations, JSON.stringify(result)).toContainEqual(expect.objectContaining({ path: release.path, head: release.head }));
});
+ it('does not retry a retained checkout through orphan cleanup when the merged inventory read fails', async () => {
+ const { release, run } = await outsideCleanupFixture('daily cleanup');
+ injected.failScheduleWrite = injected.scheduleWrites + 3;
+ expect(await run()).toMatchObject({ mergedReaped: 0, worktreesReaped: 0 });
+ injected.failMergedInventory = true;
+ expect(await run()).toMatchObject({ mergedReaped: 0, worktreesReaped: 0,
+ worktreeHolds: [{ reason: 'merged-worktree-reap-failed' }] });
+ expect(injected.inventoryFailures).toBe(1);
+ expect(existsSync(release.path)).toBe(true);
+ expect(await registered(release.path)).toBe(true);
+ expect((await execGit(['rev-parse', release.branch], repo)).stdout.trim()).toBe(release.head);
+ expect((await getBranchReconcileOutcome('app-1')).retired || []).toEqual([]);
+ });
+
it('retains a failed daily preservation even when orphan inventory fails and external discovery retries it', async () => {
const { release, run } = await outsideCleanupFixture('daily cleanup');
injected.failScheduleWrite = injected.scheduleWrites + 3;
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.js
index a19cb2b0c5..2fe0e511b4 100644
--- a/server/services/worktreeManager.js
+++ b/server/services/worktreeManager.js
@@ -2187,7 +2187,9 @@ export async function reapMergedWorktrees(sourceWorkspace, {
{ path: claudeTreesRoot, requireAgentId: false },
];
- const worktrees = await listWorktrees(sourceWorkspace).catch(() => []);
+ // An unreadable inventory is not an empty successful sweep: callers must
+ // retain work rather than falling through to receipt-free orphan cleanup.
+ const worktrees = await listWorktrees(sourceWorkspace);
const reaped = [];
const skipped = [];