aKode: robustezza dell'agente (punto 4, blocco B)

- Compattazione: prima del riassunto l'output degli strumenti più vecchi
  (tutti tranne gli ultimi 3, sopra i 400 caratteri) diventa un segnaposto
  breve; le chiamate restano. La compattazione usa sempre il provider
  configurato: verificato, con un test sulle capacità dei provider.
- Protezione dai loop in stile Gemini CLI, accanto a max_repeated_failures:
  [agent] max_identical_tool_calls (5 risposte di fila con la stessa chiamata,
  qualunque sia il risultato; esclusi write_stdin e wait_agent; le chiamate
  parallele di una risposta contano una volta) e [agent] max_repeated_text
  (lo stesso testo ripetuto 10 volte di seguito mentre arriva, nella risposta
  o nel ragionamento; esclusi blocchi di codice e separatori).
- Cancellazioni ricorsive protette (rm -r, rm -rf, find -delete): radice,
  home, radice del repository, cartella di lavoro, percorsi fuori da essa o
  calcolati a runtime ($VAR, $(...), xargs) chiedono sempre la conferma
  dell'utente, anche con regole che li permetterebbero; in accesso completo
  vengono rifiutati.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
andrea.difonzo
2026-11-16 17:59:19 +01:00
parent 4e1f551b84
commit 0460c74f38
15 changed files with 1014 additions and 8 deletions

View File

@@ -75,13 +75,14 @@ impl PreparedExecCommand {
Some("windows") | Some(_) => DangerousCommandPlatform::Windows,
None => DangerousCommandPlatform::host(),
};
session
let approval_policy = step.settings.approval_policy();
let requirement = session
.services
.exec_policy
.create_exec_approval_requirement_for_shell(
ExecApprovalRequest {
command: &self.resolved.command,
approval_policy: step.settings.approval_policy(),
approval_policy,
permission_profile: environment.permission_profile().clone(),
environment_policy: environment.config().exec_policy.as_ref(),
windows_sandbox_level: windows_sandbox_level_for_legacy_checks(
@@ -99,7 +100,17 @@ impl PreparedExecCommand {
configured_shell,
command_platform,
)
.await
.await;
// POSIX shells only: the check parses `rm`/`find` and POSIX paths.
if command_platform == DangerousCommandPlatform::Posix {
return protect_recursive_deletion(
requirement,
&self.resolved.command,
&self.cwd,
approval_policy,
);
}
requirement
}
pub(crate) fn approval_action(
@@ -316,3 +327,129 @@ impl<'a> ParsedExecCommand<'a> {
})
}
}
/// Recursive deletions of protected paths (root, home, repository root, working directory,
/// anything outside it, run-time targets) always need a fresh human decision, whatever the
/// sandbox, the approval mode or the rules; where nobody can be asked they are refused.
fn protect_recursive_deletion(
requirement: ExecApprovalRequirement,
command: &[String],
cwd: &PathUri,
approval_policy: akode_protocol::protocol::AskForApproval,
) -> ExecApprovalRequirement {
use akode_shell_command::recursive_deletion::DeletionContext;
use akode_shell_command::recursive_deletion::protected_recursive_deletion;
if matches!(requirement, ExecApprovalRequirement::Forbidden { .. }) {
return requirement;
}
let Ok(cwd) = cwd.to_abs_path() else {
return requirement;
};
let cwd = cwd.as_path().to_path_buf();
let repo_root = cwd
.ancestors()
.find(|dir| dir.join(".git").exists())
.map(Path::to_path_buf);
let context = DeletionContext {
home: dirs::home_dir(),
repo_root,
cwd,
};
let Some(reason) = protected_recursive_deletion(command, &context) else {
return requirement;
};
tracing::warn!(%reason, "protected recursive deletion");
// Like the other dangerous commands, the prompt is allowed in granular modes; only
// `never` (full access without prompts) has nobody to ask.
if matches!(approval_policy, akode_protocol::protocol::AskForApproval::Never) {
ExecApprovalRequirement::Forbidden {
reason: format!(
"rifiutato: {reason}. aKode non esegue cancellazioni ricorsive di percorsi protetti senza la conferma dell'utente: chiedigli di farlo a mano, se serve davvero."
),
}
} else if prompt_is_rejected_by_policy(approval_policy, /*prompt_is_rule*/ true).is_some() {
// Granular modes that refuse rule prompts still ask for dangerous commands (as for
// `rm -f`): a plain prompt with no reusable rule, so the user still decides.
ExecApprovalRequirement::NeedsApproval {
reason: Some(format!("{reason}: serve la tua conferma")),
proposed_execpolicy_amendment: None,
}
} else {
// A fresh human decision, never a saved rule or an automatic review.
ExecApprovalRequirement::NeedsHumanApproval {
reason: Some(format!("{reason}: serve la tua conferma")),
}
}
}
#[cfg(test)]
mod recursive_deletion_tests {
use super::*;
use akode_protocol::protocol::AskForApproval;
fn zsh(script: &str) -> Vec<String> {
vec!["/bin/zsh".to_string(), "-lc".to_string(), script.to_string()]
}
fn skip() -> ExecApprovalRequirement {
ExecApprovalRequirement::Skip {
bypass_sandbox: false,
proposed_execpolicy_amendment: None,
}
}
fn cwd() -> PathUri {
let dir = std::env::temp_dir().join("akode-rm-guard");
PathUri::from_host_native_path(dir).expect("cwd")
}
#[test]
fn full_access_refuses_and_other_modes_ask_a_human() {
let home = zsh("rm -rf ~");
assert!(matches!(
protect_recursive_deletion(skip(), &home, &cwd(), AskForApproval::Never),
ExecApprovalRequirement::Forbidden { reason } if reason.contains("cartella home")
));
assert!(matches!(
protect_recursive_deletion(skip(), &home, &cwd(), AskForApproval::OnRequest),
ExecApprovalRequirement::NeedsHumanApproval { reason: Some(reason) }
if reason.contains("cartella home")
));
// Granular modes without rule prompts still ask (like the other dangerous commands).
let granular = AskForApproval::Granular(akode_protocol::protocol::GranularApprovalConfig {
sandbox_approval: false,
rules: false,
skill_approval: false,
request_permissions: true,
mcp_elicitations: true,
});
assert!(matches!(
protect_recursive_deletion(skip(), &home, &cwd(), granular),
ExecApprovalRequirement::NeedsApproval {
proposed_execpolicy_amendment: None,
..
}
));
// Rules that would allow the command do not lift the protection.
assert!(matches!(
protect_recursive_deletion(skip(), &zsh("rm -rf $OUT/"), &cwd(), AskForApproval::OnRequest),
ExecApprovalRequirement::NeedsHumanApproval { .. }
));
}
#[test]
fn ordinary_commands_keep_their_requirement() {
assert!(matches!(
protect_recursive_deletion(skip(), &zsh("rm -rf build"), &cwd(), AskForApproval::Never),
ExecApprovalRequirement::Skip { .. }
));
let forbidden = ExecApprovalRequirement::Forbidden {
reason: "blocked".to_string(),
};
assert!(matches!(
protect_recursive_deletion(forbidden, &zsh("rm -rf /"), &cwd(), AskForApproval::OnRequest),
ExecApprovalRequirement::Forbidden { reason } if reason == "blocked"
));
}
}