Skip to content

Gate AgentSession.system_prompt_override behind testing feature (closes #1093) - #1132

Merged
malibio merged 2 commits into
mainfrom
feature/issue-1093-system-prompt-override-testing-feature
May 17, 2026
Merged

malibio merged 2 commits into
mainfrom
feature/issue-1093-system-prompt-override-testing-feature

Conversation

@malibio

@malibio malibio commented May 17, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Adds a testing Cargo feature on nodespace-agent and gates AgentSession.system_prompt_override, LocalAgentService::set_system_prompt, and the override branch in run_turn behind #[cfg(any(test, feature = "testing"))].
  • Production builds (no features) compile without the field, the method, or the override branch — so the test-only hook never reaches the Tauri serialization layer or the production prompt-assembly path.
  • tests/ollama_integration.rs is declared as [[test]] required-features = ["testing"], so cargo test -p nodespace-agent skips it cleanly without the feature and builds/runs it under cargo 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 on AgentSession. A testing Cargo 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, 22m23s
  • cargo check -p nodespace-agent --features testing --tests — clean, 5m15s, proves the feature-gated code and ollama_integration.rs compile
  • bun run quality:fix — passes clean (eslint 0 errors, svelte-check 0 warnings, cargo fmt + cargo clippy -D warnings across the workspace)
  • Frontend test baseline: 128 files / 3918 tests passed (unchanged — no frontend touched)

Test plan

  • cargo build -p nodespace-agent (no features) compiles cleanly
  • cargo check -p nodespace-agent --features testing --tests compiles cleanly
  • bun run quality:fix clean across workspace
  • Frontend tests unchanged from baseline (no frontend touched)
  • Reviewer: confirm Tauri-serialized AgentSession no longer carries system_prompt_override in production (the field doesn't exist in the prod build)

🤖 Generated with Claude Code

#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 malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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:

  1. Tauri boundary is outbound-only. Grep across packages/desktop-app/src-tauri/ shows AgentSession appears only as a return type (Result<Vec<AgentSession>, CommandError> in local_agent_get_sessions at local_agent.rs:572) and never as a Tauri command parameter. The TS interface at packages/desktop-app/src/lib/types/agent-types.ts:312-319 already omits system_prompt_override, consistent with skip_serializing_if = "Option::is_none". Gating the field out of prod cannot cause a deserialization failure because no production code path deserializes AgentSession from JSON.

  2. No other crate consumes the agent's testing feature. packages/desktop-app/src-tauri/Cargo.toml:36 depends on nodespace-agent without features, so the production Tauri build will have the field/method/branch entirely absent. Good.

  3. #[cfg(test)] mod tests at agent_loop.rs:990 is unaffected. The test helper new_session() at line 1197 still sets system_prompt_override: None — that compiles because cfg(test) is always true under cargo test --lib, so the field is present. No change needed.

  4. CI/workflow impact is nil. .github/workflows/release.yml doesn't run cargo test. package.json:48 defines rust:test as cargo test --lib -p nodespace-core (scoped to the core crate, not agent), so nothing in the standard developer workflow expects ollama_integration.rs to build under bare cargo test. The required-features declaration correctly causes --all-targets clippy to skip — empirically verified by the clean bun run quality:fix run cited in the PR description.

  5. 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.

  6. 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 on LocalAgentService, and the override-consumption branch is compiled out of run_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]>
@malibio

malibio commented May 17, 2026

Copy link
Copy Markdown
Collaborator Author

Review feedback addressed

Pushed 106687cf to address the review.

Changes made

✅ 🟡 Improvement — Duplicated cfg branches for base_prompt (packages/agent/src/local_agent/agent_loop.rs)

Introduced a small file-level helper session_prompt_override(&AgentSession) -> Option<&str> with two cfg variants:

  • Under cfg(any(test, feature = "testing")): returns session.system_prompt_override.as_deref()
  • Under cfg(not(...)): returns None

The call site in run_turn now has a single override → assembler → fallback chain. The duplicated assembler/fallback logic that the reviewer flagged as a drift hazard is gone.

Skipped (per reviewer's own guidance)

⏭️ 🟢 Nit — Feature naming (testing vs test-hooks/test-support)
Reviewer explicitly noted "Not worth changing for this PR — flagging only because future contributors may add a second feature." No change.

⏭️ 🟢 Nit — Doc comment phrasing on the gate
Reviewer explicitly noted "current wording is accurate for the production concern, which is what the doc cares about." No change.

Verification

  • cargo clippy --all-targets -- -D warnings (no features) — clean
  • cargo clippy -p nodespace-agent --features testing --tests -- -D warnings — clean
  • bun run quality:fix — clean

Diff for this commit: +17/-13 across one file.

@malibio
malibio merged commit a992b3d into main May 17, 2026
@malibio
malibio deleted the feature/issue-1093-system-prompt-override-testing-feature branch May 17, 2026 16:26
malibio added a commit that referenced this pull request Jun 22, 2026
#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]>
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.

1 participant