Repository navigation
Gate AgentSession.system_prompt_override behind testing feature (closes #1093) - #1132
Conversation
#1093) `system_prompt_override` and the `set_system_prompt` method were test infrastructure embedded in a production, Tauri-serialized struct. They are now gated by a `testing` Cargo feature on `nodespace-agent`, so production builds compile without the field, the method, or the override branch in `run_turn`. Integration tests in `tests/ollama_integration.rs` link against the library in non-test mode, which is why a bare `#[cfg(test)]` is not sufficient. The new `testing` feature, plus `required-features` on the `[[test]]` declaration, makes the test-only hook compile only when the feature is enabled and the integration binary only build under `cargo test --features testing`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
malibio
left a comment
There was a problem hiding this comment.
Code Review Summary
Recommendation: APPROVE
This is a small, focused, well-justified change that does exactly what issue #1093 asks for: it moves test-only infrastructure (system_prompt_override, set_system_prompt, and the override-consumption branch) off the production domain model and behind a Cargo feature flag. The chosen approach (#4 from the issue) is the conventional Rust idiom for "this hook needs to be visible to tests/ integration binaries but absent from production." Diff is +39/-1 across three files, no API surface changes for production consumers, no migration concerns, and bun run quality:fix already exercised cargo clippy --all-targets -D warnings cleanly with the feature off — which proves Cargo's required-features correctly skips ollama_integration.rs rather than failing to build it.
Findings
Critical Issues
None.
Suggested Improvements
🟡 [Improvement] packages/agent/src/local_agent/agent_loop.rs:238-260 — Duplicated cfg branches for base_prompt.
The current approach duplicates the entire assembler → fallback chain across two cfg arms:
#[cfg(any(test, feature = "testing"))]
let base_prompt = if let Some(ref override_prompt) = session.system_prompt_override {
override_prompt.clone()
} else if let Some(ref assembler) = self.prompt_assembler {
assembler.assemble(&template_ctx, all_tools.clone()).await.system_prompt
} else {
prompt_templates::fallback_system_prompt(dynamic_ctx)
};
#[cfg(not(any(test, feature = "testing")))]
let base_prompt = if let Some(ref assembler) = self.prompt_assembler {
assembler.assemble(&template_ctx, all_tools.clone()).await.system_prompt
} else {
prompt_templates::fallback_system_prompt(dynamic_ctx)
};A small accessor with two cfg variants collapses this back to a single branch and removes the duplicated assembler/fallback logic — which matters because the duplication will silently drift if either branch is edited without the other:
// Near AgentSession or as a free fn in this module:
#[cfg(any(test, feature = "testing"))]
fn session_prompt_override(s: &AgentSession) -> Option<&str> {
s.system_prompt_override.as_deref()
}
#[cfg(not(any(test, feature = "testing")))]
fn session_prompt_override(_: &AgentSession) -> Option<&str> { None }
// Then at the call site, one chain only:
let base_prompt = if let Some(override_prompt) = session_prompt_override(session) {
override_prompt.to_string()
} else if let Some(ref assembler) = self.prompt_assembler {
assembler.assemble(&template_ctx, all_tools.clone()).await.system_prompt
} else {
prompt_templates::fallback_system_prompt(dynamic_ctx)
};Rationale (DRY): the assembler/fallback fallback chain is identical in both arms — only the override prelude differs. The compiler will optimize the None-returning prod variant to a no-op, so there's no runtime cost. This is a small refactor and not a merge blocker, but it eliminates a real maintenance hazard.
Nitpicks
🟢 Nit: packages/agent/Cargo.toml:34-41 — Feature comment is excellent but consider naming.
testing is a common name and works fine here; some crates in the broader ecosystem prefer test-hooks or test-support to make it unambiguous that enabling this feature widens the API surface beyond what production code sees. Not worth changing for this PR — flagging only because future contributors may add a second feature and want a naming pattern to follow.
🟢 Nit: packages/agent/src/agent_types.rs:543-549 — Doc comment phrasing.
The doc now says "Gated by the testing feature so the field does not exist in production builds and never reaches the Tauri serialization layer." Strictly speaking the gate is any(test, feature = "testing"), so #[cfg(test)] library tests also see it. Minor — current wording is accurate for the production concern, which is what the doc cares about.
Verification Notes
I confirmed the load-bearing assumptions from the review brief:
-
Tauri boundary is outbound-only. Grep across
packages/desktop-app/src-tauri/showsAgentSessionappears only as a return type (Result<Vec<AgentSession>, CommandError>inlocal_agent_get_sessionsatlocal_agent.rs:572) and never as a Tauri command parameter. The TS interface atpackages/desktop-app/src/lib/types/agent-types.ts:312-319already omitssystem_prompt_override, consistent withskip_serializing_if = "Option::is_none". Gating the field out of prod cannot cause a deserialization failure because no production code path deserializesAgentSessionfrom JSON. -
No other crate consumes the agent's
testingfeature.packages/desktop-app/src-tauri/Cargo.toml:36depends onnodespace-agentwithout features, so the production Tauri build will have the field/method/branch entirely absent. Good. -
#[cfg(test)] mod testsatagent_loop.rs:990is unaffected. The test helpernew_session()at line 1197 still setssystem_prompt_override: None— that compiles becausecfg(test)is always true undercargo test --lib, so the field is present. No change needed. -
CI/workflow impact is nil.
.github/workflows/release.ymldoesn't runcargo test.package.json:48definesrust:testascargo test --lib -p nodespace-core(scoped to the core crate, not agent), so nothing in the standard developer workflow expectsollama_integration.rsto build under barecargo test. Therequired-featuresdeclaration correctly causes--all-targetsclippy to skip — empirically verified by the cleanbun run quality:fixrun cited in the PR description. -
CLAUDE.md compliance. No
#[deprecated], no#[allow(dead_code)], no compatibility shims. The PR deletes the old "always-present field" design rather than wrapping it. -
Acceptance criteria from #1093. The issue's stated intent — "move test infrastructure off the production domain model" — is delivered: in a production build, the field doesn't exist on
AgentSession, the method doesn't exist onLocalAgentService, and the override-consumption branch is compiled out ofrun_turn.
The Improvement above is genuinely worth doing (DRY hazard on the duplicated chain), but it's not a merge blocker — the PR is a clear net positive in its current form and unblocks the intent of #1093 immediately.
Per the 🟡 finding on PR #1132: replace the two duplicated assembler → fallback chains with a single chain that consults a cfg-variant accessor `session_prompt_override`. The accessor returns `None` in production and the override (when set) in test/feature builds, so the override → assembler → fallback logic now lives in one place and cannot silently drift. Skipped the two 🟢 nits per the reviewer's own guidance: - Feature name `testing` (reviewer: "Not worth changing for this PR") - Doc phrasing on the gate (reviewer: "current wording is accurate for the production concern") Verification: `cargo clippy --all-targets -- -D warnings` (no features) and `cargo clippy -p nodespace-agent --features testing --tests -- -D warnings` both pass clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Review feedback addressedPushed Changes made✅ 🟡 Improvement — Duplicated cfg branches for Introduced a small file-level helper
The call site in Skipped (per reviewer's own guidance)⏭️ 🟢 Nit — Feature naming ( ⏭️ 🟢 Nit — Doc comment phrasing on the gate Verification
Diff for this commit: +17/-13 across one file. |
#1093) (#1132) * Gate AgentSession system_prompt_override behind testing feature (closes #1093) `system_prompt_override` and the `set_system_prompt` method were test infrastructure embedded in a production, Tauri-serialized struct. They are now gated by a `testing` Cargo feature on `nodespace-agent`, so production builds compile without the field, the method, or the override branch in `run_turn`. Integration tests in `tests/ollama_integration.rs` link against the library in non-test mode, which is why a bare `#[cfg(test)]` is not sufficient. The new `testing` feature, plus `required-features` on the `[[test]]` declaration, makes the test-only hook compile only when the feature is enabled and the integration binary only build under `cargo test --features testing`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * Address review: collapse cfg-split base_prompt via accessor Per the 🟡 finding on PR #1132: replace the two duplicated assembler → fallback chains with a single chain that consults a cfg-variant accessor `session_prompt_override`. The accessor returns `None` in production and the override (when set) in test/feature builds, so the override → assembler → fallback logic now lives in one place and cannot silently drift. Skipped the two 🟢 nits per the reviewer's own guidance: - Feature name `testing` (reviewer: "Not worth changing for this PR") - Doc phrasing on the gate (reviewer: "current wording is accurate for the production concern") Verification: `cargo clippy --all-targets -- -D warnings` (no features) and `cargo clippy -p nodespace-agent --features testing --tests -- -D warnings` both pass clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> --------- Co-authored-by: Michael Libio <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Summary
testingCargo feature onnodespace-agentand gatesAgentSession.system_prompt_override,LocalAgentService::set_system_prompt, and the override branch inrun_turnbehind#[cfg(any(test, feature = "testing"))].tests/ollama_integration.rsis declared as[[test]] required-features = ["testing"], socargo test -p nodespace-agentskips it cleanly without the feature and builds/runs it undercargo test -p nodespace-agent --features testing.Why this approach
Integration tests in
tests/link against the library as a non-#[cfg(test)]crate, which is why the originally-flagged design (per PR #1091 review) put the field directly onAgentSession. AtestingCargo feature is the conventional Rust solution: the gate is visible to integration tests when the feature is enabled, but absent from production builds entirely. No wrapper indirection, no API churn, minimal diff.Verification
cargo build -p nodespace-agent(no features) — clean, 22m23scargo check -p nodespace-agent --features testing --tests— clean, 5m15s, proves the feature-gated code andollama_integration.rscompilebun run quality:fix— passes clean (eslint 0 errors, svelte-check 0 warnings, cargo fmt + cargo clippy-D warningsacross the workspace)Test plan
cargo build -p nodespace-agent(no features) compiles cleanlycargo check -p nodespace-agent --features testing --testscompiles cleanlybun run quality:fixclean across workspaceAgentSessionno longer carriessystem_prompt_overridein production (the field doesn't exist in the prod build)🤖 Generated with Claude Code