1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! JSON wrapper for the localized-vs-global maintenance decision
//! (arXiv 2606.24775).
//!
//! Stateless helper, binding-only — see
//! `docs/proposals/agent-native-memory-diagnostic.md`. Applies the paper's
//! finding that localized maintenance is more cost-efficient than global
//! reorganization: given how much is dirty, the store size, the per-strategy
//! costs, and the value of a global reorg's structural gain, decide whether to
//! patch locally or reorganize the whole store.
use car_memgine::maintenance::{decide_maintenance, MaintenanceInput};
/// Decide localized vs. global maintenance. `input_json` is a [`MaintenanceInput`]
/// (`{ dirty_regions, total_regions, localized_cost_per_region,
/// global_cost_per_region, global_structural_gain, gain_value }`; omitted fields
/// default to 0). Returns the [`car_memgine::maintenance::MaintenanceDecision`]
/// as JSON — `{ strategy: "no_op" | "localized" | "global", localized_cost,
/// global_cost, global_extra_value, global_net_advantage, rationale }`.
pub fn decide(input_json: &str) -> Result<String, String> {
let input: MaintenanceInput = crate::from_json("input", input_json)?;
let decision = decide_maintenance(&input);
crate::to_json(&decision)
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::Value;
#[test]
fn large_store_small_dirty_is_localized() {
let input = r#"{
"dirty_regions": 5, "total_regions": 1000,
"localized_cost_per_region": 1.0, "global_cost_per_region": 1.0,
"global_structural_gain": 0.1, "gain_value": 10.0
}"#;
let out = decide(input).unwrap();
let v: Value = serde_json::from_str(&out).unwrap();
assert_eq!(v["strategy"], "localized");
}
#[test]
fn empty_input_is_noop() {
let out = decide("{}").unwrap();
let v: Value = serde_json::from_str(&out).unwrap();
assert_eq!(v["strategy"], "no_op");
}
#[test]
fn invalid_json_errors() {
assert!(decide("not json").is_err());
}
}