Skip to content

fix(config): preserve agent-level apiKey/baseUrl during models.json merge#27293

Merged
gumadeiras merged 5 commits intoopenclaw:mainfrom
Sid-Qin:fix/27243-agent-models-json-override
Feb 26, 2026
Merged

fix(config): preserve agent-level apiKey/baseUrl during models.json merge#27293
gumadeiras merged 5 commits intoopenclaw:mainfrom
Sid-Qin:fix/27243-agent-models-json-override

Conversation

@Sid-Qin
Copy link
Contributor

@Sid-Qin Sid-Qin commented Feb 26, 2026

Summary

  • Problem: Agent-level models.json entries (with custom apiKey/baseUrl) are silently overwritten by the main openclaw.json config during ensureOpenClawModelsJson() merge, because the shallow merge { ...existingProviders, ...providers } replaces entire provider objects.
  • Why it matters: Users who configure per-agent API keys or custom base URLs lose those settings every time the models config is refreshed.
  • What changed: The merge loop now preserves existing agent-level apiKey and baseUrl when merging new provider data, while still accepting model catalog updates (models, api type, etc.) from the main config.
  • What did NOT change (scope boundary): Replace mode (non-merge), model catalog refresh, and providers not already in the agent's models.json are unaffected.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

User-visible / Behavior Changes

Agent-level apiKey and baseUrl in ~/.openclaw/agents/{agentId}/agent/models.json are now preserved across config refreshes. Previously they were silently replaced by the main config values.

Security Impact (required)

  • New permissions/capabilities: None
  • Auth/token changes: Agent-level API keys are no longer silently dropped — this is a security improvement (prevents fallback to unintended keys)
  • Data exposure risk: None — agent-level keys are already stored in mode 0o600 files

Testing

  • npx vitest run src/agents/models-config — 43 tests ✓

Rollback Plan

Revert the single commit. No migration or data changes involved.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 26, 2026

Greptile Summary

Correctly preserves agent-level apiKey and baseUrl during merge-mode config refresh. The fix replaces a shallow spread merge with explicit preservation logic that:

  • Maintains all existing providers from the agent's models.json
  • For providers also in main config: updates model catalog and other fields while preserving custom apiKey and baseUrl (if non-empty strings)
  • For new providers: adds them without modification
  • Only affects merge mode; replace mode is unchanged

The implementation properly guards against edge cases (empty strings, non-string values, undefined) and maintains backward compatibility.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The fix is well-scoped, only affects merge mode behavior, includes proper type guards and runtime checks, preserves backward compatibility, and all 43 existing tests pass. The logic correctly implements the stated goal of preserving agent-level apiKey/baseUrl while updating other provider fields from main config.
  • No files require special attention

Last reviewed commit: c61a624

@gumadeiras gumadeiras self-assigned this Feb 26, 2026
@gumadeiras gumadeiras force-pushed the fix/27243-agent-models-json-override branch from c61a624 to 130b90f Compare February 26, 2026 08:39
@openclaw-barnacle openclaw-barnacle bot added size: S docs Improvements or additions to documentation gateway Gateway runtime and removed size: XS labels Feb 26, 2026
SidQin-cyber and others added 5 commits February 26, 2026 03:44
…erge

When ensureOpenClawModelsJson runs in merge mode, the shallow merge
{ ...existingProviders, ...providers } causes the main config to
overwrite agent-level models.json entries entirely.  This drops any
apiKey or baseUrl that the user set specifically for an agent.

Preserve existing agent-level apiKey and baseUrl when merging, while
still accepting all other updates (model catalog, api type, etc.)
from the newly computed provider config.

Closes openclaw#27243
@gumadeiras gumadeiras force-pushed the fix/27243-agent-models-json-override branch from 448e48e to 6b4b37b Compare February 26, 2026 08:45
@gumadeiras gumadeiras merged commit c289b5f into openclaw:main Feb 26, 2026
21 checks passed
@gumadeiras
Copy link
Member

Merged via squash.

Thanks @Sid-Qin!

NOVA-Openclaw added a commit to NOVA-Openclaw/nova-openclaw that referenced this pull request Feb 26, 2026
Syncs 29 upstream commits including:
- Account-scoped bind and routing commands (openclaw#27195)
- Single-account config migration to accounts.default (openclaw#27334)
- Preserve agent-level apiKey/baseUrl during models.json merge (openclaw#27293)
- Plugin-owned interactive channel onboarding flows (openclaw#27191)
- Android notifications.list, invoke refactoring
- Daemon launchd restart hardening
- SSRF dispatcher fix, doctor improvements

Conflict resolution: 4 import-order conflicts in onboarding/channel
commands resolved by taking upstream (no custom code in those files).
steipete pushed a commit that referenced this pull request Feb 26, 2026
…erge (#27293)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 6b4b37b
Co-authored-by: Sid-Qin <201593046+Sid-Qin@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
execute008 pushed a commit to execute008/openclaw that referenced this pull request Feb 27, 2026
…erge (openclaw#27293)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 6b4b37b
Co-authored-by: Sid-Qin <201593046+Sid-Qin@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
r4jiv007 pushed a commit to r4jiv007/openclaw that referenced this pull request Feb 28, 2026
…erge (openclaw#27293)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 6b4b37b
Co-authored-by: Sid-Qin <201593046+Sid-Qin@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
vincentkoc pushed a commit to Sid-Qin/openclaw that referenced this pull request Feb 28, 2026
…erge (openclaw#27293)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 6b4b37b
Co-authored-by: Sid-Qin <201593046+Sid-Qin@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
vincentkoc pushed a commit to rylena/rylen-openclaw that referenced this pull request Feb 28, 2026
…erge (openclaw#27293)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 6b4b37b
Co-authored-by: Sid-Qin <201593046+Sid-Qin@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
steipete pushed a commit to Sid-Qin/openclaw that referenced this pull request Mar 2, 2026
…erge (openclaw#27293)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 6b4b37b
Co-authored-by: Sid-Qin <201593046+Sid-Qin@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
steipete pushed a commit to Sid-Qin/openclaw that referenced this pull request Mar 2, 2026
…erge (openclaw#27293)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 6b4b37b
Co-authored-by: Sid-Qin <201593046+Sid-Qin@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
dorgonman pushed a commit to kanohorizonia/openclaw that referenced this pull request Mar 3, 2026
…erge (openclaw#27293)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 6b4b37b
Co-authored-by: Sid-Qin <201593046+Sid-Qin@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
zooqueen pushed a commit to hanzoai/bot that referenced this pull request Mar 6, 2026
…erge (openclaw#27293)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 6b4b37b
Co-authored-by: Sid-Qin <201593046+Sid-Qin@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
thebenjaminlee pushed a commit to escape-velocity-ventures/openclaw that referenced this pull request Mar 7, 2026
…erge (openclaw#27293)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 6b4b37b
Co-authored-by: Sid-Qin <201593046+Sid-Qin@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation gateway Gateway runtime size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Agent-level models.json apiKey not overriding main config

2 participants