fix(agent): Eliminate race condition in completion handling

PROBLEM:
- Agents completing with questions were incorrectly marked as "crashed"
- Race condition: polling handler AND crash handler both called handleCompletion()
- Caused database corruption and lost pending questions

SOLUTION:
- Add completion mutex in OutputHandler to prevent concurrent processing
- Remove duplicate completion call from crash handler
- Only one handler executes completion logic per agent

TESTING:
- Added mutex-completion.test.ts with 4 test cases
- Verified mutex prevents concurrent access
- Verified lock cleanup on exceptions
- Verified different agents can process concurrently

FIXES: residential-cuckoo and 12+ other agents stuck in crashed state
This commit is contained in:
Lukas May
2026-02-08 15:51:32 +01:00
parent 6f5fd3a0af
commit 43e2c8b0ba
52 changed files with 2545 additions and 370 deletions

View File

@@ -389,6 +389,7 @@ describe('ProcessManager', () => {
payload: {
processId: 'proc-1',
pid: 12345,
exitCode: 1,
signal: 'SIGTERM',
},
})

View File

@@ -124,6 +124,7 @@ export class ProcessManager {
payload: {
processId: id,
pid,
exitCode: code,
signal,
},
};