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): kill the PTY and honor once-only onComplete on finish()'s throw path

[._.]/ Adam Eivyc07b25eb90dc
diff --git a/server/lib/tuiPromptRunner.js b/server/lib/tuiPromptRunner.js
index 0498182ba2..3285d8010f 100644
--- a/server/lib/tuiPromptRunner.js
+++ b/server/lib/tuiPromptRunner.js
@@ -279,6 +279,11 @@ ${prompt}`;
const finish = async ({ success, exitCode = 0, error = null, reason = 'completed' }) => {
if (finalized) return;
finalized = true;
+ // Tracks whether the normal-path onComplete was reached, so the catch
+ // below re-surfaces failure ONLY when a step BEFORE onComplete threw —
+ // never re-invoking onComplete when onComplete itself was the throw
+ // source (that would violate the once-only completion contract).
+ let onCompleteInvoked = false;
// finish() is invoked fire-and-forget from PTY/timer callbacks outside the
// request lifecycle (onData, onExit, sendPrompt's write-catch,
// responseFileWatchTimer, hardTimeoutTimer) — none of them await or
@@ -321,9 +326,16 @@ ${prompt}`;
duration: Date.now() - startTime, completionReason: reason,
};
});
+ onCompleteInvoked = true;
onComplete?.({ ...metadata, text: responseText, usedResponseFile, outputTruncated: outputBufferTruncated });
} catch (err) {
console.error(`❌ TUI run ${runId} finish() failed: ${err?.message || err}`);
+ // Ensure the original PTY is torn down even when a cleanup step BEFORE
+ // the kill above threw (e.g. unregisterExternalSession). Otherwise the
+ // failure we report below rejects executeProviderRunOnce and spins up a
+ // fallback provider while the original PTY keeps running — two live runs
+ // for one request. Idempotent: no-op if the kill above already fired.
+ try { if (ptyProcess && !ptyProcess.killed) ptyProcess.kill(); } catch { /* already gone */ }
// A step BEFORE onComplete threw, so the caller's onComplete-driven
// settle never fired. executeProviderRunOnce (promptRunner.js) settles
// its OUTER Promise only via onComplete (→ safeReject) or the returned
@@ -331,17 +343,21 @@ ${prompt}`;
// rejects), so `.catch(safeReject)` won't fire either. Without this,
// resolving the inner promise leaves the central-prompt/pipeline caller
// pending forever — the exact hang this patch exists to prevent. Deliver
- // failure metadata so onComplete → safeReject settles the caller. Guard
- // the callback itself: if IT was the throw source, don't re-throw out of
- // finish() (which is un-awaited — an escape would become an unhandled
- // rejection) and still fall through to resolve().
- try {
- onComplete?.({
- runId, success: false, exitCode, error: `finish() failed: ${err?.message || err}`,
- duration: Date.now() - startTime, completionReason: reason,
- });
- } catch (cbErr) {
- console.error(`❌ TUI run ${runId} onComplete threw during finish() error handling: ${cbErr?.message || cbErr}`);
+ // failure metadata so onComplete → safeReject settles the caller. Skip
+ // this when onComplete itself was the throw source (onCompleteInvoked) —
+ // re-invoking it would break the once-only completion contract and could
+ // emit a contradictory success-then-failure. Guard the callback so a
+ // throw here doesn't escape finish() (un-awaited → unhandled rejection)
+ // and still fall through to resolve().
+ if (!onCompleteInvoked) {
+ try {
+ onComplete?.({
+ runId, success: false, exitCode, error: `finish() failed: ${err?.message || err}`,
+ duration: Date.now() - startTime, completionReason: reason,
+ });
+ } catch (cbErr) {
+ console.error(`❌ TUI run ${runId} onComplete threw during finish() error handling: ${cbErr?.message || cbErr}`);
+ }
}
} finally {
resolve();
diff --git a/server/lib/tuiPromptRunner.test.js b/server/lib/tuiPromptRunner.test.js
index 912946ca6e..c42c43c618 100644
--- a/server/lib/tuiPromptRunner.test.js
+++ b/server/lib/tuiPromptRunner.test.js
@@ -753,6 +753,29 @@ describe('executeTuiRun', () => {
success: false,
error: expect.stringContaining('finish() failed'),
}));
+ expect(onComplete).toHaveBeenCalledTimes(1);
+ // The PTY must still be torn down on the throw path — the failure we
+ // report spins up a fallback provider in the real caller, and a
+ // never-killed original PTY would run alongside it (two live runs).
+ expect(ptyInstances[0].kill).toHaveBeenCalled();
+ });
+
+ it('does not re-invoke onComplete when onComplete itself throws inside finish()', async () => {
+ // When the throw source is onComplete AFTER it has already been reached,
+ // the catch must NOT call onComplete a second time — that would violate
+ // the once-only completion contract and could emit a contradictory
+ // success-then-failure. The run promise must still resolve exactly once.
+ const provider = { id: 'claude', type: 'tui', command: 'echo' };
+ const onComplete = vi.fn(() => { throw new Error('boom: caller onComplete blew up'); });
+ const promise = executeTuiRun({ runId: 'run-oncomplete-throws', provider, prompt: 'a prompt long enough', workspacePath: '/cwd', onComplete, timeout: 60000 });
+ await flushAsync();
+
+ ptyInstances[0].emitExit({ exitCode: 0 });
+
+ await expect(promise).resolves.toBeUndefined();
+ expect(console.error).toHaveBeenCalledWith(expect.stringContaining('finish() failed'));
+ // Reached exactly once (the normal-path call), never re-invoked by the catch.
+ expect(onComplete).toHaveBeenCalledTimes(1);
});
it('finishes with reason "killed" and surfaces the signal in the error when the PTY is terminated', async () => {