Repository navigation
fix: clear stale model-selection overrides in cron session resolution - #61291
kiranvk-2011 wants to merge 3 commits into
Conversation
Greptile SummaryConditionally clears six stale model-selection override fields in The tests in Confidence Score: 5/5Safe to merge; the fix is correct and well-tested, with only a minor P2 companion-field omission All findings are P2 or lower. The core logic is correct, the new payloadModel guard matches the stated intent exactly, and the five new test cases cover the critical branches including backward-compatibility preservation. The single P2 note is a stylistic consistency issue that does not affect runtime correctness. No files require special attention; the P2 omission in session.ts is flagged inline but does not affect correctness. Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/cron/isolated-agent/session.ts
Line: 93-101
Comment:
**Companion fields `authProfileOverrideSource` / `authProfileOverrideCompactionCount` not cleared alongside `authProfileOverride`**
These two metadata fields are always written and cleared together with `authProfileOverride` (see `clearSessionAuthProfileOverride` in `src/agents/auth-profiles/session-override.ts`, lines 36–38). Leaving them behind from a prior session creates an inconsistent entry: `authProfileOverride` is `undefined` but the source/count metadata still describes a profile that no longer applies. In practice this is safe today because `resolveSessionAuthProfileOverride` checks `authProfileOverride` first and short-circuits all consumer paths when `current` is falsy — but it breaks the invariant maintained by `clearSessionAuthProfileOverride` and leaves stale data readable before that resolver runs.
```suggestion
...(isNewSession &&
params.payloadModel && {
providerOverride: undefined,
modelOverride: undefined,
authProfileOverride: undefined,
authProfileOverrideSource: undefined,
authProfileOverrideCompactionCount: undefined,
fallbackNoticeActiveModel: undefined,
fallbackNoticeSelectedModel: undefined,
fallbackNoticeReason: undefined,
}),
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix: clear stale model-selection overrid..." | Re-trigger Greptile |
| ...(isNewSession && | ||
| params.payloadModel && { | ||
| providerOverride: undefined, | ||
| modelOverride: undefined, | ||
| authProfileOverride: undefined, | ||
| fallbackNoticeActiveModel: undefined, | ||
| fallbackNoticeSelectedModel: undefined, | ||
| fallbackNoticeReason: undefined, | ||
| }), |
There was a problem hiding this comment.
Companion fields
authProfileOverrideSource / authProfileOverrideCompactionCount not cleared alongside authProfileOverride
These two metadata fields are always written and cleared together with authProfileOverride (see clearSessionAuthProfileOverride in src/agents/auth-profiles/session-override.ts, lines 36–38). Leaving them behind from a prior session creates an inconsistent entry: authProfileOverride is undefined but the source/count metadata still describes a profile that no longer applies. In practice this is safe today because resolveSessionAuthProfileOverride checks authProfileOverride first and short-circuits all consumer paths when current is falsy — but it breaks the invariant maintained by clearSessionAuthProfileOverride and leaves stale data readable before that resolver runs.
| ...(isNewSession && | |
| params.payloadModel && { | |
| providerOverride: undefined, | |
| modelOverride: undefined, | |
| authProfileOverride: undefined, | |
| fallbackNoticeActiveModel: undefined, | |
| fallbackNoticeSelectedModel: undefined, | |
| fallbackNoticeReason: undefined, | |
| }), | |
| ...(isNewSession && | |
| params.payloadModel && { | |
| providerOverride: undefined, | |
| modelOverride: undefined, | |
| authProfileOverride: undefined, | |
| authProfileOverrideSource: undefined, | |
| authProfileOverrideCompactionCount: undefined, | |
| fallbackNoticeActiveModel: undefined, | |
| fallbackNoticeSelectedModel: undefined, | |
| fallbackNoticeReason: undefined, | |
| }), |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cron/isolated-agent/session.ts
Line: 93-101
Comment:
**Companion fields `authProfileOverrideSource` / `authProfileOverrideCompactionCount` not cleared alongside `authProfileOverride`**
These two metadata fields are always written and cleared together with `authProfileOverride` (see `clearSessionAuthProfileOverride` in `src/agents/auth-profiles/session-override.ts`, lines 36–38). Leaving them behind from a prior session creates an inconsistent entry: `authProfileOverride` is `undefined` but the source/count metadata still describes a profile that no longer applies. In practice this is safe today because `resolveSessionAuthProfileOverride` checks `authProfileOverride` first and short-circuits all consumer paths when `current` is falsy — but it breaks the invariant maintained by `clearSessionAuthProfileOverride` and leaves stale data readable before that resolver runs.
```suggestion
...(isNewSession &&
params.payloadModel && {
providerOverride: undefined,
modelOverride: undefined,
authProfileOverride: undefined,
authProfileOverrideSource: undefined,
authProfileOverrideCompactionCount: undefined,
fallbackNoticeActiveModel: undefined,
fallbackNoticeSelectedModel: undefined,
fallbackNoticeReason: undefined,
}),
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Good catch — you're right that clearSessionAuthProfileOverride() in session-override.ts (lines 36–38) always clears all three fields together as an invariant. Leaving the companion metadata behind would create an inconsistent session entry even if it's currently safe at runtime.
Fixed in d3cdfdcd59 — the spread now clears all 8 fields including authProfileOverrideSource and authProfileOverrideCompactionCount. Tests updated to include these companion fields in both the "clears overrides" and "preserves overrides" assertions.
There was a problem hiding this comment.
Follow-up: after further analysis prompted by a Codex review comment, the authProfileOverride family has been removed from the cleared set in a174da0888.
The reason: resolveSessionAuthProfileOverride() uses authProfileOverride as the current input for pickNextAvailable(current) on new sessions. Clearing it forces every new cron session to pickFirstAvailable(), breaking round-robin rotation across multiple auth profiles. Since auth-profile rotation state is semantically distinct from model-selection contamination, it should be preserved.
The fix now clears only 5 fields (the model-selection + fallback-notice group). Tests explicitly assert that all 3 auth-profile fields are preserved.
180cc8e to
d3cdfdc
Compare
|
Thanks for the thorough review. The P2 companion field observation is correct — addressed in d3cdfdcd59. The spread now clears all 8 fields: the original 6 plus Branch has been rebased onto latest |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d3cdfdcd59
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| authProfileOverride: undefined, | ||
| authProfileOverrideSource: undefined, | ||
| authProfileOverrideCompactionCount: undefined, |
There was a problem hiding this comment.
Keep auth-profile rotation state when payload model is set
Clearing authProfileOverride, authProfileOverrideSource, and authProfileOverrideCompactionCount here wipes the previous profile state before prepareCronRunContext calls resolveSessionAuthProfileOverride(...), so new sessions with payload.model always look like they have no prior profile and fall back to pickFirstAvailable() instead of rotating from the previous profile. In practice this regresses multi-profile failover/round-robin for isolated cron jobs (which are always new sessions), making repeated runs stick to the first profile and more likely to re-hit the same rate-limited credentials.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid catch — agreed. The authProfileOverride family serves a different purpose (profile rotation state for resolveSessionAuthProfileOverride()) than the model-selection fields (providerOverride/modelOverride). Clearing them would force pickFirstAvailable() on every new cron session instead of pickNextAvailable(current), effectively defeating round-robin rotation.
Fixed in a174da0888: removed all three auth-profile fields (authProfileOverride, authProfileOverrideSource, authProfileOverrideCompactionCount) from the cleared set. The fix now only clears 5 model-selection fields:
providerOverridemodelOverridefallbackNoticeActiveModelfallbackNoticeSelectedModelfallbackNoticeReason
Tests updated to assert that auth-profile fields are explicitly preserved while stale model-selection overrides are cleared.
d3cdfdc to
a174da0
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a174da0888
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| ...(isNewSession && | ||
| params.payloadModel && { | ||
| providerOverride: undefined, |
There was a problem hiding this comment.
Restrict override clearing to isolated cron sessions
The new isNewSession && params.payloadModel gate clears providerOverride/modelOverride for all new cron sessions, not just isolated ones. Agent-turn cron jobs can also run with sessionTarget: "current" or session:<id> (src/cron/service/jobs.ts:135-148), and run.ts only forces new sessions for "isolated" (src/cron/isolated-agent/run.ts:288). That means a stale shared session now loses user-set model overrides when a payload model is present, which mutates interactive session state and regresses override persistence across inactivity rollovers. This clearing should be scoped to isolated/cron-owned sessions (or equivalent) rather than every isNewSession case.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Addressed in ba37ee1c47 — the condition has been narrowed from isNewSession && params.payloadModel to params.forceNew && params.payloadModel.
forceNew is only true when sessionTarget === "isolated" (set at run.ts:288), so shared session targets (session:<id>) are never modified. This preserves user-set overrides across inactivity rollovers while still clearing stale state for cron-owned sessions that create a fresh entry every run.
New test added: "preserves model-selection overrides on shared sessions even with payloadModel" — asserts all 5 fields survive when forceNew is absent.
Branch has been squashed to a single commit and rebased onto latest main.
d3d2b49 to
4be1dc8
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4be1dc8e8c
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| ...(params.forceNew && | ||
| params.payloadModel && { |
There was a problem hiding this comment.
Scope override clearing to cron-owned isolated sessions
Even with the new forceNew guard, this still clears model-selection state for non-cron shared sessions: the hook runner always sets sessionTarget: "isolated" but can reuse a caller/default sessionKey (src/gateway/server/hooks.ts:60-66,79-87), and run.ts persists back to that same key whenever it is not cron:* (src/cron/isolated-agent/run.ts:292-299). In that path, payload.model now causes providerOverride/modelOverride/fallback notice fields to be deleted from an interactive session entry, so a hook call can silently wipe user-set /model state. This is fresh evidence beyond the earlier isNewSession discussion because it uses the forceNew hook path that still targets shared keys.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid catch — thank you.
Hook-dispatched jobs (hooks.ts:60) always set sessionTarget: "isolated" (→ forceNew: true) but pass through the caller's sessionKey, which can be a shared interactive session key (not a cron:* key). In run.ts:286-288, runSessionKey only scopes to a per-run key when baseSessionKey.startsWith("cron:") — otherwise it persists back to the original agentSessionKey. So a hook call with payload.model set would clear providerOverride/modelOverride/fallback fields from the interactive session entry, silently wiping user-set /model state.
Fixed in 6c6929c076: Added isCronSession?: boolean parameter to resolveCronSession(), gated on baseSessionKey.startsWith("cron:") in run.ts. The clearing guard is now a triple condition:
if (params.forceNew && params.payloadModel && params.isCronSession) {This ensures only actual cron sessions (with cron:* base keys) clear stale overrides. Hook-dispatched isolated sessions with non-cron keys skip the clearing entirely, preserving interactive session state.
New test added: "preserves model-selection overrides on hook-dispatched isolated sessions (non-cron key)" — verifies forceNew: true + payloadModel set + isCronSession: false leaves all override fields untouched.
Cherry-pick of openclaw#61291 (kiranvk-2011). Resolves conflict: our branch already has direct imports where PR used lazy-loaded helpers; core fix in session.ts applied cleanly. Added payloadModel pass-through at resolveCronSession call site in run.ts to complete the wire-up.
4be1dc8 to
6c6929c
Compare
6c6929c to
2d48e41
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2d48e41da8
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| @@ -281,6 +281,8 @@ async function prepareCronRunContext(params: { | |||
| agentId, | |||
| nowMs: now, | |||
| forceNew: input.job.sessionTarget === "isolated", | |||
| payloadModel: input.job.payload.kind === "agentTurn" ? input.job.payload.model : undefined, | |||
| isCronSession: baseSessionKey.startsWith("cron:"), | |||
There was a problem hiding this comment.
Classify cron-owned runs without relying on sessionKey prefix
Using baseSessionKey.startsWith("cron:") to set isCronSession can still misclassify shared hook runs as cron-owned: in src/gateway/server/hooks.ts the hook dispatcher always runs with sessionTarget: "isolated" (so forceNew is true) but uses a configurable sessionKey, and src/gateway/hooks.ts allows configured/requested keys that can start with cron:. When that happens and payload.model is set, resolveCronSession() will clear providerOverride/modelOverride on what is effectively a shared session, silently wiping persisted /model state. This should be keyed off an explicit ownership signal (for example deliveryContract) rather than a string-prefix heuristic.
Useful? React with 👍 / 👎.
2d48e41 to
705d81f
Compare
|
Good catch — the session-key prefix is fragile for exactly the reason you describe. Replaced with Changes in 705d81fec1:
All 17 tests pass. |
41c3c68 to
e2114a1
Compare
When a cron job shares a sessionTarget with an interactive chat session, resolveCronSession() copies the full session entry via spread, inheriting stale providerOverride / modelOverride / fallbackNotice* fields. These force the cron run to retry against a rate-limited provider before the payload model's fallback chain activates, adding 30-50 s of wasted retries per run. Clear the five model-selection override fields when starting an isolated cron session (forceNew) that specifies its own payloadModel. The guard is scoped to forceNew rather than isNewSession so that shared session targets — which persist back to the interactive session store — never lose user-set overrides. authProfileOverride and its companions are intentionally preserved: resolveSessionAuthProfileOverride() uses the previous value for round-robin profile rotation via pickNextAvailable(). Fixes openclaw#57947 Fixes openclaw#47592 > Generated with AI assistance (Claude claude-opus-4.6 via OpenCode).
… ownership
Replace baseSessionKey.startsWith("cron:") heuristic with an explicit
ownership signal derived from deliveryContract. Hook dispatchers pass
deliveryContract: "shared" while cron-scheduled jobs default to
"cron-owned", making this a reliable discriminator that cannot be
fooled by configurable session keys that happen to start with "cron:".
e2114a1 to
4bdbd0e
Compare
|
Closing this as implemented after Codex review. Current What I checked:
So I’m closing this as already implemented rather than keeping a duplicate issue open. Review notes: reviewed against 7b5307acfc34; fix evidence: release v2026.4.23, commit a9797214338b. |
Summary
Cron jobs that share a
sessionTargetwith an interactive chat session inherit staleproviderOverride/modelOverride/fallbackNotice*values from that session. This causes cron jobs to retry against rate-limited providers (4× retries, ~30-50s delay) instead of using their configuredpayload.model, even when a healthy fallback provider is available.This PR adds conditional override clearing in
resolveCronSession(): when the cron job specifies apayloadModeland it is an isolated session (forceNew), the 5 model-selection override fields are stripped from the inherited session entry before the session is used.The Problem
resolveCronSession()insrc/cron/isolated-agent/session.tsuses{ ...entry }to construct the cron session from the stored session entry. This copies all fields, including:providerOverride— forces a specific provider beforepayload.modelis evaluatedmodelOverride— forces a specific modelfallbackNoticeActiveModel/fallbackNoticeSelectedModel/fallbackNoticeReason— stale rate-limit noticesWhen an interactive Telegram/WhatsApp session shares the same
sessionTargetas a cron job, any provider override set by that session (e.g., from a/new sonnet46command, or from the gateway re-writingproviderOverrideafter each successful run) contaminates the cron session.The gateway's provider resolution evaluates
providerOverridebeforepayload.model, so the cron job's configured model is bypassed entirely. The job retries 4 times against the overridden (often rate-limited) provider before falling back — adding 30-50 seconds of unnecessary latency per cron run.The Fix
resolveCronSession()now accepts optionalpayloadModelandforceNewparameters. When both conditions are met:params.forceNewistrue(isolated cron session — always creates a fresh entry)params.payloadModelis truthy (cron job specifies a model in its payload)...the function clears 5 override fields from the session entry:
Why
forceNewinstead ofisNewSessionThe guard is scoped to
forceNew(isolated sessions) rather thanisNewSessionso that shared session targets — which persist back to the interactive session store viapersistSessionEntry— never lose user-set overrides.forceNewis onlytruewhensessionTarget === "isolated"(set atrun.ts:288), making this safe for all session target types.Why
authProfileOverrideis NOT clearedauthProfileOverrideand its companions (authProfileOverrideSource,authProfileOverrideCompactionCount) are intentionally preserved.resolveSessionAuthProfileOverride()uses the previousauthProfileOverridevalue as input forpickNextAvailable(current)to rotate across auth profiles. Clearing it would forcepickFirstAvailable()on every new cron session, defeating round-robin failover.Files Changed
src/cron/isolated-agent/session.tspayloadModel?: stringandforceNew?: booleanparams; conditional override clearing blocksrc/cron/isolated-agent/run.tspayloadModelfrom job payload toresolveCronSession()src/cron/isolated-agent/session.test.tsTest Coverage
6 new test cases in
session.test.ts:forceNewabsent, overrides surviveAll 16 tests pass (10 existing + 6 new). Lint (
pnpm lint) and typecheck (pnpm tsgo) clean.Reproduction (Real-World)
Observed on a production OpenClaw instance running v2026.3.28:
knowledge-sync(hourly,payload.model: "bailian/qwen3.5-plus")agent:opus46:telegram:direct:7295404152withproviderOverride: "github-copilot"github-copilotbefore falling back tobailian/qwen3.5-plus, adding ~40s latency per runproviderOverridemanually: Cron runs completed in ~27s using the payload model directlyFixes #57947
Fixes #47592