@zq-silk/yui 0.15.0 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -69,7 +69,12 @@ function claudeConfigFromSnapshot(snapshot) {
69
69
  : { settingsSources: [...snapshot.settingsSources] })
70
70
  };
71
71
  }
72
- export function effectiveLaunchSnapshotsCompatible(existing, desired) {
72
+ /**
73
+ * Exactness fence for one launch: the same resolved launch must be observed by
74
+ * every participant of that launch. Desired-revision bookkeeping is provenance
75
+ * and never part of the resolved launch itself.
76
+ */
77
+ export function sameEffectiveLaunch(existing, desired) {
73
78
  validateEffectiveLaunchSnapshot(existing);
74
79
  validateEffectiveLaunchSnapshot(desired);
75
80
  const withoutDesiredRevision = (snapshot) => {
@@ -79,20 +84,24 @@ export function effectiveLaunchSnapshotsCompatible(existing, desired) {
79
84
  return isDeepStrictEqual(withoutDesiredRevision(existing), withoutDesiredRevision(desired));
80
85
  }
81
86
  /**
82
- * Task Role Sessions keep one physical workspace while Turn-scoped facts move.
83
- * Candidate commits, ReviewRound identity and desired-revision bookkeeping do
84
- * not define a native Session. Agent, adapter, permission, model, sandbox,
85
- * manifest, Role context and physical workspace identity still do.
87
+ * Whether a live native Session can still serve the next launch request.
88
+ *
89
+ * Only facts that make continuation impossible participate: the Session
90
+ * protocol, the provider identity that owns the conversation, and the physical
91
+ * workspace the Session runs in. Launch configuration such as model, effort,
92
+ * permission, Role context, declared write scope, and Turn-scoped facts like
93
+ * ReviewRound identity or candidate commits shape the next Host activation
94
+ * instead of ending the Session; that divergence is acknowledged where the
95
+ * configuration changes and stays visible as launch provenance.
96
+ *
97
+ * Session kind needs no separate check: a Role's review Turns run in their own
98
+ * ReviewRound workspace, so the physical workspace already separates a review
99
+ * Session from an execution Session.
86
100
  */
87
- export function effectiveLaunchSnapshotsCompatibleForTaskSession(existing, desired) {
88
- if (effectiveLaunchSnapshotsCompatible(existing, desired))
89
- return true;
101
+ export function roleSessionMayContinue(existing, desired) {
90
102
  validateEffectiveLaunchSnapshot(existing);
91
103
  validateEffectiveLaunchSnapshot(desired);
92
- if ((existing.reviewRoundId === undefined) !== (desired.reviewRoundId === undefined)) {
93
- return false;
94
- }
95
- return isDeepStrictEqual(taskSessionCompatibleSnapshot(existing), taskSessionCompatibleSnapshot(desired));
104
+ return isDeepStrictEqual(sessionContinuitySnapshot(existing), sessionContinuitySnapshot(desired));
96
105
  }
97
106
  /** Preserves a fixed Session's launch configuration while freezing fresh Task-main Git facts. */
98
107
  export function effectiveLaunchWithTaskMainWorkspace(existing, workspace) {
@@ -109,13 +118,21 @@ export function effectiveLaunchWithTaskMainWorkspace(existing, workspace) {
109
118
  }
110
119
  });
111
120
  }
112
- function taskSessionCompatibleSnapshot(snapshot) {
113
- const { sourceDesiredRevision: _sourceDesiredRevision, reviewRoundId: _reviewRoundId, reviewBaseCommit: _reviewBaseCommit, workspace, ...launch } = snapshot;
121
+ function sessionContinuitySnapshot(snapshot) {
114
122
  return {
115
- ...launch,
123
+ schemaVersion: snapshot.schemaVersion,
124
+ contextProtocolVersion: snapshot.contextProtocolVersion,
125
+ agentId: snapshot.agentId,
126
+ adapterId: snapshot.adapterId,
116
127
  workspace: {
117
- root: workspace.root,
118
- entries: workspace.entries.map(({ baseCommit: _baseCommit, baseRef: _baseRef, ...entry }) => entry)
128
+ root: snapshot.workspace.root,
129
+ entries: snapshot.workspace.entries.map((entry) => ({
130
+ projectId: entry.projectId,
131
+ directory: entry.directory,
132
+ access: entry.access,
133
+ path: entry.path,
134
+ branch: entry.branch
135
+ }))
119
136
  }
120
137
  };
121
138
  }
@@ -15,7 +15,7 @@ import { resolveTaskRoleSessionTitle } from "../runtime/sessionTitle.js";
15
15
  import { nativeSessionIdForLaunch } from "../runtime/preallocatedNativeSession.js";
16
16
  import { classifyWorkspacePreflight, formatWorkspacePreflightError } from "./workspacePreflightClassification.js";
17
17
  import { activeLiveRoleAgentSession } from "./agentExecutor.js";
18
- import { effectiveLaunchSnapshotsCompatibleForTaskSession, effectiveLaunchSnapshotsCompatible, effectiveRoleForLaunch, resolveEffectiveLaunch } from "./effectiveLaunch.js";
18
+ import { roleSessionMayContinue, effectiveRoleForLaunch, resolveEffectiveLaunch } from "./effectiveLaunch.js";
19
19
  import { YUI_CONTROL_PLANE_DESCRIPTOR, createExactControlPlaneDescriptor, serializeExactDescriptor } from "../runtime/exactControlPlane.js";
20
20
  import { detectRunningRelease } from "../release/runtimeRelease.js";
21
21
  import { parseTaskRuntimeIsolationDescriptor, taskRuntimeIsolationEnvironment } from "../runtime/taskRuntimeIsolation.js";
@@ -176,9 +176,7 @@ export class FileRoleLaunchPlanner {
176
176
  const effective = input.effective ?? resolvedEffective;
177
177
  const existing = sessionSet?.sessions[effective.agentId];
178
178
  const compatibleExisting = existing !== undefined
179
- && (input.mode === "resume"
180
- ? effectiveLaunchSnapshotsCompatibleForTaskSession(existing.effective, effective)
181
- : effectiveLaunchSnapshotsCompatible(existing.effective, effective));
179
+ && roleSessionMayContinue(existing.effective, effective);
182
180
  if (input.mode === "resume" && !compatibleExisting) {
183
181
  throw new Error(`Task Role resume effective snapshot drifted: ${task.id}/${role.name}.`);
184
182
  }
@@ -204,7 +202,7 @@ export class FileRoleLaunchPlanner {
204
202
  const effective = input.effective ?? resolvedEffective;
205
203
  const existing = sessionSet?.sessions[effective.agentId];
206
204
  const compatibleExisting = existing !== undefined
207
- && effectiveLaunchSnapshotsCompatible(existing.effective, effective);
205
+ && roleSessionMayContinue(existing.effective, effective);
208
206
  if (input.mode === "resume" && !compatibleExisting) {
209
207
  throw new Error(`Global Role resume effective snapshot drifted: ${role.name}.`);
210
208
  }
@@ -246,7 +244,9 @@ export class FileRoleLaunchPlanner {
246
244
  : undefined,
247
245
  trustWorkspace: true
248
246
  });
249
- assertCodexLaunchOverridesAvailable(codexConfig, ["developerInstructions", "notify"]);
247
+ assertCodexLaunchOverridesAvailable(codexConfig, owner.scope === "global"
248
+ ? ["developerInstructions"]
249
+ : ["developerInstructions", "notify"]);
250
250
  }
251
251
  const runtimeIsolation = input.runtimeIsolation === undefined
252
252
  ? undefined
@@ -365,10 +365,10 @@ export class FileRoleLaunchPlanner {
365
365
  args.push("--plugin-dir", ensureClaudeLifecyclePlugin(this.home, this.#cliPath));
366
366
  }
367
367
  if (binding.adapterId === "codex") {
368
- // Global/interactive Codex sessions still use notify for presentation.
368
+ // Interactive Task sessions may use notify for presentation.
369
369
  // Managed Turns receive lifecycle facts through their ordinary App Server
370
370
  // subscription, avoiding a second Hook channel for the same Turn.
371
- if (owner.scope !== "task" || input.turnId === undefined) {
371
+ if (owner.scope === "task" && input.turnId === undefined) {
372
372
  args = addCodexSessionNotify(args, launchMode, this.#cliPath);
373
373
  }
374
374
  // Managed Codex Turns use disposable proxy clients against the shared
@@ -472,6 +472,9 @@ export class FileRoleLaunchPlanner {
472
472
  YUI_WORKSPACE: effectiveWorkspace,
473
473
  YUI_SESSION_MANIFEST: sessionContext.sessionManifestPath,
474
474
  YUI_SESSION_CLI: sessionContext.sessionCliPath,
475
+ ...(owner.scope === "global" && configured.adapterId === "codex"
476
+ ? { YUI_AGENT_BASE_ARGS: JSON.stringify(configured.baseArgs) }
477
+ : {}),
475
478
  ...(jobCallerKey === undefined ? {} : { YUI_JOB_CALLER_KEY: jobCallerKey }),
476
479
  ...(owner.scope !== "task"
477
480
  ? {}
@@ -17,6 +17,7 @@ import { persistRuntimeProcessExitObservation, replayRuntimeProcessExitOutbox }
17
17
  import { readRuntimeStopReceipt, removeRuntimeStopReceipt } from "./runtimeStopReceipt.js";
18
18
  import { AGENT_HOST_CONTROL_TIMEOUT_MS, AGENT_HOST_READY_TIMEOUT_MS } from "./runtimeDeadlines.js";
19
19
  import { serializeAgentErrorRaw } from "./agentError.js";
20
+ import { runCodexInteractiveHost } from "./codexInteractiveHost.js";
20
21
  export const AGENT_HOST_CONTROL_PROTOCOL = "yui-agent-host/v4";
21
22
  const HOST_CONTROL_MAX_BYTES = 32 * 1024;
22
23
  const CODEX_CLIENT_STABLE_MS = 5_000;
@@ -28,6 +29,11 @@ export async function runAgentHost(input) {
28
29
  const hostInstanceId = randomUUID();
29
30
  let hostSequence = 0;
30
31
  let payload = await redeem(input.home, input.runtimeGenerationId, input.ticket);
32
+ if (payload.environment.YUI_SESSION_SCOPE === "global"
33
+ && payload.environment.YUI_ADAPTER_ID === "codex"
34
+ && payload.providerControl === undefined) {
35
+ return runCodexInteractiveHost(input.home, payload);
36
+ }
31
37
  let session;
32
38
  let sessionPayload;
33
39
  let activeTurnPayload;
@@ -882,6 +888,7 @@ export async function waitForAgentHostLaunchAck(input) {
882
888
  const code = error.code;
883
889
  if (code !== "ENOENT" && code !== "ECONNREFUSED")
884
890
  throw error;
891
+ await input.assertHostRunning?.();
885
892
  }
886
893
  await new Promise((resolvePromise) => setTimeout(resolvePromise, 50));
887
894
  }
@@ -0,0 +1,191 @@
1
+ import { spawn } from "node:child_process";
2
+ import { randomBytes } from "node:crypto";
3
+ import { once } from "node:events";
4
+ import WebSocket, { WebSocketServer } from "ws";
5
+ import { AGENT_HOST_CONTROL_PROTOCOL, openAgentHostControl } from "./agentHost.js";
6
+ import { openCodexInteractiveConnection } from "./structuredProviderHost.js";
7
+ /**
8
+ * Keep the native TUI and its transparent App Server attachment in one pane.
9
+ * The existing Host acknowledgement carries the ID from that TUI's exact
10
+ * thread/start or thread/resume response, before any user/model Turn.
11
+ *
12
+ * Codex 0.150.1 cannot resume a pre-created empty Thread: no rollout exists
13
+ * until its first message. Observing the TUI's own startup avoids creating a
14
+ * second Thread or manufacturing a bootstrap message to materialize history.
15
+ */
16
+ export async function runCodexInteractiveHost(home, payload) {
17
+ const environment = payload.environment;
18
+ if (environment.YUI_SESSION_SCOPE !== "global" || environment.YUI_ADAPTER_ID !== "codex"
19
+ || payload.providerControl !== undefined) {
20
+ throw new Error("Interactive Codex Host requires a global native TUI launch.");
21
+ }
22
+ const remoteIndex = payload.args.indexOf("--remote");
23
+ if (remoteIndex < 0 || payload.args[remoteIndex + 1] !== "unix://") {
24
+ throw new Error("Global Codex must target the default shared daemon.");
25
+ }
26
+ const baseArgs = JSON.parse(environment.YUI_AGENT_BASE_ARGS ?? "[]");
27
+ if (!Array.isArray(baseArgs) || baseArgs.some((arg) => typeof arg !== "string")) {
28
+ throw new Error("Codex proxy base arguments are invalid.");
29
+ }
30
+ const expectedId = environment.YUI_NATIVE_SESSION_ID;
31
+ let snapshot = {
32
+ schemaVersion: 2, state: "starting", adapterId: "codex",
33
+ runtimeGenerationId: payload.runtimeGenerationId, updatedAt: new Date().toISOString()
34
+ };
35
+ const control = await openAgentHostControl(home, payload, () => snapshot, async (request) => {
36
+ if (request.type !== "status") {
37
+ throw new Error("Global Codex uses its native TUI; stop it before switching Sessions.");
38
+ }
39
+ return { protocol: AGENT_HOST_CONTROL_PROTOCOL, outcome: "status", snapshot };
40
+ });
41
+ let child;
42
+ let client;
43
+ let closing = false;
44
+ let startupRequestId;
45
+ let connection;
46
+ let relay;
47
+ const signals = ["SIGTERM", "SIGHUP", "SIGINT"];
48
+ const stop = () => { child?.kill("SIGTERM"); };
49
+ const fail = (error) => {
50
+ if (closing || snapshot.state === "failed")
51
+ return;
52
+ snapshot = {
53
+ ...snapshot, state: "failed",
54
+ detail: error instanceof Error ? error.message : String(error),
55
+ updatedAt: new Date().toISOString()
56
+ };
57
+ process.stderr.write(`Yui Codex attachment failed: ${snapshot.detail}\n`);
58
+ client?.terminate();
59
+ stop();
60
+ };
61
+ try {
62
+ // This proxy is a disposable client, not the daemon. No Yui initialization
63
+ // or Thread request is injected; all requests below belong to the TUI.
64
+ connection = await openCodexInteractiveConnection({
65
+ command: payload.command, args: [...baseArgs, "app-server", "proxy"],
66
+ environment, cwd: payload.cwd
67
+ });
68
+ connection.onClose(fail);
69
+ const token = randomBytes(32).toString("hex");
70
+ relay = new WebSocketServer({
71
+ host: "127.0.0.1", port: 0,
72
+ maxPayload: 16 * 1024 * 1024, perMessageDeflate: false,
73
+ verifyClient: (info) => info.req.headers.authorization === `Bearer ${token}`
74
+ });
75
+ relay.on("error", fail);
76
+ relay.on("connection", (socket) => {
77
+ if (client !== undefined || closing) {
78
+ socket.close(1008, "This attachment already has its native TUI.");
79
+ return;
80
+ }
81
+ client = socket;
82
+ socket.on("error", fail);
83
+ socket.on("close", () => {
84
+ if (!closing) {
85
+ closing = true;
86
+ connection?.close();
87
+ stop();
88
+ }
89
+ });
90
+ socket.on("message", (data, binary) => {
91
+ try {
92
+ if (binary)
93
+ throw new Error("Codex TUI sent a binary App Server request.");
94
+ const message = jsonObject(JSON.parse(data.toString()));
95
+ if (snapshot.nativeSessionId === undefined
96
+ && (message.method === "thread/start" || message.method === "thread/resume")) {
97
+ if (startupRequestId !== undefined)
98
+ throw new Error("Codex sent overlapping startup requests.");
99
+ if (typeof message.id !== "string" && typeof message.id !== "number") {
100
+ throw new Error("Codex startup request has no correlation id.");
101
+ }
102
+ const params = jsonObject(message.params);
103
+ if (expectedId === undefined ? message.method !== "thread/start"
104
+ : message.method !== "thread/resume" || params.threadId !== expectedId) {
105
+ throw new Error("Codex TUI startup does not match the reserved Session.");
106
+ }
107
+ startupRequestId = message.id;
108
+ }
109
+ void connection.send(message).catch(fail);
110
+ }
111
+ catch (error) {
112
+ fail(error);
113
+ }
114
+ });
115
+ });
116
+ connection.onMessage((message) => {
117
+ try {
118
+ let nativeSessionId;
119
+ if (startupRequestId !== undefined && message.id === startupRequestId
120
+ && message.method === undefined) {
121
+ if (message.error !== undefined) {
122
+ throw new Error(`Codex startup failed: ${JSON.stringify(message.error)}`);
123
+ }
124
+ const id = jsonObject(jsonObject(message.result).thread).id;
125
+ if (typeof id !== "string" || id.length === 0 || id.trim() !== id || id.includes("\0")
126
+ || (expectedId !== undefined && id !== expectedId)) {
127
+ throw new Error("Codex startup returned an invalid or mismatched Thread identity.");
128
+ }
129
+ nativeSessionId = id;
130
+ }
131
+ if (client?.readyState !== WebSocket.OPEN)
132
+ throw new Error("Codex TUI attachment is closed.");
133
+ client.send(JSON.stringify(message), (error) => {
134
+ if (error) {
135
+ fail(error);
136
+ return;
137
+ }
138
+ if (nativeSessionId === undefined || snapshot.state === "failed")
139
+ return;
140
+ startupRequestId = undefined;
141
+ snapshot = {
142
+ ...snapshot, state: "ready", nativeSessionId, conversationId: nativeSessionId,
143
+ updatedAt: new Date().toISOString()
144
+ };
145
+ });
146
+ }
147
+ catch (error) {
148
+ fail(error);
149
+ }
150
+ });
151
+ await once(relay, "listening");
152
+ if (snapshot.state === "failed")
153
+ throw new Error(snapshot.detail);
154
+ const address = relay.address();
155
+ if (typeof address !== "object" || address === null)
156
+ throw new Error("Codex TUI relay did not bind.");
157
+ const args = [...payload.args];
158
+ args[remoteIndex + 1] = `ws://127.0.0.1:${address.port}`;
159
+ args.splice(remoteIndex + 2, 0, "--remote-auth-token-env", "YUI_CODEX_REMOTE_AUTH_TOKEN");
160
+ child = spawn(payload.command, args, {
161
+ cwd: payload.cwd,
162
+ env: { ...environment, YUI_CODEX_REMOTE_AUTH_TOKEN: token },
163
+ stdio: "inherit"
164
+ });
165
+ for (const signal of signals)
166
+ process.on(signal, stop);
167
+ const [code] = await once(child, "exit");
168
+ return typeof code === "number" ? code : 1;
169
+ }
170
+ catch (error) {
171
+ fail(error);
172
+ throw error;
173
+ }
174
+ finally {
175
+ closing = true;
176
+ for (const signal of signals)
177
+ process.removeListener(signal, stop);
178
+ stop();
179
+ client?.terminate();
180
+ connection?.close();
181
+ if (relay !== undefined)
182
+ await new Promise((done) => relay.close(() => done()));
183
+ await control.close();
184
+ }
185
+ }
186
+ function jsonObject(value) {
187
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
188
+ throw new Error("Invalid Codex App Server object.");
189
+ }
190
+ return value;
191
+ }
@@ -78,11 +78,32 @@ export async function startStructuredProviderSession(payload, input = {}) {
78
78
  throw error;
79
79
  }
80
80
  }
81
+ /**
82
+ * Open an uninitialized, transparent connection for a native TUI. The TUI
83
+ * owns its requests; Yui observes only its exact startup response.
84
+ */
85
+ export async function openCodexInteractiveConnection(launch) {
86
+ const child = spawn(launch.command, [...launch.args], {
87
+ cwd: launch.cwd,
88
+ env: { ...launch.environment },
89
+ stdio: ["pipe", "pipe", "pipe"],
90
+ detached: true
91
+ });
92
+ child.stderr.resume();
93
+ try {
94
+ return await CodexProxyWebSocketChannel.connect(child, () => { });
95
+ }
96
+ catch (error) {
97
+ terminateProcessGroup(child, "SIGTERM");
98
+ throw error;
99
+ }
100
+ }
81
101
  class CodexProxyWebSocketChannel {
82
102
  child;
83
103
  mirror;
84
104
  #pending = new Map();
85
105
  #listeners = new Set();
106
+ #closeListeners = new Set();
86
107
  #nextId = 1;
87
108
  #closedError;
88
109
  #ready = false;
@@ -127,6 +148,17 @@ class CodexProxyWebSocketChannel {
127
148
  this.#listeners.add(listener);
128
149
  return () => this.#listeners.delete(listener);
129
150
  }
151
+ onClose(listener) {
152
+ if (this.#closedError !== undefined)
153
+ listener(this.#closedError);
154
+ else
155
+ this.#closeListeners.add(listener);
156
+ return () => this.#closeListeners.delete(listener);
157
+ }
158
+ close() {
159
+ this.#webSocket.terminate();
160
+ this.#close(new Error("Codex App Server proxy client closed."));
161
+ }
130
162
  async request(method, params) {
131
163
  await this.#readyPromise;
132
164
  if (this.#closedError !== undefined)
@@ -227,6 +259,9 @@ class CodexProxyWebSocketChannel {
227
259
  pending.reject(error);
228
260
  }
229
261
  this.#pending.clear();
262
+ for (const listener of this.#closeListeners)
263
+ listener(error);
264
+ this.#closeListeners.clear();
230
265
  if (this.child.exitCode === null && this.child.signalCode === null) {
231
266
  terminateProcessGroup(this.child, "SIGTERM");
232
267
  }
@@ -210,6 +210,23 @@ export class TmuxSessionHost {
210
210
  throw toRuntimeLaunchFailure(error, "validation", launchContext);
211
211
  }
212
212
  }
213
+ const interactiveCodex = request.owner.scope === "global" && request.adapterId === "codex";
214
+ let reuseInteractivePane = false;
215
+ if (interactiveCodex) {
216
+ let status;
217
+ try {
218
+ status = await probeRoleStatus(this.tmux, hostId, request.owner.roleName);
219
+ }
220
+ catch (error) {
221
+ throw new RuntimeHostContentionError("previous-process", `Global Role pane state is unknown; preserving it: ${error instanceof Error ? error.message : String(error)}`);
222
+ }
223
+ if (status === "running") {
224
+ if (request.mode === "new") {
225
+ throw new RuntimeHostContentionError("previous-process", "Global Role already has a live pane; stop or record its exact Session first.");
226
+ }
227
+ reuseInteractivePane = true;
228
+ }
229
+ }
213
230
  const plannedNativeSessionId = planned.session?.nativeSessionId;
214
231
  if (request.mode === "resume"
215
232
  && plannedNativeSessionId !== undefined
@@ -232,23 +249,26 @@ export class TmuxSessionHost {
232
249
  });
233
250
  const yuiHome = planned.launch.env.YUI_HOME;
234
251
  const childLifecycle = planned.launch.childLifecycle;
235
- // Interactive/global Roles remain native TUIs even when their Driver
236
- // advertises a persistent child lifecycle. Provider control metadata is
237
- // the discriminator for the structured Agent Host path. A managed Task
238
- // Turn has no terminal-write fallback and must expose that contract.
239
- if (yuiHome === undefined
240
- || childLifecycle === undefined
241
- || planned.launch.providerControl === undefined) {
252
+ // Global Codex retains the native TUI inside a thin Host that acknowledges
253
+ // its own App Server startup response. Existing live TUIs (including
254
+ // 0.15.0 Sessions) remain directly attachable without replacing them.
255
+ if (reuseInteractivePane
256
+ || (!interactiveCodex && (yuiHome === undefined
257
+ || childLifecycle === undefined
258
+ || planned.launch.providerControl === undefined))) {
242
259
  if (request.owner.scope === "task" && request.turnId !== undefined) {
243
260
  throw new Error("Managed Task Turn is missing its structured Agent Host contract.");
244
261
  }
245
262
  let hostCreated;
246
263
  try {
247
- hostCreated = await ensureRoleWindow(this.tmux, hostId, planned.role, planned.launch);
264
+ hostCreated = await ensureRoleWindow(this.tmux, hostId, planned.role, reuseInteractivePane ? undefined : planned.launch);
248
265
  }
249
266
  catch (error) {
250
267
  throw toRuntimeLaunchFailure(error, "host-start", launchContext);
251
268
  }
269
+ if (reuseInteractivePane && hostCreated) {
270
+ throw new Error("Global Role pane changed during attachment.");
271
+ }
252
272
  let binding = createRuntimeBinding({
253
273
  id: bindingId,
254
274
  runtimeGenerationId: request.runtimeGenerationId,
@@ -282,6 +302,9 @@ export class TmuxSessionHost {
282
302
  }
283
303
  return binding;
284
304
  }
305
+ if (yuiHome === undefined || childLifecycle === undefined) {
306
+ throw new Error("Agent Host launch is missing its Home or child lifecycle.");
307
+ }
285
308
  const broker = launchBrokerForHome(yuiHome);
286
309
  const sessionManifest = planned.launch.env.YUI_SESSION_MANIFEST;
287
310
  const frozenControlPlane = planned.launch.env[YUI_CONTROL_PLANE_DESCRIPTOR];
@@ -341,17 +364,36 @@ export class TmuxSessionHost {
341
364
  try {
342
365
  hostCreated = await ensureRoleWindow(this.tmux, hostId, planned.role, hostLaunch);
343
366
  if (hostCreated && planned.launch.deferProviderStart !== true) {
367
+ const assertInteractivePane = async () => {
368
+ const pane = await inspectRolePane(this.tmux, hostId, request.owner.roleName);
369
+ if (pane === undefined)
370
+ throw new Error("Global Codex startup has no observable pane state.");
371
+ if (pane.dead) {
372
+ await deadHostLaunchFailure(this.tmux, hostId, request.owner.roleName, pane, launchContext);
373
+ }
374
+ };
344
375
  providerSnapshot = await waitForAgentHostLaunchAck({
345
376
  home: yuiHome,
346
377
  scope: request.owner.scope,
347
378
  ...(request.owner.scope === "task" ? { taskId: request.owner.taskId } : {}),
348
379
  roleName: request.owner.roleName,
349
380
  runtimeGenerationId: reservation.runtimeGenerationId,
350
- requireTurnAck: false
381
+ requireTurnAck: false,
382
+ ...(interactiveCodex ? { assertHostRunning: assertInteractivePane } : {})
351
383
  });
384
+ if (interactiveCodex) {
385
+ if (providerSnapshot.nativeSessionId === undefined) {
386
+ throw new Error("Global Codex startup acknowledgement has no Thread identity.");
387
+ }
388
+ requireSafeIdentity(providerSnapshot.nativeSessionId, "Codex Thread identity");
389
+ await assertInteractivePane();
390
+ }
352
391
  providerDispatchObserved = true;
353
392
  }
354
393
  if (!hostCreated) {
394
+ if (interactiveCodex) {
395
+ throw new RuntimeHostContentionError("previous-process", "Global Role pane appeared during launch; preserving its existing Session.");
396
+ }
355
397
  const controlResult = await sendAgentHostLaunchControl({
356
398
  home: yuiHome,
357
399
  scope: request.owner.scope,
@@ -1,5 +1,5 @@
1
1
  import { serializeTurnInputEnvelope } from "../context/turnInputContract.js";
2
- import { effectiveLaunchSnapshotsCompatible, effectiveLaunchSnapshotsCompatibleForTaskSession } from "../executor/effectiveLaunch.js";
2
+ import { roleSessionMayContinue, sameEffectiveLaunch } from "../executor/effectiveLaunch.js";
3
3
  import { RuntimeLifecycleBusyError } from "../runtime/lifecycleReservation.js";
4
4
  import { managedProviderTurnId } from "../runtime/providerRuntimeIdentity.js";
5
5
  import { serializeAgentErrorRaw } from "../runtime/agentError.js";
@@ -231,8 +231,8 @@ function validateRoleSession(role, turn, existing, mode, session) {
231
231
  throw new Error(`Ready Role session identity changed: ${role.taskId}/${role.name}.`);
232
232
  }
233
233
  const compatible = mode === "resume"
234
- ? effectiveLaunchSnapshotsCompatibleForTaskSession(session.effective, turn.effective)
235
- : effectiveLaunchSnapshotsCompatible(session.effective, turn.effective);
234
+ ? roleSessionMayContinue(session.effective, turn.effective)
235
+ : sameEffectiveLaunch(session.effective, turn.effective);
236
236
  if (!compatible) {
237
237
  throw new Error(`Ready Role session effective snapshot changed: ${role.taskId}/${role.name}.`);
238
238
  }
@@ -249,7 +249,7 @@ function requireResumeSession(role, turn, session) {
249
249
  if (session === null || !hasText(session.nativeSessionId)) {
250
250
  throw new Error(`Role resume has no fixed native session: ${role.taskId}/${role.name}.`);
251
251
  }
252
- if (!effectiveLaunchSnapshotsCompatibleForTaskSession(session.effective, turn.effective)) {
252
+ if (!roleSessionMayContinue(session.effective, turn.effective)) {
253
253
  throw new Error(`Role resume effective snapshot drifted: ${role.taskId}/${role.name}.`);
254
254
  }
255
255
  return session.nativeSessionId;
@@ -1,5 +1,5 @@
1
1
  import { createTurnInput } from "../context/turnInputContract.js";
2
- import { effectiveLaunchSnapshotsCompatible, effectiveLaunchSnapshotsCompatibleForTaskSession } from "../executor/effectiveLaunch.js";
2
+ import { roleSessionMayContinue } from "../executor/effectiveLaunch.js";
3
3
  import { roleAgentSessionResumeMode } from "../executor/agentExecutor.js";
4
4
  import { createTurn } from "../turn/turn.js";
5
5
  import { isSchedulerTaskWorkspaceReady } from "./ports.js";
@@ -63,9 +63,8 @@ export async function processLeaderWakeups(store, delivery, now, selection) {
63
63
  try {
64
64
  const reopening = wakeup.reasons.includes("task-reopened");
65
65
  const existingSession = store.getRoleSession(task.id, role.name, reopening ? undefined : role.effective.agentId);
66
- const compatible = existingSession !== null && (reopening
67
- ? effectiveLaunchSnapshotsCompatible(existingSession.effective, role.effective)
68
- : effectiveLaunchSnapshotsCompatibleForTaskSession(existingSession.effective, role.effective));
66
+ const compatible = existingSession !== null
67
+ && roleSessionMayContinue(existingSession.effective, role.effective);
69
68
  if (hasNativeSession(existingSession)
70
69
  && existingSession.status === "active"
71
70
  && !compatible