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: auto-discard lockfile changes in worktree cleanup to prevent false preservation

Adam Eivy6bbf559bdf61
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.js
index 6aaa17eee4..5778c5ba87 100644
--- a/server/services/worktreeManager.js
+++ b/server/services/worktreeManager.js
@@ -16,6 +16,8 @@ import { join } from 'path';
import { ensureDir, PATHS } from '../lib/fileUtils.js';
const WORKTREES_DIR = PATHS.worktrees;
+// Lockfiles that npm/yarn/pnpm modify as a side-effect — safe to discard during worktree cleanup
+const AUTO_GENERATED_LOCKFILES = ['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml'];
/**
* Execute a git command and return stdout
@@ -125,9 +127,22 @@ export async function removeWorktree(agentId, sourceWorkspace, branchName, optio
return { merged: false, removed: false, uncommittedSaved: false, warnings };
}
if (dirtyFiles) {
- console.log(`⚠️ Preserving worktree for ${agentId} — uncommitted changes detected, aborting cleanup to avoid data loss`);
- warnings.push(`Worktree preserved — uncommitted changes detected in ${worktreePath}`);
- return { merged: false, removed: false, uncommittedSaved: false, warnings };
+ // Discard auto-generated lockfile changes that agents don't intend to commit
+ // (e.g., npm install resolving ^version to exact version in package-lock.json)
+ const dirtyList = dirtyFiles.split('\n').filter(l => l.trim());
+ const lockfileChanges = dirtyList.filter(line =>
+ AUTO_GENERATED_LOCKFILES.some(f => line.endsWith(f))
+ );
+ if (lockfileChanges.length > 0 && lockfileChanges.length === dirtyList.length) {
+ // Extract filepath from porcelain output (XY<space>path), handling trimmed first-line
+ const lockfilePaths = lockfileChanges.map(line => line.replace(/^\s*\S+\s+/, ''));
+ console.log(`🧹 Discarding ${lockfileChanges.length} auto-generated lockfile change(s) in worktree ${agentId}`);
+ await execGit(['checkout', '--', ...lockfilePaths], worktreePath);
+ } else {
+ console.log(`⚠️ Preserving worktree for ${agentId} — uncommitted changes detected, aborting cleanup to avoid data loss`);
+ warnings.push(`Worktree preserved — uncommitted changes detected in ${worktreePath}`);
+ return { merged: false, removed: false, uncommittedSaved: false, warnings };
+ }
}
let merged = false;
diff --git a/server/services/worktreeManager.test.js b/server/services/worktreeManager.test.js
index 526bd17699..d606daa7db 100644
--- a/server/services/worktreeManager.test.js
+++ b/server/services/worktreeManager.test.js
@@ -169,6 +169,68 @@ describe('Uncommitted Changes Detection', () => {
});
});
+describe('Auto-generated Lockfile Detection', () => {
+ // Mirrors the lockfile-discard logic in removeWorktree
+ const AUTO_GENERATED_LOCKFILES = ['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml'];
+
+ function allAutoGenerated(porcelainOutput) {
+ const dirtyList = porcelainOutput.trim().split('\n').filter(l => l.trim());
+ if (dirtyList.length === 0) return false;
+ return dirtyList.every(line =>
+ AUTO_GENERATED_LOCKFILES.some(f => line.endsWith(f))
+ );
+ }
+
+ // Mirrors the path extraction regex in removeWorktree
+ function extractPath(porcelainLine) {
+ return porcelainLine.replace(/^\s*\S+\s+/, '');
+ }
+
+ it('should identify package-lock.json as auto-generated', () => {
+ expect(allAutoGenerated(' M autofixer/package-lock.json')).toBe(true);
+ });
+
+ it('should identify yarn.lock as auto-generated', () => {
+ expect(allAutoGenerated(' M yarn.lock')).toBe(true);
+ });
+
+ it('should identify pnpm-lock.yaml as auto-generated', () => {
+ expect(allAutoGenerated(' M pnpm-lock.yaml')).toBe(true);
+ });
+
+ it('should identify nested lockfiles as auto-generated', () => {
+ expect(allAutoGenerated(' M client/package-lock.json')).toBe(true);
+ });
+
+ it('should identify multiple lockfiles as all auto-generated', () => {
+ expect(allAutoGenerated(' M package-lock.json\n M server/package-lock.json')).toBe(true);
+ });
+
+ it('should return false when real files are mixed with lockfiles', () => {
+ expect(allAutoGenerated(' M package-lock.json\n M src/index.js')).toBe(false);
+ });
+
+ it('should return false for non-lockfile changes', () => {
+ expect(allAutoGenerated(' M src/index.js')).toBe(false);
+ });
+
+ it('should return false for empty output', () => {
+ expect(allAutoGenerated('')).toBe(false);
+ });
+
+ it('should extract path from porcelain line with leading space', () => {
+ expect(extractPath(' M autofixer/package-lock.json')).toBe('autofixer/package-lock.json');
+ });
+
+ it('should extract path from trimmed porcelain line (first line after .trim())', () => {
+ expect(extractPath('M autofixer/package-lock.json')).toBe('autofixer/package-lock.json');
+ });
+
+ it('should extract path from untracked file', () => {
+ expect(extractPath('?? package-lock.json')).toBe('package-lock.json');
+ });
+});
+
describe('Orphaned Worktree Detection', () => {
function findOrphanedWorktrees(worktrees, worktreesDir, activeAgentIds) {
return worktrees.filter(wt => {