Skip to content

Commit 12cd4b2

Browse files
philazarclaude
andcommitted
Fix Codex Advanced discovery on saved agents and Install focus
Saved-agent Codex model discovery applied the edit through Save validation, so switching a saved Default agent to Advanced failed before the catalog loaded. Discovery now resolves without the incomplete model/effort intent; Save still requires it. Hand Codex Install focus to its status when a successful install removes the focused button, matching Claude Code setup. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Philip Azar <[email protected]>
1 parent d8bb3a0 commit 12cd4b2

4 files changed

Lines changed: 166 additions & 3 deletions

File tree

‎crates/agent-controller/src/runtime.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,12 +684,21 @@ impl Controller {
684684
crate::pi::PiContext::new(&agent.harness, &agent.workspace, &agent.environment)
685685
}
686686
/// Resolve a saved Codex draft only after its revision has been fenced.
687+
/// Discovery precedes the Advanced model/effort choice and does not depend
688+
/// on it, so validate that intent at Save, as Create does.
687689
pub fn codex_model_context(
688690
&self,
689691
id: &str,
690692
revision: u64,
691-
edit: AgentEdit,
693+
mut edit: AgentEdit,
692694
) -> Result<crate::codex::CodexContext> {
695+
if matches!(
696+
edit.harness.configuration,
697+
Some(crate::AiConfiguration::Advanced { .. })
698+
) {
699+
edit.harness.configuration = Some(crate::AiConfiguration::Default);
700+
edit.harness.model.clear();
701+
}
693702
let agent = self.edited_agent(id, revision, edit)?;
694703
crate::codex::CodexContext::for_agent(
695704
&agent.harness.command,

‎crates/agent-controller/src/runtime/tests.rs‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,55 @@ fn codex_command_binds_exact_interpreters_and_default_selection() {
946946
));
947947
}
948948

949+
#[test]
950+
#[cfg(unix)]
951+
fn codex_saved_default_agent_discovers_models_before_advanced_choice() {
952+
if !codex_launch_child(
953+
"runtime::tests::codex_saved_default_agent_discovers_models_before_advanced_choice",
954+
) {
955+
return;
956+
}
957+
let dir = tempfile::tempdir().unwrap();
958+
let (saved, context) = codex_fixture(dir.path());
959+
select_codex_cli(&context);
960+
let mut store = Store::open(dir.path().join("config")).unwrap();
961+
store.insert(vec![saved.clone()]).unwrap();
962+
let mut controller = Controller::new(
963+
store,
964+
Arc::new(Memory),
965+
Err("No runtime".into()),
966+
dir.path().join("ownership"),
967+
);
968+
// Default -> Advanced before a model or effort has been chosen.
969+
let mut harness = saved.harness.clone();
970+
harness.configuration = Some(crate::AiConfiguration::Advanced {
971+
effort: crate::EffortSelection::Value {
972+
value: String::new(),
973+
},
974+
});
975+
let edit: AgentEdit = serde_json::from_value(json!({
976+
"name": saved.name, "systemPrompt": saved.system_prompt, "workspace": saved.workspace,
977+
"harness": harness, "environment": {}
978+
}))
979+
.unwrap();
980+
assert!(
981+
controller
982+
.codex_model_context(&saved.id, saved.revision, edit.clone())
983+
.unwrap()
984+
== context
985+
);
986+
assert!(controller
987+
.codex_model_context(&saved.id, saved.revision + 1, edit.clone())
988+
.is_err());
989+
assert_eq!(
990+
controller
991+
.save(&saved.id, saved.revision, edit)
992+
.err()
993+
.as_deref(),
994+
Some("Choose a model for Advanced Codex configuration")
995+
);
996+
}
997+
949998
#[test]
950999
#[cfg(unix)]
9511000
fn codex_command_applies_advanced_selection_and_never_falls_back() {

‎src/app/AgentSettings.test.tsx‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,88 @@ it("offers the Codex ACP adapter Install only when the adapter is missing, then
472472
expect(install).toHaveBeenCalledTimes(2);
473473
});
474474

475+
it.each([false, true] as const)(
476+
"keeps Codex keyboard retry usable and hands off success only while Install owns focus (moved: %s)",
477+
async (moveFocus) => {
478+
const user = userEvent.setup();
479+
const report: HarnessInstallReport = {
480+
ready: false,
481+
restarted: 0,
482+
restartFailures: 0,
483+
logPath: "/fixture/codex-install.log",
484+
output: "npm ERR! network",
485+
error: "Installation failed",
486+
};
487+
let finish!: (report: HarnessInstallReport) => void;
488+
const pending = new Promise<HarnessInstallReport>((resolve) => {
489+
finish = resolve;
490+
});
491+
const install = vi
492+
.fn<NonNullable<AgentControlHost["installCodex"]>>()
493+
.mockReturnValue(pending);
494+
const fixture = codexHarnesses("adapter-needed");
495+
fixture.host.installCodex = install;
496+
fixture.host.checkCodexAuth = async () => !moveFocus;
497+
const control = createAgentControl(fixture.host);
498+
disposals.push(() => control.dispose());
499+
render(<AgentSettings control={control} />, { wrapper: ToastProvider });
500+
501+
await screen.findByText("Codex");
502+
const button = await codexRow().findByRole("button", { name: "Install" });
503+
for (let i = 0; i < 20 && document.activeElement !== button; i++) {
504+
await user.tab();
505+
}
506+
expect(button).toHaveFocus();
507+
await user.keyboard("{Enter}");
508+
try {
509+
expect(
510+
await screen.findByText(
511+
"Installing Node.js and the Codex ACP adapter…",
512+
),
513+
).toBeVisible();
514+
expect(button).toHaveFocus();
515+
} finally {
516+
await act(async () => {
517+
finish(report);
518+
await pending;
519+
});
520+
}
521+
expect(await codexRow().findByRole("alert")).toHaveTextContent(
522+
"Installation failed",
523+
);
524+
expect(button).toHaveFocus();
525+
const retry = new Promise<HarnessInstallReport>((resolve) => {
526+
finish = resolve;
527+
});
528+
install.mockReturnValue(retry);
529+
await user.keyboard("{Enter}");
530+
await waitFor(() => expect(install).toHaveBeenCalledTimes(2));
531+
let next: Element | null = null;
532+
try {
533+
expect(button).toHaveFocus();
534+
if (moveFocus) {
535+
next = screen.getByRole("button", { name: "Add harness" });
536+
for (let i = 0; i < 20 && document.activeElement !== next; i++) {
537+
await user.tab();
538+
}
539+
expect(next).toHaveFocus();
540+
}
541+
fixture.data.harnessOptions = codexHarnessOptions("ready");
542+
} finally {
543+
await act(async () => {
544+
finish({ ...report, ready: true, error: null });
545+
await retry;
546+
});
547+
}
548+
const status = await codexRow().findByText(
549+
moveFocus ? "Sign-in needed" : "Ready",
550+
);
551+
expect(codexRow().queryByRole("button", { name: "Install" })).toBeNull();
552+
if (moveFocus) expect(next).toHaveFocus();
553+
else expect(status).toHaveFocus();
554+
},
555+
);
556+
475557
it("offers manual copying when clipboard access fails", async () => {
476558
const user = userEvent.setup();
477559
setupHarnesses("cli-needed");

‎src/app/AgentSettings.tsx‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { SettingsGroup } from "../shared/design-system/ui/SettingsGroup";
2-
import { useEffect, useRef, useState, useSyncExternalStore } from "react";
2+
import {
3+
useCallback,
4+
useEffect,
5+
useRef,
6+
useState,
7+
useSyncExternalStore,
8+
} from "react";
39
import {
410
harnessPresets,
511
harnessKind,
@@ -146,6 +152,16 @@ export function AgentSettings({
146152
authCheck,
147153
control,
148154
]);
155+
// Like Claude Code: keep keyboard focus when a successful install removes Install.
156+
const codexStatusRef = useRef<HTMLSpanElement>(null);
157+
const codexInstallRef = useCallback((button: HTMLButtonElement | null) => {
158+
if (!button) return;
159+
return () => {
160+
if (button.ownerDocument.activeElement === button) {
161+
codexStatusRef.current?.focus();
162+
}
163+
};
164+
}, []);
149165
const offerCodexInstall =
150166
!!control.installCodex &&
151167
!!codexOption?.installSupported &&
@@ -265,7 +281,13 @@ export function AgentSettings({
265281
<span>{option?.label}</span>
266282
</span>
267283
<span className="flex items-center gap-2">
268-
<span className="text-secondary">
284+
<span
285+
ref={
286+
option?.id === "codex" ? codexStatusRef : undefined
287+
}
288+
tabIndex={option?.id === "codex" ? -1 : undefined}
289+
className="text-secondary"
290+
>
269291
{option?.id === "codex"
270292
? codexStatus
271293
: option?.status
@@ -301,6 +323,7 @@ export function AgentSettings({
301323
)}
302324
{option?.id === "codex" && offerCodexInstall && (
303325
<Button
326+
ref={codexInstallRef}
304327
size="sm"
305328
type="button"
306329
loading={!!codexInstall?.installing}

0 commit comments

Comments
 (0)