Skip to content

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

Merged
edelauna merged 3 commits into
mainfrom
fix/ollama-context-management-recreated
Nov 21, 2026
Merged

edelauna merged 3 commits into
mainfrom
fix/ollama-context-management-recreated

Conversation

@zoomote

@zoomote zoomote Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

​Created by Roomote. Follow up by mentioning @roomote, in the web UI, or in Discord.

Related GitHub Issue

Closes: #1151

Description

Supersedes #1174. This is the same fix, rebased onto current main. The original PR cannot merge because its author is a deleted ghost account.

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.

Conflict resolution notes for the rebase:

  • main has changed since the original PR. PR feat(api): add ensureModelFetched to the six catalog providers #2191 added NativeOllamaHandler.ensureModelFetched() and BaseProvider.ensureCatalogLoaded(). Task.safeEnsureModelFetched() now runs before context management. The ordering part of the original fix is already on main.
  • This PR keeps that structure. It adds the part that is still missing: NativeOllamaHandler.fetchModel() no longer refetches /api/tags and /api/show on every createMessage and completePrompt call. It skips the fetch once the catalog is populated, shares one in-flight fetch between concurrent callers, and clears the in-flight promise after a failure so the next call retries. The optional abort signal still reaches the discovery fetch.
  • The original PR also extracted a shared fetchModelWithSingleFlight helper into BaseProvider and rewrote RouterProvider.fetchModel() to use it. That refactor was dropped. RouterProvider.fetchModel() on main now has negative caching and refresh logic that the simpler helper cannot express.

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.

Test Procedure

  • pnpm --dir src exec vitest run api/providers/__tests__/native-ollama.spec.ts — 102 tests pass. New tests cover: the detected 1M context window replacing the 128k fallback, skipping repeat fetches once models are populated, deduplication of concurrent fetches, and retry after a rejected request.
  • pnpm --dir src exec vitest run core/task/__tests__/Task.spec.ts — 231 tests pass. The new integration test asserts that the fetched context window reaches manageContext instead of the 128k fallback.
  • pnpm --dir src run check-types passes.
  • Full pnpm test passes (13,899 tests).

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Listing Discussion (new provider or MCP server only): I opened a GitHub Discussion for this listing and linked it in the PR description.
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I reviewed my own code with care.
  • Writing Style: The PR title and description use Simplified Technical English (ASD-STE100): short sentences, plain words, active voice, one idea per sentence, no contractions.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Test Helpers (test changes only): Shared test helpers (src/test-utils/, @/utils/test-utils) are used only for mechanical duplication; test intent and scenario-specific mocks remain visible.
  • Visual Snapshot (UI changes only): Not applicable. No user-visible UI change.
  • Documentation Impact: I checked if my changes need documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.
  • Yes, documentation updates are required. (Describe what to update, or link to a PR in the docs repository).

Zoo-Code-Docs: Zoo-Code-Org/Zoo-Code-Docs#23

Additional Notes

No visual proof is attached. The change is provider-side fetch behavior with no browser-renderable surface. The test results above are the primary evidence.

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@zoomote

zoomote Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

1 issue outstanding. See session

  • src/api/providers/native-ollama.ts:678 — A cancelled or timed-out caller aborts shared model discovery for other concurrent requests.

Reviewed a8bd7a0

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Review status

This PR was opened by an automated account. A human maintainer must verify the change intent, provenance, and validation before merging.

Current step: Required CI passed. Waiting for automated review of the latest commit.

If automated review does not start, a maintainer must restart it.

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.

Comment thread src/api/providers/native-ollama.ts Outdated

if (!this.modelFetchPromise) {
const fetchOpts: [] | [{ signal: AbortSignal }] = signal ? [{ signal }] : []
this.modelFetchPromise = getOllamaModels(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The shared promise is created with whichever request signal arrives first. If that caller is cancelled or its timeoutMs expires, getOllamaModels aborts and rejects this promise for every concurrent createMessage/completePrompt caller, including callers whose own signals remain active. Keep cancellation scoped to each waiter rather than letting one request cancel shared discovery, and add a cold-cache regression test where one concurrent caller aborts while the other still succeeds.

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.

Fixed in 0b75a51. fetchModel() now uses BaseProvider.ensureCatalogLoaded() for shared discovery. The fetch owns its timeout signal. Each caller's signal cancels only that caller's wait. Empty and failed loads remain retryable.

Added cold-cache property tests across fetchModel, ensureModelFetched, createMessage, and completePrompt, with both caller orders and cancellation before or after discovery settles. Added a concurrent prompt-timeout test where the streaming caller still succeeds. The original code failed 16 of the 64 cancellation schedules; the fix passes all 64.

The TLA+ model passes with independent cancellation and rejects the request-owned signal variant. Focused provider and Task suites pass (400 tests). Type checking and ESLint pass, with no increase in suppression counts.

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@edelauna
edelauna marked this pull request as ready for review November 21, 2026 22:14
@edelauna
edelauna force-pushed the fix/ollama-context-management-recreated branch from 0b75a51 to 09411da Compare November 21, 2026 22:15
@edelauna
edelauna enabled auto-merge November 21, 2026 22:15
@edelauna
edelauna added this pull request to the merge queue Nov 21, 2026
@github-actions github-actions Bot added coderabbit-review-active Required CI passed; CodeRabbit review is active awaiting-coderabbit Waiting for CodeRabbit to approve the latest commit labels Nov 21, 2026
Merged via the queue into main with commit cc271bd Nov 21, 2026
22 of 23 checks passed
@edelauna
edelauna deleted the fix/ollama-context-management-recreated branch November 21, 2026 22:28
easonLiangWorldedtech added a commit to easonLiangWorldedtech/Zoo-Code that referenced this pull request Nov 22, 2026
One conflict: src/api/providers/native-ollama.ts, where this branch's abort-hook refactor and
Zoo-Code-Org#2520's shared-catalog rewrite touch the same lines. The merge base is 75a93a9. The merged tip is
2a082ce ("fix(vertex): prevent Gemini requests from ending with a
model turn (Zoo-Code-Org#2641)"), not the 410e5ad the branch was one behind.

Resolved for behaviour, not for text. Both sides must survive:

- Zoo-Code-Org#2520's side: model discovery is a shared, bounded catalog load, so the caller's signal cancels
  only its own wait while the shared fetch keeps its own timeout. That means main's fetchModel
  (ensureCatalogLoaded(loader, signal) with AbortSignal.timeout(MODEL_CATALOG_FETCH_TIMEOUT_MS) on
  the loader and 'return Object.keys(this.models).length > 0'), main's ensureModelFetched body
  ('await this.fetchModel(signal)') and both of main's comments about cancelling the wait came over
  whole - including the comment above discoverySignal, which this branch's wording would have
  contradicted: after Zoo-Code-Org#2520 the per-request signal no longer reaches the discovery network fetch.
- This branch's side: both bridges come from bridgeExternalAbortSignal, createMessage registers it
  with { onAbort } only because it needs bridge.controller.signal, and completePrompt registers it
  with { controller, onAbort } after the client exists so an already-aborted caller cancels through
  the hook. The method-local controller keeps that name here because the shared hook owns
  bridge.controller in createMessage; main's requestController is gone with the plumbing it named,
  as are the onExternalAbort / removeEventListener blocks it replaced.

Nothing from either side was dropped: against main the merged file reads +36/-45 and against the
branch +15/-25, which is exactly each side's own delta to the merge base, and no line either side
added is missing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-coderabbit Waiting for CodeRabbit to approve the latest commit coderabbit-review-active Required CI passed; CodeRabbit review is active

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

2 participants