Skip to content

an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions

chore: checkpoint terminal automation and codex remote work

Vyapak Goyal1d95952b5983
diff --git a/src/renderer/src/components/terminalAutomation.ts b/src/renderer/src/components/terminalAutomation.ts
index 620d3123..d9c169e1 100644
--- a/src/renderer/src/components/terminalAutomation.ts
+++ b/src/renderer/src/components/terminalAutomation.ts
@@ -29,11 +29,44 @@ export function shouldFollowTerminalOutput(viewportY: number, baseY: number): bo
return baseY - viewportY <= 1;
}
+/** How long an untouched draft on the prompt keeps blocking queue delivery.
+ * A draft blocks so automation can't fuse its text with the user's, but the
+ * block MUST expire: the renderer's line buffer is only a model of the real
+ * input line, and a TUI that swallows keystrokes for its own UI can leave the
+ * buffer non-empty while the prompt looks clear — which used to wedge an
+ * agent's queue permanently with the composer still claiming it was sending. */
+export const STALE_INPUT_MS = 60_000;
+
export interface TerminalAutomationState {
exited: boolean;
pickerOpen: boolean;
inputDirty: boolean;
settleUntil: number;
+ inputDirtyAt?: number; // last keystroke that left a draft; absent ⇒ never expires
+}
+
+/** Why automation may not own the prompt right now, or null when it may. */
+export type TerminalAutomationBlock = 'exited' | 'picker' | 'draft' | 'settling' | null;
+
+/** A draft nobody has touched for STALE_INPUT_MS is treated as abandoned. */
+export function isStaleTerminalDraft(
+ state: TerminalAutomationState,
+ now = Date.now()
+): boolean {
+ return state.inputDirty
+ && state.inputDirtyAt !== undefined
+ && now - state.inputDirtyAt >= STALE_INPUT_MS;
+}
+
+export function terminalAutomationBlock(
+ state: TerminalAutomationState,
+ now = Date.now()
+): TerminalAutomationBlock {
+ if (state.exited) return 'exited';
+ if (state.pickerOpen) return 'picker';
+ if (state.inputDirty && !isStaleTerminalDraft(state, now)) return 'draft';
+ if (now < state.settleUntil) return 'settling';
+ return null;
}
/** Automatic writes may own the prompt only when no user draft or picker does. */
@@ -41,8 +74,5 @@ export function canAutomateTerminal(
state: TerminalAutomationState,
now = Date.now()
): boolean {
- return !state.exited
- && !state.pickerOpen
- && !state.inputDirty
- && now >= state.settleUntil;
+ return terminalAutomationBlock(state, now) === null;
}
diff --git a/src/renderer/src/components/terminalPool.ts b/src/renderer/src/components/terminalPool.ts
index 41db4549..b4907bcb 100644
--- a/src/renderer/src/components/terminalPool.ts
+++ b/src/renderer/src/components/terminalPool.ts
@@ -26,8 +26,11 @@ import {
} from './terminalRecovery';
import {
canAutomateTerminal,
+ isStaleTerminalDraft,
opensInteractiveTerminalUi,
- shouldFollowTerminalOutput
+ shouldFollowTerminalOutput,
+ terminalAutomationBlock,
+ type TerminalAutomationBlock
} from './terminalAutomation';
import '@xterm/xterm/css/xterm.css';
@@ -53,6 +56,7 @@ export interface TerminalEntry {
automationBlocked: boolean;
/** True while the user has unsubmitted text in the live TUI prompt. */
inputDirty: boolean;
+ inputDirtyAt: number; // when the draft was last typed into; drives staleness expiry
automationSettleUntil: number;
webgl?: WebglAddon;
}
@@ -113,6 +117,7 @@ export function acquireTerminal(ptyId: string, theme?: ThemeMap, fontSize = 14):
needsRendererRepaint: false,
automationBlocked: false,
inputDirty: false,
+ inputDirtyAt: 0,
automationSettleUntil: 0
};
@@ -212,6 +217,9 @@ export function acquireTerminal(ptyId: string, theme?: ThemeMap, fontSize = 14):
}
}
entry.inputDirty = lineBuf.length > 0;
+ // Re-stamped on every keystroke, so the staleness clock measures time since
+ // the user last touched the draft — not since they started it.
+ if (entry.inputDirty) entry.inputDirtyAt = Date.now();
});
pool.set(ptyId, entry);
@@ -223,12 +231,51 @@ export function acquireTerminal(ptyId: string, theme?: ThemeMap, fontSize = 14):
export function isTerminalAutomationSafe(ptyId: string, now = Date.now()): boolean {
const entry = pool.get(ptyId);
if (!entry) return true;
- return canAutomateTerminal({
+ return canAutomateTerminal(automationStateOf(entry), now);
+}
+
+function automationStateOf(entry: TerminalEntry) {
+ return {
exited: entry.exited,
pickerOpen: entry.automationBlocked,
inputDirty: entry.inputDirty,
+ inputDirtyAt: entry.inputDirty ? entry.inputDirtyAt : undefined,
settleUntil: entry.automationSettleUntil
- }, now);
+ };
+}
+
+/** Why queue delivery is currently held back for this pty, or null if it isn't.
+ * The composer shows this instead of claiming it is sending. */
+export function terminalAutomationBlockFor(
+ ptyId: string | undefined,
+ now = Date.now()
+): TerminalAutomationBlock {
+ if (!ptyId) return null;
+ const entry = pool.get(ptyId);
+ if (!entry) return null;
+ return terminalAutomationBlock(automationStateOf(entry), now);
+}
+
+/** Wipe the TUI prompt's current line and re-arm automation. Ctrl-U is the
+ * readline kill-to-start binding every supported CLI's input honors. */
+export function clearTerminalDraft(ptyId: string): void {
+ const entry = pool.get(ptyId);
+ if (!entry) return;
+ void window.cth.writePty(ptyId, '\x15');
+ entry.inputDirty = false;
+ entry.inputDirtyAt = 0;
+ entry.automationBlocked = false;
+ // Let the TUI repaint the cleared line before automation types into it.
+ entry.automationSettleUntil = Date.now() + 300;
+}
+
+/** Drop a draft nobody has touched for STALE_INPUT_MS so it cannot fuse with the
+ * text automation is about to type. No-op while the draft is still fresh. */
+export function clearStaleTerminalDraft(ptyId: string, now = Date.now()): boolean {
+ const entry = pool.get(ptyId);
+ if (!entry || !isStaleTerminalDraft(automationStateOf(entry), now)) return false;
+ clearTerminalDraft(ptyId);
+ return true;
}
/** Re-parent a pty's terminal into `container`, opening xterm on first attach. */
@@ -366,6 +413,7 @@ export function resetTerminal(
// latched `exited`, which otherwise makes onData drop keystrokes silently.
entry.exited = false;
entry.inputDirty = false;
+ entry.inputDirtyAt = 0;
try {
if (opts.preserveScrollback) {
entry.term.writeln('\r\n\x1b[2m─ resuming existing session ─\x1b[0m');
diff --git a/src/renderer/src/hooks/useHive.ts b/src/renderer/src/hooks/useHive.ts
index e1d6aa8a..c3e9fc7c 100644
--- a/src/renderer/src/hooks/useHive.ts
+++ b/src/renderer/src/hooks/useHive.ts
@@ -13,7 +13,7 @@ import {
terminalReadyToReceive
} from '../../../shared/providerAutomation';
import type { AgentProvider } from '../../../shared/agentProvider';
-import { isTerminalAutomationSafe } from '@/components/terminalPool';
+import { clearStaleTerminalDraft, isTerminalAutomationSafe } from '@/components/terminalPool';
import { deliverWithAcknowledgement } from './queueDelivery';
const GOD_ID = 'god';
@@ -522,6 +522,9 @@ export function useHive(config: HarnessConfig | null): void {
if ((bootGraceUntil.current[target.id] ?? 0) >= now) return { sent: false };
if (!isTerminalAutomationSafe(target.ptyId, now)) return { sent: false };
if (now - (lastFlush.current[target.id] ?? 0) < FLUSH_COOLDOWN_MS) return { sent: false };
+ // The gate above lets an ABANDONED draft through. Wipe the prompt first so
+ // the leftover half-line can't fuse with the message we're about to type.
+ clearStaleTerminalDraft(target.ptyId, now);
const flightKey = `${srcId}:${next.id}`;
if (inFlight.has(flightKey)) return { sent: false };
inFlight.add(flightKey);
diff --git a/src/shared/codexRemote.ts b/src/shared/codexRemote.ts
index 52a86b8b..a58daed4 100644
--- a/src/shared/codexRemote.ts
+++ b/src/shared/codexRemote.ts
@@ -4,17 +4,38 @@ import { join } from 'node:path';
export const CODEX_REMOTE_SOCKET_RELATIVE =
'app-server-control/app-server-control.sock';
-/** Keep the CODEX_HOME spelling short enough for macOS's Unix-socket limit. */
+/** macOS caps a Unix socket path at 104 bytes (`sun_path`), and Codex builds its
+ * control socket as `$CODEX_HOME/app-server-control/app-server-control.sock` —
+ * 42 bytes of suffix. So the alias home itself must fit in ~61 bytes.
+ *
+ * `$TMPDIR` cannot host it: macOS spells it
+ * `/var/folders/xx/<30-char-hash>/T/` (49 bytes) and the alias came out at 121
+ * — LONGER than the 118-byte real home it was introduced to shorten, so every
+ * daemon start failed with `path must be shorter than SUN_LEN`. Root the alias
+ * at a fixed short prefix instead and keep the digest to 8 hex chars: the whole
+ * socket path then lands at 60 bytes with room to spare. */
+export const CODEX_REMOTE_ALIAS_ROOT = '/tmp/mdc';
+
+/** Longest socket path the platform will accept, minus a small safety margin. */
+export const CODEX_REMOTE_SOCKET_MAX = 104;
+
+/** Keep the CODEX_HOME spelling short enough for macOS's Unix-socket limit.
+ * `tempRoot` defaults to the short fixed root; callers may override it (tests). */
export function codexRemoteAliasPath(
realHome: string,
agentId: string,
- tempRoot: string
+ tempRoot: string = CODEX_REMOTE_ALIAS_ROOT
): string {
const digest = createHash('sha256')
.update(`${realHome}\0${agentId}`)
.digest('hex')
- .slice(0, 16);
- return join(tempRoot, 'munder-codex', digest);
+ .slice(0, 8);
+ return join(tempRoot, digest);
+}
+
+/** Whether a candidate home yields a control socket the platform can bind. */
+export function codexRemoteSocketFits(shortHome: string): boolean {
+ return join(shortHome, CODEX_REMOTE_SOCKET_RELATIVE).length < CODEX_REMOTE_SOCKET_MAX;
}
export function codexRemoteEndpoint(shortHome: string): string {
diff --git a/test/codex-remote.test.cjs b/test/codex-remote.test.cjs
index 51570fbe..7f46d50f 100644
--- a/test/codex-remote.test.cjs
+++ b/test/codex-remote.test.cjs
@@ -7,7 +7,10 @@ const loadTs = require('./load-ts.cjs');
const {
codexRemoteAliasPath,
codexRemoteEndpoint,
- withCodexRemoteArgs
+ codexRemoteSocketFits,
+ withCodexRemoteArgs,
+ CODEX_REMOTE_SOCKET_MAX,
+ CODEX_REMOTE_SOCKET_RELATIVE
} = loadTs('src/shared/codexRemote.ts');
test('Codex remote uses a short stable per-agent home alias', () => {
@@ -20,6 +23,26 @@ test('Codex remote uses a short stable per-agent home alias', () => {
assert.match(codexRemoteEndpoint(first), /^unix:\/\/\/tmp\//);
});
+test('the default alias root yields a socket within sun_path', () => {
+ // The real hive home that failed with "path must be shorter than SUN_LEN".
+ const realHome =
+ '/Users/vyapakgoyal/Documents/HarnessAgents/hive/agents/dev2-mrxb3l43/.codex';
+ const socket =
+ codexRemoteAliasPath(realHome, 'dev2-mrxb3l43') + '/' + CODEX_REMOTE_SOCKET_RELATIVE;
+ assert.ok(
+ socket.length < CODEX_REMOTE_SOCKET_MAX,
+ `socket path is ${socket.length} bytes: ${socket}`
+ );
+ // …and shorter than the home it replaces, which the $TMPDIR version was not.
+ assert.ok(socket.length < (realHome + '/' + CODEX_REMOTE_SOCKET_RELATIVE).length);
+});
+
+test('an over-long alias root is rejected instead of failing at bind time', () => {
+ const tmpdirStyle = '/var/folders/v6/9f10q5d148z7bxdzhr22xl7r0000gn/T/munder-codex';
+ assert.equal(codexRemoteSocketFits(codexRemoteAliasPath('/h/.codex', 'a', tmpdirStyle)), false);
+ assert.equal(codexRemoteSocketFits(codexRemoteAliasPath('/h/.codex', 'a')), true);
+});
+
test('remote endpoint precedes both fresh and resumed Codex invocations', () => {
const endpoint = 'unix:///tmp/munder-codex/a/app-server-control/app-server-control.sock';
assert.deepEqual(
diff --git a/test/terminal-automation.test.cjs b/test/terminal-automation.test.cjs
index 8018f47b..a8fbb2c1 100644
--- a/test/terminal-automation.test.cjs
+++ b/test/terminal-automation.test.cjs
@@ -6,8 +6,11 @@ const loadTs = require('./load-ts.cjs');
const {
canAutomateTerminal,
+ isStaleTerminalDraft,
opensInteractiveTerminalUi,
- shouldFollowTerminalOutput
+ shouldFollowTerminalOutput,
+ terminalAutomationBlock,
+ STALE_INPUT_MS
} = loadTs('src/renderer/src/components/terminalAutomation.ts');
test('interactive provider commands pause queue automation', () => {
@@ -27,6 +30,36 @@ test('terminal automation waits for user drafts and interactive states', () => {
assert.equal(canAutomateTerminal({ ...ready, settleUntil: 101 }, 100), false);
});
+test('an abandoned draft stops blocking delivery once it goes stale', () => {
+ const typedAt = 1_000_000;
+ const draft = {
+ exited: false, pickerOpen: false, inputDirty: true,
+ settleUntil: 0, inputDirtyAt: typedAt
+ };
+ // Fresh draft: the user is mid-sentence, automation must not type over it.
+ assert.equal(canAutomateTerminal(draft, typedAt + 1), false);
+ assert.equal(isStaleTerminalDraft(draft, typedAt + 1), false);
+ // Untouched past the window: the queue must not stay wedged forever.
+ assert.equal(isStaleTerminalDraft(draft, typedAt + STALE_INPUT_MS), true);
+ assert.equal(canAutomateTerminal(draft, typedAt + STALE_INPUT_MS), true);
+ // A draft with no timestamp keeps the old never-expires behavior.
+ assert.equal(canAutomateTerminal({ ...draft, inputDirtyAt: undefined }, typedAt + 1e9), false);
+});
+
+test('automation block reports why delivery is held', () => {
+ const ready = { exited: false, pickerOpen: false, inputDirty: false, settleUntil: 0 };
+ assert.equal(terminalAutomationBlock(ready, 100), null);
+ assert.equal(terminalAutomationBlock({ ...ready, exited: true }, 100), 'exited');
+ assert.equal(terminalAutomationBlock({ ...ready, pickerOpen: true }, 100), 'picker');
+ assert.equal(terminalAutomationBlock({ ...ready, inputDirty: true }, 100), 'draft');
+ assert.equal(terminalAutomationBlock({ ...ready, settleUntil: 101 }, 100), 'settling');
+ // A picker outranks a draft: both are true while a slash menu is open.
+ assert.equal(
+ terminalAutomationBlock({ ...ready, pickerOpen: true, inputDirty: true }, 100),
+ 'picker'
+ );
+});
+
test('terminal output follows only when already at the bottom', () => {
assert.equal(shouldFollowTerminalOutput(100, 100), true);
assert.equal(shouldFollowTerminalOutput(99, 100), true);