Skip to content

Commit 2c9bcf3

Browse files
fix(codex): keep /codex model scoped to the native binding (#128751)
* fix(codex): keep /codex model scoped to the native binding /codex model wrote the outer OpenClaw session model override (and marked a pending live model switch) any time a session context was present, even for a thread bound through /codex bind. In a dual-runtime setup that let a Codex-scoped model change silently redirect the next turn to a different configured runtime. The binding store is now the sole owner of this selection; only /model may change the outer session override. * fix(codex): make /codex model status binding-owned The no-argument /codex model status read still preferred the outer SessionEntry override over the native binding, so it could report a stale model that diverged from what /codex model just wrote to the binding. Read the binding first and fall back to the outer session model only when no binding exists yet. * fix(codex): preserve authoritative model selection ownership Co-authored-by: chelsealong <[email protected]> * fix(codex): retain session identity narrowing --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 160e2ba commit 2c9bcf3

4 files changed

Lines changed: 170 additions & 48 deletions

File tree

‎extensions/codex/src/command-handler-actions.ts‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,10 @@ export async function setConversationModel(
352352
? (currentSession.modelOverride ?? currentSession.model)
353353
: undefined;
354354
const binding = await deps.bindingStore.read(target.identity);
355-
const activeModel = selectedModel ?? binding?.model;
355+
// Direct sessions report their desired selection; bound conversations
356+
// must never mistake an ambient outer-session model for native ownership.
357+
const activeModel =
358+
target.identity.kind === "conversation" ? binding?.model : (selectedModel ?? binding?.model);
356359
return activeModel
357360
? `Codex model: ${formatCodexDisplayText(activeModel)}`
358361
: "Usage: /codex model <model>";
@@ -364,15 +367,6 @@ export async function setConversationModel(
364367
model: normalized,
365368
agentDir: target.agentDir,
366369
config: ctx.config,
367-
...(ctx.sessionId && ctx.sessionKey
368-
? {
369-
session: {
370-
agentId: target.agentId,
371-
sessionId: ctx.sessionId,
372-
sessionKey: ctx.sessionKey,
373-
},
374-
}
375-
: {}),
376370
});
377371
}
378372

‎extensions/codex/src/commands.test.ts‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6163,6 +6163,69 @@ describe("codex command", () => {
61636163
});
61646164
});
61656165

6166+
it("updates a bound conversation without changing its ambient outer session", async () => {
6167+
const sessionKey = "agent:main:session-1";
6168+
const storePath = resolveStorePath(undefined, { agentId: "main" });
6169+
await upsertSessionEntry({
6170+
agentId: "main",
6171+
storePath,
6172+
sessionKey,
6173+
entry: {
6174+
sessionId: "session-1",
6175+
updatedAt: Date.now(),
6176+
providerOverride: "anthropic",
6177+
modelOverride: "claude-sonnet-4-6",
6178+
agentRuntimeOverride: "claude-cli",
6179+
authProfileOverride: "anthropic:personal",
6180+
authProfileOverrideSource: "user",
6181+
},
6182+
});
6183+
await writeTestBinding(
6184+
{ kind: "conversation", bindingId: "binding-data-1" },
6185+
{ threadId: "thread-conversation", cwd: "/repo", model: "gpt-5.4", modelProvider: "openai" },
6186+
);
6187+
const getCurrentConversationBinding = async () => ({
6188+
bindingId: "binding-1",
6189+
pluginId: "codex",
6190+
pluginRoot: "/plugin",
6191+
channel: "test",
6192+
accountId: "default",
6193+
conversationId: "conversation",
6194+
boundAt: 1,
6195+
data: {
6196+
kind: "codex-app-server-session" as const,
6197+
version: 2 as const,
6198+
bindingId: "binding-data-1",
6199+
workspaceDir: "/repo",
6200+
},
6201+
});
6202+
6203+
await expect(
6204+
handleCodexCommand(
6205+
createContext("model gpt-5.5", undefined, {
6206+
sessionKey,
6207+
getCurrentConversationBinding,
6208+
}),
6209+
{ deps: createDeps() },
6210+
),
6211+
).resolves.toEqual({ text: "Codex model set to gpt-5.5." });
6212+
6213+
await expect(
6214+
testCodexAppServerBindingStore.read({ kind: "conversation", bindingId: "binding-data-1" }),
6215+
).resolves.toMatchObject({
6216+
threadId: "thread-conversation",
6217+
model: "gpt-5.5",
6218+
modelProvider: "openai",
6219+
});
6220+
expect(getSessionEntry({ storePath, sessionKey })).toMatchObject({
6221+
providerOverride: "anthropic",
6222+
modelOverride: "claude-sonnet-4-6",
6223+
agentRuntimeOverride: "claude-cli",
6224+
authProfileOverride: "anthropic:personal",
6225+
authProfileOverrideSource: "user",
6226+
});
6227+
});
6228+
61666229
it.each([
61676230
{
61686231
name: "rejects owner yolo without admin scope",
@@ -6340,6 +6403,83 @@ describe("codex command", () => {
63406403
expect(setCodexConversationPermissions).toHaveBeenCalledOnce();
63416404
});
63426405

6406+
it("reports the desired direct-session model before its stale native binding reloads", async () => {
6407+
const sessionKey = "agent:main:diverged-model";
6408+
const storePath = resolveStorePath(undefined, { agentId: "main" });
6409+
await upsertSessionEntry({
6410+
agentId: "main",
6411+
storePath,
6412+
sessionKey,
6413+
entry: {
6414+
sessionId: "session-1",
6415+
updatedAt: Date.now(),
6416+
modelOverride: "outer-override-model",
6417+
},
6418+
});
6419+
await writeTestBinding(
6420+
{ kind: "session", agentId: "main", sessionId: "session-1", sessionKey },
6421+
{ threadId: "thread-diverged", cwd: "/repo", model: "bound-model" },
6422+
);
6423+
6424+
await expect(
6425+
handleCodexCommand(
6426+
createContext("model", undefined, { sessionId: "session-1", sessionKey }),
6427+
{
6428+
deps: createDeps(),
6429+
},
6430+
),
6431+
).resolves.toEqual({ text: "Codex model: outer-override-model" });
6432+
});
6433+
6434+
it.each([
6435+
{ boundModel: "bound-model", expected: "Codex model: bound-model" },
6436+
{ boundModel: undefined, expected: "Usage: /codex model <model>" },
6437+
])("keeps conversation model status independent from its ambient session", async (testCase) => {
6438+
const sessionKey = "agent:main:conversation-model";
6439+
await upsertSessionEntry({
6440+
agentId: "main",
6441+
storePath: resolveStorePath(undefined, { agentId: "main" }),
6442+
sessionKey,
6443+
entry: {
6444+
sessionId: "session-1",
6445+
updatedAt: Date.now(),
6446+
modelOverride: "outer-override-model",
6447+
},
6448+
});
6449+
await writeTestBinding(
6450+
{ kind: "conversation", bindingId: "binding-data-1" },
6451+
{
6452+
threadId: "thread-conversation",
6453+
cwd: "/repo",
6454+
...(testCase.boundModel ? { model: testCase.boundModel } : {}),
6455+
},
6456+
);
6457+
6458+
const result = await handleCodexCommand(
6459+
createContext("model", undefined, {
6460+
sessionKey,
6461+
getCurrentConversationBinding: async () => ({
6462+
bindingId: "binding-1",
6463+
pluginId: "codex",
6464+
pluginRoot: "/plugin",
6465+
channel: "test",
6466+
accountId: "default",
6467+
conversationId: "conversation",
6468+
boundAt: 1,
6469+
data: {
6470+
kind: "codex-app-server-session",
6471+
version: 2,
6472+
bindingId: "binding-data-1",
6473+
workspaceDir: tempDir,
6474+
},
6475+
}),
6476+
}),
6477+
{ deps: createDeps() },
6478+
);
6479+
6480+
expect(result).toEqual({ text: testCase.expected });
6481+
});
6482+
63436483
it("escapes current bound model status before chat display", async () => {
63446484
const sessionFile = path.join(tempDir, "session.jsonl");
63456485
await writeTestBinding(

‎extensions/codex/src/conversation-control.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ describe("codex conversation controls", () => {
420420
expect(sharedClientMocks.getSharedCodexAppServerClient).not.toHaveBeenCalled();
421421
});
422422

423-
it("persists ordinary model selection on SessionEntry without overwriting native ownership", async () => {
423+
it("persists direct-session model selection without overwriting the active native binding", async () => {
424424
const sessionKey = "agent:main:model-session";
425425
const sessionId = "session-model-authority";
426426
const identity = { kind: "session" as const, agentId: "main", sessionId, sessionKey };
@@ -468,7 +468,7 @@ describe("codex conversation controls", () => {
468468
expect(sharedClientMocks.getSharedCodexAppServerClient).not.toHaveBeenCalled();
469469
});
470470

471-
it("drops an incompatible pinned auth profile when selecting another provider", async () => {
471+
it("clears incompatible direct-session auth when the selected provider changes", async () => {
472472
const sessionKey = "agent:main:model-provider-switch";
473473
const sessionId = "session-provider-switch";
474474
const identity = { kind: "session" as const, agentId: "main", sessionId, sessionKey };
@@ -502,6 +502,11 @@ describe("codex conversation controls", () => {
502502
}),
503503
).resolves.toBe("Codex model set to gpt-5.5.");
504504

505+
await expect(testCodexAppServerBindingStore.read(identity)).resolves.toMatchObject({
506+
threadId: "thread-provider-switch",
507+
model: "local-model",
508+
modelProvider: "lmstudio",
509+
});
505510
expect(getSessionEntry({ storePath, sessionKey })).toMatchObject({
506511
providerOverride: "openai",
507512
modelOverride: "gpt-5.5",

‎extensions/codex/src/conversation-control.ts‎

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { resolveAgentDir } from "openclaw/plugin-sdk/agent-runtime";
21
// Codex plugin module implements conversation control behavior.
2+
import { resolveAgentDir } from "openclaw/plugin-sdk/agent-runtime";
33
import {
44
applyModelOverrideWithAuthProfileCompatibility,
55
ModelSelectionLockedError,
@@ -187,7 +187,6 @@ export async function setCodexConversationModel(params: {
187187
pluginConfig?: unknown;
188188
agentDir?: string;
189189
config?: CodexAppServerBindingLookup["config"];
190-
session?: { agentId: string; sessionId: string; sessionKey: string };
191190
}): Promise<string> {
192191
const model = params.model.trim();
193192
if (!model) {
@@ -218,31 +217,27 @@ export async function setCodexConversationModel(params: {
218217
});
219218
const nextModel = modelSelection.model;
220219
const modelChanged = nextModel !== binding.model || nextModelProvider !== binding.modelProvider;
221-
const session =
222-
params.session ??
223-
(params.identity.kind === "session" && params.identity.sessionKey
224-
? {
225-
agentId: params.identity.agentId,
226-
sessionId: params.identity.sessionId,
227-
sessionKey: params.identity.sessionKey,
228-
}
229-
: undefined);
230-
if (session) {
220+
const projectionPatch =
221+
modelChanged && binding.contextEngine?.projection
222+
? { contextEngine: { ...binding.contextEngine, projection: undefined } }
223+
: {};
224+
const identity = params.identity;
225+
if (identity.kind === "session" && identity.sessionKey) {
226+
// SessionEntry owns the desired model; retain the loaded binding until
227+
// lifecycle reconciliation can rotate its native generation safely.
231228
const updated = await patchSessionEntry({
232-
agentId: session.agentId,
233-
storePath: resolveStorePath(params.config?.session?.store, { agentId: session.agentId }),
234-
sessionKey: session.sessionKey,
229+
agentId: identity.agentId,
230+
storePath: resolveStorePath(params.config?.session?.store, { agentId: identity.agentId }),
231+
sessionKey: identity.sessionKey,
235232
requireWriteSuccess: true,
236-
// Model override helpers delete stale credentials and model metadata;
237-
// replacing the snapshot is required because partial patches merge fields.
238233
replaceEntry: true,
239234
update: (entry) => {
240-
if (entry.sessionId !== session.sessionId) {
235+
if (entry.sessionId !== identity.sessionId) {
241236
throw new Error("Codex session changed while applying the model selection.");
242237
}
243238
applyModelOverrideWithAuthProfileCompatibility({
244239
cfg: params.config ?? {},
245-
agentDir: params.agentDir ?? resolveAgentDir(params.config ?? {}, session.agentId),
240+
agentDir: params.agentDir ?? resolveAgentDir(params.config ?? {}, identity.agentId),
246241
entry,
247242
currentProvider: binding.modelProvider ?? "openai",
248243
selection: { provider: nextModelProvider ?? "openai", model: nextModel },
@@ -254,28 +249,16 @@ export async function setCodexConversationModel(params: {
254249
if (!updated) {
255250
throw new Error("Codex session changed while applying the model selection.");
256251
}
257-
// SessionEntry owns desired selection; the native binding remains the
258-
// currently loaded model so generation transitions still rotate safely.
259-
if (params.identity.kind === "conversation") {
260-
await patchThreadBinding(params.bindingStore, params.identity, binding.threadId, {
261-
model: nextModel,
262-
modelProvider: nextModelProvider,
263-
...(modelChanged && binding.contextEngine?.projection
264-
? { contextEngine: { ...binding.contextEngine, projection: undefined } }
265-
: {}),
266-
});
267-
} else if (modelChanged && binding.contextEngine?.projection) {
268-
await patchThreadBinding(params.bindingStore, params.identity, binding.threadId, {
269-
contextEngine: { ...binding.contextEngine, projection: undefined },
270-
});
252+
if (modelChanged && binding.contextEngine?.projection) {
253+
await patchThreadBinding(params.bindingStore, identity, binding.threadId, projectionPatch);
271254
}
272255
} else {
256+
// Conversation bindings and ephemeral sessions own native selection;
257+
// ambient outer-session metadata must never redirect their runtime.
273258
await patchThreadBinding(params.bindingStore, params.identity, binding.threadId, {
274259
model: nextModel,
275260
modelProvider: nextModelProvider,
276-
...(modelChanged && binding.contextEngine?.projection
277-
? { contextEngine: { ...binding.contextEngine, projection: undefined } }
278-
: {}),
261+
...projectionPatch,
279262
});
280263
}
281264
return `Codex model set to ${formatCodexDisplayText(nextModel)}.`;

0 commit comments

Comments
 (0)