Skip to content

feat: per-user per-model chat compaction threshold overrides#23412

Merged
ibetitsmike merged 32 commits intomainfrom
mike/user-compaction-thresholds
Mar 23, 2026
Merged

feat: per-user per-model chat compaction threshold overrides#23412
ibetitsmike merged 32 commits intomainfrom
mike/user-compaction-thresholds

Conversation

@ibetitsmike
Copy link
Copy Markdown
Collaborator

What

Adds per-user per-model auto-compaction threshold overrides. Users can now customize the percentage of context window usage that triggers chat compaction, independently for each enabled model.

Why

The compaction threshold was previously only configurable at the deployment level (chat_model_configs.compression_threshold). Different users have different preferences — some want aggressive compaction to keep costs low, others prefer higher thresholds to retain more context. This gives users control without requiring admin intervention.

Architecture

Storage: Reuses the existing user_configs table (no migration needed). Overrides are stored as key/value pairs with keys shaped chat_compaction_threshold:<modelConfigID> and integer percent values.

API: Three new experimental endpoints under /api/experimental/chats/config/:

  • GET /user-compaction-thresholds — list all overrides for the current user
  • PUT /user-compaction-thresholds/{modelConfig} — upsert an override (validates model exists and is enabled, validates 0–100 range)
  • DELETE /user-compaction-thresholds/{modelConfig} — clear an override (idempotent)

Runtime resolution: In coderd/chatd/chatd.go, a new resolveUserCompactionThreshold() helper runs at the start of each chat turn (inside runChat()), after the model config is resolved but before CompactionOptions is built. If a valid override exists, it replaces modelConfig.CompressionThreshold. The threshold source (user_override vs model_default) is logged with each compaction event.

Precedence: effectiveThreshold = userOverride ?? modelConfig.CompressionThreshold

UI: New "Context Compaction" subsection in the Agents → Settings → Behavior tab, placed after Personal Instructions. Shows one row per enabled model with the system default, a number input for the override, and Save/Reset controls.

Testing

  • 9 API subtests covering CRUD, validation (boundary values 0/100, out-of-range rejection), upsert behavior, idempotent delete, user isolation, and non-existent model config
  • 4 dbauthz tests (16 scenarios) verifying ActionReadPersonal / ActionUpdatePersonal on all query methods
  • 4 Storybook stories with play functions (Default, WithOverrides, Loading, Error)
Implementation plan

Phase 1 — Tests

  • Backend API tests in coderd/chats_test.go (9 subtests)
  • Database auth wrapper tests in coderd/database/dbauthz/dbauthz_test.go (4 methods)
  • Frontend stories in UserCompactionThresholdSettings.stories.tsx (4 stories)

Phase 2 — Backend preference surface

  • 4 SQL queries in coderd/database/queries/users.sql (list, get, upsert, delete)
  • make gen to propagate into generated artifacts
  • Auth/metrics wrappers in dbauthz and dbmetrics
  • SDK types and client methods in codersdk/chats.go
  • HTTP handlers and routes in coderd/chats.go and coderd/coderd.go
  • Key prefix constant shared between handlers and runtime

Phase 3 — Runtime override

  • resolveUserCompactionThreshold() helper in coderd/chatd/chatd.go
  • Override injection in runChat() before building CompactionOptions
  • threshold_source field added to compaction log

Phase 4 — Settings UI

  • API client methods and React Query hooks in site/src/api/
  • UserCompactionThresholdSettings component extracted from SettingsPageContent
  • Per-model mutation tracking (only the active row disables during save)
  • 100% warning, "System default" label, helpful empty state copy

Phase 5 — Refactor and review fixes

  • Consolidated key prefix constant in codersdk
  • Explicit PUT range validation (not just struct tags)
  • GET handler gracefully skips malformed rows instead of 500
  • Boundary value, upsert, and non-existent model config tests
  • UX improvements: per-model mutation state, aria-live on errors

@ibetitsmike ibetitsmike marked this pull request as ready for review March 22, 2026 23:54
@ibetitsmike ibetitsmike changed the title feat(coderd): per-user per-model chat compaction threshold overrides feat: per-user per-model chat compaction threshold overrides Mar 22, 2026
@coder-tasks
Copy link
Copy Markdown
Contributor

coder-tasks bot commented Mar 22, 2026

Documentation Check

Updates Needed

  • docs/ai-coder/agents/architecture.md — The "Context compaction" section (line ~86) says the threshold is a fixed setting, but users can now override it per model. Add a note that the threshold can be customized per model in Agents → Settings → Behavior.
  • docs/ai-coder/agents/models.md — The Compression Threshold row in the general options table describes it as the percentage at which compaction triggers, but does not mention it is a system default that individual users can override. Update the description to reflect that users can set per-model overrides in their personal settings.

Automated review via Coder Tasks

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0c7017bce6

ℹ️ 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".

@ibetitsmike ibetitsmike force-pushed the mike/user-compaction-thresholds branch from 0c7017b to 3948c9e Compare March 23, 2026 00:04
@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3948c9e3e6

ℹ️ 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".

@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fafde313b6

ℹ️ 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".

@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

1 similar comment
@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fafde313b6

ℹ️ 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".

Copy link
Copy Markdown
Contributor

@DanielleMaywood DanielleMaywood left a comment

Choose a reason for hiding this comment

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

Unsure on the best practices so converse with your agent, but it feels like I'm expecting to see a <form> somewhere but I'm not.

@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

1 similar comment
@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

ℹ️ 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".

@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5db337a048

ℹ️ 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".

@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

ℹ️ 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".

@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

ℹ️ 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".

@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c30d97c979

ℹ️ 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".

@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2f3ef6f5bc

ℹ️ 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".

@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

@codex review

@ibetitsmike ibetitsmike force-pushed the mike/user-compaction-thresholds branch from 342059c to 3b4a437 Compare March 23, 2026 22:40
@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

ℹ️ 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".

@ibetitsmike ibetitsmike requested a review from mafredri March 23, 2026 23:47
@ibetitsmike ibetitsmike dismissed mafredri’s stale review March 23, 2026 23:48

feedback adressed

@ibetitsmike ibetitsmike merged commit 82f965a into main Mar 23, 2026
29 checks passed
@ibetitsmike ibetitsmike deleted the mike/user-compaction-thresholds branch March 23, 2026 23:48
@github-actions github-actions bot locked and limited conversation to collaborators Mar 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants