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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): isolate success-path hooks so a throwing onRunCompleted can't flip a successful run to failed (#1825)
[._.]/ Adam Eivyd86de7fc6c4b
diff --git a/server/lib/aiToolkit/runner.js b/server/lib/aiToolkit/runner.jsindex 7f36457ff8..f783e6ea51 100644--- a/server/lib/aiToolkit/runner.js+++ b/server/lib/aiToolkit/runner.js@@ -293,13 +293,15 @@ export function createRunnerService(config = {}) {await atomicWrite(metadataPath, metadata);+ // Isolate the completion hooks + onComplete from the outer catch — a+ // throwing onRunCompleted must NOT be reinterpreted as a finalization+ // failure that flips a successful run to success:false for the caller.if (metadata.success) {- hooks.onRunCompleted?.(metadata, output);+ safeSettle(() => hooks.onRunCompleted?.(metadata, output), `Run ${runId} onRunCompleted hook`);} else {- hooks.onRunFailed?.(metadata, metadata.error, output);+ safeSettle(() => hooks.onRunFailed?.(metadata, metadata.error, output), `Run ${runId} onRunFailed hook`);}-- onComplete?.(metadata);+ safeSettle(() => onComplete?.(metadata), `Run ${runId} onComplete`);} catch (err) {console.error(`❌ Run ${runId} close handler error: ${err.message}`);const failMetadata = {@@ -507,8 +509,10 @@ export function createRunnerService(config = {}) {await atomicWrite(metadataPath, metadata);- hooks.onRunFailed?.(metadata, metadata.error, output);- onComplete?.(metadata);+ // Isolate the hook + onComplete so a throwing onRunFailed doesn't+ // bounce into the recovery path and call onRunFailed a second time.+ safeSettle(() => hooks.onRunFailed?.(metadata, metadata.error, output), `Run ${runId} onRunFailed hook`);+ safeSettle(() => onComplete?.(metadata), `Run ${runId} onComplete`);} catch (handlerErr) {console.error(`❌ Run ${runId} failure handler error: ${handlerErr.message}`);// Still settle callers waiting on onComplete so a persistence/hookdiff --git a/server/services/runner.js b/server/services/runner.jsindex 739fddbcfb..b4feb80905 100644--- a/server/services/runner.js+++ b/server/services/runner.js@@ -285,13 +285,15 @@ export async function executeCliRun({ runId, provider, prompt, workspacePath, onawait writeFile(metadataPath, JSON.stringify(metadata, null, 2));+ // Isolate the completion hooks + onComplete from the outer catch — a+ // throwing onRunCompleted must NOT be reinterpreted as a finalization+ // failure that flips a successful run to success:false for the caller.if (metadata.success) {- runnerConfig.hooks?.onRunCompleted?.(metadata, output);+ safeSettle(() => runnerConfig.hooks?.onRunCompleted?.(metadata, output), `Run ${runId} onRunCompleted hook`);} else {- runnerConfig.hooks?.onRunFailed?.(metadata, metadata.error, output);+ safeSettle(() => runnerConfig.hooks?.onRunFailed?.(metadata, metadata.error, output), `Run ${runId} onRunFailed hook`);}-- onComplete?.(metadata);+ safeSettle(() => onComplete?.(metadata), `Run ${runId} onComplete`);} catch (err) {console.error(`❌ Run ${runId} close handler error: ${err.message}`);// The timeout was already cleared and the child has closed, so callersdiff --git a/server/services/runner.test.js b/server/services/runner.test.jsindex cb0837ee5a..c232ca1df7 100644--- a/server/services/runner.test.js+++ b/server/services/runner.test.js@@ -210,6 +210,34 @@ describe('executeCliRun — close handler crash guard', () => {expect(onComplete.mock.calls[0][0]).toMatchObject({ success: false, errorCategory: 'finalization_error' });errorSpy.mockRestore();});++ it('does not flip a successful run to failed when onRunCompleted throws', async () => {+ const child = makeChild();+ spawn.mockReturnValue(child);+ const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});+ // Writes succeed (success path runs); the success hook throws — the caller+ // must still receive success:true, not a finalization-failure flip.+ writeFile.mockResolvedValue(undefined);+ setAIToolkit(fakeToolkit(), {+ dataDir: '/tmp/test-runner',+ hooks: { onRunCompleted: () => { 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-success-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: true });+ errorSpy.mockRestore();+ });});describe('buildCliArgs — claude-code defaultModel honoring', () => {