Skip to content

[AI-assisted] fix(models-config): preserve existing models.json baseUrls on regen - #67893

Merged
obviyus merged 3 commits into
openclaw:mainfrom
lawrence3699:fix/models-json-baseurl-merge
Apr 17, 2026
Merged

obviyus merged 3 commits into
openclaw:mainfrom
lawrence3699:fix/models-json-baseurl-merge

Conversation

@lawrence3699

Copy link
Copy Markdown
Contributor

Summary

  • Problem: restarting the gateway rewrites a non-empty agent models.json baseUrl back to the config/provider value, so custom endpoints like https://api.kilo.ai/api/gateway get forced back to /v1.
  • Why it matters: merge mode is documented to preserve an existing agent models.json baseUrl, and users editing custom provider endpoints lose those changes on restart.
  • What changed: removed the merge special-case that let explicit config baseUrl values override non-empty agent models.json baseUrl values; added regression tests at the merge, plan, and runtime-snapshot layers.
  • What did NOT change (scope boundary): models.mode: "replace" behavior is unchanged, and stale baseUrl values still refresh when the provider API surface changes.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor required for the fix
  • 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

Root Cause (if applicable)

  • Root cause: the recent models.json merge refactor added an explicitBaseUrlProviders override in src/agents/models-config.merge.ts, which forced config models.providers.*.baseUrl to win even in merge mode.
  • Missing detection / guardrail: merge tests encoded the wrong precedence, and there was no plan-level regression test covering a restart rewrite from an existing models.json file.
  • Contributing context (if known): the documented merge semantics in docs/concepts/models.md and docs/gateway/configuration-reference.md already say that a non-empty agent models.json baseUrl should win.

Regression Test Plan (if applicable)

  • Coverage level that should have caught this:
    • Unit test
    • Seam / integration test
    • End-to-end test
    • Existing coverage already sufficient
  • Target test or file:
    • src/agents/models-config.uses-first-github-copilot-profile-env-tokens.test.ts
    • src/agents/models-config.merge.test.ts
    • src/agents/models-config.runtime-source-snapshot.test.ts
  • Scenario the test should lock in: when merge mode regenerates models.json, an existing non-empty agent baseUrl survives restart/regeneration unless the provider API surface changed.
  • Why this is the smallest reliable guardrail: the bug is in the merge/planning path, so these tests exercise the exact persistence behavior without requiring a full gateway boot.
  • Existing test that already covers this (if any): none.
  • If no new test is added, why not: N/A.

User-visible / Behavior Changes

  • In merge mode, custom provider baseUrl values already present in ~/.openclaw/agents/<agentId>/agent/models.json now survive gateway regeneration/restart again.

Diagram (if applicable)

Before:
[user edits models.json baseUrl] -> [gateway restart] -> [config baseUrl overwrites existing value]

After:
[user edits models.json baseUrl] -> [gateway restart] -> [existing models.json baseUrl preserved] -> [custom endpoint remains usable]

Security Impact (required)

  • New permissions/capabilities? (Yes/No) No
  • Secrets/tokens handling changed? (Yes/No) No
  • New/changed network calls? (Yes/No) No
  • Command/tool execution surface changed? (Yes/No) No
  • Data access scope changed? (Yes/No) No
  • If any Yes, explain risk + mitigation:

Repro + Verification

Environment

  • OS: macOS 15.x
  • Runtime/container: Node 24.14.0 via nvm
  • Model/provider: custom OpenAI-compatible provider config (kilocode repro)
  • Integration/channel (if any): none
  • Relevant config (redacted): models.mode: "merge", models.providers.kilocode.baseUrl: "https://api.kilo.ai/api/gateway/v1"

Steps

  1. Start with merge mode and a configured custom provider whose config baseUrl ends in /v1.
  2. Write an existing agent models.json entry for the same provider with a non-empty baseUrl that omits /v1.
  3. Regenerate models.json via the normal planning path (or restart the gateway).

Expected

  • The existing agent models.json baseUrl stays unchanged in merge mode.

Actual

  • Before this fix, regeneration rewrote the agent baseUrl back to the config value with /v1.

Evidence

Attach at least one:

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

Human Verification (required)

What you personally verified (not just CI), and how:

  • Verified scenarios: reproduced the rewrite locally with mergeWithExistingProviderSecrets and planOpenClawModelsJsonWithDeps, then verified the updated tests and commit hook preserve the existing baseUrl while still refreshing other fields.
  • Edge cases checked: stale baseUrl values still refresh when the provider API surface changes; runtime-source-snapshot regeneration still updates refreshable headers under the same snapshot.
  • What you did not verify: I did not run an actual Android/Termux gateway restart or Kilo Code end-to-end integration.

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

If a bot review conversation is addressed by this PR, resolve that conversation yourself. Do not leave bot review conversation cleanup for maintainers.

Compatibility / Migration

  • Backward compatible? (Yes/No) Yes
  • Config/env changes? (Yes/No) No
  • Migration needed? (Yes/No) No
  • If yes, exact upgrade steps:

Risks and Mitigations

  • Risk: users who relied on merge mode to force config baseUrl updates into an existing agent models.json will now keep the existing value instead.
    • Mitigation: this restores the documented merge precedence; models.mode: "replace" and API-surface mismatch handling still provide the explicit overwrite paths.

AI Assistance

  • AI-assisted: Codex
  • Testing note: fully tested for the touched surface (build, targeted test, full pre-commit check)
  • Prompt/session summary: fix the regression where merge mode rewrites a non-empty agent models.json baseUrl back to the config value on restart, with concrete repro, minimal scope, and regression coverage.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Apr 17, 2026
@obviyus
obviyus force-pushed the fix/models-json-baseurl-merge branch from 0d6f0fb to b66a5ea Compare April 17, 2026 02:47
@obviyus obviyus self-assigned this Apr 17, 2026
@obviyus
obviyus marked this pull request as ready for review April 17, 2026 02:47
@greptile-apps

greptile-apps Bot commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Removes the explicitBaseUrlProviders override introduced in the recent models.json merge refactor. That override caused config-level baseUrl values to win over existing agent models.json values in merge mode, silently overwriting user-edited custom endpoints on every gateway restart. The fix restores the documented merge precedence — existing non-empty baseUrl survives regeneration unless the provider API surface changes — and adds regression coverage at the unit, plan, and runtime-snapshot layers.

Confidence Score: 5/5

Safe to merge; the fix is targeted, the logic is correct, and the regression tests cover the exact failure path.

All changed files are well-reasoned: the removal of explicitBaseUrlProviders is complete and consistent across merge.ts and plan.ts, test expectations are updated correctly, and a new end-to-end regression test locks in the preservation semantics. The only open item is a P2 style suggestion to add a comment in the runtime-snapshot test. No blocking issues exist.

No files require special attention.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/agents/models-config.runtime-source-snapshot.test.ts
Line: 317-326

Comment:
**Consider a clarifying comment on the preserved-baseUrl assertion**

After the fix, `ensureOpenClawModelsJson(secondCandidate)` rewrites `models.json` (triggered by the `headers` change) but keeps the `baseUrl` from the first write rather than adopting `secondCandidate`'s `"https://mirror.example/v1"`. That's correct, but the test was originally the primary place where "config baseUrl change propagates" was verified, so a future reader may be surprised to see line 324 asserting the old value. A one-line comment would make the intent clear:

```suggestion
          // baseUrl is preserved from the first write; only headers change causes the rewrite.
          await ensureOpenClawModelsJson(secondCandidate);
          parsed = await readGeneratedModelsJson<{
            providers: Record<
              string,
              { baseUrl?: string; apiKey?: string; headers?: Record<string, string> }
            >;
          }>();
          expect(parsed.providers.openai?.baseUrl).toBe("https://api.openai.com/v1");
          expect(parsed.providers.openai?.apiKey).toBe("OPENAI_API_KEY"); // pragma: allowlist secret
          expect(parsed.providers.openai?.headers?.["X-OpenClaw-Test"]).toBe("two");
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "test: distill models-config baseUrl regr..." | Re-trigger Greptile

Copilot AI review requested due to automatic review settings April 17, 2026 03:01

@obviyus obviyus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verified the merge-mode regression where regenerating models.json rewrote an existing provider baseUrl back to the config value, and confirmed the fix removes the explicit-baseUrl override so existing non-empty baseUrls persist unless the provider API surface changes.

Maintainer follow-up: distilled the new regression test setup, added one clarifying test comment for the preserved-baseUrl assertion, added the unreleased changelog entry, and resolved the current-head review thread.

Local gate: pnpm test src/agents/models-config.merge.test.ts src/agents/models-config.runtime-source-snapshot.test.ts src/agents/models-config.uses-first-github-copilot-profile-env-tokens.test.ts.

@obviyus
obviyus merged commit 35fb3f7 into openclaw:main Apr 17, 2026
44 checks passed
@obviyus

obviyus commented Apr 17, 2026

Copy link
Copy Markdown
Collaborator

Landed on main.

Thanks @lawrence3699.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a merge-mode regression where regenerating an agent’s models.json would overwrite an existing non-empty provider baseUrl with the configured provider baseUrl, causing custom endpoints (e.g. OpenAI-compatible gateways without /v1) to be reset on restart.

Changes:

  • Removed the “explicit config baseUrl wins” override so merge mode preserves an existing non-empty models.json baseUrl.
  • Updated merge logic/tests to preserve baseUrl from models.json unless the provider API surface changes.
  • Added/updated regression tests across merge, plan, and runtime snapshot layers; documented the fix in the changelog.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/agents/models-config.plan.ts Removes explicit-baseUrl override plumbing so merge planning no longer forces config baseUrl over existing agent models.json.
src/agents/models-config.merge.ts Updates baseUrl preservation logic to ignore “explicit config baseUrl” and preserve existing non-empty baseUrl when API surface matches.
src/agents/models-config.merge.test.ts Adjusts merge precedence expectations and adds coverage for the restored semantics.
src/agents/models-config.uses-first-github-copilot-profile-env-tokens.test.ts Adds a plan-level regression test ensuring merge regeneration yields noop when only config baseUrl differs from existing non-empty models.json baseUrl.
src/agents/models-config.runtime-source-snapshot.test.ts Ensures runtime snapshot regeneration can still rewrite other fields (e.g. headers) without rewriting an existing baseUrl in merge mode.
CHANGELOG.md Notes the user-visible behavior fix for merge-mode baseUrl preservation.

xudaiyanzi pushed a commit to xudaiyanzi/openclaw that referenced this pull request Apr 17, 2026
…lawrence3699)

* models-config: preserve existing models.json baseUrls

* test: distill models-config baseUrl regression test

* fix: preserve models.json baseUrls on regen (openclaw#67893) (thanks @lawrence3699)

---------

Co-authored-by: lawrence3699 <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
kvnkho pushed a commit to kvnkho/openclaw that referenced this pull request Apr 17, 2026
…lawrence3699)

* models-config: preserve existing models.json baseUrls

* test: distill models-config baseUrl regression test

* fix: preserve models.json baseUrls on regen (openclaw#67893) (thanks @lawrence3699)

---------

Co-authored-by: lawrence3699 <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
kaijiabo added a commit to blink-new/openclaw that referenced this pull request Apr 19, 2026
…boot

Root cause: upstream v2026.4.15 openai-completions provider normalization
writes an extra trailing "/v1" to the blink provider baseUrl on first
persistence of /data/agents/main/agent/models.json:

  "blink": { "baseUrl": "https://core.blink.new/api/v1/ai/v1", ... }

The openai-completions adapter then appends "/chat/completions", producing
  https://core.blink.new/api/v1/ai/v1/chat/completions  ← HTTP 404

Every LLM call fails with failoverReason=model_not_found. Agents appear
non-responsive even though the machine is healthy and the gateway /healthz
returns ok — the gateway just can't reach the upstream model.

Upstream PR openclaw#67893 "preserve models.json baseUrls on regen" (pulled in via
our v2026.4.15 merge) then locks the corrupted baseUrl forever across
restarts — it's preserved from disk even when the code wants to regenerate
it from buildBlinkProvider(). So the bug is fleet-wide and sticky.

Fix: the entrypoint now rewrites providers.blink.baseUrl to the canonical
${BLINK_APIS_URL}/api/v1/ai on every container start, unconditionally.
Breaks the preserve-lock specifically for blink without affecting any
other provider. Runs before the gateway starts, so the correct URL is in
place before any LLM call.

Verified end-to-end on clw_ji90uejs + 3 canary agents: LLM calls return
normally via direct WS and via the claw-manager SSE pipeline.

Pairs with scripts/claw/fix-blink-baseurl-all.sh in the parent repo for
bulk recovery of the currently-running fleet without waiting for the new
image to roll out.
kaijiabo added a commit to blink-new/openclaw that referenced this pull request Apr 19, 2026
…watcher

The initial fix (6eb7983) only sanitized /data/agents/main/agent/models.json
if the file already existed at entrypoint run time. On a fresh volume that is
not true — OpenClaw gateway lazy-writes models.json during its own startup
(after the entrypoint has already exec'd gosu node), and the upstream
normalization bug writes a corrupted blink.baseUrl ".../api/v1/ai/v1" at that
moment. Combined with openclaw#67893 preserve-baseUrl-on-regen, the corrupted value
then persists forever — so every FIRST LLM call from a brand-new agent 404s,
even on an image with the "fixed" entrypoint.

Reproduction verified on clw_ji90uejs: rm /data/agents/main/agent/models.json
+ restart → gateway boots → models.json appears with
"https://core.blink.new/api/v1/ai/v1" → first chat returns HTTP 404.

Fix: the entrypoint now runs a background watcher (disowned, adopted by init)
that polls for ~60s, invokes the sanitize helper as soon as models.json
appears, and SIGHUPs the running gateway so it reloads the corrected config
immediately. The synchronous steady-state sanitize from the previous commit
is preserved for already-provisioned agents. Both paths share a single
sanitize_models_json() helper so the rewrite logic lives in one place.

Also: the helper now uses `ps -e -o pid=,comm=` to find the gateway process
instead of `pgrep -x openclaw-gateway`. pgrep caps program-name match at 15
chars and openclaw-gateway is 16 chars, so pgrep was silently finding zero
matches and never delivering SIGHUP. ps+awk has no such limit.

End-to-end test plan after this ships:
  1. Wait for :latest to rebuild and redeploy across the fleet.
  2. Provision a new test agent (or `rm models.json && restart` an existing
     one to simulate a fresh volume).
  3. Send a chat within the first 2 minutes of boot. Should respond normally,
     with models.json on disk holding the canonical baseUrl.
Mquarmoc pushed a commit to Mquarmoc/openclaw that referenced this pull request Apr 20, 2026
…lawrence3699)

* models-config: preserve existing models.json baseUrls

* test: distill models-config baseUrl regression test

* fix: preserve models.json baseUrls on regen (openclaw#67893) (thanks @lawrence3699)

---------

Co-authored-by: lawrence3699 <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
…lawrence3699)

* models-config: preserve existing models.json baseUrls

* test: distill models-config baseUrl regression test

* fix: preserve models.json baseUrls on regen (openclaw#67893) (thanks @lawrence3699)

---------

Co-authored-by: lawrence3699 <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
…lawrence3699)

* models-config: preserve existing models.json baseUrls

* test: distill models-config baseUrl regression test

* fix: preserve models.json baseUrls on regen (openclaw#67893) (thanks @lawrence3699)

---------

Co-authored-by: lawrence3699 <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request May 13, 2026
…lawrence3699)

* models-config: preserve existing models.json baseUrls

* test: distill models-config baseUrl regression test

* fix: preserve models.json baseUrls on regen (openclaw#67893) (thanks @lawrence3699)

---------

Co-authored-by: lawrence3699 <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
…lawrence3699)

* models-config: preserve existing models.json baseUrls

* test: distill models-config baseUrl regression test

* fix: preserve models.json baseUrls on regen (openclaw#67893) (thanks @lawrence3699)

---------

Co-authored-by: lawrence3699 <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
…lawrence3699)

* models-config: preserve existing models.json baseUrls

* test: distill models-config baseUrl regression test

* fix: preserve models.json baseUrls on regen (openclaw#67893) (thanks @lawrence3699)

---------

Co-authored-by: lawrence3699 <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
…lawrence3699)

* models-config: preserve existing models.json baseUrls

* test: distill models-config baseUrl regression test

* fix: preserve models.json baseUrls on regen (openclaw#67893) (thanks @lawrence3699)

---------

Co-authored-by: lawrence3699 <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
Heetisk pushed a commit to Heetisk/openclaw that referenced this pull request Sep 28, 2026
…lawrence3699)

* models-config: preserve existing models.json baseUrls

* test: distill models-config baseUrl regression test

* fix: preserve models.json baseUrls on regen (openclaw#67893) (thanks @lawrence3699)

---------

Co-authored-by: lawrence3699 <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: models.json baseUrl automatically appends "/v1" on restart

3 participants