fix: Clean up agent worktrees, branches, and logs on dismiss and auto-cleanup
- Track worktree removal success in autoCleanupAfterCompletion() instead of always returning removed:true when removeAgentWorktrees() throws - Add removeAgentBranches() call to auto-cleanup path (agent/* branches were never cleaned after completion) - Add filesystem cleanup (worktrees, branches, logs) to dismiss() to prevent resource leaks until next server restart
This commit is contained in:
@@ -297,7 +297,7 @@ export class CleanupManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Auto-cleanup agent workdir after successful completion.
|
* Auto-cleanup agent workdir after successful completion.
|
||||||
* Removes worktrees and logs but preserves branches and DB record.
|
* Removes worktrees, branches, and logs. Preserves DB record.
|
||||||
*/
|
*/
|
||||||
async autoCleanupAfterCompletion(
|
async autoCleanupAfterCompletion(
|
||||||
agentId: string,
|
agentId: string,
|
||||||
@@ -320,10 +320,18 @@ export class CleanupManager {
|
|||||||
await this.archiveForDebug(alias, agentId);
|
await this.archiveForDebug(alias, agentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let worktreeRemoved = true;
|
||||||
try {
|
try {
|
||||||
await this.removeAgentWorktrees(alias, initiativeId);
|
await this.removeAgentWorktrees(alias, initiativeId);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.warn({ agentId, alias, err: err instanceof Error ? err.message : String(err) }, 'auto-cleanup: failed to remove worktrees');
|
log.warn({ agentId, alias, err: err instanceof Error ? err.message : String(err) }, 'auto-cleanup: failed to remove worktrees');
|
||||||
|
worktreeRemoved = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.removeAgentBranches(alias, initiativeId);
|
||||||
|
} catch (err) {
|
||||||
|
log.warn({ agentId, alias, err: err instanceof Error ? err.message : String(err) }, 'auto-cleanup: failed to remove branches');
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -333,7 +341,7 @@ export class CleanupManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.info({ agentId, alias }, 'auto-cleanup: workdir and logs removed');
|
log.info({ agentId, alias }, 'auto-cleanup: workdir and logs removed');
|
||||||
return { clean: true, removed: true };
|
return { clean: true, removed: worktreeRemoved };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -855,6 +855,17 @@ export class MultiProviderAgentManager implements AgentManager {
|
|||||||
log.info({ agentId, name: agent.name }, 'dismissing agent');
|
log.info({ agentId, name: agent.name }, 'dismissing agent');
|
||||||
|
|
||||||
this.cleanupAgentState(agentId);
|
this.cleanupAgentState(agentId);
|
||||||
|
this.commitRetryCount.delete(agentId);
|
||||||
|
|
||||||
|
// Best-effort filesystem cleanup
|
||||||
|
try { await this.cleanupManager.removeAgentWorktrees(agent.name, agent.initiativeId); }
|
||||||
|
catch (err) { log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'dismiss: failed to remove worktrees'); }
|
||||||
|
|
||||||
|
try { await this.cleanupManager.removeAgentBranches(agent.name, agent.initiativeId); }
|
||||||
|
catch (err) { log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'dismiss: failed to remove branches'); }
|
||||||
|
|
||||||
|
try { await this.cleanupManager.removeAgentLogs(agent.name); }
|
||||||
|
catch (err) { log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'dismiss: failed to remove logs'); }
|
||||||
|
|
||||||
await this.repository.update(agentId, {
|
await this.repository.update(agentId, {
|
||||||
userDismissedAt: new Date(),
|
userDismissedAt: new Date(),
|
||||||
|
|||||||
Reference in New Issue
Block a user