Skip to content

fix(ui): preserve selected agent after config save on agents page#39301

Closed
MumuTW wants to merge 1 commit intoopenclaw:mainfrom
MumuTW:fix/agents-page-preserve-selection-after-save
Closed

fix(ui): preserve selected agent after config save on agents page#39301
MumuTW wants to merge 1 commit intoopenclaw:mainfrom
MumuTW:fix/agents-page-preserve-selection-after-save

Conversation

@MumuTW
Copy link
Copy Markdown
Contributor

@MumuTW MumuTW commented Mar 8, 2026

Summary

Fixes #39254.

When editing a non-default agent on the /agents page and saving config changes, the UI resets back to the default agent instead of staying on the edited agent.

Root cause: The onConfigSave handler for the agents page called saveConfig(state) without reloading the agents list or preserving the selected agent ID. When the config save triggers a gateway reconnect cycle, the agent selection could be lost.

Fix: The agents page onConfigSave handler now:

  1. Captures the currently selected agent ID before save
  2. Saves config (which internally reloads the config snapshot)
  3. Reloads the agents list to pick up any config-driven changes
  4. Restores the selected agent ID if the agent still exists in the refreshed list

This mirrors the pattern used by handleChannelConfigSave which also reloads related data after config saves.

Test plan

  • Added unit tests for loadAgents verifying:
    • Selected agent is preserved when it still exists in the list
    • Selection resets to default when the selected agent is removed
    • Default agent is selected when no agent was previously selected
  • pnpm test passes
  • pnpm check passes

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 8, 2026

Greptile Summary

This PR fixes a UI regression on the /agents page where editing a non-default agent and saving config would silently reset the selection back to the default agent. The fix expands the onConfigSave handler in app-render.ts to capture the selected agent ID before the save, explicitly refresh the agents list after the save, and restore the selection if the agent still exists in the refreshed list.

Key changes:

  • onConfigSave in app-render.ts is now async, and explicitly calls await loadAgents(state) after await saveConfig(state) — without this, no agents-list refresh was triggered on save, leaving the UI out of sync.
  • A post-loadAgents restoration block re-applies state.agentsSelectedId = selectedBefore when the agent still exists. This acts as a safety net against a gateway reconnect triggered mid-saveConfig that could asynchronously null out agentsSelectedId before loadAgents runs — a window that loadAgents's own preservation logic cannot protect against once the reset has already occurred.
  • Three new unit tests are added for loadAgents documenting the selection-preservation, reset-to-default, and no-prior-selection behaviours that the fix depends on. The tests correctly exercise the underlying controller function in isolation.

Confidence Score: 5/5

  • This PR is safe to merge — the change is narrow, well-motivated, and backed by targeted tests.
  • The fix correctly addresses the root cause (missing loadAgents call after saveConfig on the agents page) and mirrors the same pattern used by handleChannelConfigSave. Both saveConfig and loadAgents handle errors internally, so no try/catch is needed in the handler. The post-loadAgents restoration block is technically redundant in the common case (since loadAgents already preserves the selection), but provides legitimate defence against async reconnect-driven state resets. Tests cover all three meaningful loadAgents selection scenarios. No unintended side effects are introduced.
  • No files require special attention.

Last reviewed commit: cb44463

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cb44463f28

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

selectedBefore &&
state.agentsList?.agents.some((a) => a.id === selectedBefore)
) {
state.agentsSelectedId = selectedBefore;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid restoring a stale agent selection after async save

selectedBefore is captured, then saveConfig and loadAgents are awaited before this value is written back. If state.agentsSelectedId changes during that async window (for example, the user selects a different agent while save is in flight), this assignment forces the old ID back and overrides the newer selection. This creates a reproducible UI jump that looks like user input was ignored; restore should be conditional on the selection still matching the pre-save value.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 079c913cf1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +729 to +733
if (
selectedBefore &&
state.agentsList?.agents.some((a) => a.id === selectedBefore)
) {
state.agentsSelectedId = selectedBefore;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Guard selection restore against in-flight user changes

This restore step can overwrite a newer user selection made while save/reload is in progress: selectedBefore is captured before await saveConfig(state) and await loadAgents(state), then written back unconditionally if still present. Fresh evidence: agent row clicks remain active during saves (ui/src/ui/views/agents.ts:137-141), so a user can switch agents mid-save and then get forced back to the old one here, causing a visible selection jump and stale panel data.

Useful? React with 👍 / 👎.

@MumuTW MumuTW force-pushed the fix/agents-page-preserve-selection-after-save branch from 079c913 to 15b393b Compare March 8, 2026 01:50
@MumuTW MumuTW force-pushed the fix/agents-page-preserve-selection-after-save branch from 15b393b to 0448f39 Compare March 8, 2026 01:52
steipete added a commit that referenced this pull request Mar 8, 2026
Landed from contributor PR #39301 by @MumuTW.

Co-authored-by: MumuTW <[email protected]>
@steipete
Copy link
Copy Markdown
Contributor

steipete commented Mar 8, 2026

Rebased onto the latest main, deep-reviewed, and landed.

What changed on top of the original PR:

  • moved the agents-page save/reload/selection-restore path into the agents controller for tighter test coverage
  • added controller regressions covering both restore-after-save and fallback-to-default when the edited agent disappears
  • updated CHANGELOG.md
  • ran pnpm lint, pnpm build, and pnpm test on the rebased landing commit

Landed commit: c0a7c30
Source commit: 0448f39

Thanks @MumuTW.

@steipete steipete closed this Mar 8, 2026
openperf pushed a commit to openperf/moltbot that referenced this pull request Mar 8, 2026
mcaxtr pushed a commit to mcaxtr/openclaw that referenced this pull request Mar 8, 2026
Saitop pushed a commit to NomiciAI/openclaw that referenced this pull request Mar 8, 2026
GordonSH-oss pushed a commit to GordonSH-oss/openclaw that referenced this pull request Mar 9, 2026
jenawant pushed a commit to jenawant/openclaw that referenced this pull request Mar 10, 2026
dhoman pushed a commit to dhoman/chrono-claw that referenced this pull request Mar 11, 2026
senw-developers pushed a commit to senw-developers/va-openclaw that referenced this pull request Mar 17, 2026
V-Gutierrez pushed a commit to V-Gutierrez/openclaw-vendor that referenced this pull request Mar 17, 2026
alexey-pelykh pushed a commit to remoteclaw/remoteclaw that referenced this pull request Mar 22, 2026
Landed from contributor PR openclaw#39301 by @MumuTW.

Co-authored-by: MumuTW <[email protected]>
(cherry picked from commit c0a7c30)
alexey-pelykh added a commit to remoteclaw/remoteclaw that referenced this pull request Mar 22, 2026
)

* Secrets: refresh baseline for tts line drift

(cherry picked from commit 0018f47)

* fix: auto-create inherited agent override entries

Landed from contributor PR openclaw#39326 by @dunamismax.

Co-authored-by: dunamismax <[email protected]>
(cherry picked from commit 49261b0)

* fix: preserve agents-page selection after config save

Landed from contributor PR openclaw#39301 by @MumuTW.

Co-authored-by: MumuTW <[email protected]>
(cherry picked from commit c0a7c30)

* Agents UI: compose save state from config state

(cherry picked from commit 96f4f50)

* Agents UI: complete config state test fixture

(cherry picked from commit 0125bd9)

* Secrets: refresh baseline for model provider docs

(cherry picked from commit 14916fb)

* docs: dedupe changelog contributor attribution

(cherry picked from commit 75a44de)

* build: reduce build log noise

(cherry picked from commit dd8fd98)

* build: prepare 2026.3.7-beta.1 release

(cherry picked from commit 3596a46)

* fix(ui): align control-ui device auth token signing

(cherry picked from commit e0f80cf)

* build: prepare 2026.3.7 release

(cherry picked from commit 42a1394)

---------

Co-authored-by: Vincent Koc <[email protected]>
Co-authored-by: Peter Steinberger <[email protected]>
Co-authored-by: dunamismax <[email protected]>
Co-authored-by: MumuTW <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(ui): agent config page resets to default agent after saving changes

2 participants