Skip to content

Cache TUI model registry - #496

Merged
kevincodex1 merged 1 commit into
Gitlawb:mainfrom
GautamBytes:perf/cache-tui-model-registry
Jul 5, 2026
Merged

Cache TUI model registry#496
kevincodex1 merged 1 commit into
Gitlawb:mainfrom
GautamBytes:perf/cache-tui-model-registry

Conversation

@GautamBytes

@GautamBytes GautamBytes commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #494

Summary

  • cache the curated model registry once per TUI model\n- reuse it for context-window and vision checks
  • add regression tests plus a lookup benchmark.

Summary by CodeRabbit

  • Performance

    • Reduced repeated model-registry lookups by reusing a cached model catalog during model checks.
  • Bug Fixes

    • Improved how supported context windows and vision capability are determined for models.
    • Kept discovered-model handling and name-based fallback behavior intact when needed.
  • Tests

    • Added coverage for cached catalog lookups, vision-capability checks, and discovery interactions.
    • Added a benchmark comparing repeated registry loading versus cached lookup performance.

Copilot AI review requested due to automatic review settings July 4, 2026 14:37

Copilot AI 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.

Pull request overview

This PR addresses #494 by avoiding repeated reconstruction of the curated model registry during TUI rendering, caching the registry per TUI model instance and reusing it for context-window resolution and vision capability checks.

Changes:

  • Add a cached modelCatalog registry field to the TUI model and initialize it once in newModel.
  • Switch context-window and vision checks to consult the cached registry rather than calling modelregistry.DefaultRegistry() repeatedly.
  • Add regression tests and a benchmark to validate/quantify cached vs uncached lookup behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/tui/picker_test.go Updates context-window test setup to use the model’s cached registry field.
internal/tui/model.go Adds cached registry field and initializes it during model construction; wires registry into default usage tracker creation.
internal/tui/model_catalog.go Uses cached registry for modelContextWindow resolution instead of rebuilding the registry each call.
internal/tui/model_catalog_test.go Adds regression tests ensuring cached-registry behavior plus a benchmark for lookup cost.
internal/tui/image_attach.go Uses cached registry for vision checks instead of rebuilding the registry each call.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/tui/model.go Outdated
Comment thread internal/tui/image_attach.go Outdated
@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 058d28ce-ba9c-4211-be04-e41dd69cb3e4

📥 Commits

Reviewing files that changed from the base of the PR and between 3323b3e and 2e8a848.

📒 Files selected for processing (5)
  • internal/tui/image_attach.go
  • internal/tui/model.go
  • internal/tui/model_catalog.go
  • internal/tui/model_catalog_test.go
  • internal/tui/picker_test.go
🚧 Files skipped from review as they are similar to previous changes (4)
  • internal/tui/picker_test.go
  • internal/tui/model.go
  • internal/tui/model_catalog.go
  • internal/tui/image_attach.go

Walkthrough

The TUI model now caches the curated model registry at construction and reuses it for context-window and vision-capability checks. The render path no longer loads the default registry on each call, and tests plus a benchmark cover the updated lookup flow.

Changes

Cached model catalog for TUI lookups

Layer / File(s) Summary
Model struct and initialization
internal/tui/model.go
model gains a modelCatalog field; newModel loads modelregistry.DefaultRegistry() once when needed, passes it into usage.NewTracker, and stores it on the model.
Render-path lookups use cached catalog
internal/tui/model_catalog.go, internal/tui/image_attach.go
modelContextWindow and modelSupportsVisionTUI resolve curated model data from m.modelCatalog instead of rebuilding the registry on each call.
Tests, benchmark, and test wiring
internal/tui/model_catalog_test.go, internal/tui/picker_test.go
Adds coverage for cached context-window and vision behavior, a benchmark for cached vs. uncached lookups, registry-building helpers, and updates an existing picker test to inject the loaded registry.

Estimated code review effort: 2 (Simple) | ~15 minutes

Possibly related PRs

  • Gitlawb/zero#143: Also changes internal/tui/image_attach.go vision gating logic in modelSupportsVisionTUI().
  • Gitlawb/zero#332: Overlaps with modelContextWindow resolution in internal/tui/model_catalog.go.
  • Gitlawb/zero#356: Also changes the modelContextWindow lookup path in internal/tui/model_catalog.go.

Suggested reviewers: gnanam1990, anandh8x

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: caching the TUI model registry.
Linked Issues check ✅ Passed The PR caches the registry per TUI model and reuses it for context-window and vision checks as requested in #494.
Out of Scope Changes check ✅ Passed The added tests and benchmark directly support the caching change and do not appear unrelated to the issue scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
internal/tui/model_catalog.go (1)

64-95: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Panic on modelCatalogErr unconditionally
newModel only surfaces DefaultRegistry() failures when usageTracker == nil; if a tracker is supplied, the constructor can cache a zero-value catalog and silently lose curated context-window and vision lookups for the whole TUI session. internal/tui/model.go should panic on modelCatalogErr regardless of UsageTracker.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/tui/model_catalog.go` around lines 64 - 95, The constructor path for
the TUI model catalog is swallowing modelCatalogErr when a UsageTracker is
present, which can leave modelCatalog in a zero-value state for the whole
session. Update newModel in internal/tui/model.go so it always fails fast on
DefaultRegistry()/catalog initialization errors, regardless of whether
usageTracker is nil, and make sure modelContextWindow still relies on a valid
catalog for curated lookups.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/tui/model.go`:
- Around line 665-671: The registry load failure in modelCatalog initialization
is only handled when usageTracker is nil, which can silently cache an empty
registry when a tracker is pre-supplied. Update the modelCatalog/modelCatalogErr
handling in model.go so DefaultRegistry errors are checked and surfaced
unconditionally before any UsageTracker branching, ensuring m.modelCatalog never
stores a zero-value Registry and preserving correct behavior for
modelContextWindow and modelSupportsVisionTUI.

---

Outside diff comments:
In `@internal/tui/model_catalog.go`:
- Around line 64-95: The constructor path for the TUI model catalog is
swallowing modelCatalogErr when a UsageTracker is present, which can leave
modelCatalog in a zero-value state for the whole session. Update newModel in
internal/tui/model.go so it always fails fast on DefaultRegistry()/catalog
initialization errors, regardless of whether usageTracker is nil, and make sure
modelContextWindow still relies on a valid catalog for curated lookups.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6f7e939b-3cba-44a4-adbb-aa57ef180d0b

📥 Commits

Reviewing files that changed from the base of the PR and between f401c66 and 3323b3e.

📒 Files selected for processing (5)
  • internal/tui/image_attach.go
  • internal/tui/model.go
  • internal/tui/model_catalog.go
  • internal/tui/model_catalog_test.go
  • internal/tui/picker_test.go

Comment thread internal/tui/model.go Outdated
@GautamBytes
GautamBytes force-pushed the perf/cache-tui-model-registry branch from 3323b3e to 2e8a848 Compare July 4, 2026 14:46

@jatmn jatmn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Vasanthdev2004

I found one process blocker that needs a maintainer decision before this is ready.

Findings

  • [P2] Resolve the approved-issue requirement for this community PR
    CONTRIBUTING.md:19
    This PR links #494, but the linked issue currently only has the enhancement label, while the contribution policy says community PRs must be tied to an existing issue that has already been reviewed and marked with issue-approved. Please get the linked issue marked as approved, or have a maintainer explicitly confirm that this PR falls under the team-member/internal-work exception, before proceeding with the code changes.

@Vasanthdev2004 Vasanthdev2004 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good fix. modelContextWindow runs a couple of times per render frame (header, context gauge, sidebar), and on main each call rebuilt the whole registry — validate 13 entries, compile the regexes, deep-clone everything — which the codebase itself warns against (session_controls.go:156: "MUST NOT be called from the render path"). Caching the catalog once in newModel and reading it on lookup is the right minimal fix, and reusing that same registry for the usage tracker instead of building a second one is a nice touch.

I checked the thing that worried me — staleness after a /model switch — and it's fine: the cache is the whole immutable catalog, not a memoized per-model answer, so Resolve(m.modelName) still resolves the new model correctly. Discovered/Ollama models stay in their own live maps, untouched. No race either — built eagerly in newModel, only ever read after, and the registry methods clone on read.

Bonus: the vision reorder actually fixes a latent bug on main where the name heuristic could short-circuit true before the discovered list was consulted (a discovered text-only gpt-5-* would wrongly report vision support). There's a regression test for it.

CodeRabbit's one real catch — the registry error getting swallowed when a UsageTracker is injected — is handled at HEAD; the DefaultRegistry() error now panics unconditionally before the tracker branch, so the cached registry can't be a zero value.

Only non-code thing: #494 has enhancement but not issue-approved — your call. Approving.

@kevincodex1 kevincodex1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@kevincodex1
kevincodex1 merged commit e7d88b4 into Gitlawb:main Jul 5, 2026
7 checks passed
@GautamBytes
GautamBytes deleted the perf/cache-tui-model-registry branch July 5, 2026 10:29
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.

TUI render rebuilds model registry every frame

5 participants