anda_brain 0.13.5

🧠 Anda Brain (大脑) — Autonomous Graph Memory for AI Agents
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use super::*;
use anda_cognitive_nexus::{CognitiveNexus, content_digest, nexus::DEFAULT_SPACE};
use anda_db::database::AndaDB;
use anda_kip::{
    Json,
    cognitive::{EvaluationInput, EvaluationRule, EvaluationSamples},
};
use object_store::memory::InMemory;
use serde_json::json;
use std::{collections::BTreeMap, sync::Arc};

fn digest(label: &str) -> String {
    content_digest(&json!({"identity":label})).unwrap()
}

pub(super) fn plan(n: usize) -> PairedTrialPlan {
    let budget = AttemptBudget {
        tool_calls: 4,
        elapsed_ms: 10_000,
        input_tokens: 4096,
        output_tokens: 1024,
    };
    PairedTrialPlan {
        task_family: "tool_workflow.precondition.v1".into(),
        candidate_revision: "C-2".into(),
        environment_digest: digest("environment"),
        base_memory_digest: digest("same-factual-memory"),
        model_digest: digest("same-business-model"),
        budget_digest: budget.digest().unwrap(),
        tool_versions: BTreeMap::from([("workflow".into(), "1".into())]),
        observation_window: "first-attempt-within-budget".into(),
        pairs: (0..n)
            .map(|i| {
                (
                    format!("pair-{i:04}"),
                    PairCase {
                        task_digest: digest(&format!("task-{i}")),
                        initial_state_digest: digest(&format!("state-{i}")),
                        seed: i.to_string(),
                    },
                )
            })
            .collect(),
        alpha: 0.05,
        minimum_effect: 0.1,
        execution: ExecutionContract {
            workflow: super::execution::pin(&workflow_contract()).unwrap(),
            budget,
            cutoff: "2099-01-01T00:00:00.000Z".into(),
            review_due_at: "2099-01-02T00:00:00.000Z".into(),
            maximum_failure_rate: 0.25,
            review_of: None,
        },
    }
}

fn fixture(n: usize, baseline_success: bool, candidate_success: bool) -> EvaluationInput {
    let p = plan(n);
    let mut attempts = serde_json::Map::new();
    let mut outcomes = serde_json::Map::new();
    let mut baseline_refs = Vec::new();
    for (i, pair_id) in p.pairs.keys().enumerate() {
        for (arm, success) in [(0, baseline_success), (1, candidate_success)] {
            let reference = format!("X-{}", 1 + i * 2 + arm);
            if arm == 0 {
                baseline_refs.push(reference.clone());
            }
            let revisions = if arm == 0 {
                Vec::new()
            } else {
                p.treatment_revisions()
            };
            attempts.insert(
                reference.clone(),
                json!({"principal_id":"actor","record":{
                    "attempt_id":format!("attempt-{i}-{arm}"),
                    "context":p.attempt_context(pair_id).unwrap(),
                    "selection_policy":p.pin().unwrap(),
                    "environment_digest":p.environment_digest,
                    "tool_versions":p.tool_versions,
                    "preconditions_satisfied":"yes",
                    "applied_revisions":revisions,
                    "trial_ref":if arm == 0 { Json::Null } else { json!("X-99999") }
                }}),
            );
            outcomes.insert(format!("E-{}", 1 + i * 2 + arm), json!({
                "principal_id":"independent-instrument", "status":"active", "corrected_by":[],
                "record":{"task_family":p.task_family,"attempt_ref":reference,"terminal":true,"metric":"success",
                    "observer_config_digest":p.execution.observer_configuration_digest().unwrap(),
                    "window":p.observation_window,"outcome_status":if success {"success"} else {"failure"}}
            }));
        }
    }
    EvaluationInput {
        rule: paired_rule_artifact(),
        parameters: p.artifact().unwrap(),
        trial: json!({"parameters":p.pin().unwrap(),"comparability":p.comparability(&digest("observers")).unwrap(),
            "revision_refs":p.treatment_revisions(),"quota":n,"observation_window":p.observation_window,
            "baseline_attempt_refs":baseline_refs}),
        attempts: Json::Object(attempts),
        outcomes: Json::Object(outcomes),
        samples: EvaluationSamples::default(),
        minimum_independent_attempts: 2,
    }
}

#[test]
fn paired_rule_requires_incremental_improvement_and_uncertainty() {
    let improved = PairedRule.evaluate(&fixture(64, false, true)).unwrap();
    assert_eq!(improved["status"], "improved");
    assert_eq!(improved["effect"], 1.0);
    assert!(improved["uncertainty"]["lower_bound"].as_f64().unwrap() > 0.1);
    assert_eq!(
        PairedRule.evaluate(&fixture(64, true, true)).unwrap()["status"],
        "not_improved"
    );
    assert_eq!(
        PairedRule.evaluate(&fixture(64, true, false)).unwrap()["effect"],
        -1.0
    );
    // A perfect outcome on two pairs cannot clear this uncertainty bound.
    assert_eq!(
        PairedRule.evaluate(&fixture(2, false, true)).unwrap()["status"],
        "not_improved"
    );
}

#[test]
fn fixed_cohort_prevents_early_stopping_and_cherry_picking() {
    let mut input = fixture(64, false, true);
    input.attempts.as_object_mut().unwrap().remove("X-2");
    input.outcomes.as_object_mut().unwrap().remove("E-2");
    assert_eq!(
        PairedRule.evaluate(&input).unwrap()["status"],
        "insufficient"
    );

    input.attempts.as_object_mut().unwrap().remove("X-1");
    input.outcomes.as_object_mut().unwrap().remove("E-1");
    assert!(
        PairedRule.evaluate(&input).is_err(),
        "baseline cannot be trimmed after enrollment"
    );
}

#[test]
fn missing_treatment_is_failure_missing_baseline_cannot_invent_gain() {
    let mut input = fixture(64, false, true);
    input.outcomes.as_object_mut().unwrap().remove("E-2");
    assert_eq!(PairedRule.evaluate(&input).unwrap()["effect"], 63.0 / 64.0);
    input.outcomes.as_object_mut().unwrap().remove("E-1");
    assert_eq!(
        PairedRule.evaluate(&input).unwrap()["status"],
        "insufficient"
    );
    assert!(PairedRule.evaluate(&input).unwrap()["effect"].is_null());

    let mut input = fixture(8, false, true);
    input.outcomes["E-1"]["record"]["outcome_status"] = json!("unknown");
    assert_eq!(
        PairedRule.evaluate(&input).unwrap()["status"],
        "insufficient"
    );
    let mut input = fixture(8, false, true);
    input.outcomes["E-2"]["record"]["outcome_status"] = json!("unknown");
    assert_eq!(PairedRule.evaluate(&input).unwrap()["effect"], 7.0 / 8.0);
}

#[test]
fn wrong_family_and_nonterminal_or_corrected_outcomes_are_rejected() {
    let mut partial = fixture(8, false, true);
    partial.outcomes["E-2"]["record"]["outcome_status"] = json!("partial");
    partial.outcomes["E-2"]["record"]["magnitude"] = json!(1.0);
    assert!(
        PairedRule.evaluate(&partial).is_err(),
        "a partial receipt cannot masquerade as complete workflow success"
    );
    for (field, value) in [
        ("task_family", json!("another-family")),
        ("metric", json!("latency")),
        ("window", json!("another-window")),
        ("terminal", json!(false)),
        ("observer_config_digest", json!(digest("wrong-instrument"))),
    ] {
        let mut input = fixture(8, false, true);
        input.outcomes["E-2"]["record"][field] = value;
        assert!(PairedRule.evaluate(&input).is_err(), "{field}");
    }
    let mut input = fixture(8, false, true);
    input.outcomes["E-2"]["corrected_by"] = json!(["E-999"]);
    assert!(PairedRule.evaluate(&input).is_err());
}

#[test]
fn changed_pair_pins_and_cross_trial_attempts_are_rejected() {
    for field in [
        "task_digest",
        "initial_state_digest",
        "seed",
        "base_memory_digest",
        "model_digest",
        "budget_digest",
        "task_family",
        "pair_id",
        "stratum",
    ] {
        let mut input = fixture(8, false, true);
        input.attempts["X-2"]["record"]["context"][field] = json!("changed");
        assert!(PairedRule.evaluate(&input).is_err(), "{field}");
    }
    for (field, value) in [
        ("environment_digest", json!(digest("changed"))),
        ("tool_versions", json!({"workflow":"2"})),
        ("applied_revisions", json!(["C-3"])),
        ("trial_ref", json!("X-100000")),
        (
            "selection_policy",
            json!({"content_digest":digest("changed")}),
        ),
    ] {
        let mut input = fixture(8, false, true);
        input.attempts["X-2"]["record"][field] = value;
        assert!(PairedRule.evaluate(&input).is_err(), "{field}");
    }
}

#[test]
fn duplicate_observations_do_not_create_independent_successes() {
    let mut input = fixture(8, false, true);
    input.outcomes["E-999"] = input.outcomes["E-2"].clone();
    assert!(PairedRule.evaluate(&input).is_err());
    let mut input = fixture(8, false, true);
    input.attempts["X-999"] = input.attempts["X-2"].clone();
    assert!(PairedRule.evaluate(&input).is_err());
    let mut input = fixture(8, false, true);
    input.attempts["X-1"]["record"]["applied_revisions"] = json!(["C-2"]);
    assert!(
        PairedRule.evaluate(&input).is_err(),
        "control must not apply the candidate"
    );
}

#[test]
fn frozen_parameters_and_rule_cannot_be_silently_replaced() {
    let mut legacy = fixture(8, false, true);
    legacy.rule = json!({"engine":"anda-brain:paired-bounded-v1"});
    assert!(
        PairedRule.evaluate(&legacy).is_err(),
        "the old rule digest cannot acquire new budget/failure semantics"
    );
    let mut input = fixture(8, false, true);
    input.parameters["minimum_effect"] = json!(0.001);
    assert!(PairedRule.evaluate(&input).is_err());
    let mut input = fixture(8, false, true);
    input.rule = json!({"engine":"kip:binary-stratified-v1"});
    assert!(PairedRule.evaluate(&input).is_err());
    let mut input = fixture(8, false, true);
    input.minimum_independent_attempts = 100;
    assert_eq!(
        PairedRule.evaluate(&input).unwrap()["status"],
        "insufficient"
    );
}

#[test]
fn manifest_rejects_unbounded_or_duplicate_experiments() {
    let mut p = plan(8);
    p.pairs
        .insert("duplicate".into(), p.pairs.values().next().unwrap().clone());
    assert!(p.validate().is_err());
    for alpha in [0.0, 1.0, f64::NAN, f64::INFINITY] {
        let mut p = plan(8);
        p.alpha = alpha;
        assert!(p.validate().is_err());
    }
    let mut p = plan(8);
    p.minimum_effect = 0.0;
    assert!(p.validate().is_err());
    let mut p = plan(8);
    p.model_digest = "unknown".into();
    assert!(p.validate().is_err());
    assert!(plan(1).validate().is_err());
    let mut artifact = plan(8).artifact().unwrap();
    artifact["incumbent_revisions"] = json!(["C-3"]);
    assert!(
        serde_json::from_value::<PairedTrialPlan>(artifact).is_err(),
        "v1 cannot silently ignore extra applied Skills"
    );
}

#[test]
fn execution_contract_freezes_budget_cutoff_review_and_absolute_failure_gate() {
    let p = plan(64);
    assert!(
        p.execution
            .validate_settlement(&p.execution.cutoff, "2098-12-31T23:59:59.999Z")
            .is_err()
    );
    assert!(
        p.execution
            .validate_settlement("2099-01-01T00:00:01.000Z", "2099-01-02T00:00:00.000Z")
            .is_err()
    );
    assert!(
        p.execution
            .validate_settlement(&p.execution.cutoff, "2099-01-02T00:00:00.000Z")
            .is_ok()
    );
    let mut changed = p.clone();
    changed.execution.budget.tool_calls += 1;
    assert!(
        changed.validate().is_err(),
        "a changed budget must change its identity"
    );
    let mut changed = p.clone();
    changed.execution.review_due_at = p.execution.cutoff.clone();
    assert!(changed.validate().is_err());
    let mut changed = p.clone();
    changed.execution.review_of = Some(AdoptionBasis {
        revision_ref: "C-99".into(),
        trial_ref: "X-1".into(),
        evaluation_ref: "X-2".into(),
    });
    assert!(changed.validate().is_err());
    assert_eq!(workflow_contract()["task_family"], p.task_family);

    let mut input = fixture(64, false, true);
    // Still a large comparative gain, but one failure breaches a frozen zero
    // failure ceiling. Passing the interval alone cannot confer improvement.
    let mut strict = p;
    strict.execution.maximum_failure_rate = 0.0;
    input.parameters = strict.artifact().unwrap();
    input.trial["parameters"] = json!(strict.pin().unwrap());
    for wrapped in input.attempts.as_object_mut().unwrap().values_mut() {
        wrapped["record"]["selection_policy"] = json!(strict.pin().unwrap());
    }
    input.outcomes["E-2"]["record"]["outcome_status"] = json!("failure");
    let result = PairedRule.evaluate(&input).unwrap();
    assert!(result["uncertainty"]["lower_bound"].as_f64().unwrap() > 0.1);
    assert_eq!(result["status"], "not_improved");
}

#[tokio::test]
async fn manifest_is_persisted_and_rule_registration_is_restored_explicitly() {
    let db = Arc::new(
        AndaDB::connect(
            Arc::new(InMemory::new()),
            crate::testkit::db_config("paired_rule_registry"),
        )
        .await
        .unwrap(),
    );
    let nexus = CognitiveNexus::connect(db.clone()).await.unwrap();
    let p = plan(8);
    let pin = nexus
        .system_session()
        .put_artifact(DEFAULT_SPACE, p.artifact().unwrap(), vec![])
        .await
        .unwrap();
    assert_eq!(pin, p.pin().unwrap());
    let rule_digest = register_paired_rule(&nexus).unwrap();
    assert_eq!(
        rule_digest,
        content_digest(&paired_rule_artifact()).unwrap()
    );
    assert!(
        register_paired_rule(&nexus).is_err(),
        "a digest cannot be rebound"
    );
    assert_eq!(
        nexus
            .store
            .evaluation_rules
            .evaluate(&fixture(8, false, true))
            .unwrap()["status"],
        "improved"
    );
    let restarted = CognitiveNexus::connect(db.clone()).await.unwrap();
    assert_eq!(
        restarted
            .system_session()
            .read_artifact(DEFAULT_SPACE, &pin)
            .await
            .unwrap(),
        p.artifact().unwrap()
    );
    assert!(
        restarted
            .store
            .evaluation_rules
            .evaluate(&fixture(8, false, true))
            .is_err(),
        "stored artifact cannot execute code"
    );
    register_paired_rule(&restarted).unwrap();
    assert_eq!(
        restarted
            .store
            .evaluation_rules
            .evaluate(&fixture(8, false, true))
            .unwrap()["status"],
        "improved"
    );
    db.close().await.unwrap();
}