Repository navigation
fix(dao): honor tenant-configured context window override (D22/D23) - #18171
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (5)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThe change makes model context-length resolution tenant-aware. The DAO applies active tenant ChangesModel context resolution
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant LLMComponent
participant ModelService
participant DAO
participant TenantModel
participant ProviderCatalog
participant MessageFitter
LLMComponent->>ModelService: resolve context length with tenant ID
ModelService->>DAO: resolve tenant model context length
DAO->>TenantModel: inspect model reference and extra
TenantModel-->>DAO: max_tokens override or model metadata
DAO->>ProviderCatalog: read catalog content_length when needed
ProviderCatalog-->>DAO: catalog context length
DAO-->>ModelService: resolved context length
ModelService-->>LLMComponent: context length
LLMComponent->>MessageFitter: fit messages within context length
MessageFitter-->>LLMComponent: fitted messages
Possibly related PRs
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
internal/service/model_service.go (1)
3597-3607: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winRemove migration history from this method comment.
Remove the
PR#17839`` migration reference. State the current contract directly:content_lengthis the context window, and `max_output` is the generation cap.As per coding guidelines, “keep package and doc comments aligned with the current runtime path rather than migration history.”
🤖 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/service/model_service.go` around lines 3597 - 3607, Update the ResolveModelContextLength doc comment to remove the PR `#17839` migration reference and describe the current contract directly: content_length is the total context window, while max_output is the generation cap. Preserve the remaining behavior and resolution details.Source: Coding guidelines
internal/agent/component/llm_test.go (1)
268-277: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAttach each test description to its test.
Lines 268-272 describe
TestLLM_Invoke_UUIDModel_ResolvesContentLength, but the next declaration isTestLLM_Invoke_UUIDModel_CustomContextOverride. Move lines 268-272 above line 336. Keep lines 273-277 above the custom-override test.As per coding guidelines, remove stale documentation instead of preserving it.
🤖 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/agent/component/llm_test.go` around lines 268 - 277, Move the comment describing TestLLM_Invoke_UUIDModel_ResolvesContentLength so it immediately precedes that test’s declaration near line 336, while keeping the TestLLM_Invoke_UUIDModel_CustomContextOverride description directly above its test. Remove the misplaced duplicate or stale comment rather than retaining it.Source: Coding guidelines
🤖 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/dao/model_context_test.go`:
- Around line 140-143: Update the custom-model assertion in
ResolveModelContentLength to exercise the no-override path: clear the existing
max_tokens Extra value or use a separately seeded custom model without Extra,
then expect 0 instead of 32000. Keep the existing override assertion covered
separately.
In `@internal/dao/model_context.go`:
- Around line 21-22: Update modelExtraMaxTokens to accept positive max_tokens
values represented as either JSON numbers or numeric strings, preserving the
existing catalog/8192 fallback for invalid or non-positive values. Add coverage
for the string-form value, including tenant override behavior.
---
Nitpick comments:
In `@internal/agent/component/llm_test.go`:
- Around line 268-277: Move the comment describing
TestLLM_Invoke_UUIDModel_ResolvesContentLength so it immediately precedes that
test’s declaration near line 336, while keeping the
TestLLM_Invoke_UUIDModel_CustomContextOverride description directly above its
test. Remove the misplaced duplicate or stale comment rather than retaining it.
In `@internal/service/model_service.go`:
- Around line 3597-3607: Update the ResolveModelContextLength doc comment to
remove the PR `#17839` migration reference and describe the current contract
directly: content_length is the total context window, while max_output is the
generation cap. Preserve the remaining behavior and resolution details.
🪄 Autofix
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: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 4dd55ea7-604e-4242-babe-ff9d12aff239
📒 Files selected for processing (6)
internal/agent/component/llm.gointernal/agent/component/llm_test.gointernal/dao/model_context.gointernal/dao/model_context_test.gointernal/ingestion/component/extractor.gointernal/service/model_service.go
ResolveModelContentLength now takes tenantID and resolves the tenant_model row before any catalog read: an active row's extra["max_tokens"] override wins over the provider catalog's content_length (Python model_extra parity), and catalog content_length is only a fallback. Custom/local models no longer fall back to 8192 in the agent or skip fitting in the extractor. service.ModelProviderService.ResolveModelContextLength delegates to the shared dao resolver; the parallel modelContextLengthByID/ByName implementations are removed (one resolution path). Callers pass tenantID: the extractor already has it, and the agent LLM component reads it from canvas state. Tests cover UUID/composite/custom-model overrides, invalid/zero overrides falling back to the catalog, inactive rows, and an end-to-end agent test proving the override drives trimming.
- modelExtraMaxTokens accepts both JSON numbers and numeric strings for extra.max_tokens (some callers persist it as a string); non-numeric strings still fall back to the catalog. - TestResolveModelContentLength_CustomModelExtraComposite now actually exercises the no-override path (clears extra, expects 0) instead of repeating the override assertion. - ResolveModelContextLength doc comment drops the migration-history reference; llm_test.go comment blocks are attached to their own tests.
7d62780 to
ad46681
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
internal/dao/model_context_test.go (1)
209-228: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winTest the inactive composite fallback that the comment describes.
The test uses a UUID reference and correctly expects
0. It does not execute the composite/catalog fallback described in Lines 209-211. Add an assertion for"gpt-4o@OpenAI"with"tenant-1"that expects the catalog value128000, or revise the comment to describe UUID-only behavior.As per coding guidelines, “Preserve still-valid behavior with focused tests.”
🤖 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/dao/model_context_test.go` around lines 209 - 228, The test TestResolveModelContentLength_InactiveTenantModel currently covers only the UUID path, not the composite/catalog fallback described by its comment. Add a focused assertion calling ResolveModelContentLength with model reference "gpt-4o@OpenAI" and tenant "tenant-1", expecting the catalog value 128000, while preserving the existing UUID assertion.Source: Coding guidelines
🤖 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/dao/model_context_test.go`:
- Around line 91-108: Update UUID resolution in lookupTenantModel to require the
matched TenantModelProvider.TenantID to equal the requested tenant ID, rather
than using an unscoped GetByID result. In
TestResolveModelContentLength_ExtraOverrideUUID and related positive UUID
assertions, pass "tenant-1"; add a test asserting the same UUID returns 0 for
"tenant-2" when no driver fallback is provided.
---
Nitpick comments:
In `@internal/dao/model_context_test.go`:
- Around line 209-228: The test
TestResolveModelContentLength_InactiveTenantModel currently covers only the UUID
path, not the composite/catalog fallback described by its comment. Add a focused
assertion calling ResolveModelContentLength with model reference "gpt-4o@OpenAI"
and tenant "tenant-1", expecting the catalog value 128000, while preserving the
existing UUID assertion.
🪄 Autofix
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: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 863a451c-4374-4baf-b8a1-e8c3238d55ab
📒 Files selected for processing (4)
internal/agent/component/llm_test.gointernal/dao/model_context.gointernal/dao/model_context_test.gointernal/service/model_service.go
🚧 Files skipped from review as they are similar to previous changes (2)
- internal/service/model_service.go
- internal/dao/model_context.go
lookupTenantModel resolved a UUID with an unscoped GetByID, so a caller holding another tenant's UUID could read that tenant's per-model max_tokens override (and tenant-row catalog read). Require the tenant_model row's provider to belong to the requested tenantID. UUID-positive dao tests now pass the owning tenant id, and a new TestResolveModelContentLength_UUIDCrossTenantRejected asserts the same UUID resolves to 0 for an unrelated tenant.
…ite, robust parsing Address the PR review regressions: - UUID resolution is unscoped again (globally unique, unguessable, and shared/joined-tenant models are legitimate — matching the old service behavior and Python's get_model_config_by_id), fixing the broken service unit test and the joined-tenant context-length regression. - splitCompositeModelRef now recombines 4+ '@'-separated segments into the model name (preserving embedded '@', e.g. LM Studio name@quant@lmstudio@LM-Studio), so the service path no longer degrades multi-segment chat model refs to 0. - modelExtraMaxTokens parses via json.Number, accepting int, float, and numeric-string forms. - Composite override lookup requires an active instance (provider has no status field; tenant+provider is unique-indexed). New coverage: service override (UUID + composite), agent composite override, extractor end-to-end override, inactive-instance composite fallback to catalog, multi-segment splitter behavior. Cross-tenant rejection test removed (behavior intentionally unscoped).
A composite reference whose model row is inactive applies neither the override nor the tenant-row catalog read and falls back to the catalog by reference parts (gpt-4o -> 128000).
|
LGTM |
Summary
Make
ResolveModelContentLengthhonor the per-model custom context window length (content_length) — stored in the Python-legacytenant_model.extra["max_tokens"]field, whose semantic meaning is the context window, NOT the generation cap — before any provider-catalog read, and remove the parallel service-layer implementation so every consumer shares one resolution path.Why
Python resolves a chat model's context window as
model_extra.get("max_tokens") or 8192— the per-model "max_tokens" a user configures IS the context window length (content_length/ Pythonmax_length), not the output cap. Go ignored that override: custom/local models (not in the catalog) resolved to 0, so the agent silently fell back to the 8192 budget (truncating large contexts) and the extractor skipped fitting entirely (D22). The service layer also had a duplicate catalog-only resolver feeding the knowledge compiler (RAPTOR/dedup/merge) (D23).Changes
internal/dao/model_context.go:ResolveModelContentLengthnow takestenantIDand resolves thetenant_modelrow (UUID or composite via tenant provider/instance/model) before any catalog read:extra["max_tokens"] > 0; the field name is a Python legacy for the context window, i.e.content_length) → that override wins;content_length(the context window length, via the tenant row, the composite reference parts, or resolved driver + model name).internal/service/model_service.go:ResolveModelContextLengthdelegates to the shared dao resolver;modelContextLengthByID/modelContextLengthByName(parallel catalog-only implementations) are removed.internal/ingestion/component/extractor.go:extractorContextLengthpassestenantID.internal/agent/component/llm.go: agent readstenant_idfrom canvas state and passes it.Testing
TestLLM_Invoke_UUIDModel_CustomContextOverrideproves a 2000-token context-window override drives trimming (not gpt-4o's 128k catalog content_length); existing content_length/fallback tests still pass.bash build.sh --test ./internal/dao/... ./internal/agent/component/... ./internal/ingestion/component/...and service/ingestion-task compiles pass.Note
Branched from
pr/3-agent-content-length(the #18093 agent content_length work) so the agent caller is included; once #18093 merges, this PR's diff shrinks to the resolver/service/extractor changes. D24 (chat_pipeline using catalogMaxOutputas context) is tracked separately.Review updates (2026-08-12)
get_model_config_by_id). This also fixes a service unit test that passes a user id as the tenant id.splitCompositeModelRefrecombines 4+@-separated segments into the model name (LM Studio stylename@quant@provider@...), preserving the old serviceparseModelNamecapability.modelExtraMaxTokensparses viajson.Number(int / float / numeric string).Known limitations / follow-ups
extra["max_tokens"]semantics are not yet unified across Go:internal/ingestion/component/dispatch_model.gostill interprets the same field as amax_output(generation cap) override, while this PR (and Python) treat it as the context window length (content_length). A follow-up should reconcile that path.findSoleActiveProviderInstance); a 2-segmentmodel@providerref on a tenant whose default instance is not literally nameddefaultwill fall back to the catalog length.MaxOutputas the context window) is tracked separately in the extractor bug notes.