Skip to content

Commit 909509a

Browse files
authored
fix(codex): resume catalog sessions with the catalog's CODEX_HOME; add main-content terminal placement (#120708)
* fix(codex): pin catalog CODEX_HOME onto terminal resume spawns The Codex session catalog lists threads through the plugin's supervision app-server connection, which can run on the agent-scoped Codex home (homeScope: "agent"). The terminal resume plan spawned a bare 'codex resume <threadId>' under the operator login shell, so the CLI resolved its default ~/.codex and failed with "No saved session found". Terminal plans now carry the catalog connection's CODEX_HOME (local and paired-node paths), the gateway merges plan env into the PTY spawn, and the triplicated home-scope resolution collapses into one shared resolveCodexAppServerLocalHomeDir. * feat(ui): add main-content terminal placement The terminal panel gains a third dock placement, "main", filling the content region next to the sidebar like the transcript pane. The panel header gets a bottom/right/main placement switcher (persisted in the existing openclaw.terminal.panel.v1 store), and opening a Codex/Claude session from the session catalog selects main mode so resumed sessions appear as a full window instead of a bottom strip.
1 parent 36c6b85 commit 909509a

24 files changed

Lines changed: 300 additions & 66 deletions

‎docs/web/control-ui.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ The dockable operator terminal is enabled by default; set `gateway.terminal.enab
397397
The terminal is an unconfined host shell and inherits the Gateway process environment. Disable it with `gateway.terminal.enabled: false` on deployments where admin operators should not get a host shell. OpenClaw refuses terminal sessions for agents with `sandbox.mode: "all"`; changing an active agent to that mode closes its existing and in-flight terminal sessions.
398398
</Warning>
399399

400-
Use **Ctrl + backtick** to toggle the dock. The layout supports bottom and right docking, resizes with the browser viewport, and keeps multiple shell tabs. See [Gateway configuration](/gateway/configuration-reference#gateway) for `gateway.terminal.enabled` and the optional `gateway.terminal.shell` override.
400+
Use **Ctrl + backtick** to toggle the terminal. The layout supports bottom and right docking or a main-content mode, resizes with the browser viewport, and keeps multiple shell tabs. Opening a Codex or Claude Code session from the catalog selects main-content mode. See [Gateway configuration](/gateway/configuration-reference#gateway) for `gateway.terminal.enabled` and the optional `gateway.terminal.shell` override.
401401

402402
Owner-authorized, unsandboxed agents can use the `terminal` tool for long or interactive work that the operator should watch. Each tool call can open, read, write, resize, close, or list the agent's own gateway PTYs. New sessions open a co-attached Control UI tab by default, so the agent and operator share output and either can type or resize. Agent access is exact-session scoped: an agent cannot read or control operator-created terminals or terminals opened by another agent session.
403403

‎extensions/codex/index.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,13 @@ export default definePluginEntry({
133133
api,
134134
bindingStore,
135135
control: sessionCatalogControl,
136+
getPluginConfig: resolveCurrentPluginConfig,
136137
getRuntimeConfig: resolveCurrentConfig,
137138
});
138-
for (const command of createCodexSessionCatalogNodeHostCommands(sessionCatalogControl)) {
139+
for (const command of createCodexSessionCatalogNodeHostCommands(sessionCatalogControl, {
140+
getPluginConfig: resolveCurrentPluginConfig,
141+
getRuntimeConfig: resolveCurrentConfig,
142+
})) {
139143
api.registerNodeHostCommand(command);
140144
}
141145
}

‎extensions/codex/src/app-server/auth-bridge.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ import {
2323
} from "openclaw/plugin-sdk/agent-runtime";
2424
import { hasUsableOAuthCredential } from "openclaw/plugin-sdk/provider-auth";
2525
import { readSecretFile } from "openclaw/plugin-sdk/secret-file";
26-
import { resolveCodexAppServerHomeDir, withEphemeralCodexAuthStore } from "./auth-start-options.js";
26+
import {
27+
resolveCodexAppServerHomeDir,
28+
resolveCodexAppServerLocalHomeDir,
29+
withEphemeralCodexAuthStore,
30+
} from "./auth-start-options.js";
2731
import type { CodexAppServerClient } from "./client.js";
2832
import { ensureCodexComputerUseSharedPluginCache } from "./computer-use-cache.js";
2933
import { ensureCodexComputerUseServiceApp } from "./computer-use-service.js";
3034
import {
31-
resolveCodexAppServerUserHomeDir,
3235
resolveCodexComputerUseConfig,
3336
type CodexAppServerHomeScope,
3437
type CodexAppServerStartOptions,
@@ -489,11 +492,7 @@ async function withCodexHomeEnvironment(
489492
agentDir: string,
490493
pluginConfig?: unknown,
491494
): Promise<CodexAppServerStartOptions> {
492-
const codexHome = startOptions.env?.[CODEX_HOME_ENV_VAR]?.trim()
493-
? startOptions.env[CODEX_HOME_ENV_VAR]
494-
: startOptions.homeScope === "user"
495-
? resolveCodexAppServerUserHomeDir(process.env)
496-
: resolveCodexAppServerHomeDir(agentDir);
495+
const codexHome = resolveCodexAppServerLocalHomeDir(startOptions, agentDir);
497496
const nativeHome = startOptions.env?.[HOME_ENV_VAR]?.trim()
498497
? startOptions.env[HOME_ENV_VAR]
499498
: undefined;

‎extensions/codex/src/app-server/auth-start-options.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from "node:path";
2+
import { resolveCodexAppServerUserHomeDir } from "./config-reviewer.js";
23
import type { CodexAppServerStartOptions } from "./config.js";
34

45
const CODEX_APP_SERVER_HOME_DIRNAME = "codex-home";
@@ -8,6 +9,21 @@ export function resolveCodexAppServerHomeDir(agentDir: string): string {
89
return path.join(path.resolve(agentDir), CODEX_APP_SERVER_HOME_DIRNAME);
910
}
1011

12+
/** Resolves the local CODEX_HOME used when starting one app-server connection. */
13+
export function resolveCodexAppServerLocalHomeDir(
14+
startOptions: CodexAppServerStartOptions,
15+
agentDir: string,
16+
env: NodeJS.ProcessEnv = process.env,
17+
): string {
18+
const configured = startOptions.env?.CODEX_HOME;
19+
if (configured?.trim()) {
20+
return configured;
21+
}
22+
return startOptions.homeScope === "user"
23+
? resolveCodexAppServerUserHomeDir(env)
24+
: resolveCodexAppServerHomeDir(agentDir);
25+
}
26+
1127
/** Forces OpenClaw-owned Codex auth to remain process-local. */
1228
export function withEphemeralCodexAuthStore(params: {
1329
startOptions: CodexAppServerStartOptions;

‎extensions/codex/src/app-server/plugin-app-cache-key.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
buildCodexAppInventoryCacheKey,
1111
type CodexAppInventoryCacheKeyInput,
1212
} from "./app-inventory-cache.js";
13-
import { resolveCodexAppServerHomeDir } from "./auth-bridge.js";
13+
import {
14+
resolveCodexAppServerHomeDir,
15+
resolveCodexAppServerLocalHomeDir,
16+
} from "./auth-start-options.js";
1417
import type { CodexAppServerRuntimeIdentity } from "./client.js";
1518
import {
1619
resolveCodexAppServerUserHomeDir,
@@ -103,7 +106,7 @@ function resolveCodexAppServerConnectionHome(
103106
if (start.homeScope === "user") {
104107
return resolveCodexAppServerUserHomeDir(process.env);
105108
}
106-
return agentDir ? resolveCodexAppServerHomeDir(agentDir) : null;
109+
return agentDir ? resolveCodexAppServerLocalHomeDir(start, agentDir) : null;
107110
}
108111

109112
/** Serializes app-server endpoint identity, including credential fingerprints. */

‎extensions/codex/src/session-catalog-terminal.ts‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Codex catalog terminal ownership: validated resume commands and terminal plans.
2+
import { resolveDefaultAgentDir } from "openclaw/plugin-sdk/agent-runtime";
3+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
24
import {
35
decodeNodePtyResumeParams,
46
resolveNodeHostExecutable,
@@ -10,6 +12,8 @@ import type {
1012
} from "openclaw/plugin-sdk/plugin-entry";
1113
import type { PluginRuntime } from "openclaw/plugin-sdk/plugin-runtime";
1214
import type { SessionCatalogTerminalPlan } from "openclaw/plugin-sdk/session-catalog";
15+
import { resolveCodexAppServerLocalHomeDir } from "./app-server/auth-start-options.js";
16+
import { resolveCodexSupervisionAppServerRuntimeOptions } from "./app-server/config.js";
1317
import {
1418
CatalogParamsError,
1519
CODEX_APP_SERVER_THREADS_CAPABILITY,
@@ -29,6 +33,22 @@ import type {
2933

3034
export const CODEX_TERMINAL_RESUME_COMMAND = "codex.terminal.resume.v1";
3135

36+
export type CodexTerminalConfigSources = {
37+
getPluginConfig: () => unknown;
38+
getRuntimeConfig: () => OpenClawConfig | undefined;
39+
};
40+
41+
function resolveCodexCatalogTerminalHome(sources: CodexTerminalConfigSources): string {
42+
const runtimeConfig = sources.getRuntimeConfig();
43+
if (!runtimeConfig) {
44+
throw new Error("OpenClaw runtime config is unavailable");
45+
}
46+
const startOptions = resolveCodexSupervisionAppServerRuntimeOptions({
47+
pluginConfig: sources.getPluginConfig(),
48+
}).start;
49+
return resolveCodexAppServerLocalHomeDir(startOptions, resolveDefaultAgentDir(runtimeConfig));
50+
}
51+
3252
export function resolveLocalCodexTerminalExecutable(
3353
env: NodeJS.ProcessEnv = process.env,
3454
): string | undefined {
@@ -106,6 +126,7 @@ async function findCatalogEligibleThread(
106126

107127
export function createCodexTerminalNodeHostCommand(
108128
control: CodexSessionCatalogControl,
129+
configSources: CodexTerminalConfigSources,
109130
): OpenClawPluginNodeHostCommand {
110131
return {
111132
command: CODEX_TERMINAL_RESUME_COMMAND,
@@ -148,6 +169,7 @@ export function createCodexTerminalNodeHostCommand(
148169
file: resolution.executable,
149170
args: ["resume", resume.threadId],
150171
cwd: record.cwd,
172+
env: { CODEX_HOME: resolveCodexCatalogTerminalHome(configSources) },
151173
cols: resume.cols,
152174
rows: resume.rows,
153175
},
@@ -195,13 +217,15 @@ async function resolveNodeCatalogEligibleThread(params: {
195217
throw new CatalogParamsError("Codex session is not a non-archived interactive Codex session");
196218
}
197219

198-
export async function openCodexCatalogTerminal(params: {
199-
api: OpenClawPluginApi;
200-
control: CodexSessionCatalogControl;
201-
hostId: string;
202-
threadId: string;
203-
parseCatalogPage: (value: unknown) => CodexSessionCatalogPage;
204-
}): Promise<SessionCatalogTerminalPlan> {
220+
export async function openCodexCatalogTerminal(
221+
params: {
222+
api: OpenClawPluginApi;
223+
control: CodexSessionCatalogControl;
224+
hostId: string;
225+
threadId: string;
226+
parseCatalogPage: (value: unknown) => CodexSessionCatalogPage;
227+
} & CodexTerminalConfigSources,
228+
): Promise<SessionCatalogTerminalPlan> {
205229
const title = `codex resume ${params.threadId.slice(0, 8)}…`;
206230
if (params.hostId === CODEX_LOCAL_SESSION_HOST_ID) {
207231
const record = await requireCatalogEligibleThread(params.control, params.threadId);
@@ -215,6 +239,7 @@ export async function openCodexCatalogTerminal(params: {
215239
kind: "local",
216240
argv: [resolution.executable, "resume", params.threadId],
217241
...(record.cwd ? { cwd: record.cwd } : {}),
242+
env: { CODEX_HOME: resolveCodexCatalogTerminalHome(params) },
218243
...(resolution.pathEnv ? { pathEnv: resolution.pathEnv } : {}),
219244
title,
220245
};

‎extensions/codex/src/session-catalog.test.ts‎

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createHash } from "node:crypto";
44
import fs from "node:fs/promises";
55
import os from "node:os";
66
import path from "node:path";
7+
import { resolveDefaultAgentDir } from "openclaw/plugin-sdk/agent-runtime";
78
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
89
import {
910
validateJsonSchemaValue,
@@ -14,6 +15,8 @@ import type { PluginRuntime } from "openclaw/plugin-sdk/plugin-runtime";
1415
import type { SessionCatalogProvider } from "openclaw/plugin-sdk/session-catalog";
1516
import { resolveStorePath } from "openclaw/plugin-sdk/session-store-runtime";
1617
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
18+
import { resolveCodexAppServerHomeDir } from "./app-server/auth-start-options.js";
19+
import { resolveCodexAppServerUserHomeDir } from "./app-server/config.js";
1720
import type { CodexThread } from "./app-server/protocol.js";
1821
import { sessionBindingIdentity } from "./app-server/session-binding.js";
1922
import {
@@ -26,12 +29,13 @@ import { catalogError, parseCatalogPage } from "./session-catalog-parsing.js";
2629
import {
2730
CODEX_TERMINAL_RESUME_COMMAND,
2831
requireCatalogEligibleThread,
32+
type CodexTerminalConfigSources,
2933
} from "./session-catalog-terminal.js";
3034
import {
3135
CODEX_LOCAL_SESSION_HOST_ID,
3236
codexSessionCatalogRuntime,
3337
createCodexSessionCatalogControl,
34-
createCodexSessionCatalogNodeHostCommands,
38+
createCodexSessionCatalogNodeHostCommands as createCodexSessionCatalogNodeHostCommandsRuntime,
3539
createCodexSessionCatalogNodeInvokePolicies,
3640
} from "./session-catalog.js";
3741

@@ -51,7 +55,28 @@ const archiveLocalCodexSession = codexSessionCatalogRuntime.archiveLocal;
5155
const continueLocalCodexSession = codexSessionCatalogRuntime.continueLocal;
5256
const listCodexSessionCatalog = codexSessionCatalogRuntime.list;
5357
const readCodexSessionTranscript = codexSessionCatalogRuntime.readTranscript;
54-
const registerCodexSessionCatalog = codexSessionCatalogRuntime.register;
58+
const registerCodexSessionCatalogRuntime = codexSessionCatalogRuntime.register;
59+
60+
function registerCodexSessionCatalog(
61+
params: Omit<Parameters<typeof registerCodexSessionCatalogRuntime>[0], "getPluginConfig"> & {
62+
getPluginConfig?: () => unknown;
63+
},
64+
) {
65+
return registerCodexSessionCatalogRuntime({
66+
...params,
67+
getPluginConfig: params.getPluginConfig ?? (() => undefined),
68+
});
69+
}
70+
71+
function createCodexSessionCatalogNodeHostCommands(
72+
control: CodexSessionCatalogControl,
73+
configSources: CodexTerminalConfigSources = {
74+
getPluginConfig: () => undefined,
75+
getRuntimeConfig: () => config,
76+
},
77+
) {
78+
return createCodexSessionCatalogNodeHostCommandsRuntime(control, configSources);
79+
}
5580

5681
const commandRpcMocks = vi.hoisted(() => ({
5782
codexControlRequest: vi.fn(),
@@ -1537,6 +1562,10 @@ describe("Codex supervision catalog", () => {
15371562
],
15381563
})),
15391564
}),
1565+
{
1566+
getPluginConfig: () => ({ appServer: { homeScope: "agent" } }),
1567+
getRuntimeConfig: () => config,
1568+
},
15401569
).find((candidate) => candidate.command === CODEX_TERMINAL_RESUME_COMMAND);
15411570
if (!command || command.duplex !== true) {
15421571
throw new Error("Codex terminal command was not registered as duplex");
@@ -1550,7 +1579,13 @@ describe("Codex supervision catalog", () => {
15501579

15511580
expect(command.dangerous).toBe(false);
15521581
expect(nodeHostMocks.runNodePtyCommand).toHaveBeenCalledWith(
1553-
expect.objectContaining({ file: executable, cwd: "/node/catalog/cwd" }),
1582+
expect.objectContaining({
1583+
file: executable,
1584+
cwd: "/node/catalog/cwd",
1585+
env: {
1586+
CODEX_HOME: resolveCodexAppServerHomeDir(resolveDefaultAgentDir(config)),
1587+
},
1588+
}),
15541589
expect.any(Object),
15551590
);
15561591
});
@@ -3760,10 +3795,12 @@ describe("Codex supervision actions", () => {
37603795
};
37613796
const { runtime } = createRuntime({ nodes: [node], invoke });
37623797
const { api, getProvider } = createGatewayApi(runtime);
3798+
let pluginConfig: unknown = { appServer: { homeScope: "agent" } };
37633799
registerCodexSessionCatalog({
37643800
api,
37653801
bindingStore: createCodexTestBindingStore(),
37663802
control,
3803+
getPluginConfig: () => pluginConfig,
37673804
getRuntimeConfig: () => config,
37683805
});
37693806

@@ -3806,6 +3843,15 @@ describe("Codex supervision actions", () => {
38063843
kind: "local",
38073844
argv: [executable, "resume", threadId],
38083845
cwd: "/workspace/local",
3846+
env: {
3847+
CODEX_HOME: resolveCodexAppServerHomeDir(resolveDefaultAgentDir(config)),
3848+
},
3849+
});
3850+
pluginConfig = { appServer: { homeScope: "user" } };
3851+
await expect(
3852+
getProvider()?.openTerminal?.({ hostId: CODEX_LOCAL_SESSION_HOST_ID, threadId }),
3853+
).resolves.toMatchObject({
3854+
env: { CODEX_HOME: resolveCodexAppServerUserHomeDir(process.env) },
38093855
});
38103856
await expect(
38113857
getProvider()?.openTerminal?.({ hostId: "node:devbox", threadId }),

‎extensions/codex/src/session-catalog.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import {
101101
openCodexCatalogTerminal,
102102
requireCatalogEligibleThread,
103103
resolveLocalCodexTerminalExecutable,
104+
type CodexTerminalConfigSources,
104105
} from "./session-catalog-terminal.js";
105106
import { toGenericTranscriptItem } from "./session-catalog-transcript-item.js";
106107
import type {
@@ -667,6 +668,7 @@ async function listCodexSessionCatalog(params: {
667668
/** Builds the node-local read-only Codex app-server catalog command. */
668669
export function createCodexSessionCatalogNodeHostCommands(
669670
control: CodexSessionCatalogControl,
671+
configSources: CodexTerminalConfigSources,
670672
): OpenClawPluginNodeHostCommand[] {
671673
return [
672674
{
@@ -713,7 +715,7 @@ export function createCodexSessionCatalogNodeHostCommands(
713715
}
714716
},
715717
},
716-
createCodexTerminalNodeHostCommand(control),
718+
createCodexTerminalNodeHostCommand(control, configSources),
717719
];
718720
}
719721

@@ -1434,6 +1436,7 @@ function registerCodexSessionCatalog(params: {
14341436
api: OpenClawPluginApi;
14351437
bindingStore: CodexAppServerBindingStore;
14361438
control: CodexSessionCatalogControl;
1439+
getPluginConfig: () => unknown;
14371440
getRuntimeConfig: () => OpenClawConfig | undefined;
14381441
}): void {
14391442
const provider: SessionCatalogProvider = {
@@ -1531,6 +1534,8 @@ function registerCodexSessionCatalog(params: {
15311534
openCodexCatalogTerminal({
15321535
api: params.api,
15331536
control: params.control,
1537+
getPluginConfig: params.getPluginConfig,
1538+
getRuntimeConfig: params.getRuntimeConfig,
15341539
parseCatalogPage,
15351540
...request,
15361541
}),

‎src/gateway/server-methods/terminal.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ describe("terminal gateway policy", () => {
201201
const openTerminal = vi.fn(async () => ({
202202
kind: "local" as const,
203203
argv: ["codex", "resume", "thread"],
204+
env: { CODEX_HOME: "/agent/codex-home" },
204205
pathEnv: "/login-shell/bin:/usr/bin",
205206
title: "codex resume thread",
206207
}));
@@ -230,7 +231,10 @@ describe("terminal gateway policy", () => {
230231
expect.objectContaining({
231232
shell: expect.any(String),
232233
args: ["-il", "-c", "'codex' 'resume' 'thread'"],
233-
env: expect.objectContaining({ PATH: "/login-shell/bin:/usr/bin" }),
234+
env: expect.objectContaining({
235+
CODEX_HOME: "/agent/codex-home",
236+
PATH: "/login-shell/bin:/usr/bin",
237+
}),
234238
}),
235239
);
236240
expect(respond).toHaveBeenCalledWith(

‎src/gateway/server-methods/terminal.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,13 @@ export const terminalHandlers: GatewayRequestHandlers = {
293293
}
294294
const spawnPlan = resolveTerminalOpenSpawnPlan(refreshedLaunch.plan, catalogPlan);
295295
const terminalEnv = buildTerminalEnv(process.env);
296-
if (catalogPlan?.kind === "local" && catalogPlan.pathEnv) {
297-
// Preserve the PATH that found a login-shell CLI so env-based shebangs
298-
// can resolve their interpreter inside the spawned terminal process.
299-
terminalEnv.PATH = catalogPlan.pathEnv;
296+
if (catalogPlan?.kind === "local") {
297+
Object.assign(terminalEnv, catalogPlan.env);
298+
if (catalogPlan.pathEnv) {
299+
// Preserve the PATH that found a login-shell CLI so env-based shebangs
300+
// can resolve their interpreter inside the spawned terminal process.
301+
terminalEnv.PATH = catalogPlan.pathEnv;
302+
}
300303
}
301304
let openingTerminal: ReturnType<typeof manager.open> | undefined;
302305
let outcome: Awaited<ReturnType<typeof manager.open>>;

0 commit comments

Comments
 (0)