Skip to content

[Fix] Ollama tasks condense before their configured context limit - #1174

Open
ghost wants to merge 3 commits into
mainfrom
fix/ollama-context-management-1kv2451y1vp71
Open

ghost wants to merge 3 commits into
mainfrom
fix/ollama-context-management-1kv2451y1vp71

Conversation

@ghost

@ghost ghost commented Aug 6, 2026 •

Copy link
Copy Markdown

Opened on behalf of Naved Merchant. View the task or mention @roomote for follow-up asks.

What changed

Native Ollama now fetches model metadata before task context management reads the model limits. The fetch is single-flight, avoids repeat requests once metadata is populated, and can recover after a failed request.

Focused regression coverage verifies that a detected 1M context window replaces the 128k fallback before a model request, while preserving duplicate suppression and retry behavior.

Why this change was made

After VS Code restarted or rebuilt the API handler, existing Ollama tasks could condense around 125k because context management ran before native Ollama fetched /api/show metadata.

Closes #1151.

Impact

Ollama tasks now use the model's detected context window for condensing decisions from the start of a resumed task, avoiding premature context loss for large-context models.

Related PRs

@codecov

codecov Bot commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@navedmerchant
navedmerchant marked this pull request as ready for review August 6, 2026 06:07
@github-actions github-actions Bot added the awaiting-review PR changes are ready and waiting for maintainer re-review label Aug 6, 2026

@edelauna edelauna 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.

Has a test comment, and implementation comment, to see if we could reduce duplication.

Comment thread src/api/providers/native-ollama.ts Outdated
Comment on lines +546 to +557
if (!this.modelFetchPromise) {
this.modelFetchPromise = getOllamaModels(this.options.ollamaBaseUrl, this.options.ollamaApiKey)
.then((models) => {
this.models = models
return this.getModel()
})
.finally(() => {
this.modelFetchPromise = undefined
})
}

return this.modelFetchPromise

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.

This single-flight fetch looks like a near-verbatim copy of RouterProvider.fetchModel/ensureModelFetched at router-provider.ts:59-86 — same modelFetchPromise guard, .then/.finally shape. Both providers extend BaseProvider. Worth extracting a shared helper so the two stay in sync, or is keeping them side-by-side intentional?

})
})

describe("ensureModelFetched", () => {

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.

This unit test proves ensureModelFetched populates contextWindow, but no test asserts the fetched value actually reaches context management — Task.spec stubs getModel to a fixed 200k, so nothing guards against the 128k fallback being used instead of the fetched window, which is exactly the symptom this PR fixes. Worth an integration assertion that the fetched value reaches modelInfo.contextWindow?

@github-actions github-actions Bot added awaiting-author PR is waiting for the author to address requested changes and removed awaiting-review PR changes are ready and waiting for maintainer re-review labels Aug 8, 2026
@github-actions github-actions Bot added awaiting-author PR is waiting for the author to address requested changes and removed awaiting-author PR is waiting for the author to address requested changes labels Aug 9, 2026
… integration test

Address PR review comments:
- Extract the single-flight model metadata fetch into
  BaseProvider.fetchModelWithSingleFlight and use it from both
  NativeOllamaHandler and RouterProvider so the two stay in sync.
- Add a Task integration test asserting the fetched model context
  window reaches manageContext instead of the 128k fallback.
@github-actions github-actions Bot added awaiting-author PR is waiting for the author to address requested changes has-conflicts PR has merge conflicts with the base branch and removed awaiting-author PR is waiting for the author to address requested changes has-conflicts PR has merge conflicts with the base branch labels Aug 10, 2026
@github-actions github-actions Bot added has-conflicts PR has merge conflicts with the base branch awaiting-author PR is waiting for the author to address requested changes and removed has-conflicts PR has merge conflicts with the base branch awaiting-author PR is waiting for the author to address requested changes labels Aug 19, 2026
@github-actions

github-actions Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Review status

Thanks for contributing. This comment tracks the review sequence and the next action.

Current step: Resolve the merge conflicts. The review sequence resumes after the branch is mergeable.

Review-state labels are managed by this workflow; do not edit them manually. community-approved is managed the same way — do not add or remove it manually. It signals a fresh community code approval for the current head as an advisory priority only; maintainer review is still required.

@github-actions

Copy link
Copy Markdown
Contributor

This PR has been awaiting author changes for 14 days and will be automatically closed in 7 days. Please address the review comments or leave a comment if you need more time.

@pmltn

pmltn commented Oct 27, 2026

Copy link
Copy Markdown

Hi @navedmerchant and @edelauna!

I'd like to follow up on this fix, as the issue is still affecting native Ollama users with large-context models.

My setup uses ollama cloud with a reported context window of 1,000,000 tokens. The UI correctly detects 1M, but after restarting VS Code or restoring a task, context condensation can happen around 125k due to the 128k fallback.

As of v3.92.0, the underlying issue appears to remain unresolved.

I noticed that this PR has been blocked by merge conflicts for over two months and is now at risk of automatic closure.

Could someone rebase/update this PR, or could another contributor take it over?

The fix and regression tests already seem largely implemented, and losing context prematurely is quite disruptive for long coding sessions.

Thanks for your work on Zoo Code!

This branch has not been deployed

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

Labels

awaiting-author PR is waiting for the author to address requested changes has-conflicts PR has merge conflicts with the base branch stale-awaiting-author PR is stale while waiting for requested author changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Native Ollama still uses 128k fallback for context management before fetching model metadata in v3.74.0

3 participants