fix(sessions): provider-qualified context limits (#62472)#62493
Conversation
Greptile SummaryFixes provider-qualified context window resolution across the memory-flush and preflight-compaction gates, inline-directive persist, and followup runners. Previously these paths used bare model-id lookups that could collide when multiple providers share a model id, writing the wrong Confidence Score: 5/5Safe to merge; the fix correctly threads provider context through all affected paths and adds targeted regression coverage. All remaining findings are P2. The priority inversion for src/auto-reply/reply/memory-flush.ts — minor Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/auto-reply/reply/memory-flush.ts
Line: 13-22
Comment:
**`agentCfgContextTokens` priority inconsistency with main run path**
In `agent-runner.ts` and `followup-runner.ts`, `agentCfgContextTokens` takes the highest priority (`agentCfgContextTokens ?? resolveContextTokensForModel(...)`), and in `directive-handling.persist.ts` it is passed as `contextTokensOverride` so it wins there too. Here the order is reversed — `resolveContextTokensForModel` wins, and `agentCfgContextTokens` only fires when no model is found in config. If an agent has an explicit `contextTokens` override smaller than the provider's configured window, the flush/compaction gate will use the larger model value and may not trigger when it should. Threading it as `contextTokensOverride` would restore consistency:
```suggestion
return (
resolveContextTokensForModel({
cfg: params.cfg,
provider: params.provider,
model: params.modelId,
contextTokensOverride: params.agentCfgContextTokens,
allowAsyncLoad: false,
}) ??
DEFAULT_CONTEXT_TOKENS
);
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix(sessions): provider-qualified contex..." | Re-trigger Greptile |
Review follow-up (commit
|
c248c9d to
8b5bba5
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8b5bba51c6
ℹ️ 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".
8b5bba5 to
6b8859c
Compare
fcc533d to
6e84928
Compare
obviyus
left a comment
There was a problem hiding this comment.
Reviewed latest changes; landing now.
|
Landed on main. Thanks @neeravmakwana. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6e84928f1a
ℹ️ 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".
| sessionEntry?.contextTokens ?? | ||
| DEFAULT_CONTEXT_TOKENS; | ||
| resolveContextTokensForModel({ | ||
| cfg: queued.run.config, |
There was a problem hiding this comment.
Resolve follow-up context window from runtime config
This lookup uses queued.run.config even though the follow-up run itself is executed and persisted with runtimeConfig right below. If the runtime snapshot differs (for example after a hot config reload), contextTokensUsed can be derived from stale provider/model limits and then written into session usage, which skews /status reporting and memory-flush/compaction thresholds for that turn. Use the same runtimeConfig object here so context sizing matches the config actually used for the run.
Useful? React with 👍 / 👎.
…avmakwana) * fix(sessions): provider-qualified context limits (#62472) * fix(sessions): honor agent context cap in memory-flush gate * refactor(sessions): unify context token resolution * fix: keep followup snapshot freshness on the active provider (#62493) (thanks @neeravmakwana) --------- Co-authored-by: Ayaan Zaidi <hi@obviy.us>
@neeravmakwana) * fix(sessions): provider-qualified context limits (openclaw#62472) * fix(sessions): honor agent context cap in memory-flush gate * refactor(sessions): unify context token resolution * fix: keep followup snapshot freshness on the active provider (openclaw#62493) (thanks @neeravmakwana) --------- Co-authored-by: Ayaan Zaidi <hi@obviy.us>
@neeravmakwana) * fix(sessions): provider-qualified context limits (openclaw#62472) * fix(sessions): honor agent context cap in memory-flush gate * refactor(sessions): unify context token resolution * fix: keep followup snapshot freshness on the active provider (openclaw#62493) (thanks @neeravmakwana) --------- Co-authored-by: Ayaan Zaidi <hi@obviy.us>
@neeravmakwana) * fix(sessions): provider-qualified context limits (openclaw#62472) * fix(sessions): honor agent context cap in memory-flush gate * refactor(sessions): unify context token resolution * fix: keep followup snapshot freshness on the active provider (openclaw#62493) (thanks @neeravmakwana) --------- Co-authored-by: Ayaan Zaidi <hi@obviy.us>
@neeravmakwana) * fix(sessions): provider-qualified context limits (openclaw#62472) * fix(sessions): honor agent context cap in memory-flush gate * refactor(sessions): unify context token resolution * fix: keep followup snapshot freshness on the active provider (openclaw#62493) (thanks @neeravmakwana) --------- Co-authored-by: Ayaan Zaidi <hi@obviy.us>
@neeravmakwana) * fix(sessions): provider-qualified context limits (openclaw#62472) * fix(sessions): honor agent context cap in memory-flush gate * refactor(sessions): unify context token resolution * fix: keep followup snapshot freshness on the active provider (openclaw#62493) (thanks @neeravmakwana) --------- Co-authored-by: Ayaan Zaidi <hi@obviy.us>
Summary
lookupContextTokens/lookupCachedContextTokens) for context limits. When several providers expose the same model id with different configured context windows, the wrong limit could be written tosessionEntry.contextTokensand affect/status, compaction, and flush behavior ([Bug]: Context token / context window can be persisted incorrectly when multiple providers share the same model id, causing wrong/statusvalues and potentially premature compaction #62472).contextTokensdrives usage display, compaction thresholds, and related safeguards; a stale or collided value can make the runtime overly conservative or inconsistent with the active provider.resolveContextTokensForModel({ cfg, provider, model, allowAsyncLoad: false })in the auto-reply agent and follow-up runners, inpersistInlineDirectivesreturn value, and inresolveMemoryFlushContextWindowTokens(threadingcfg+providerfrom the follow-up run). Follow-up runs now resolveproviderUsedfrom agent meta when present, matching the main reply path.updateSessionStoreAfterAgentRunalready used provider-qualified resolution; gateway session row building and status logic were left as-is. The global context cache structure is unchanged; resolution goes through the existingresolveContextTokensForModelcontract.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
/statusvalues and potentially premature compaction #62472Root Cause (if applicable)
resolveContextTokensForModelcorrectly scanscfg.models.providersper provider, but several hot paths still used bare-model cache lookups or cached tokens keyed only by model id, so duplicate ids across providers could leak the wrong window into persisted session metadata.resolveMemoryFlushContextWindowTokenswhen two providers list the same model id with different limits.Regression Test Plan (if applicable)
src/auto-reply/reply/reply-state.test.tscontextWindowvalues;resolveMemoryFlushContextWindowTokensreturns the limit for the requestedprovider.resolveContextTokensForModelpath used by reply usage persistence and memory sizing without standing up a full channel run.resolveContextTokensForModeltests insrc/agents/context.lookup.test.ts; this adds coverage at the memory-flush entry point.User-visible / Behavior Changes
contextTokensand related usage / flush thresholds should match the active provider’s configured window when multiple providers reuse the same model id.Diagram (if applicable)
N/A
Security Impact (required)
Testing
pnpm exec oxlint --type-awareon touchedsrc/auto-reply/reply/*.tsfilespnpm test src/auto-reply/reply/reply-state.test.ts -t resolveMemoryFlushContextWindowTokens