Skip to content

feat(libsy): make LlmClassifier recent_turn_window configurable - #156

Open
sabhatinas wants to merge 1 commit into
mainfrom
sabhatinas/switch-1110-classifier-window
Open

feat(libsy): make LlmClassifier recent_turn_window configurable#156
sabhatinas wants to merge 1 commit into
mainfrom
sabhatinas/switch-1110-classifier-window

Conversation

@sabhatinas

@sabhatinas sabhatinas commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

LlmClassifier's classifier-prompt window (recent_turn_window — first user message + last N turns) was hardcoded to 5 (trim_messages(..., 5)). This makes it user-configurable, closing a parity gap with the v2 ClassifierConfig.recent_turn_window.

  • LlmClassifier: new recent_turn_window field + DEFAULT_RECENT_TURN_WINDOW = 5 + with_recent_turn_window(n) builder (non-breaking; new() signature unchanged); call site uses self.recent_turn_window.
  • Server config: recent_turn_window: Option<usize> on the llm_classifier route type, threaded to the classifier when set (unset → default 5).
  • Test: builder default + override.

libsy + server build clean; cargo fmt + clippy clean; libsy + server tests pass.

Linear: SWITCH-1110 (parity work item)

Summary by CodeRabbit

  • New Features
    • Added configurable trailing conversation context for LLM classification prompts.
    • Added support for setting the recent-turn window through server TOML routing configuration.
    • Existing configurations continue using the default context window when no value is provided.

Signed-off-by: Sabhatina Selvam <sabhatinas@nvidia.com>
@sabhatinas
sabhatinas requested a review from a team as a code owner July 27, 2026 21:49
@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

The classifier now supports configurable trailing prompt context. A default is defined in LlmClassifier, an override builder is available, prompt trimming uses the configured value, and server TOML routing can optionally supply it.

Changes

Classifier Context Configuration

Layer / File(s) Summary
Classifier API and prompt trimming
crates/libsy/src/algorithms/llm_class.rs
LlmClassifier defaults and exposes recent_turn_window; classifier prompt trimming uses that value, with unit-test coverage.
Server route configuration wiring
crates/switchyard-server/src/config.rs
The llm_classifier route accepts an optional TOML setting and applies it during algorithm construction.

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

Poem

I’m a bunny with a prompt-sized tune,
Trimming old turns beneath the moon.
Five was fixed, but settings now steer,
Fresh context hops much nearer.
Config hops in—what a clever idea!

🚥 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 summarizes the main change: making LlmClassifier's recent_turn_window configurable.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

🧹 Nitpick comments (2)
crates/switchyard-server/src/config.rs (1)

275-285: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Add coverage for the TOML-to-classifier configuration contract.

The construction wiring is correct, but no corresponding server configuration test is shown. Test an omitted field (internal default) and an explicit value (overridden prompt trimming) so deserialization or wiring regressions are caught.

🤖 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 `@crates/switchyard-server/src/config.rs` around lines 275 - 285, Add server
configuration tests around the LlmClassifier construction path to cover the
TOML-to-classifier contract: verify an omitted recent_turn_window uses the
classifier’s internal default, and verify an explicitly configured value is
propagated and overrides prompt trimming. Reuse the existing config parsing and
classifier inspection/testing patterns rather than changing the construction
logic.
crates/libsy/src/algorithms/llm_class.rs (1)

299-305: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Test prompt trimming, not only field assignment.

This test would pass even if create_run_task ignored the configured value and continued using a hardcoded window. Add a multi-message test that inspects the classifier request for both the default and overridden windows, including the intended zero-window behavior.

🤖 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 `@crates/libsy/src/algorithms/llm_class.rs` around lines 299 - 305, Expand
recent_turn_window_defaults_and_is_configurable to exercise create_run_task with
a multi-message conversation and inspect the generated classifier request.
Verify the default window, an overridden window of 2, and a zero-window
configuration each trim the prompt to the expected messages, ensuring the
configured value—not a hardcoded constant—is used.
🤖 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 `@crates/libsy/src/algorithms/llm_class.rs`:
- Around line 100-105: Update the docstring for with_recent_turn_window to state
that a zero window includes the first user message and, when available, the
latest user message. Add a prompt-level test covering
with_recent_turn_window(0), verifying both messages are included when distinct.

---

Nitpick comments:
In `@crates/libsy/src/algorithms/llm_class.rs`:
- Around line 299-305: Expand recent_turn_window_defaults_and_is_configurable to
exercise create_run_task with a multi-message conversation and inspect the
generated classifier request. Verify the default window, an overridden window of
2, and a zero-window configuration each trim the prompt to the expected
messages, ensuring the configured value—not a hardcoded constant—is used.

In `@crates/switchyard-server/src/config.rs`:
- Around line 275-285: Add server configuration tests around the LlmClassifier
construction path to cover the TOML-to-classifier contract: verify an omitted
recent_turn_window uses the classifier’s internal default, and verify an
explicitly configured value is propagated and overrides prompt trimming. Reuse
the existing config parsing and classifier inspection/testing patterns rather
than changing the construction logic.
🪄 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: Enterprise

Run ID: 494804e8-b4b1-4358-8e21-0da5e22debd5

📥 Commits

Reviewing files that changed from the base of the PR and between 239a413 and ab2aa04.

📒 Files selected for processing (2)
  • crates/libsy/src/algorithms/llm_class.rs
  • crates/switchyard-server/src/config.rs

Comment on lines +100 to +105
/// Set the number of trailing conversation turns included in the classifier
/// prompt (default [`DEFAULT_RECENT_TURN_WINDOW`]). `0` sends only the first
/// user message.
#[must_use]
pub fn with_recent_turn_window(mut self, recent_turn_window: usize) -> Self {
self.recent_turn_window = recent_turn_window;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Map the file structure and inspect the relevant Rust source.
git ls-files crates/libsy/src/algorithms/llm_class.rs crates/libsy/src/algorithms | cat
wc -l crates/libsy/src/algorithms/llm_class.rs
sed -n '1,220p' crates/libsy/src/algorithms/llm_class.rs
printf '\n--- TESTS ---\n'
sed -n '220,380p' crates/libsy/src/algorithms/llm_class.rs

Repository: NVIDIA-NeMo/Switchyard

Length of output: 14296


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Search for zero-window handling and related tests elsewhere in the crate.
rg -n "recent_turn_window|trim_messages|first user message|latest user message|window 0|0 sends" crates/libsy/src

Repository: NVIDIA-NeMo/Switchyard

Length of output: 1688


Update the zero-window contract
with_recent_turn_window(0) keeps the first user message and, when present, the latest user message. Update the docstring and add a prompt-level test for that case.

🤖 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 `@crates/libsy/src/algorithms/llm_class.rs` around lines 100 - 105, Update the
docstring for with_recent_turn_window to state that a zero window includes the
first user message and, when available, the latest user message. Add a
prompt-level test covering with_recent_turn_window(0), verifying both messages
are included when distinct.

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.

3 participants