Skip to content

fix(runtime): unify background model_invoke model selection and loading - #644

Merged
LeoXing1996 merged 1 commit into
mainfrom
fix/background-task-model-selection
Nov 24, 2026
Merged

LeoXing1996 merged 1 commit into
mainfrom
fix/background-task-model-selection

Conversation

@DolphinMiner

Copy link
Copy Markdown
Contributor

Summary

Unifies background runner.model.invoke execution (Wiki, Memory, Agent Memory, Daily Brief, Approval, Profile, Insight, and Voice Ask) with the interactive/Routine (run.start) model selection and local-model loading path across Codex, Claude, and Pi.

Refs WHI-985

Problem

  • Foreground run.start explicitly passes the Agent's configured (runtime_type, runtime_model) and runs ensureLocalModel for Pi.
  • Background model_invoke omitted model on the Runner Gateway wire and deferred to each Runtime's local CLI default:
    • Codex fell back to ~/.codex/config.toml, repeatedly failing with runtime_incompatible when the user's global config.toml named a model unsupported by the signed-in account even though interactive chat worked.
    • Claude omitted --model and ignored the Agent's configured model.
    • Pi could not call ensureLocalModel and failed when a downloaded local model had not yet been loaded.

Changes

  • Contracts & TeamHub
    • Added optional runtime_model to AnotherMePlacement (contracts/openapi/components/agent-runtime-state.yaml, ListAnotherMePlacements SQL query, agentregistry/runtime_config.go).
    • Added optional model to Runner Gateway ModelInvoke (contracts/runner-gateway/envelope.schema.json, asyncapi.yaml) and regenerated Go/TypeScript contracts.
  • Node Services
    • Passed ghost.runtime.model in agent-memory/coordinator.ts and mapped runtime_model from TeamHub placements in runtime-model-call.ts.
    • Resolved the wire model in runtime-model-call.ts: use the Agent's configured model on the same Runtime, and fall back to the target Runtime catalog's default on cross-Runtime fallback or legacy/unconfigured placements (never leaking a model ID across Runtimes).
    • Included the resolved wire model in background circuit-breaker fingerprints.
    • Gated wire model on @linger/computer >= 0.62.0 (FIRST_MODEL_INVOKE_MODEL_COMPUTER_VERSION) because older Daemons decode envelopes with DisallowUnknownFields().
  • Local Computer Daemon (@linger/computer 0.62.0)
    • Forwarded explicit model to Codex (-m for codex exec and --config model= for CodexSessions), Claude (--model), and Pi (--model).
    • Added ensureLocalModel(ctx, runtimeType, request.Model) on Pi model_invoke calls (matching run.start), restricting modelInvokeUnready refusal to calls that omit model.
    • Threaded request.Model into reasoning-effort normalization and endpoint reachability checks (ModelEndpoints).
  • Docs & Tests
    • Updated apps/computer/AGENTS.md, apps/computer/local-daemon/README.md, and apps/node-services/src/agent/AGENTS.md.
    • Added unit/contract tests across TeamHub, Node Services, Computer Daemon, and scripts/runner-gateway-contract.test.ts.

Compatibility & Rollout

  • Backward/forward compatible:
    • Older Daemons (< 0.62.0) never receive model on ModelInvoke and keep legacy behavior.
    • Newer Daemons (>= 0.62.0) talking to older Agent Services accept omitted model and fall back to local CLI defaults.
    • AnotherMePlacement.runtime_model is optional so mixed TeamHub / Node Services versions interoperate cleanly.
  • No changes to the interactive user message -> provider_ready critical path.

Verification

  • pnpm contracts:verify (pass)
  • make check-repository & commitlint & check-docs & check-contracts (pass)
  • make -C apps/teamhub fmt-check && make -C apps/teamhub lint-go BASE=origin/main && go test ./internal/agentregistry ./internal/app ./tests/integration/layout -count=1 (pass)
  • make check-node-services (272 test files / 2,833 tests pass, typecheck + lint clean)
  • pnpm --filter @linger/computer typecheck && lint && test, make -C apps/teamhub lint-go-daemon, make check-computer-go (pass)
  • Live local Desktop verification with ~/.codex/config.toml set to an unsupported model: runner.model.invoke logged explicit model=gpt-5.6-sol, invoked codex exec ... -m gpt-5.6-sol, and completed (phase=completed).

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approve with nits (1 P2)

Scope: base main, merge base a3b6f44b, 1 commit, 37 files reviewed at f1786d41. Helper skills loaded: coderbook (sections 2, 3, 5). ponytail-review and karpathy-guidelines were not loaded; I applied their checklists from the review instructions. Ran in a separate session.

Findings

P2

  • apps/node-services/src/runtime-model-call.ts:434-445: a second hard-coded list of legacy default model IDs, and a test double that hides it.
    The real catalog already handles legacy defaults. runtimeModelCatalogForComputer (apps/node-services/src/agent/runner-gateway/local-computer-management.ts:590-602) sets runtimeModel: model.id on every discovered model except the legacy defaults it reads from DEFAULT_LOCAL_RUNTIME_MODELS. run.start relies on only that field (agent-runner-lifecycle.ts:465). Through the real gateway, wireModelFor therefore always takes the model.runtimeModel branch or returns undefined. The LEGACY_DEFAULT_MODEL_IDS set and the return model.id fallback only run for test catalogs that leave out runtimeModel, and tests/runtime-model-call.test.ts is one of them: the new test passes "gpt-large" only through that fallback. The result is a second copy of the legacy-ID rule (it also lists antigravity-default) that can drift from the catalog's, and a test that doesn't exercise the field production uses.
    Fix: reduce wireModelFor to model?.runtimeModel, delete LEGACY_DEFAULT_MODEL_IDS, and set runtimeModel in the test catalog fixtures the way runtimeModelCatalogForComputer does. The Daemon's own "codex-default"/"claude-default" filters in codexInvokeModel/claudeInvokeModel (provider/model_invoke.go:531,751) guard against input that Node never sends for the same reason, so they can go too.

Verified as fine

  • Rollout gating. model goes on the wire only when supportsModelInvokeModel confirms Computer >= 0.62.0 on the same connection epoch (runner-model-invocation.ts:267-271, runner-connections.ts:874-884). @linger/computer is bumped to 0.62.0, and main has no other 0.62.0 claim. Older Daemons keep their strict decoding without seeing the field. The new public test covers both 0.61.0 and 0.62.0.
  • TeamHub. runtime_model is NOT NULL with length 1..120 (migrations/00852_baseline.sql:2229,2263), so a placement always carries one. The contract field is optional, and older Node Services ignore it.
  • No model ID crosses Runtimes. resolveTarget uses the configured model only when runtimeType === placement.runtimeType (runtime-model-call.ts:833-842). The chain rebuilds its placement with the target Runtime's model (:1441-1453). The breaker fingerprint includes the wire model (:947).
  • Pi loading. ensureLocalModel returns at once for an empty model or a model outside the library (daemon/local_models.go:66-69, localmodels/library.go:1003-1016). Explicit Pi calls now load on demand, the same as run.start, and a legacy call that names no model still gets the modelInvokeUnready refusal (daemon/model_invoke.go:119). Covered by TestModelInvokeLoadsNamedPiLocalModelOnDemand.
  • Codex, Claude, Pi argv. An explicit model wins over config.toml in codex exec and in CodexSessions (provider/model_invoke.go:377,497). Claude passes --model and Pi passes --model unless it is pi-default. Reasoning-effort normalization and endpoint reachability now use the requested model (daemon/model_invoke.go:264-273, provider/model_endpoints.go:34).
  • Generated contracts. The generated Go and TS outputs match the edits to envelope.schema.json and agent-runtime-state.yaml, and the docs are updated in apps/computer/AGENTS.md, local-daemon/README.md and src/agent/AGENTS.md.
  • Repository rules. No CJK in added lines or in the commit message, and no merge commits.
  • Unverified. I did not check how a reused CodexSessions session behaves if a later call names a different model. One chain keeps one model, so this probably can't happen.

Process notes

  • The branch is 2 commits behind main (df3fc360, 4657e442). It needs a rebase before it can merge.

CI

8 checks pass. 7 are still running: check (computer-go), check (desktop), check (packages), check (teamhub), web tests (1/2), web tests (2/2) and web-static. I ran no tests locally.

Automated review by Claude Code


Automated review of f1786d4 by Claude Code.

Background model_invoke calls (Wiki, Memory, Agent Memory, Daily Brief,
Approval, Profile, Insight, and Voice Ask) previously omitted the model on
the Runner Gateway wire and deferred to each Runtime's local CLI default.
That diverged from interactive and Routine runs (run.start), causing Codex
background calls to fail with runtime_incompatible when ~/.codex/config.toml
named an unsupported model, Claude calls to ignore the Agent's model, and Pi
calls to fail when a downloaded local model had not been loaded yet.

Unify background model_invoke with the Agent's configured (runtime, model)
and local model loading path:
- Expose optional runtime_model on TeamHub AnotherMePlacement and pass
  ghost.runtime.model from Agent Memory coordinator calls.
- Add optional model to Runner Gateway ModelInvoke (gated on @linger/computer
  >= 0.62.0 for strict-decoder backward compatibility) and bump
  @linger/computer to 0.62.0.
- Resolve the wire model in runtime-model-call: use the Agent's configured
  model on the same Runtime, fall back to the target Runtime catalog default
  on cross-Runtime fallback or legacy/unconfigured placements, and include the
  wire model in background circuit-breaker fingerprints.
- Forward explicit model in Local Computer Daemon across Codex (-m /
  CodexSessions --config model=), Claude (--model), and Pi (--model), run
  ensureLocalModel on Pi model_invoke calls just like run.start, and thread
  the target model into reasoning-effort normalization and endpoint checks.
@DolphinMiner
DolphinMiner force-pushed the fix/background-task-model-selection branch from f1786d4 to f60ac78 Compare November 24, 2026 13:39

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approve with nits (1 P2)

Scope: base main at merge-base 41a3e2a8; 1 commit, 37 files reviewed. This is a follow-up on f1786d4. The PR was rebased since then, so I compared the old and new patches with git range-diff and also re-checked the full merge-base diff. Helper skills loaded: coderbook. I applied the ponytail-review and karpathy-guidelines checklists as the review skill describes them, without loading those two skills. Ran in a separate session.

Previous findings

  • Resolved: legacy default model ID list duplicated in runtime-model-call.ts. LEGACY_DEFAULT_MODEL_IDS and wireModelFor are gone. resolveTarget now uses configured?.runtimeModel and defaultRuntimeModel(models)?.runtimeModel directly (apps/node-services/src/runtime-model-call.ts:820-829, :928-931). The test catalog now sets runtimeModel, and a legacy codex-default case was added (tests/runtime-model-call.test.ts). On the Daemon side, the codex-default and claude-default sentinels were also dropped, because the Agent Service never sends them.

Findings

P2

  • apps/computer/local-daemon/internal/provider/native/codex_model_session.go:192: the Codex session server keeps a single app-server per Daemon, and that server's identity includes runtime.Model. When a call arrives with a different identity, retireLocked() runs, and its own comment says "a turn still running on it fails" (:211). Before this PR, every background session call on a Computer resolved to the same named model (config.toml or the catalog default), so the identity never changed. Now sessionInvoke passes codexInvokeModel(request.Model, …) (internal/provider/model_invoke.go:377, :389), so the model varies by caller.
    • Failure scenario: one Computer hosts the Another Me placements of two tenants (the unique index is per tenant and human, agent_registrations_one_another_me_per_human), both on Codex with different configured models. Their Memory session calls (memory-agent.ts:385 sets sessionKey) interleave. Each switch kills the other tenant's in-flight turn and cold-starts a new app-server, which costs retries and breaker strikes. The same happens when a Claude placement falls back to Codex (it gets the catalog default) while a Codex placement on that Computer uses a non-default model.
    • Fix: keep one server per model identity (for example, a small map keyed by identity, with the same idle timer). At minimum, add a test covering two models alternating on CodexSessions.

Verified as fine

  • Wire model resolution matches run.start:
    • resolveTarget sends runtimeModel from the catalog built by runtimeModelCatalogForComputer. That function sets runtimeModel only for IDs that are not legacy defaults (agent-runner-lifecycle.ts:465, local-computer-management.ts:602).
    • On cross-Runtime fallback, the target Runtime's catalog default is sent, never the placement's model ID (runtime-model-call.ts:928-931).
  • Rollout gating: model is sent only when supportsModelInvokeModel (>= 0.62.0, checked against the connection epoch) holds (runner-model-invocation.ts:267-284). @linger/computer was bumped from 0.61.0 to 0.62.0. A new Daemon receiving no model keeps the old behavior: codexInvokeModel falls back to config.toml or the catalog default (model_invoke.go:529-537), Claude adds no --model, and Pi falls back to its default model.
  • Pi:
    • ensureLocalModel does nothing when the model is empty or not in the library (daemon/local_models.go:66-69, localmodels/library.go:1015-1018).
    • The modelInvokeUnready refusal still applies when no model is named. That covers the legacy pi-default entry, which has no runtimeModel.
    • InvokePiModel skips the pi-default sentinel (native/pi_model_invoke.go:20).
  • TeamHub: runtime_model is NOT NULL with length 1-120 (migrations/00852_baseline.sql:2229, :2263), so the string scan in another_me.sql.go is safe. The optional pointer is set only when the value is non-blank (runtime_config.go:162-165).
  • Breaker fingerprint and effort clamping: the breaker fingerprint now includes the wire model, so a model switch re-admits the call (runtime-model-call.ts:934). Effort is clamped against the requested model, falling back to the default model (daemon/model_invoke.go:264-273).
  • Docs: apps/computer/AGENTS.md, the local-daemon README and node-services/src/agent/AGENTS.md are updated. The user message → provider_ready path is untouched.
  • Repository rules: no CJK in added lines or the commit message, and no merge commits.

Process notes

  • None. The branch is on current main (merge-base equals origin/main).

CI

  • All checks pass, including computer-go, computer-go-windows, contracts, packages, teamhub and repository-gate.
  • CI runs the Daemon Go tests without -race. This PR adds no new locking, so I ran no local tests. I did not test the session-restart issue above; it comes from reading the code.

Automated review by Claude Code


Automated review of f60ac78 by Claude Code.

@LeoXing1996
LeoXing1996 added this pull request to the merge queue Nov 24, 2026
Merged via the queue into main with commit d60d104 Nov 24, 2026
16 checks passed
@LeoXing1996
LeoXing1996 deleted the fix/background-task-model-selection branch November 24, 2026 15:00
@yinwu01

yinwu01 commented Nov 24, 2026

Copy link
Copy Markdown

Heads-up: since this landed, the Pi case in go-local-daemon-process.test.ts ("runs Memory and Wiki on a Pi-only Daemon with whatever model Pi defaults to") fails locally. After the test switches Pi's settings.json default to fixture-own-key/hosted, the next Wiki call still comes back with model: "fixture-local/model" (the case at d60d1043).

I ran the file on d60d104 (this PR on main) and on its parent 94ada67, both with #646 applied, so that the file's Codex cases pass and only this case differs (on main, their fake Codex reports a version below the Daemon's minimum). The case fails on d60d104 and passes on 94ada67; on 54c7a37 without #646 it fails the same way. CI doesn't run this file. Repro from the repo root:

LINGER_GO_LOCAL_DAEMON_TEST=1 bash apps/computer/run-test.sh pnpm --filter @linger/node-services exec vitest run --project agent tests/agent/runner-gateway/go-local-daemon-process.test.ts

I haven't looked into the cause, or into whether the test or the code should change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants