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

address review (codex): isolate onComplete from a throwing recovery hook (#1825)

[._.]/ Adam Eivyad5404cdc11e
diff --git a/server/lib/aiToolkit/runner.js b/server/lib/aiToolkit/runner.js
index ac00753e50..952fee27bb 100644
--- a/server/lib/aiToolkit/runner.js
+++ b/server/lib/aiToolkit/runner.js
@@ -482,19 +482,25 @@ export function createRunnerService(config = {}) {
} catch (handlerErr) {
console.error(`❌ Run ${runId} failure handler error: ${handlerErr.message}`);
// Still settle callers waiting on onComplete so a persistence/hook
- // failure surfaces as a failed run instead of hanging forever.
+ // failure surfaces as a failed run instead of hanging forever. Isolate
+ // the hook from onComplete — a throwing onRunFailed must NOT prevent
+ // onComplete from settling the caller.
+ const failMetadata = {
+ endTime: new Date().toISOString(),
+ duration: Date.now() - startTime,
+ success: false,
+ error: `Run finalization failed: ${handlerErr.message}`,
+ outputSize: Buffer.byteLength(output),
+ };
try {
- const failMetadata = {
- endTime: new Date().toISOString(),
- duration: Date.now() - startTime,
- success: false,
- error: `Run finalization failed: ${handlerErr.message}`,
- outputSize: Buffer.byteLength(output),
- };
hooks.onRunFailed?.(failMetadata, failMetadata.error, output);
+ } catch (hookErr) {
+ console.error(`❌ Run ${runId} onRunFailed hook threw during recovery: ${hookErr.message}`);
+ }
+ try {
onComplete?.(failMetadata);
} catch (settleErr) {
- console.error(`❌ Run ${runId} failed to settle after failure handler error: ${settleErr.message}`);
+ console.error(`❌ Run ${runId} onComplete threw during recovery: ${settleErr.message}`);
}
}
});
diff --git a/server/services/runner.js b/server/services/runner.js
index 2c0e9886a3..d3a9cf5b47 100644
--- a/server/services/runner.js
+++ b/server/services/runner.js
@@ -282,20 +282,26 @@ export async function executeCliRun({ runId, provider, prompt, workspacePath, on
// The timeout was already cleared and the child has closed, so callers
// waiting on onComplete would hang forever if we only logged. Settle them
// with failure metadata so a disk/hook failure surfaces as a failed run.
+ // The hook and onComplete are isolated from each other — a throwing
+ // onRunFailed must NOT prevent onComplete from settling the caller.
+ const failMetadata = {
+ endTime: new Date().toISOString(),
+ duration: Date.now() - startTime,
+ exitCode: code,
+ success: false,
+ error: `Run finalization failed: ${err.message}`,
+ errorCategory: 'finalization_error',
+ outputSize: Buffer.byteLength(output),
+ };
try {
- const failMetadata = {
- endTime: new Date().toISOString(),
- duration: Date.now() - startTime,
- exitCode: code,
- success: false,
- error: `Run finalization failed: ${err.message}`,
- errorCategory: 'finalization_error',
- outputSize: Buffer.byteLength(output),
- };
runnerConfig.hooks?.onRunFailed?.(failMetadata, failMetadata.error, output);
+ } catch (hookErr) {
+ console.error(`❌ Run ${runId} onRunFailed hook threw during recovery: ${hookErr.message}`);
+ }
+ try {
onComplete?.(failMetadata);
} catch (settleErr) {
- console.error(`❌ Run ${runId} failed to settle after close handler error: ${settleErr.message}`);
+ console.error(`❌ Run ${runId} onComplete threw during recovery: ${settleErr.message}`);
}
}
});
diff --git a/server/services/runner.test.js b/server/services/runner.test.js
index fcbdcb919a..6de24dbefd 100644
--- a/server/services/runner.test.js
+++ b/server/services/runner.test.js
@@ -194,6 +194,37 @@ describe('executeCliRun — close handler crash guard', () => {
expect(onComplete.mock.calls[0][0]).toMatchObject({ success: false, errorCategory: 'finalization_error' });
errorSpy.mockRestore();
});
+
+ it('still settles onComplete when the recovery onRunFailed hook itself throws', async () => {
+ const child = makeChild();
+ spawn.mockReturnValue(child);
+ const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
+ // Metadata write fails, AND the recovery onRunFailed hook throws — the
+ // caller must STILL be settled (the hook must not block onComplete).
+ writeFile
+ .mockResolvedValueOnce(undefined)
+ .mockRejectedValueOnce(new Error('ENOSPC: disk full'));
+ setAIToolkit(fakeToolkit(), {
+ dataDir: '/tmp/test-runner',
+ hooks: { onRunFailed: () => { throw new Error('hook boom'); } },
+ });
+
+ const provider = {
+ id: 'codex', command: 'codex', args: [],
+ defaultModel: 'codex-configured-default', timeout: 5000,
+ };
+
+ const onComplete = vi.fn();
+ await executeCliRun({ runId: 'run-hook-throws', provider, prompt: 'test prompt', workspacePath: '/workspace', onComplete });
+
+ child.stdout.emit('data', Buffer.from('output'));
+ child.emit('close', 0);
+ await new Promise((resolve) => setImmediate(resolve));
+
+ expect(onComplete).toHaveBeenCalledTimes(1);
+ expect(onComplete.mock.calls[0][0]).toMatchObject({ success: false, errorCategory: 'finalization_error' });
+ errorSpy.mockRestore();
+ });
});
describe('buildCliArgs — claude-code defaultModel honoring', () => {