fix: proper container lifecycle management to prevent stopped container accumulation

- Name containers (nanoclaw-{group}-{timestamp}) for trackability
- Replace SIGKILL timeout with graceful `container stop` so --rm fires
- Add startup sweep to clean up stopped nanoclaw containers from previous runs

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
gavrielc
2026-02-06 07:10:26 +02:00
parent 3a4d340f80
commit db216a459e
2 changed files with 62 additions and 11 deletions

View File

@@ -832,6 +832,24 @@ function ensureContainerSystemRunning(): void {
throw new Error('Apple Container system is required but failed to start');
}
}
// Clean up stopped NanoClaw containers from previous runs
try {
const output = execSync('container ls -a --format {{.Names}}', {
stdio: ['pipe', 'pipe', 'pipe'],
encoding: 'utf-8',
});
const stale = output
.split('\n')
.map((n) => n.trim())
.filter((n) => n.startsWith('nanoclaw-'));
if (stale.length > 0) {
execSync(`container rm ${stale.join(' ')}`, { stdio: 'pipe' });
logger.info({ count: stale.length }, 'Cleaned up stopped containers');
}
} catch {
// No stopped containers or ls/rm not supported
}
}
async function main(): Promise<void> {