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
refactor: thread TUI launch shape instead of misnamed directLaunch boolean (#9612)
Adam Eivybc0ce999d5d3
diff --git a/server/lib/tuiHandshake.js b/server/lib/tuiHandshake.jsindex 9942d47a3d..d412c5a544 100644--- a/server/lib/tuiHandshake.js+++ b/server/lib/tuiHandshake.js@@ -467,18 +467,21 @@ const OBSERVE_TAIL_MAX_LEN = 4000;// rolling tail. When supplied, `ready` also requires that marker to have been// seen (see AGY_INPUT_READY_PATTERN).//-// directLaunch: the TUI was pty.spawn'd DIRECTLY — there is no launch shell on-// the PTY, so the shell's paste-mode OFF (`ESC[?2004l`) that normally proves-// "the command is now running" never occurs; the first `ESC[?2004h` in the-// stream comes from the TUI itself and IS the ready signal. Without this flag a-// direct-launch session can never become ready (the durable-runner regression:-// every runner-tui claude agent died `tui-not-ready` at the 45s deadline while-// its input box sat live on screen).-export function createInputReadyTracker({ readyTextPattern = null, directLaunch = false } = {}) {+// launchShape: which PTY shape this TUI runs in. 'runner' and 'direct' shapes+// own their PTY from byte zero and have no launch shell, so the shell's+// paste-mode OFF (`ESC[?2004l`) that normally proves "the command is now+// running" never occurs; the first `ESC[?2004h` in the stream comes from the+// TUI itself and IS the ready signal. Without this knowledge a runner/direct+// session can never become ready (the durable-runner regression: every+// runner-tui claude agent died `tui-not-ready` at the 45s deadline while its+// input box sat live on screen). 'login-shell' has a persistent shell that+// handles paste-mode negotiation.+export function createInputReadyTracker({ readyTextPattern = null, launchShape = 'login-shell' } = {}) {let pasteModeOn = false; // LIVE bracketed-paste mode state from the stream- // Shell turned paste mode OFF to run the command. Pre-latched for direct- // launches, where the TUI owns the PTY from byte zero and no shell OFF exists.- let sawCommandRun = directLaunch;+ // Shell turned paste mode OFF to run the command. Pre-latched when the TUI+ // owns its own PTY (runner/direct shapes), where no shell OFF exists.+ const hasOwnPty = launchShape === 'runner' || launchShape === 'direct';+ let sawCommandRun = hasOwnPty;let needsTrust = false;let trustAnswered = false;let trustChoiceReady = false;diff --git a/server/services/agentTuiSpawning.js b/server/services/agentTuiSpawning.jsindex 9a9a658643..36655a9cdd 100644--- a/server/services/agentTuiSpawning.js+++ b/server/services/agentTuiSpawning.js@@ -94,12 +94,29 @@ export function resolveTuiLaunchShape({ useDurableRunner = false, safetyProfilereturn isPublicReviewRestrictedProfile(safetyProfile) ? 'direct' : 'login-shell';}+/**+ * Test whether a launch shape owns its own PTY process.+ * 'runner' and 'direct' shapes own their PTY; 'login-shell' is hosted in a shell.+ */+export function shapeHasOwnPty(launchShape) {+ return launchShape === 'runner' || launchShape === 'direct';+}++/**+ * Test whether a launch shape has a login shell to fall back to.+ * Only 'login-shell' has a persistent shell; 'runner' and 'direct' do not.+ */+export function shapeHasLoginShell(launchShape) {+ return launchShape === 'login-shell';+}+/*** Open the agent TUI's PTY and pair the returned session id with its underlying* pty process, so callers don't have to make a second `getSessionProcess` call* inline. Centralizes the agent-side defaults (kind, label, initialCommand).*- * Which of the three PTY shapes it opens is `resolveTuiLaunchShape`'s call.+ * The launch shape is resolved by the caller and threaded through to avoid+ * the hand-shake mismatch documented in resolveTuiLaunchShape.** Returns `{ sessionId, ptyProcess, pid }`. When the shell service fails* to create the session, `sessionId` is null and the caller is expected@@ -115,7 +132,7 @@ export async function createAgentTuiSession({forgeTokenEnv = {},agentApiEnv = {},doneSentinelPath = null,- useDurableRunner = false,+ launchShape,safetyProfile = null,onData,onExit,@@ -152,7 +169,6 @@ export async function createAgentTuiSession({label: `${provider.name} ${agentId}`,command: tuiConfig.commandLine,};- const launchShape = resolveTuiLaunchShape({ useDurableRunner, safetyProfile });let sessionId;if (launchShape === 'runner') {// The CoS runner is a shared long-lived process and builds its own child@@ -275,11 +291,10 @@ export async function spawnTuiAgent({// gets the same allowlisted environment its headless sibling would.safetyProfile = null,}) {- // The SAME call `createAgentTuiSession` branches on — resolved here because- // the prompt handshake below is wired before the spawn happens. Everything- // that is not a login shell has the provider binary as its own PTY process,- // which is what both consumers below actually depend on.- const directLaunch = resolveTuiLaunchShape({ useDurableRunner, safetyProfile }) !== 'login-shell';+ // Resolved here once because the prompt handshake below is wired before the+ // spawn happens. Threaded through to createAgentTuiSession and the controller+ // so the shape is derived once per run and both consumers see the same answer.+ const launchShape = resolveTuiLaunchShape({ useDurableRunner, safetyProfile });const outputFile = join(agentDir, 'output.txt');// Raw PTY bytes spool to disk continuously rather than accumulate in-memory.// A chatty TUI (token-tick repaints, status lines) emits hundreds of chunks@@ -361,7 +376,7 @@ export async function spawnTuiAgent({rawFile,executionId,laneName,- directLaunch,+ launchShape,prOwnership,mergeGateIsOwed,spooler,@@ -465,7 +480,7 @@ export async function spawnTuiAgent({forgeTokenEnv,agentApiEnv,doneSentinelPath,- useDurableRunner,+ launchShape,safetyProfile,onData: controller.handleData,onExit: controller.handleExit,diff --git a/server/services/agentTuiSpawning/sessionController.js b/server/services/agentTuiSpawning/sessionController.jsindex 618485573f..33e8b3f8ad 100644--- a/server/services/agentTuiSpawning/sessionController.js+++ b/server/services/agentTuiSpawning/sessionController.js@@ -41,6 +41,7 @@ import { createClaudeSessionLimitBannerDetector, createImmediateFallbackSignalDeimport { isAntigravityCommand } from '../../lib/antigravity.js';import { isCodexCommand } from '../../lib/codex.js';import { isClaudeCommand } from '../../lib/providerModels.js';+import { shapeHasLoginShell } from '../agentTuiSpawning.js';import { createCodexModelRejectionGate } from './codexModelRejection.js';import {READY_POLL_INTERVAL_MS,@@ -115,7 +116,7 @@ function createPasteRetryController({write,paste,hasLiveChild,- directLaunch,+ launchShape,prompt,tuiConfig,mcpBoot,@@ -225,12 +226,12 @@ function createPasteRetryController({// `^[[200~ …` session. If the shell has no live child, the command is gone:// fail loudly with whatever it printed instead of pasting into the shell.//- // A direct launch — runner mode, or a public-content stage spawned as its- // own PTY (#6159) — has no launch shell: the TUI IS the PTY process. "Does- // this pid have a live child?" is then the wrong question (claude may have- // zero children at paste time) and a TUI exit kills the PTY, firing onExit.- // Skip the probe there.- if (!directLaunch && !(await hasLiveChild())) {+ // A launch shape with a login shell has an intermediate shell; without one+ // (runner or direct), the TUI IS the PTY process and "does this pid have a+ // live child?" is the wrong question (the TUI may have zero children at+ // paste time). A TUI exit kills the PTY in those shapes, firing onExit.+ // Skip the probe when there is no shell to host the TUI.+ if (shapeHasLoginShell(launchShape) && !(await hasLiveChild())) {if (isFinalized()) return; // a real onExit may have finalized during the probe awaitawait finishStartupFailure('tui-exited-early',@@ -473,7 +474,7 @@ export function createTuiSessionController({rawFile,executionId,laneName,- directLaunch,+ launchShape,prOwnership,mergeGateIsOwed,spooler,@@ -550,9 +551,9 @@ export function createTuiSessionController({const detectTruncatedResponse = createTruncatedResponseDetector();const truncationNudgeGate = createTruncationNudgeGate();// agy quitting back to its launch shell (see createAgyResumeGate). Only a- // login-shell launch has a shell to fall back to; a direct PTY dies with agy- // and finishes through handleExit instead.- const agyResumeGate = !directLaunch && isAntigravityCommand(tuiConfig.command) ? createAgyResumeGate() : null;+ // login-shell launch has a shell to fall back to; runner/direct PTYs die with+ // agy and finish through handleExit instead.+ const agyResumeGate = shapeHasLoginShell(launchShape) && isAntigravityCommand(tuiConfig.command) ? createAgyResumeGate() : null;let agyResumeAwaitingComposer = false;// A request the TUI keeps retrying and the provider never answers. Every// reaper reads such a session as busy (the retry ladder repaints the screen),@@ -644,11 +645,11 @@ export function createTuiSessionController({// paste into a startup banner, a trust menu, or a returned shell prompt.// agy enables bracketed paste on alt-screen entry, before its composer (and// before its trust gate) exists, so it needs the extra composer-footer gate.- // A direct launch pty.spawns the TUI itself (no launch shell), so the tracker- // must not wait for a shell paste-mode OFF that will never come.+ // Runner/direct shapes pty.spawn the TUI itself (no launch shell), so the+ // tracker must not wait for a shell paste-mode OFF that will never come.const inputReady = createInputReadyTracker({...(isAntigravityCommand(tuiConfig.command) ? { readyTextPattern: AGY_INPUT_READY_PATTERN } : {}),- directLaunch,+ launchShape,});// Which of the TUI's startup dialogs this session has already answered. One// record instead of four sibling booleans; the arms themselves live in@@ -1559,7 +1560,7 @@ export function createTuiSessionController({write: (keys) => session.write(sessionId, keys),paste: (text, options) => (sessionId ? session.paste(sessionId, text, options) : false),hasLiveChild: () => session.hasLiveChild(pid),- directLaunch,+ launchShape,prompt,tuiConfig,mcpBoot,diff --git a/server/services/agentTuiSpawning/sessionController.test.js b/server/services/agentTuiSpawning/sessionController.test.jsindex 75e5d62ec0..4b24c7352d 100644--- a/server/services/agentTuiSpawning/sessionController.test.js+++ b/server/services/agentTuiSpawning/sessionController.test.js@@ -80,7 +80,7 @@ function makeController({rawFile: '/tmp/workspace/raw.txt',executionId: 'exec-1',laneName: 'lane-1',- directLaunch: true,+ launchShape: 'direct',prOwnership: { prOpenedBy: 'agent-inline', prClaimExpected: true, taskOpenPR: true },mergeGateIsOwed,spooler: {