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): observe rejected async-hook promises in safeSettle (#1825)

[._.]/ Adam Eivy01a7a3bf8568
diff --git a/server/lib/aiToolkit/runner.js b/server/lib/aiToolkit/runner.js
index f783e6ea51..ff9c7765f5 100644
--- a/server/lib/aiToolkit/runner.js
+++ b/server/lib/aiToolkit/runner.js
@@ -10,9 +10,14 @@ import { analyzeError, analyzeHttpError, ERROR_CATEGORIES } from './errorDetecti
// propagates — these run at out-of-request boundaries where an uncaught throw
// is an unhandled rejection that crashes the process. Each call is isolated so
// a throwing hook can't prevent a later `onComplete` from settling the caller.
+// Handles both a synchronous throw AND a rejected promise from an async hook —
+// the latter would otherwise surface as an unhandled rejection.
function safeSettle(fn, label) {
try {
- fn();
+ const result = fn();
+ if (result && typeof result.then === 'function') {
+ result.catch(err => console.error(`❌ ${label} threw during recovery: ${err.message}`));
+ }
} catch (err) {
console.error(`❌ ${label} threw during recovery: ${err.message}`);
}
diff --git a/server/services/runner.js b/server/services/runner.js
index b4feb80905..cb6dc1a290 100644
--- a/server/services/runner.js
+++ b/server/services/runner.js
@@ -43,9 +43,14 @@ export function setAIToolkit(toolkit, config = {}) {
// propagates — these run at out-of-request boundaries where an uncaught throw
// is an unhandled rejection that crashes the process. Each call is isolated so
// a throwing hook can't prevent a later `onComplete` from settling the caller.
+// Handles both a synchronous throw AND a rejected promise from an async hook —
+// the latter would otherwise surface as an unhandled rejection.
function safeSettle(fn, label) {
try {
- fn();
+ const result = fn();
+ if (result && typeof result.then === 'function') {
+ result.catch(err => console.error(`❌ ${label} threw during recovery: ${err.message}`));
+ }
} catch (err) {
console.error(`❌ ${label} threw during recovery: ${err.message}`);
}
diff --git a/server/services/runner.test.js b/server/services/runner.test.js
index c232ca1df7..29180537d8 100644
--- a/server/services/runner.test.js
+++ b/server/services/runner.test.js
@@ -238,6 +238,35 @@ describe('executeCliRun — close handler crash guard', () => {
expect(onComplete.mock.calls[0][0]).toMatchObject({ success: true });
errorSpy.mockRestore();
});
+
+ it('observes a rejected promise from an async completion hook (no unhandled rejection)', async () => {
+ const child = makeChild();
+ spawn.mockReturnValue(child);
+ const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
+ writeFile.mockResolvedValue(undefined);
+ // An async hook that REJECTS — safeSettle must attach a .catch so it does
+ // not escape as an unhandled rejection, and onComplete must still settle.
+ setAIToolkit(fakeToolkit(), {
+ dataDir: '/tmp/test-runner',
+ hooks: { onRunCompleted: () => Promise.reject(new Error('async hook boom')) },
+ });
+
+ const provider = {
+ id: 'codex', command: 'codex', args: [],
+ defaultModel: 'codex-configured-default', timeout: 5000,
+ };
+ const onComplete = vi.fn();
+ await executeCliRun({ runId: 'run-async-hook-rejects', 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: true });
+ expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('onRunCompleted hook threw during recovery'));
+ errorSpy.mockRestore();
+ });
});
describe('buildCliArgs — claude-code defaultModel honoring', () => {