fix(zellij): injectIntoTab silently typed into the wrong pane on missing tab

Real safety bug surfaced while auditing the home/ dispatch path. When
injectIntoTab was asked to write into a tab that didn't exist (closed,
typo'd, zellij not reachable from the caller's env), the sequence:

  zellij action go-to-tab-name <missing>    # no-op, exits 0
  waitForTabFocus(missing)                  # times out, returns void
  zellij action write-chars <prompt>        # → types into whatever IS focused

…would type the prompt into the user's CURRENTLY-FOCUSED pane, which on
a long-running worker process is almost certainly NOT the intended
agent tab. Could garble a private terminal session, paste secrets, or
trigger a wrong agent. Silent failure — every caller's try/catch
considered it a success.

  src/lib/zellij.ts
    waitForTabFocus now returns boolean (true = focused, false = timeout)
    injectIntoTab throws when waitForTabFocus returns false
    sendRawKey throws likewise (same wrong-pane risk for raw key codes)

Error message names the tab + hints at the likely cause:
  "injectIntoTab: zellij tab "<name>" did not gain focus within 1s —
   is the tab open and zellij reachable?"

Caller impact: every existing call site (orchestration/run, inject,
agent/launch, switch-agent, plus home/worker.ts) already wraps
injectIntoTab in try/catch, so the new throw cleanly surfaces as a
500 / worker.crashed event instead of corrupting an unrelated pane.

All five home/ self-test suites still green: state 8/8, worker 6/6,
decide 10/10, render 12/12, projects 7/7.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Opus 4.7 committed 2026-05-20 03:58:54 +02:00
1 parent b8fecb9a79
commit 5cd54d586c
1 file changed
+21 -5
+21 -5
View File
@@ -44,16 +44,24 @@ function getCurrentTab(): string | null {
}
}
/** Poll dump-layout until the named tab has focus, up to maxWaitMs. */
function waitForTabFocus(tab: string, maxWaitMs = 1000): void {
/**
* Poll dump-layout until the named tab has focus, up to maxWaitMs.
* Returns true on success, false on timeout. Callers that mutate the
* focused pane (write-chars, etc.) MUST check the return value — if the
* tab doesn't exist or zellij isn't reachable, the polled focus never
* matches and writing characters then types into whatever pane IS focused,
* which can be the user's private terminal session.
*/
function waitForTabFocus(tab: string, maxWaitMs = 1000): boolean {
const deadline = Date.now() + maxWaitMs;
while (Date.now() < deadline) {
try {
const active = execFileSync("bash", ["-c", DUMP_CMD], { timeout: 2000 }).toString().trim();
if (active === tab) return;
if (active === tab) return true;
} catch { /* dump-layout unavailable or parse failed — fall through */ }
execSync("sleep 0.05");
}
return false;
}
/**
@@ -85,7 +93,9 @@ export function isUserTypingInTab(tab: string): boolean {
export function sendRawKey(tab: string, keyCode: number): void {
const originalTab = getCurrentTab();
execSync(`zellij action go-to-tab-name ${shellEscape(tab)}`);
waitForTabFocus(tab);
if (!waitForTabFocus(tab)) {
throw new Error(`sendRawKey: zellij tab "${tab}" did not gain focus within 1s — is the tab open?`);
}
execSync(`zellij action write ${keyCode}`);
if (originalTab && originalTab.toLowerCase() !== tab.toLowerCase()) {
try { execSync(`zellij action go-to-tab-name ${shellEscape(originalTab)}`); } catch { /* best effort */ }
@@ -101,7 +111,13 @@ export function injectIntoTab(tab: string, prompt: string): void {
// go-to-tab-name is fire-and-forget; confirm the switch landed before
// sending characters so write-chars never types into the wrong pane.
execSync(`zellij action go-to-tab-name ${shellEscape(tab)}`);
waitForTabFocus(tab);
if (!waitForTabFocus(tab)) {
// Hard fail — typing into the wrong pane would garble the user's
// private terminal. Surface this so the caller (home/worker.ts or
// any of the API routes) emits a worker.crashed event / 500 instead
// of silently corrupting whatever pane is focused.
throw new Error(`injectIntoTab: zellij tab "${tab}" did not gain focus within 1s — is the tab open and zellij reachable?`);
}
execSync(`zellij action write-chars ${shellEscape(prompt)}`);
execSync("sleep 0.1");
execSync("zellij action write 13");