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): close the probe-echo/commandInjected race in waitForPromptReady

[._.]/ Adam Eivyfe94ed8eb75a
diff --git a/server/services/shell.js b/server/services/shell.js
index 6142c951c9..cb08c2c225 100644
--- a/server/services/shell.js
+++ b/server/services/shell.js
@@ -250,8 +250,29 @@ export function createShellSession(socket, options = {}) {
// start observing claude's input-readiness ONLY after the real command is
// in flight — so the readiness probe's own shell activity (below) can't
// prematurely satisfy its bracketed-paste gate.
+ //
+ // For the waitForPromptReady path, `sendInitial` runs from the probe's
+ // `ptyProcess.onData` listener (below), registered AFTER the main output
+ // listener above that dispatches `session.onData` via `runHook`/
+ // `session.hookQueue`. node-pty invokes same-event listeners in
+ // registration order, but `runHook` only QUEUES the caller's onData
+ // handler as a microtask — it doesn't run it inline. So calling
+ // `options.onInitialCommandSent?.()` synchronously here would fire it
+ // BEFORE the caller's onData handler has actually processed the very
+ // chunk that proved the probe round-tripped, even though that handler was
+ // enqueued first. A consumer gating output on "has the command been
+ // injected yet" (see agentTuiSpawning.js's commandInjected) would then
+ // misread the probe's own echoed marker as post-injection output. Routing
+ // this callback through the SAME `session.hookQueue` guarantees it runs
+ // only after that chunk's onData handler has finished — while the actual
+ // PTY write below stays synchronous/immediate, unaffected.
const sendInitial = () => {
- options.onInitialCommandSent?.();
+ const session = shellSessions.get(sessionId);
+ if (session && options.onInitialCommandSent) {
+ runHook('onInitialCommandSent', session, options.onInitialCommandSent);
+ } else {
+ options.onInitialCommandSent?.();
+ }
submitToSession(sessionId, initialCommand);
};
if (options.waitForPromptReady) {
diff --git a/server/services/shell.test.js b/server/services/shell.test.js
index 19bad6ed48..991c3b412e 100644
--- a/server/services/shell.test.js
+++ b/server/services/shell.test.js
@@ -8,15 +8,29 @@ vi.mock('node-pty', () => ({
}));
const makeFakePty = () => {
+ // Real node-pty's onData supports MULTIPLE independent listeners (each
+ // registration returns its own IDisposable), all fired in registration
+ // order for every chunk — shell.js relies on this directly (one listener
+ // dispatches the caller's onData hook, a second — for waitForPromptReady —
+ // watches for the readiness-probe marker). A single-slot `_dataHandler`
+ // that the second registration overwrites would silently drop the first
+ // listener and make it impossible to test the interaction between them
+ // (see the onInitialCommandSent-ordering regression test below).
+ const dataHandlers = [];
const fake = {
- _dataHandler: null,
_exitHandler: null,
- onData: vi.fn((fn) => { fake._dataHandler = fn; }),
+ onData: vi.fn((fn) => {
+ dataHandlers.push(fn);
+ return { dispose: () => {
+ const idx = dataHandlers.indexOf(fn);
+ if (idx !== -1) dataHandlers.splice(idx, 1);
+ } };
+ }),
onExit: vi.fn((fn) => { fake._exitHandler = fn; }),
write: vi.fn(),
resize: vi.fn(),
kill: vi.fn(),
- emitData: (chunk) => fake._dataHandler?.(chunk),
+ emitData: (chunk) => { for (const fn of [...dataHandlers]) fn(chunk); },
emitExit: (payload) => fake._exitHandler?.(payload),
};
ptyInstances.push(fake);
@@ -189,7 +203,7 @@ describe('createShellSession', () => {
vi.useRealTimers();
});
- it('waitForPromptReady: holds initialCommand until the readiness probe round-trips, and fires onInitialCommandSent', () => {
+ it('waitForPromptReady: holds initialCommand until the readiness probe round-trips, and fires onInitialCommandSent', async () => {
vi.useFakeTimers();
const onInitialCommandSent = vi.fn();
shell.createShellSession(makeSocket(), { shell: '/bin/zsh', initialCommand: 'claude', waitForPromptReady: true, initialCommandDelayMs: 8000, onInitialCommandSent });
@@ -208,12 +222,58 @@ describe('createShellSession', () => {
expect(pty.write).not.toHaveBeenCalledWith('claude\r');
expect(onInitialCommandSent).not.toHaveBeenCalled();
// The assembled marker appears in the OUTPUT → shell proven, command injected.
+ // The actual PTY write stays synchronous, but onInitialCommandSent is now
+ // routed through the same per-session hook queue as onData (see shell.js
+ // sendInitial) so a native microtask tick — NOT the faked setImmediate
+ // flushMicrotasks() uses elsewhere — is needed before it's observed.
pty.emitData(`${marker}\n`);
expect(pty.write).toHaveBeenCalledWith('claude\r');
+ await Promise.resolve();
expect(onInitialCommandSent).toHaveBeenCalledTimes(1);
vi.useRealTimers();
});
+ // Regression pin for codex review [P1] (#4638): the probe listener and the
+ // main onData listener are both registered on ptyProcess.onData for the
+ // SAME chunk, but onData is only dispatched via the async session.hookQueue
+ // (runHook), not called inline. Firing onInitialCommandSent synchronously
+ // from the probe listener would therefore let it "jump the queue" ahead of
+ // the marker chunk's own onData call — a consumer gating output on "has the
+ // command been injected yet" would misread the probe's own echo as
+ // post-injection output. This pins that onInitialCommandSent only observes
+ // AFTER the marker chunk's onData handler has fully settled, even when that
+ // handler is itself async and slow.
+ it('waitForPromptReady: onInitialCommandSent fires only after the marker chunk\'s onData handler settles', async () => {
+ vi.useFakeTimers();
+ const order = [];
+ const onData = vi.fn(async () => {
+ await Promise.resolve();
+ await Promise.resolve();
+ order.push('onData');
+ });
+ const onInitialCommandSent = vi.fn(() => { order.push('onInitialCommandSent'); });
+ shell.createShellSession(makeSocket(), { shell: '/bin/zsh', initialCommand: 'claude', waitForPromptReady: true, initialCommandDelayMs: 8000, onData, onInitialCommandSent });
+ const pty = ptyInstances[0];
+ vi.advanceTimersByTime(50);
+ const probeCall = pty.write.mock.calls.find(([d]) => /printf/.test(d));
+ const nonce = probeCall[0].match(/PORTOSRDY''([0-9a-f]+)'/)[1];
+ const marker = `PORTOSRDY${nonce}`;
+ pty.emitData(`${marker}\n`);
+ // The PTY write for the real command stays synchronous/immediate...
+ expect(pty.write).toHaveBeenCalledWith('claude\r');
+ // ...but onInitialCommandSent must wait for onData's queued (and here,
+ // deliberately slow) handler to finish first.
+ expect(onInitialCommandSent).not.toHaveBeenCalled();
+ // Adopting a thenable into a promise chain costs extra microtask ticks per
+ // link (PromiseResolveThenableJob), and this chain has several links
+ // (onData's own two awaits, runHook's Promise.resolve(fn(arg)) wrapper,
+ // the .catch() continuation, then onInitialCommandSent's own link) — so
+ // drain generously rather than hand-count an exact number of ticks.
+ for (let i = 0; i < 20; i++) await Promise.resolve();
+ expect(order).toEqual(['onData', 'onInitialCommandSent']);
+ vi.useRealTimers();
+ });
+
it('waitForPromptReady: injects the command on the fallback timeout if the probe never round-trips', () => {
vi.useFakeTimers();
shell.createShellSession(makeSocket(), { shell: '/bin/zsh', initialCommand: 'claude', waitForPromptReady: true, initialCommandDelayMs: 8000 });
@@ -240,7 +300,7 @@ describe('createShellSession', () => {
vi.useRealTimers();
});
- it('waitForPromptReady: uses a Write-Output probe on PowerShell and round-trips it the same way', () => {
+ it('waitForPromptReady: uses a Write-Output probe on PowerShell and round-trips it the same way', async () => {
vi.useFakeTimers();
const onInitialCommandSent = vi.fn();
shell.createShellSession(makeSocket(), { shell: 'pwsh.exe', initialCommand: 'claude', waitForPromptReady: true, initialCommandDelayMs: 8000, onInitialCommandSent });
@@ -257,11 +317,13 @@ describe('createShellSession', () => {
expect(onInitialCommandSent).not.toHaveBeenCalled();
pty.emitData(`${marker}\r\n`);
expect(pty.write).toHaveBeenCalledWith('claude\r');
+ // See the native-microtask-tick comment in the zsh variant of this test above.
+ await Promise.resolve();
expect(onInitialCommandSent).toHaveBeenCalledTimes(1);
vi.useRealTimers();
});
- it('waitForPromptReady: cmd.exe has no probe and relies solely on the fallback timeout', () => {
+ it('waitForPromptReady: cmd.exe has no probe and relies solely on the fallback timeout', async () => {
vi.useFakeTimers();
const onInitialCommandSent = vi.fn();
shell.createShellSession(makeSocket(), { shell: 'cmd.exe', initialCommand: 'claude', waitForPromptReady: true, initialCommandDelayMs: 8000, onInitialCommandSent });
@@ -273,6 +335,8 @@ describe('createShellSession', () => {
expect(pty.write).not.toHaveBeenCalledWith('claude\r');
vi.advanceTimersByTime(1); // total elapsed: 8000
expect(pty.write).toHaveBeenCalledWith('claude\r');
+ // See the native-microtask-tick comment on the zsh waitForPromptReady test above.
+ await Promise.resolve();
expect(onInitialCommandSent).toHaveBeenCalledTimes(1);
vi.useRealTimers();
});