From 14afa7e8ca2be2537263b78c8a91dca8f767cd06 Mon Sep 17 00:00:00 2001 From: CyberSecurityUP Date: Wed, 4 Nov 2026 20:34:56 -0300 Subject: [PATCH] feat(harness,cli): expose model sampling temperature Temperature was hardcoded to 0.2 in the API chat body. Add a temperature_for() helper reading NEUROSPLOIT_TEMPERATURE (clamped to 0.0..=2.0, default 0.2) and a global --temperature CLI flag that sets it before dispatch, so both run and the REPL honour it. HTTP API path only; subscription-CLI backends expose no temperature knob. Closes #49 --- neurosploit-rs/app/src/main.rs | 15 +++++++ neurosploit-rs/crates/harness/src/models.rs | 48 ++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/neurosploit-rs/app/src/main.rs b/neurosploit-rs/app/src/main.rs index d444adc..ed1a27f 100644 --- a/neurosploit-rs/app/src/main.rs +++ b/neurosploit-rs/app/src/main.rs @@ -102,6 +102,12 @@ struct Cli { /// box). Default: whichever is configured. See tools/laya_shim.py. #[arg(long = "decision-backend", global = true)] decision_backend: Option, + /// Sampling temperature for model API calls (0.0..=2.0, default 0.2). Lower + /// is more deterministic/consistent, higher more exploratory (e.g. 0.0 fully + /// deterministic, 0.7 creative). Applies to the HTTP API path only; the + /// subscription-CLI backends (claude/codex/…) expose no temperature knob. + #[arg(long = "temperature", global = true)] + temperature: Option, } #[derive(Subcommand)] @@ -816,6 +822,15 @@ async fn main() -> anyhow::Result<()> { Some(ref m) if m == "on" || m == "true" || m == "1" || m == "auto" => std::env::set_var("NEUROSPLOIT_TYPESAFE", "on"), _ => {} } + // Sampling temperature → NEUROSPLOIT_TEMPERATURE, read at call time by the + // model client (so both `run` and the REPL honour it). Set before dispatch. + if let Some(t) = cli.temperature { + if (0.0..=2.0).contains(&t) { + std::env::set_var("NEUROSPLOIT_TEMPERATURE", t.to_string()); + } else { + eprintln!(" \x1b[33m⚠ --temperature {t} out of range (0.0..=2.0) — ignoring, using default 0.2\x1b[0m"); + } + } // No subcommand → launch the Claude-Code-style interactive session. let cmd = match cli.cmd.take() { diff --git a/neurosploit-rs/crates/harness/src/models.rs b/neurosploit-rs/crates/harness/src/models.rs index c117edc..503f7db 100644 --- a/neurosploit-rs/crates/harness/src/models.rs +++ b/neurosploit-rs/crates/harness/src/models.rs @@ -190,7 +190,7 @@ impl ChatClient { let body = serde_json::json!({ "model": m.model, "max_tokens": max_tokens_for(&m.model), - "temperature": 0.2, + "temperature": temperature_for(), "messages": [ {"role": "system", "content": system}, {"role": "user", "content": user} @@ -951,6 +951,23 @@ impl Default for ChatClient { } } +/// Sampling temperature for an API chat completion. The default (0.2) keeps the +/// agent consistent and low-"creativity"; raise it for more exploratory runs +/// (0.7) or drop it to 0.0 for deterministic output. Override with +/// `NEUROSPLOIT_TEMPERATURE` (the `--temperature` CLI flag sets this). Values +/// outside the accepted 0.0..=2.0 range are ignored and fall back to the default. +/// +/// Only the HTTP API path honours this; the subscription-CLI backends +/// (`chat_claude_stream` / `chat_codex_stream`) drive agentic CLIs that expose +/// no temperature knob. +fn temperature_for() -> f32 { + std::env::var("NEUROSPLOIT_TEMPERATURE") + .ok() + .and_then(|v| v.trim().parse::().ok()) + .filter(|&v| (0.0..=2.0).contains(&v)) + .unwrap_or(0.2) +} + /// Default output budget for a chat completion, keyed off the model family. /// /// Reasoning models (GLM, DeepSeek, Qwen "thinking", the o-series, …) spend a @@ -1113,6 +1130,35 @@ mod response_tests { } } + #[test] + fn temperature_defaults_and_overrides() { + // Env var drives the whole process, so keep this test self-contained and + // restore the prior value to avoid cross-test interference. + let prev = std::env::var("NEUROSPLOIT_TEMPERATURE").ok(); + + std::env::remove_var("NEUROSPLOIT_TEMPERATURE"); + assert_eq!(temperature_for(), 0.2, "default temperature"); + + std::env::set_var("NEUROSPLOIT_TEMPERATURE", "0.0"); + assert_eq!(temperature_for(), 0.0, "deterministic override"); + + std::env::set_var("NEUROSPLOIT_TEMPERATURE", "0.7"); + assert_eq!(temperature_for(), 0.7, "exploratory override"); + + // Out-of-range and garbage fall back to the default. + std::env::set_var("NEUROSPLOIT_TEMPERATURE", "2.5"); + assert_eq!(temperature_for(), 0.2, "above range → default"); + std::env::set_var("NEUROSPLOIT_TEMPERATURE", "-1"); + assert_eq!(temperature_for(), 0.2, "below range → default"); + std::env::set_var("NEUROSPLOIT_TEMPERATURE", "hot"); + assert_eq!(temperature_for(), 0.2, "unparseable → default"); + + match prev { + Some(v) => std::env::set_var("NEUROSPLOIT_TEMPERATURE", v), + None => std::env::remove_var("NEUROSPLOIT_TEMPERATURE"), + } + } + #[test] fn extracts_plain_string_content() { let msg = json!({"content": "hello"});