arvin/lib.rs
1//! Core logic for arvin, the staged kill switch for agent runs.
2//!
3//! One state file holds every run as one JSON document under the
4//! format name deadman-state/1, and the document holds each run
5//! with its signals and its decision log, so a stage change and
6//! its record publish in the same rename and can never disagree.
7//! Every mutating operation holds an exclusive flock on the lock
8//! file beside the state file across its whole read and evaluate
9//! and modify and write cycle, and publishes by renaming a
10//! complete temporary file over the state file. Reads take a
11//! shared lock and open the state file fresh each time, and a
12//! reader that changes nothing never writes.
13//!
14//! Time is an input. Every operation but the log read takes the
15//! current instant as an argument, and this module reads no wall
16//! clock. The deterministic operations call no model, and the one
17//! model entry point calls only the transport the caller supplies.
18//! The random draws in the module
19//! name temporary files, the file a publication writes before
20//! its rename and the payload file a subprocess consultation
21//! writes before its helper runs, and no draw reaches a
22//! stage, a reason, a record, or a state byte. The stage an
23//! evaluation reports is a deterministic function of the recorded
24//! state and the instant.
25//!
26//! The stage vocabulary is program vocabulary and stands in one
27//! exact order in STAGES. The trigger names of DM-3 stand in
28//! TRIGGERS.
29//!
30//! This crate is the Rust port of the Python implementation of
31//! Round 1. The port amendment in CONTRACT.md records the
32//! equivalence standard, the reshapings, and the two deliberate
33//! divergences the port carries, the lock identity check and the
34//! exclusive publish temporary.
35
36mod evaluate;
37mod instant;
38mod join;
39mod providers;
40mod reader;
41mod switch;
42mod transport;
43mod validate;
44mod value;
45mod vignette;
46mod writer;
47
48pub const FORMAT_NAME: &str = "deadman-state/1";
49pub(crate) const LOCK_SUFFIX: &str = ".lock";
50pub const NAME_MAX: usize = 64;
51pub const NOTE_MAX: usize = 200;
52
53pub const STAGES: [&str; 6] = [
54 "none",
55 "warn",
56 "throttle",
57 "cut tools",
58 "cut budget",
59 "kill",
60];
61pub const TRIGGERS: [&str; 4] = [
62 "operator pull",
63 "hard ceiling",
64 "soft ceiling",
65 "missed proofs",
66];
67pub(crate) const RECORD_KINDS: [&str; 5] = ["registered", "proof", "spend", "pull", "stage"];
68pub(crate) const RESERVED_FIELDS: [&str; 3] = ["model_name", "model_confidence", "fallback_taken"];
69
70pub const DEFAULT_PROOF_INTERVAL_SECONDS: i64 = 60;
71pub const DEFAULT_MISSED_PROOF_LIMIT: i64 = 5;
72
73pub const PROOF_INTERVAL_MAX_SECONDS: i64 = 86_399_999_999_999;
74pub const SPEND_MAX: i64 = 1_000_000_000_000_000_000;
75pub const MISSED_PROOF_LIMIT_MAX: i64 = 1_000_000;
76
77/// The stage a missed proof count below the limit warrants under
78/// DM-3, keyed by the count itself, with every higher count below
79/// the limit standing at the last entry.
80pub(crate) const MISSED_COUNT_STAGES: [&str; 4] = ["warn", "throttle", "cut tools", "cut budget"];
81
82pub(crate) const DOCUMENT_KEYS: [&str; 2] = ["format", "runs"];
83pub(crate) const RUN_KEYS: [&str; 12] = [
84 "id",
85 "registered_at",
86 "proof_interval_seconds",
87 "missed_proof_limit",
88 "soft_ceiling",
89 "hard_ceiling",
90 "spend_total",
91 "last_proof_at",
92 "stage",
93 "clear_streak",
94 "killed",
95 "log",
96];
97pub(crate) const RECORD_KEYS: [&str; 8] = [
98 "seq",
99 "kind",
100 "at",
101 "stage",
102 "reasons",
103 "model_name",
104 "model_confidence",
105 "fallback_taken",
106];
107pub(crate) const RECORD_KEYS_EXTENDED: [&str; 10] = [
108 "seq",
109 "kind",
110 "at",
111 "stage",
112 "reasons",
113 "model_name",
114 "model_confidence",
115 "fallback_taken",
116 "model_verdict",
117 "model_score",
118];
119/// The eleven key shape of the Round 7 amendment, the
120/// extended shape plus the ancestry field of DM-22.
121pub(crate) const RECORD_KEYS_ANCESTRY: [&str; 11] = [
122 "seq",
123 "kind",
124 "at",
125 "stage",
126 "reasons",
127 "model_name",
128 "model_confidence",
129 "fallback_taken",
130 "model_verdict",
131 "model_score",
132 "model_ancestry",
133];
134/// The one ancestry value the ancestry pattern of DM-22
135/// carries, a fixed literal of the format naming the below
136/// bar fall seat de-escalate that stands in a step down's
137/// ancestry, and no other value stands in the field.
138pub(crate) const ANCESTRY_BELOW_BAR: &str = "de-escalate below bar";
139/// The run key set that carries the standing certification
140/// of DM-23, the landed twelve keys plus streak_ancestry,
141/// admitted only while a certification stands unconsumed.
142pub(crate) const RUN_KEYS_WITH_CARRY: [&str; 13] = [
143 "id",
144 "registered_at",
145 "proof_interval_seconds",
146 "missed_proof_limit",
147 "soft_ceiling",
148 "hard_ceiling",
149 "spend_total",
150 "last_proof_at",
151 "stage",
152 "clear_streak",
153 "killed",
154 "streak_ancestry",
155 "log",
156];
157
158/// The default System One decision model under DM-24, reached
159/// only through a caller supplied transport under DM-20.
160pub const MODEL_NAME: &str = "jev-1.14.0";
161
162/// The legacy System One model identifier recorded in the frozen
163/// Round 4 fixture corpus.
164pub const LEGACY_MODEL_NAME: &str = "jev-1.13.0";
165
166/// First-class System One decision model identifiers carried
167/// across the five provider families under DM-24.
168pub const JUDGE_MODELS: [&str; 14] = [
169 "jev-1.14.0",
170 "jev-1.13.0",
171 "gemini-decision-1",
172 "gpt-6-luna",
173 "gpt-oss-safeguard-120b",
174 "gpt-oss-safeguard-20b",
175 "pplx-decider-v1.3",
176 "pplx-decider-latest",
177 "pplx-decider-flash-v1.3",
178 "pplx-decider-flash-latest",
179 "@cf/cloudflare/clef",
180 "@cf/cloudflare/clef-flash",
181 "clef",
182 "clef-flash",
183];
184
185/// Whether a caller-supplied model identifier stands in the 1
186/// through 128 printable ASCII non-whitespace shape of DM-24.
187pub fn is_valid_model_id(name: &str) -> bool {
188 !name.is_empty()
189 && name.len() <= MODEL_NAME_MAX
190 && name.bytes().all(|byte| (0x21..=0x7e).contains(&byte))
191}
192
193/// Validate a caller-supplied model identifier under DM-24,
194/// refusing out-of-shape identifiers as InvalidRequest.
195pub fn validate_model_id(name: &str) -> Result<()> {
196 if is_valid_model_id(name) {
197 Ok(())
198 } else {
199 Err(Error::InvalidRequest(
200 "the judge model identifier must be 1 to 128 printable ASCII non-whitespace characters"
201 .to_string(),
202 ))
203 }
204}
205
206/// The confidence bars of the combination rule in DM-15. The
207/// bars are constants of the library, named in the contract,
208/// and stand in no stored field and no caller input, and the
209/// de-escalation bar stands above the escalation bar.
210pub const ESCALATION_BAR: f64 = 0.60;
211pub const DE_ESCALATION_BAR: f64 = 0.85;
212
213/// The widest model name a record can carry under DM-17.
214pub const MODEL_NAME_MAX: usize = 128;
215
216/// The longest host context the model entry point accepts
217/// under DM-14, counted in characters.
218pub const HOST_CONTEXT_MAX: usize = 2000;
219
220/// The default deadline of a judge consultation, in seconds,
221/// standing published beside the landed defaults so neither
222/// surface restates the number and the surfaces cannot drift
223/// apart on it. Its value is the 10 seconds every in-repo
224/// driver already constructs, and the Round 5 amendment names
225/// it a contract visible fact in the manner of the bars.
226pub const DEFAULT_JUDGE_TIMEOUT_SECONDS: u64 = 10;
227
228/// The widest judge timeout value the surfaces hold whole
229/// while an out of range value crosses to its refusal, one
230/// more than the widest duration a transport can hold in
231/// whole seconds. A value at or past this ceiling refuses
232/// without being held, so no surface reads a number it
233/// cannot hold to name the value it refuses.
234pub const TIMEOUT_WHOLE_CEILING: u128 = 18446744073709551616;
235
236/// The verdict labels of DM-17, in the order the doctrine
237/// names them.
238pub(crate) const VERDICT_LABELS: [&str; 3] = ["hold", "escalate", "de-escalate"];
239
240/// The fallback classes of DM-16, a closed vocabulary the
241/// record carries and the reader validates.
242pub(crate) const FALLBACK_CLASSES: [&str; 4] =
243 ["abstention", "call failed", "silence", "malformed reply"];
244
245/// The digit limit the Python int conversion and the Python json
246/// reader apply to an integer literal, probed against the Python
247/// runtime before the port was written.
248pub(crate) const INT_DIGIT_LIMIT: usize = 4300;
249
250/// The nesting depth the strict reader will read. The Python
251/// reader refuses deeper documents at its own recursion limit,
252/// and the port amendment records the bound.
253pub(crate) const MAX_NESTING: usize = 256;
254
255pub use evaluate::{Consultation, FixtureTransport, ModelTransport, StageReport, Verdict};
256pub use instant::{format_instant, parse_instant, Instant};
257pub use join::Record;
258pub use providers::{
259 cloudflare_endpoint, cloudflare_route_model, detect_provider, format_provider_request,
260 normalize_provider_reply, wire_model_name, DecisionProvider,
261};
262pub use switch::{RunSummary, Switch};
263pub use transport::{
264 consultation_from_reply_body, judge_payload_value, validate_judge_form, SubprocessTransport,
265};
266pub use validate::{
267 validate_ceiling, validate_missed_proof_limit, validate_name, validate_note,
268 validate_proof_interval, validate_spend_amount,
269};
270pub use value::{
271 py_float_repr, py_printable, py_repr_str, py_repr_value, timeout_printable, Error, Result,
272 Value,
273};