Watch
0
0
Fork
You've already forked hyperhive
0

review classifier: run on ACP when the swarm settings choose it

The swarm settings gain `runtime` (`api-key`, the default, or `acp`) and
an `acp` provider record: an https base URL, the provider's model, and
its prices per million tokens. A PUT now changes only the fields its
body names, so the settings form that sends model, effort and limit
leaves the runtime alone; `null` clears `effort` or `acp`. The body stays
strict, and the stored document keeps #5192's lenient read.

The controller sends the runtime in each run request. On `acp` the
classifier runs one prompt on a new `opencode acp` session per
classification through hive-runtime's AcpRuntime, in a scratch
directory, with a config rendered from the settings. opencode runs
`read` and other tools without asking, so OPENCODE_PERMISSION
{"*":"deny"} takes every tool away from the model, and a deny-all policy
refuses any request on top. The title call, which resends the prompt,
is turned off. The deadline cancels the turn, and a turn that does not
end within 15s of that is dropped, which also covers an agent stuck
before its prompt. The answer is the final assistant text, as ACP has
no result event.

The provider key is read per run from
swarm/services/review-classifier/acp-provider, which the bao UI's
admins login may create and update, and reaches opencode through
AcpRuntime::with_provider_key and the trampoline's allowlist.

Each ACP classification's cost is computed from the configured prices
and the reported tokens, and added to
hyperhive.review_classifier.cost.usage labelled repo, pr, model and
currency.

Part of #5096
This commit is contained in:
atlas 2026-10-13 10:15:27 +02:00 • committed by mara
commit 1b4e764e5c
24 changed files with 1622 additions and 262 deletions

View file

@ -1,10 +1,11 @@
//! The `claude` child's environment: an allowlist, not the daemon's.
//! The model child's environment: an allowlist, not the daemon's.
//!
//! `hive-claude` sets its variables on top of the parent's environment, and
//! the daemon's holds the store identity paths and the queue coordinates. So
//! `claude` is spawned as this same binary with [`EXEC_CLAUDE`] naming the
//! real one: [`exec_if_requested`] runs first in `main`, clears the
//! environment down to [`ALLOWED`] and execs `claude` with the same
//! `hive-claude` and `hive-runtime` set their variables on top of the
//! parent's environment, and the daemon's holds the store identity paths and
//! the queue coordinates. So the child is spawned as this same binary with
//! [`EXEC_CLAUDE`] or [`EXEC_OPENCODE`] naming the real one:
//! [`exec_if_requested`] runs first in `main`, clears the environment down to
//! that runtime's allowlist and execs the real binary with the same
//! arguments, in the same process.
use std::ffi::OsString;
@ -14,9 +15,12 @@ use std::process::Command;
/// Set on the child only: the `claude` binary to exec.
pub const EXEC_CLAUDE: &str = "SWARM_REVIEW_CLASSIFIER_EXEC_CLAUDE";
/// Set on the child only: the `opencode` binary to exec.
pub const EXEC_OPENCODE: &str = "SWARM_REVIEW_CLASSIFIER_EXEC_OPENCODE";
/// Every variable `claude` keeps: the key, what it needs to run, and its
/// telemetry settings.
const ALLOWED: &[&str] = &[
const CLAUDE_ALLOWED: &[&str] = &[
"ANTHROPIC_API_KEY",
"PATH",
"HOME",
@ -29,37 +33,59 @@ const ALLOWED: &[&str] = &[
"OTEL_RESOURCE_ATTRIBUTES",
];
/// The variables of `vars` on [`ALLOWED`].
pub fn allowed(vars: impl IntoIterator<Item = (OsString, OsString)>) -> Vec<(OsString, OsString)> {
/// Every variable `opencode` keeps: the provider key, what it needs to run,
/// and the config [`crate::acp`] renders for it.
const OPENCODE_ALLOWED: &[&str] = &[
crate::acp::PROVIDER_KEY_VAR,
"PATH",
"HOME",
"OPENCODE_CONFIG",
"OPENCODE_PERMISSION",
"OPENCODE_DISABLE_PROJECT_CONFIG",
];
/// The variables of `vars` on `allowed`.
fn allowed(
allowed: &[&str],
vars: impl IntoIterator<Item = (OsString, OsString)>,
) -> Vec<(OsString, OsString)> {
vars.into_iter()
.filter(|(name, _)| ALLOWED.iter().any(|a| name == a))
.filter(|(name, _)| allowed.iter().any(|a| name == a))
.collect()
}
/// When this process is the trampoline, exec `claude` and never return.
/// When this process is the trampoline, exec the real binary and never
/// return.
pub fn exec_if_requested() {
let Some(claude) = std::env::var_os(EXEC_CLAUDE) else {
return;
};
let error = Command::new(&claude)
.args(std::env::args_os().skip(1))
.env_clear()
.envs(allowed(std::env::vars_os()))
.exec();
eprintln!("exec {}: {error}", claude.to_string_lossy());
std::process::exit(127);
for (var, allow) in [
(EXEC_CLAUDE, CLAUDE_ALLOWED),
(EXEC_OPENCODE, OPENCODE_ALLOWED),
] {
let Some(program) = std::env::var_os(var) else {
continue;
};
let error = Command::new(&program)
.args(std::env::args_os().skip(1))
.env_clear()
.envs(allowed(allow, std::env::vars_os()))
.exec();
eprintln!("exec {}: {error}", program.to_string_lossy());
std::process::exit(127);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_the_allowlist_reaches_claude() {
let daemon = [
/// The daemon's environment on a run of either runtime.
fn daemon() -> Vec<(OsString, OsString)> {
[
("ANTHROPIC_API_KEY", "k"),
(crate::acp::PROVIDER_KEY_VAR, "p"),
("HOME", "/tmp/run"),
("OTEL_RESOURCE_ATTRIBUTES", "repo=o/r,pr=7"),
("OPENCODE_PERMISSION", r#"{"*":"deny"}"#),
(
"BAO_CLIENT_KEY",
"/run/credentials/x/review-classifier-bao-key",
@ -71,15 +97,32 @@ mod tests {
"tls://nats.example:4222",
),
(EXEC_CLAUDE, "/bin/claude"),
(EXEC_OPENCODE, "/bin/opencode"),
]
.map(|(n, v)| (OsString::from(n), OsString::from(v)));
let kept: Vec<String> = allowed(daemon)
.map(|(n, v)| (OsString::from(n), OsString::from(v)))
.into()
}
fn kept(allow: &[&str]) -> Vec<String> {
allowed(allow, daemon())
.into_iter()
.map(|(n, _)| n.to_string_lossy().into_owned())
.collect();
.collect()
}
#[test]
fn only_the_allowlist_reaches_claude() {
assert_eq!(
kept,
kept(CLAUDE_ALLOWED),
["ANTHROPIC_API_KEY", "HOME", "OTEL_RESOURCE_ATTRIBUTES"]
);
}
#[test]
fn only_the_allowlist_reaches_opencode() {
assert_eq!(
kept(OPENCODE_ALLOWED),
[crate::acp::PROVIDER_KEY_VAR, "HOME", "OPENCODE_PERMISSION"]
);
}
}