Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/nix/host-modules/swarm-review-classifier.nix

274 lines
11 KiB
Nix

# The review classifier, a pod in the host's cluster on the host
# that sets `deploy.review-classifier.enable`, which `deploy.allSwarmServices`
# defaults on where the host has the classifier's store identity. It answers the
# swarm UI's review try-outs: swarm-controller sends a PR and the operator's classifications over
# the queue, and the daemon runs `claude`, or `opencode` on the ACP runtime,
# once per classification and replies.
# docs/4-implementation/swarm/review-classifier.md has the design. The pod is
# rendered (./k8s/render.nix) from the unit below
# (docs/4-implementation/k8s/runtime.md).
#
# ⚠️ A PR's diff and commit messages are model input, so the pod reaches the
# swarm services by name and nothing on the host's loopback.
#
# The unit names its store leaf by credential id; the pod logs in to the store
# with its ServiceAccount token instead. The daemon reads its queue client secret
# at start and the run's runtime's key on every run with it.
{
pkgs,
lib,
config,
...
}:
let
hyperhiveCfg = config.services.hyperhive;
# The `claude-code` the agents run, so this container does not trail them on
# the CLI; `fallback`, the container's own nixpkgs', only where this host pins
# none.
claudeCode =
fallback:
if hyperhiveCfg.deploy.hive-controller.claudeCodePackage != null then
hyperhiveCfg.deploy.hive-controller.claudeCodePackage
else
fallback;
cfg = hyperhiveCfg.deploy.review-classifier;
baoCfg = hyperhiveCfg.swarm.bao;
baoDeploy = hyperhiveCfg.deploy.bao;
natsCfg = hyperhiveCfg.swarm.nats;
autheliaCfg = hyperhiveCfg.swarm.authelia;
gatewayCfg = hyperhiveCfg.gateway;
networkCfg = hyperhiveCfg.network;
tlsCfg = hyperhiveCfg.deploy.hive-controller.tls;
machine = "swarm-review-classifier";
unit = "swarm-review-classifier";
clientId = hyperhiveCfg.swarm.review-classifier.clientId;
# The container's journal forwarder pushes to its own receiver on the swarm
# collector, the route `serviceProducers.swarm-review-classifier` in
# ./swarm-otel-service.nix declares, by the collector's gateway name.
swarmOtelCfg = hyperhiveCfg.swarm.otel;
forwarderEndpoint = "https://${swarmOtelCfg.domain}/${machine}";
forwarderUnit = "swarm-journal-forwarder-oidc";
# Loopback in the container's own netns, and distinct from the other service
# containers' forwarders all the same.
forwarderTelemetryPort = 8894;
# The standard OTLP/HTTP port, on the container's loopback.
forwarderOtlpPort = 4318;
# The daemon reaches authelia, the store and the queue by name over TLS, and
# in self-signed mode their leaves chain to the hive CA.
caTrust = import ./lib/hive-ca-trust.nix { inherit lib tlsCfg gatewayCfg; };
caBundleModule = caTrust.trustBundle {
inherit pkgs;
name = machine;
consumers = [
unit
# Go: `SSL_CERT_FILE` replaces its trust store, for the swarm
# collector's and authelia's names.
"opentelemetry-collector"
];
};
haveClientIdentity =
baoDeploy.reviewClassifierClientCertFile != null && baoDeploy.reviewClassifierClientKeyFile != null;
# Credential id → host source. The unit names the credentials by id, and the
# pod takes the id from the unit.
storeFiles = lib.filterAttrs (_: source: source != null) {
review-classifier-bao-cert = baoDeploy.reviewClassifierClientCertFile;
review-classifier-bao-key = baoDeploy.reviewClassifierClientKeyFile;
review-classifier-bao-server-ca = baoDeploy.serverCaFile;
};
credential = id: "/run/credentials/${unit}.service/${id}";
forwarderCredential = id: "/run/credentials/${forwarderUnit}.service/${id}";
# What `claude` exports its metrics with, and the daemon its ACP runs' cost:
# the container's own forwarder, which pushes them to the swarm collector as
# this service's client, on every host. The daemon adds the repo and the PR
# to each `claude` run's resource attributes, and to each cost record's
# attributes.
otelEnv = {
CLAUDE_CODE_ENABLE_TELEMETRY = "1";
OTEL_METRICS_EXPORTER = "otlp";
OTEL_EXPORTER_OTLP_ENDPOINT = "http://127.0.0.1:${toString forwarderOtlpPort}";
OTEL_EXPORTER_OTLP_PROTOCOL = "http/protobuf";
# Claude Code defaults to delta, which the metrics store drops.
OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = "cumulative";
OTEL_RESOURCE_ATTRIBUTES = "service.name=hyperhive-review-classifier,swarm=${
if hyperhiveCfg.swarm.name == null then "unknown" else hyperhiveCfg.swarm.name
}";
};
privateNetwork = true;
# The unit the pod is rendered from (`deploy.review-classifier.unit`) is
# defined in this module.
containerConfig =
{ pkgs, lib, ... }:
{
imports = [
../container-modules/swarm-container.nix
../container-modules/swarm-journal-forwarder.nix
caBundleModule
];
services.hyperhive.swarmContainer = {
inherit privateNetwork;
writesOwnResolvConf = false;
};
networking.nameservers = [ networkCfg.bridgeIp ];
networking.interfaces.eth0.useDHCP = true;
# `claude-code` is unfree, and this container evaluates its own
# nixpkgs, so the host's allowance does not reach here.
nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "claude-code" ];
# Authenticated as the daemon's own OIDC client, whose secret its
# store identity already reads: one identity for the whole container.
# That identity is a system credential, so the fetch unit loads it
# too; the daemon's `LoadCredential` below is that unit's alone.
services.hyperhive.swarmJournalForwarder = {
enable = true;
serviceName = "review-classifier";
endpoint = forwarderEndpoint;
inherit clientId;
tokenUrl = "${autheliaCfg.url}/api/oidc/token";
telemetryPort = forwarderTelemetryPort;
otlpMetricsPort = forwarderOtlpPort;
store = {
package = baoDeploy.package;
addr = "https://${baoCfg.domain}:${toString baoCfg.port}";
secretPath = "secret/swarm/services/${clientId}/oidc/client";
clientCertFile = forwarderCredential "review-classifier-bao-cert";
clientKeyFile = forwarderCredential "review-classifier-bao-key";
caFile =
if storeFiles ? review-classifier-bao-server-ca then
forwarderCredential "review-classifier-bao-server-ca"
else
null;
};
};
systemd.services.${forwarderUnit}.serviceConfig.LoadCredential = lib.attrNames storeFiles;
systemd.services.${unit} = {
description = "answer the swarm UI's review try-outs";
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
environment = {
BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}";
BAO_CLIENT_CERT = credential "review-classifier-bao-cert";
BAO_CLIENT_KEY = credential "review-classifier-bao-key";
SWARM_REVIEW_CLASSIFIER_NATS_URL = "tls://${natsCfg.domain}:${toString natsCfg.port}";
SWARM_REVIEW_CLASSIFIER_OIDC_TOKEN_ENDPOINT = "${autheliaCfg.url}/api/oidc/token";
SWARM_REVIEW_CLASSIFIER_OIDC_CLIENT_ID = clientId;
SWARM_REVIEW_CLASSIFIER_CLAUDE = "${claudeCode pkgs.claude-code}/bin/claude";
SWARM_REVIEW_CLASSIFIER_OPENCODE = "${pkgs.opencode}/bin/opencode";
}
// lib.optionalAttrs (storeFiles ? review-classifier-bao-server-ca) {
BAO_CACERT = credential "review-classifier-bao-server-ca";
}
// lib.optionalAttrs caTrust.useSelfSigned {
SWARM_REVIEW_CLASSIFIER_OIDC_CA_FILE = caTrust.bundlePathFor machine;
}
// otelEnv;
serviceConfig = {
ExecStart = "${cfg.package}/bin/swarm-review-classifier";
DynamicUser = true;
# Each classification's scratch directory, gone with the unit.
PrivateTmp = true;
# Bare ids, which the pod's login replaces.
LoadCredential = lib.attrNames storeFiles;
Restart = "on-failure";
RestartSec = "10s";
SyslogIdentifier = unit;
};
};
};
# The service's NixOS system, evaluated on its own: its unit is what the pod is
# rendered from.
podSystem =
(import "${pkgs.path}/nixos/lib/eval-config.nix" {
modules = [
(
{ options, ... }:
{
config = {
nixpkgs =
if options.nixpkgs ? hostPlatform then
{ inherit (pkgs.stdenv) hostPlatform; }
else
{ localSystem = pkgs.stdenv.hostPlatform; };
boot.isNspawnContainer = true;
networking.hostName = lib.mkDefault machine;
networking.useDHCP = false;
};
}
)
containerConfig
];
system = null;
}).config;
in
{
options.services.hyperhive.swarm.review-classifier.clientId = lib.mkOption {
type = lib.types.str;
readOnly = true;
default = "swarm-review-classifier";
description = ''
The OAuth2 client id the review classifier presents to the swarm queue.
Read-only: the host running authelia registers it, the queue's
auth-callout responder grants it the try-out subject, and
authelia's mint stores its secret in the store, all from this one value.
'';
};
options.services.hyperhive.deploy.review-classifier.package = lib.mkOption {
type = lib.types.package;
defaultText = lib.literalExpression "hyperhive.packages.\${system}.swarm-review-classifier";
description = "The review classifier daemon package.";
};
options.services.hyperhive.deploy.review-classifier.unit = lib.mkOption {
type = lib.types.raw;
readOnly = true;
internal = true;
default = podSystem.systemd.services.${unit};
description = ''
The service's evaluated systemd unit, which the cluster's manifests are
rendered from (./k8s/render.nix). It is the unit of the
pod's own configuration, evaluated alone: no container runs it.
'';
};
config = lib.mkMerge [
(lib.mkIf cfg.enable {
assertions = [
{
assertion = haveClientIdentity;
message = ''
services.hyperhive.deploy.review-classifier.enable requires the
classifier's swarm-secret-store client identity: set both
services.hyperhive.deploy.bao.reviewClassifierClientCertFile
services.hyperhive.deploy.bao.reviewClassifierClientKeyFile
The classifier reads its queue client secret and its runtime keys with it.
Both default to the pair glue-bao-tls.nix mints when
services.hyperhive.deploy.bao.enable is true (the host that runs the
store). Otherwise copy review-classifier.pem and
review-classifier-key.pem from the store host's /var/lib/swarm-bao-pki/
and name them in those two options.
'';
}
];
# The cluster's pods reach the host's services through the bridge.
services.hyperhive.network.enable = lib.mkDefault true;
})
];
}