phoebus_os/installer-rs/crates/phoebus-ai/src/assistant.rs
Phoebus a224bb72e8
Some checks are pending
ci / test (push) Waiting to run
fix(assistant): handle oversized search investigations
2026-10-03 10:06:32 -07:00

876 lines
34 KiB
Rust

//! Installed conversational assistant: natural request -> validated typed plan.
//!
//! The model is given only bounded system facts and a closed action schema. It
//! returns an [`AssistantPlan`] whose actions deserialize into the same
//! `SystemAction` enum `sys` executes. Nothing here runs with privilege: the
//! caller shows the plan, obtains approval, and the deterministic broker in
//! `phoebus-executor` executes it.
use phoebus_core::action::SystemAction;
use serde::{Deserialize, Serialize};
use crate::install::Evidence;
use crate::provider::{ChatMessage, ChatRequest, ModelTransport, ProviderKind, SystemFacts};
use crate::AiError;
/// One reason per proposed action.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AssistantRationale {
pub action: String,
pub reason: String,
}
/// A validated plan from the model.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AssistantPlan {
pub version: u32,
/// A short, human-readable explanation of the plan.
pub summary: String,
#[serde(default)]
pub actions: Vec<SystemAction>,
#[serde(default)]
pub rationale: Vec<AssistantRationale>,
}
impl AssistantPlan {
pub fn render(&self) -> String {
let mut out = String::new();
out.push_str(&format!("{}\n", self.summary));
if self.actions.is_empty() {
out.push_str("(no system actions proposed)\n");
}
for action in &self.actions {
out.push_str(&format!(" - {}\n", action.describe()));
if let Some(r) = self
.rationale
.iter()
.find(|r| r.action == action_tag(action))
{
out.push_str(&format!(" reason: {}\n", r.reason));
}
}
out
}
}
/// The serde tag of an action (`install_package`, ...).
pub fn action_tag(action: &SystemAction) -> String {
serde_json::to_value(action)
.ok()
.and_then(|v| v.get("action").and_then(|a| a.as_str()).map(str::to_string))
.unwrap_or_default()
}
/// The system prompt for the installed assistant. The action set is closed and
/// there is no shell or file action.
pub const ASSISTANT_PROMPT: &str = "\
You are the Phoebus OS system assistant. You can ONLY use actions from this \
closed set: inspect_service, enable_service, disable_service, start_service, \
stop_service, restart_service, inspect_package, \
install_package, remove_package, search_packages, choose_package, \
set_package_preference, flatpak_install, \
flatpak_remove, flatpak_list, nix_prepare, snap_find, snap_info, snap_install, \
snap_remove, snap_list, snap_refresh, snap_connections, appimage_import, \
appimage_acquire, appimage_update, appimage_remove, appimage_info, appimage_list, \
custom_ebuild_prepare, custom_ebuild_install, custom_ebuild_remove, \
custom_ebuild_list, create_snapshot, undo, rollback_status, history, \
update_system, prepare_updates, inspect_updates, discover_candidates, \
inspect_candidate, check_backend, inspect_installation, list_packages, \
package_info, package_owners, package_dependencies, depclean, clean_cache, \
check_updates, apply_updates.
Each reply is ONE JSON object with version 1 and a `kind`. An action is always \
an object with an `action` field — for example {\"action\":\"list_packages\"} or \
{\"action\":\"enable_service\",\"name\":\"sshd\"} — never a bare string.
1. Investigate (read-only evidence): \
{\"version\":1,\"kind\":\"investigate\",\"requests\":[{\"action\":\"check_backend\",\"backend\":\"flatpak\"}]}. \
Use this to answer search, inventory, information and update-check requests from \
real evidence. Useful tools: search_packages (query, optional backend), \
discover_candidates, inspect_candidate, check_backend, inspect_installation, \
list_packages (optional backend/filter), package_info, check_updates \
(optional backend), package_owners, package_dependencies. Never request a \
mutating action as evidence. Request at most six read-only actions per turn; \
you can ask for more evidence on the next turn.
2. Plan or answer: {\"version\":1,\"summary\":\"...\",\"actions\":\
[{\"action\":\"list_packages\"}],\
\"rationale\":[{\"action\":\"list_packages\",\"reason\":\"...\"}]}. Give every action a \
non-empty reason. For a pure question or a search summary, return an empty \
`actions` list and put the answer in `summary`, grounded ONLY in the evidence \
you were given: never invent package names, versions, descriptions, sizes, \
installation status or availability.
Rules: prefer inspection before mutation. Where an action takes a `kind`, it is \
exactly one of base-system, desktop-app, developer-tool, cli-tool, unclassified; a \
GUI application is kind desktop-app plus a separate `gui` boolean. \
Do not choose a software lane by \
category alone: use choose_package to compare the available lanes against the \
machine (architecture, memory, storage, GPU, Wayland) and the user's \
preferences, or install_package for an explicit Portage request. Base system \
components (kernel, drivers, systemd, the host graphics and audio stack, boot \
tooling) stay under Portage. A custom ebuild is untrusted, executable content: \
stage it with custom_ebuild_prepare so a human reviews the artifact and its \
digest; never assume it will build. Never propose disabling verification, \
enabling ~amd64 globally, weakening LUKS2/Btrfs/systemd/UEFI, or any shell, file \
or arbitrary command action. You do not have a shell, credentials, or access to \
private files. A human reviews and authenticates every mutation.";
/// Parse and validate an assistant plan, fail-closed.
pub fn parse_plan(text: &str) -> Result<AssistantPlan, AiError> {
let value: serde_json::Value = serde_json::from_str(text)?;
let plan: AssistantPlan = serde_json::from_value(value.clone())?;
if plan.version != 1 {
return Err(AiError::UnsupportedVersion(plan.version));
}
if plan.summary.trim().is_empty() {
return Err(AiError::Rejected("plan has no summary".into()));
}
// Enforce the exact action schema. serde's `deny_unknown_fields` does not
// cover internally tagged unit variants, so an action with a smuggled extra
// key would otherwise be accepted on the AI path but rejected by
// `phoebus_core::action::parse_action` on the deterministic path.
if let Some(raw_actions) = value.get("actions").and_then(|a| a.as_array()) {
for (index, action) in plan.actions.iter().enumerate() {
if let Some(raw) = raw_actions.get(index) {
phoebus_core::action::validate_action_value(raw)
.map_err(|e| AiError::Rejected(format!("{}: {e}", action.describe())))?;
}
}
}
for action in &plan.actions {
action
.validate()
.map_err(|e| AiError::Rejected(format!("{}: {e}", action.describe())))?;
let tag = action_tag(action);
let has_reason = plan
.rationale
.iter()
.any(|r| r.action == tag && !r.reason.trim().is_empty());
if !has_reason {
return Err(AiError::Rejected(format!(
"{}: missing a non-empty reason",
action.describe()
)));
}
}
Ok(plan)
}
/// Parse a JSON array of typed actions through the exact schema validator.
fn parse_action_list(value: &serde_json::Value) -> Result<Vec<SystemAction>, AiError> {
let values = value
.as_array()
.ok_or_else(|| AiError::Rejected("expected a JSON array of actions".into()))?;
values
.iter()
.map(|v| {
phoebus_core::action::validate_action_value(v)
.map_err(|e| AiError::Rejected(format!("invalid action: {e}")))
})
.collect()
}
/// Build the chat request for a user instruction plus system facts.
pub fn build_request(
provider: ProviderKind,
model: &str,
facts: &SystemFacts,
instruction: &str,
) -> ChatRequest {
let model = if model.trim().is_empty() {
provider.default_model().to_string()
} else {
model.trim().to_string()
};
let user = format!(
"Bounded system facts (JSON):\n{}\n\nUser request: {instruction}\n\n\
Respond with a version 1 assistant plan.",
facts.render()
);
ChatRequest {
model,
messages: vec![
ChatMessage {
role: "system".into(),
content: ASSISTANT_PROMPT.into(),
},
ChatMessage {
role: "user".into(),
content: user,
},
],
temperature: 0.0,
response_format: Some(crate::provider::ResponseFormat {
kind: "json_object".into(),
}),
}
}
/// The request with a corrective turn appended after a rejected plan.
pub fn build_corrected_request(
provider: ProviderKind,
model: &str,
facts: &SystemFacts,
instruction: &str,
correction: &str,
) -> ChatRequest {
let mut request = build_request(provider, model, facts, instruction);
request.messages.push(ChatMessage {
role: "user".into(),
content: format!(
"Your previous reply was rejected: {correction}. Reply with ONLY the \
corrected JSON object, with no prose and no code fence."
),
});
request
}
/// Ask the provider for a plan and parse it, fail-closed at every step. A
/// rejected reply is retried once with a corrective turn.
pub fn complete_plan(
provider: ProviderKind,
endpoint: &str,
model: &str,
facts: &SystemFacts,
instruction: &str,
transport: &dyn ModelTransport,
) -> Result<AssistantPlan, AiError> {
if provider == ProviderKind::None {
return Err(AiError::ProviderUnavailable(
"no AI provider selected".into(),
));
}
if endpoint.trim().is_empty() {
return Err(AiError::ProviderUnavailable(
"no provider endpoint configured".into(),
));
}
facts.validate_no_forbidden()?;
let attempt = |request: &ChatRequest| -> Result<AssistantPlan, AiError> {
let body = serde_json::to_string(request)?;
let response = transport.send(provider, endpoint, &body)?;
let content = crate::provider::extract_content(&response)?;
parse_plan(&content)
};
match attempt(&build_request(provider, model, facts, instruction)) {
Ok(plan) => Ok(plan),
Err(first) => {
let correction = first.to_string();
attempt(&build_corrected_request(
provider,
model,
facts,
instruction,
&correction,
))
}
}
}
/// One step of the general assistant conversation.
#[derive(Debug, Clone, PartialEq)]
pub enum AssistantStep {
/// Read-only evidence the model wants in order to answer.
Investigate { requests: Vec<SystemAction> },
/// A final plan or answer. `actions` may be empty when the reply is purely
/// informational.
Answer(AssistantPlan),
}
/// The closed set of read-only actions the general assistant may request as
/// evidence. Every non-mutating action qualifies; a mutating action never does.
pub fn is_general_investigation_action(action: &SystemAction) -> bool {
!action.is_mutating()
}
/// Parse one general-assistant reply. Absent `kind` defaults to `plan`, so the
/// existing `AssistantPlan` wire format keeps working.
pub fn parse_step(text: &str) -> Result<AssistantStep, AiError> {
let value: serde_json::Value = serde_json::from_str(text)?;
let obj = value
.as_object()
.ok_or_else(|| AiError::Rejected("the reply must be a JSON object".into()))?;
let version = obj
.get("version")
.and_then(|v| v.as_u64())
.ok_or_else(|| AiError::Rejected("missing `version`".into()))?;
if version != 1 {
return Err(AiError::UnsupportedVersion(version as u32));
}
let kind = obj.get("kind").and_then(|k| k.as_str()).unwrap_or("plan");
if kind == "investigate" {
let allowed = ["version", "kind", "requests"];
for key in obj.keys() {
if !allowed.contains(&key.as_str()) {
return Err(AiError::Rejected(format!("unexpected field `{key}`")));
}
}
let requests = parse_action_list(
&obj.get("requests")
.cloned()
.ok_or_else(|| AiError::Rejected("missing `requests`".into()))?,
)?;
if requests.is_empty() {
return Err(AiError::Rejected("empty investigation request".into()));
}
for request in &requests {
if !is_general_investigation_action(request) {
return Err(AiError::Rejected(format!(
"`{}` is not a permitted read-only investigation action",
request.describe()
)));
}
request
.validate()
.map_err(|e| AiError::Rejected(format!("invalid request: {e}")))?;
}
return Ok(AssistantStep::Investigate { requests });
}
if kind != "plan" {
return Err(AiError::Rejected(format!("unknown kind `{kind}`")));
}
// Strip the envelope keys before enforcing the strict plan schema.
let mut plan_value = value.clone();
if let Some(map) = plan_value.as_object_mut() {
map.remove("kind");
map.remove("requests");
}
let plan = parse_plan(&serde_json::to_string(&plan_value)?)?;
Ok(AssistantStep::Answer(plan))
}
/// The system-side half of the general loop: execute read-only investigation.
pub type GeneralInvestigator<'a> = dyn Fn(&[SystemAction]) -> Vec<Evidence> + 'a;
/// Run the bounded general assistant loop. The model may investigate with
/// read-only tools before returning an answer or plan. Unknown, malformed or
/// mutating replies are rejected; the reply may be corrected once per turn.
#[allow(clippy::too_many_arguments)]
pub fn run_assistant(
provider: ProviderKind,
endpoint: &str,
model: &str,
facts: &SystemFacts,
instruction: &str,
transport: &dyn ModelTransport,
investigate: &GeneralInvestigator,
limits: &crate::install::Limits,
) -> Result<AssistantPlan, AiError> {
if provider == ProviderKind::None {
return Err(AiError::ProviderUnavailable(
"no AI provider selected".into(),
));
}
crate::provider::validate_endpoint(endpoint)?;
facts.validate_no_forbidden()?;
let mut chat = build_request(provider, model, facts, instruction);
let mut repeated: std::collections::BTreeMap<String, usize> = std::collections::BTreeMap::new();
let mut turns = 0usize;
let mut corrected = false;
while turns < limits.max_turns {
turns += 1;
let body = serde_json::to_string(&chat)?;
let response = transport.send(provider, endpoint, &body)?;
let content = crate::provider::extract_content(&response)?;
let step = match parse_step(&content) {
Ok(step) => step,
Err(e) if !corrected => {
corrected = true;
chat.messages.push(ChatMessage {
role: "assistant".into(),
content: content.clone(),
});
chat.messages.push(ChatMessage {
role: "user".into(),
content: format!(
"Your previous reply was rejected: {e}. Reply with ONLY the \
corrected JSON object, with no prose and no code fence."
),
});
continue;
}
Err(e) => return Err(e),
};
match step {
AssistantStep::Investigate { requests } => {
// The whole reply has already passed the typed, read-only
// validator. Run only the bounded prefix and let the model
// request any omitted evidence on its next turn. An oversized
// batch is a model mistake, not a user-facing failure.
let requested_count = requests.len();
let requests: Vec<_> = requests
.into_iter()
.take(limits.max_requests_per_turn)
.collect();
let mut keys: Vec<String> = requests
.iter()
.filter_map(|r| serde_json::to_string(r).ok())
.collect();
keys.sort();
let key = keys.join("|");
let count = repeated.entry(key).or_insert(0);
*count += 1;
chat.messages.push(ChatMessage {
role: "assistant".into(),
content: content.clone(),
});
if *count > limits.max_identical_requests {
chat.messages.push(ChatMessage {
role: "user".into(),
content: "You already requested exactly this evidence; it will not \
be fetched again. Answer now from the evidence you have."
.to_string(),
});
continue;
}
let evidence = investigate(&requests);
let mut payload = String::new();
for item in &evidence {
payload.push_str(&format!(
"Request: {}\nOutput:\n{}\n\n",
item.request, item.output
));
if payload.len() >= limits.max_evidence_bytes {
break;
}
}
if payload.len() > limits.max_evidence_bytes {
payload.truncate(limits.max_evidence_bytes);
payload.push_str("\n[evidence truncated]");
}
if requested_count > requests.len() {
payload.push_str(&format!(
"\nOnly the first {} of {} requested checks ran. The remaining \
checks were not performed. Request any still needed evidence in \
another turn, with at most {} checks, or answer from the evidence above.",
requests.len(),
requested_count,
limits.max_requests_per_turn
));
}
chat.messages.push(ChatMessage {
role: "user".into(),
content: format!("Evidence:\n{payload}"),
});
}
AssistantStep::Answer(plan) => return Ok(plan),
}
}
Err(AiError::Rejected(format!(
"the assistant did not answer within {} turns",
limits.max_turns
)))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::provider::TestTransport;
use phoebus_core::hardware::HardwareReport;
use phoebus_core::InstallConfig;
fn facts() -> SystemFacts {
SystemFacts::from_system(
&InstallConfig::default(),
&HardwareReport::default(),
&std::collections::HashMap::new(),
true,
None,
)
}
fn wrap(plan_json: &str) -> String {
serde_json::json!({
"choices": [{ "message": { "content": plan_json } }]
})
.to_string()
}
#[test]
fn parses_a_valid_plan() {
let text = r#"{
"version": 1,
"summary": "Check ssh and install vim",
"actions": [
{"action":"inspect_service","name":"sshd"},
{"action":"install_package","atom":"app-editors/vim"}
],
"rationale": [
{"action":"inspect_service","reason":"confirm it is present"},
{"action":"install_package","reason":"the user asked for vim"}
]
}"#;
let plan = parse_plan(text).unwrap();
assert_eq!(plan.actions.len(), 2);
assert!(plan.render().contains("install package app-editors/vim"));
assert!(plan.render().contains("reason: the user asked for vim"));
}
#[test]
fn unknown_actions_and_shell_fail_closed() {
let text = r#"{"version":1,"summary":"x","actions":[{"action":"run_shell","command":"rm -rf /"}],"rationale":[{"action":"run_shell","reason":"no"}]}"#;
assert!(parse_plan(text).is_err());
}
#[test]
fn plan_actions_reject_smuggled_extra_keys() {
// `deny_unknown_fields` does not cover internally tagged unit variants,
// so an extra key on `history`/`depclean` must be rejected explicitly on
// the AI path too.
let depclean = r#"{"version":1,"summary":"x","actions":[{"action":"depclean","shell":"sh"}],"rationale":[{"action":"depclean","reason":"x"}]}"#;
assert!(parse_plan(depclean).is_err());
let history = r#"{"version":1,"summary":"x","actions":[{"action":"history","x":1}],"rationale":[{"action":"history","reason":"x"}]}"#;
assert!(parse_plan(history).is_err());
let good = r#"{"version":1,"summary":"x","actions":[{"action":"history"}],"rationale":[{"action":"history","reason":"x"}]}"#;
assert!(parse_plan(good).is_ok());
}
#[test]
fn investigation_requests_reject_smuggled_extra_keys() {
let step =
r#"{"version":1,"kind":"investigate","requests":[{"action":"history","shell":"sh"}]}"#;
assert!(parse_step(step).is_err());
let good = r#"{"version":1,"kind":"investigate","requests":[{"action":"history"}]}"#;
assert!(parse_step(good).is_ok());
}
#[test]
fn actions_require_a_reason() {
let text = r#"{"version":1,"summary":"x","actions":[{"action":"enable_service","name":"sshd"}],"rationale":[]}"#;
assert!(matches!(parse_plan(text), Err(AiError::Rejected(_))));
let empty_reason = r#"{"version":1,"summary":"x","actions":[{"action":"enable_service","name":"sshd"}],"rationale":[{"action":"enable_service","reason":" "}]}"#;
assert!(parse_plan(empty_reason).is_err());
}
#[test]
fn invalid_values_are_rejected() {
let text = r#"{"version":1,"summary":"x","actions":[{"action":"install_package","atom":"bad atom"}],"rationale":[{"action":"install_package","reason":"x"}]}"#;
assert!(parse_plan(text).is_err());
// The AI path rejects option-like operands exactly like the manual and
// JSON paths, so a model cannot smuggle a command-line option.
let option = r#"{"version":1,"summary":"x","actions":[{"action":"install_package","atom":"-C"}],"rationale":[{"action":"install_package","reason":"x"}]}"#;
assert!(parse_plan(option).is_err());
let service = r#"{"version":1,"summary":"x","actions":[{"action":"start_service","name":"--now"}],"rationale":[{"action":"start_service","reason":"x"}]}"#;
assert!(parse_plan(service).is_err());
}
#[test]
fn transport_round_trip_and_unavailable_provider() {
let plan = r#"{"version":1,"summary":"look","actions":[{"action":"history"}],"rationale":[{"action":"history","reason":"user asked"}]}"#;
let transport = TestTransport::new(wrap(plan));
let parsed = complete_plan(
ProviderKind::DeepSeek,
"https://api.deepseek.com/chat/completions",
"",
&facts(),
"what changed?",
&transport,
)
.unwrap();
assert_eq!(parsed.actions.len(), 1);
assert!(complete_plan(ProviderKind::None, "", "", &facts(), "hi", &transport).is_err());
assert!(complete_plan(ProviderKind::Local, "", "", &facts(), "hi", &transport).is_err());
}
#[test]
fn malformed_responses_are_reported_not_ignored() {
let transport = TestTransport::new(wrap("not json at all"));
assert!(complete_plan(
ProviderKind::Local,
"http://127.0.0.1:11434/v1/chat/completions",
"m",
&facts(),
"hi",
&transport
)
.is_err());
// A response with no choices is a provider error.
let empty = TestTransport::new("{\"choices\":[]}");
assert!(complete_plan(
ProviderKind::Local,
"http://127.0.0.1:11434/v1/chat/completions",
"m",
&facts(),
"hi",
&empty
)
.is_err());
}
#[test]
fn a_rejected_plan_is_retried_once_with_a_correction() {
use crate::provider::{chat_response, ScriptedTransport};
let bad =
chat_response(r#"{"version":1,"summary":"x","actions":[{"action":"run_shell"}]}"#);
let good = chat_response(
r#"{"version":1,"summary":"look","actions":[{"action":"history"}],"rationale":[{"action":"history","reason":"asked"}]}"#,
);
let transport = ScriptedTransport::new(vec![bad, good]);
let plan = complete_plan(
ProviderKind::Local,
"http://127.0.0.1:11434/v1/chat/completions",
"m",
&facts(),
"what changed?",
&transport,
)
.unwrap();
assert_eq!(plan.actions.len(), 1);
let requests = transport.requests();
assert_eq!(requests.len(), 2);
assert!(requests[1].contains("rejected"));
}
#[test]
fn action_tag_matches_the_schema() {
assert_eq!(action_tag(&SystemAction::History), "history");
assert_eq!(action_tag(&SystemAction::FlatpakList), "flatpak_list");
}
#[test]
fn the_prompt_names_the_extended_selection_actions() {
for action in [
"choose_package",
"set_package_preference",
"snap_install",
"appimage_acquire",
"custom_ebuild_prepare",
] {
assert!(
ASSISTANT_PROMPT.contains(action),
"the prompt must advertise {action}"
);
}
// The prompt must forbid the old category routing and call a custom
// ebuild untrusted.
assert!(ASSISTANT_PROMPT.contains("category"));
assert!(ASSISTANT_PROMPT.contains("untrusted"));
}
#[test]
fn a_plan_may_choose_a_lane_and_stage_a_custom_ebuild() {
let text = r#"{
"version": 1,
"summary": "pick the best source, then stage a custom ebuild",
"actions": [
{"action":"choose_package","name":"firefox","gui":true},
{"action":"snap_install","name":"thunderbird"}
],
"rationale": [
{"action":"choose_package","reason":"compare the lanes"},
{"action":"snap_install","reason":"the user asked for the snap"}
]
}"#;
let plan = parse_plan(text).unwrap();
assert_eq!(plan.actions.len(), 2);
assert!(plan.render().contains("choose the best source"));
assert!(plan.render().contains("install snap thunderbird"));
}
#[test]
fn general_step_defaults_to_plan_and_accepts_investigate() {
// No `kind` key: the legacy plan format still parses.
let plan = parse_step(
r#"{"version":1,"summary":"x","actions":[{"action":"history"}],"rationale":[{"action":"history","reason":"asked"}]}"#,
)
.unwrap();
assert!(matches!(plan, AssistantStep::Answer(_)));
let investigate = parse_step(
r#"{"version":1,"kind":"investigate","requests":[{"action":"search_packages","query":"chrome"}]}"#,
)
.unwrap();
match investigate {
AssistantStep::Investigate { requests } => {
assert_eq!(requests.len(), 1);
assert!(matches!(requests[0], SystemAction::SearchPackages { .. }));
}
_ => panic!("expected investigate"),
}
}
#[test]
fn general_investigation_rejects_mutation_and_unknown_kind() {
let err = parse_step(
r#"{"version":1,"kind":"investigate","requests":[{"action":"install_package","atom":"vim"}]}"#,
)
.unwrap_err();
assert!(err.to_string().contains("read-only"));
assert!(parse_step(r#"{"version":1,"kind":"frobnicate","requests":[]}"#).is_err());
assert!(parse_step(r#"{"version":1,"kind":"investigate","requests":[]}"#).is_err());
}
#[test]
fn run_assistant_investigates_then_answers_with_evidence() {
use crate::provider::{chat_response, ScriptedTransport};
let transport = ScriptedTransport::new(vec![
chat_response(
r#"{"version":1,"kind":"investigate","requests":[{"action":"search_packages","query":"chrome"}]}"#,
),
chat_response(r#"{"version":1,"summary":"Real candidates found.","actions":[]}"#),
]);
let calls = std::cell::Cell::new(0);
let investigate = |requests: &[SystemAction]| -> Vec<Evidence> {
calls.set(calls.get() + 1);
requests
.iter()
.map(|r| Evidence {
request: r.describe(),
output: "[{\"source\":\"flatpak\",\"id\":\"com.google.Chrome\"}]".into(),
})
.collect()
};
let plan = run_assistant(
ProviderKind::Local,
"http://127.0.0.1:11434/v1/chat/completions",
"m",
&facts(),
"search chrome",
&transport,
&investigate,
&crate::install::Limits::default(),
)
.unwrap();
assert!(plan.actions.is_empty());
assert_eq!(calls.get(), 1);
let requests = transport.requests();
assert_eq!(requests.len(), 2);
// The evidence must be fed back to the model.
assert!(requests[1].contains("com.google.Chrome"));
}
#[test]
fn oversized_investigation_runs_only_the_bounded_prefix() {
use crate::provider::{chat_response, ScriptedTransport};
let requests: Vec<_> = (0..7)
.map(|index| serde_json::json!({"action":"search_packages","query":format!("etcher{index}")}))
.collect();
let transport = ScriptedTransport::new(vec![
chat_response(
&serde_json::json!({"version":1,"kind":"investigate","requests":requests})
.to_string(),
),
chat_response(
r#"{"version":1,"summary":"No package found in the checked sources.","actions":[]}"#,
),
]);
let investigate = |requests: &[SystemAction]| -> Vec<Evidence> {
assert_eq!(requests.len(), 6);
let queries: Vec<_> = requests
.iter()
.map(|r| {
serde_json::to_value(r).unwrap()["query"]
.as_str()
.unwrap()
.to_string()
})
.collect();
assert_eq!(
queries,
(0..6)
.map(|index| format!("etcher{index}"))
.collect::<Vec<_>>()
);
requests
.iter()
.map(|r| Evidence {
request: r.describe(),
output: "[]".into(),
})
.collect()
};
let plan = run_assistant(
ProviderKind::Local,
"http://127.0.0.1:11434/v1/chat/completions",
"m",
&facts(),
"find etcher",
&transport,
&investigate,
&crate::install::Limits::default(),
)
.unwrap();
assert!(plan.actions.is_empty());
let sent = transport.requests();
assert_eq!(sent.len(), 2);
assert!(sent[1].contains("Only the first 6 of 7 requested checks ran"));
assert!(sent[1].contains("remaining checks were not performed"));
}
#[test]
fn oversized_investigation_still_rejects_a_mutation_after_the_limit() {
use crate::provider::{chat_response, ScriptedTransport};
let mut requests: Vec<_> = (0..6)
.map(|index| serde_json::json!({"action":"search_packages","query":format!("etcher{index}")}))
.collect();
requests.push(serde_json::json!({"action":"install_package","atom":"vim"}));
let transport = ScriptedTransport::new(vec![
chat_response(
&serde_json::json!({"version":1,"kind":"investigate","requests":requests})
.to_string(),
),
chat_response(
r#"{"version":1,"summary":"I can search without changing the system.","actions":[]}"#,
),
]);
let investigate = |_: &[SystemAction]| -> Vec<Evidence> {
panic!("no checks should run when any request is mutating")
};
let plan = run_assistant(
ProviderKind::Local,
"http://127.0.0.1:11434/v1/chat/completions",
"m",
&facts(),
"find etcher",
&transport,
&investigate,
&crate::install::Limits::default(),
)
.unwrap();
assert!(plan.actions.is_empty());
}
#[test]
fn run_assistant_never_executes_a_mutating_investigation_request() {
use crate::provider::{chat_response, ScriptedTransport};
// The first reply tries to use a mutation as "evidence"; it is rejected
// and never executed. The corrected reply is a normal plan.
let transport = ScriptedTransport::new(vec![
chat_response(
r#"{"version":1,"kind":"investigate","requests":[{"action":"install_package","atom":"vim"}]}"#,
),
chat_response(
r#"{"version":1,"summary":"Install vim.","actions":[{"action":"install_package","atom":"vim"}],"rationale":[{"action":"install_package","reason":"asked"}]}"#,
),
]);
let executed = std::cell::Cell::new(false);
let investigate = |_: &[SystemAction]| -> Vec<Evidence> {
executed.set(true);
Vec::new()
};
let plan = run_assistant(
ProviderKind::Local,
"http://127.0.0.1:11434/v1/chat/completions",
"m",
&facts(),
"install vim",
&transport,
&investigate,
&crate::install::Limits::default(),
)
.unwrap();
assert_eq!(plan.actions.len(), 1);
assert!(!executed.get(), "a mutating request must never be executed");
}
}