anda_brain 0.13.4

🧠 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
use super::*;
use anda_cognitive_nexus::{
    CognitiveNexus,
    attention::{WakeResume, WakeResumeInput, WakeResumeVerifier, WakeRetry, WakeState},
    content_digest,
    nexus::{DEFAULT_SPACE, Session},
};
use futures::FutureExt;
use journal::{Dedup, Job, Reply, key};
use object_store::PutMode;
use serde_json::json;
use std::time::Duration;
use tokio::sync::Mutex;

pub struct ActionRuntime {
    pub(super) receipts: parking_lot::RwLock<Option<Arc<crate::recall_receipt::RecallReceipts>>>,
    pub(super) nexus: Arc<CognitiveNexus>,
    pub(super) directory: Arc<crate::attention::Directory>,
    pub(super) bindings: Arc<ActionBindings>,
    automatic: bool,
    evaluator: Option<RuntimePin>,
    gate: Mutex<()>,
    tasks: crate::runtime::DurableTasks,
    resume_bindings: std::sync::Mutex<std::collections::BTreeSet<String>>,
}

struct Resume {
    directory: Arc<crate::attention::Directory>,
    scope: RuntimeScope,
    reference: String,
    nexus: std::sync::Weak<CognitiveNexus>,
    timeout: Duration,
}
#[async_trait]
impl WakeResumeVerifier for Resume {
    async fn verify(&self, _input: WakeResumeInput) -> Result<bool, anda_kip::KipError> {
        let check = async {
            let job = self
                .directory
                .read::<Job>(&key(&self.scope, "jobs", &self.reference)?)
                .await?
                .ok_or("action journal missing")?
                .value;
            job.validate(&self.scope, &self.reference)?;
            if job.status.state == "pending" {
                return Ok(true);
            }
            if let Some(reference) = job.status.dispatch_ref {
                let row = self
                    .nexus
                    .upgrade()
                    .ok_or("action runtime closed")?
                    .system_session()
                    .read_control(DEFAULT_SPACE, &reference, None)
                    .await?;
                return Ok(row.is_some_and(|r| {
                    r.value["state"] == "completed" || r.value["state"] == "ready"
                }));
            }
            Ok(false)
        };
        let result: Result<bool, BoxError> = match tokio::time::timeout(self.timeout, check).await {
            Ok(result) => result,
            Err(e) => Err(e.into()),
        };
        result.map_err(|e| anda_kip::KipError::constraint_violation(e.to_string()))
    }
}

impl ActionRuntime {
    /// Trusted Rust governance access for provisioning actual host/observer
    /// Principals. This capability is never exposed through model tools.
    pub fn nexus(&self) -> Arc<CognitiveNexus> {
        self.nexus.clone()
    }
    pub async fn enabled(&self) -> Result<bool, BoxError> {
        if !self.automatic {
            return Ok(false);
        }
        let scope = self.scope().await?;
        let setting = self
            .directory
            .read::<journal::Switch>(&key(&scope, "settings", "enabled")?)
            .await?;
        match setting {
            Some(s) if s.value.scope != scope => Err("action switch scope mismatch".into()),
            Some(s) => Ok(s.value.enabled),
            None => Ok(true),
        }
    }
    /// Pauses new gate/dispatch work while Watch scheduling and retained queues
    /// continue. Already admitted writes/callbacks drain before this returns.
    pub async fn set_enabled(self: &Arc<Self>, enabled: bool) -> Result<(), BoxError> {
        let this = self.clone();
        self.tasks
            .run(async move {
                let _g = this.gate.lock().await;
                if enabled && !this.automatic {
                    return Err("isolated host cannot enable actions".into());
                }
                let scope = this.scope().await?;
                let key = key(&scope, "settings", "enabled")?;
                let old = this.directory.read::<journal::Switch>(&key).await?;
                this.directory
                    .put(
                        &key,
                        &journal::Switch { scope, enabled },
                        old.map_or(PutMode::Create, |v| PutMode::Update(v.version)),
                    )
                    .await
            })
            .await
    }
    pub(crate) fn new(
        nexus: Arc<CognitiveNexus>,
        directory: Arc<crate::attention::Directory>,
        bindings: Arc<ActionBindings>,
        automatic: bool,
        evaluator: Option<RuntimePin>,
    ) -> Arc<Self> {
        Arc::new(Self {
            evaluator,
            receipts: Default::default(),
            nexus,
            directory,
            bindings,
            automatic,
            gate: Mutex::new(()),
            tasks: Default::default(),
            resume_bindings: Default::default(),
        })
    }
    pub(crate) fn is_busy(&self) -> bool {
        self.tasks.is_busy()
    }
    pub(crate) fn bind_receipts(&self, receipts: Arc<crate::recall_receipt::RecallReceipts>) {
        *self.receipts.write() = Some(receipts);
    }
    pub(crate) async fn shutdown(&self) {
        self.tasks.shutdown().await;
    }
    pub(crate) fn per_pass(&self) -> usize {
        self.bindings.limits.per_pass
    }
    pub(crate) fn pins(&self) -> RuntimePins {
        let mut pins = self.bindings.pins();
        pins.evaluator = self.evaluator.clone();
        pins
    }
    pub(crate) async fn session(&self, scope: &RuntimeScope) -> Result<Session, BoxError> {
        let auth = self
            .bounded(self.bindings.identity.authenticate(scope))
            .await?;
        if !principal_valid(&auth.principal_id)
            || auth.auth_method.is_empty()
            || auth.auth_strength == "none"
            || !auth.delegation_chain.is_empty()
        {
            return Err("actions require a directly authenticated host principal".into());
        }
        Ok(self.nexus.session(auth))
    }
    pub(super) async fn bounded<T>(
        &self,
        future: impl std::future::Future<Output = Result<T, BoxError>>,
    ) -> Result<T, BoxError> {
        tokio::time::timeout(
            Duration::from_millis(self.bindings.limits.callbacks_ms),
            std::panic::AssertUnwindSafe(future).catch_unwind(),
        )
        .await?
        .map_err(|_| "action callback panicked")?
    }
    pub(crate) async fn install(&self) -> Result<(), BoxError> {
        if let Some(lookup) = &self.bindings.lookup {
            let s = self.nexus.system_session();
            let key = format!(
                "attention-lookup-observer/v1/{}",
                &content_digest(&json!(lookup.observer.binding))?[7..]
            );
            match s.read_control(DEFAULT_SPACE, &key, None).await? {
                Some(old) if old.value != json!(lookup.observer) => {
                    return Err(
                        "installed lookup observer differs; explicit host migration required"
                            .into(),
                    );
                }
                Some(_) => {}
                None => {
                    s.set_dispatch_lookup_observer(DEFAULT_SPACE, 0, lookup.observer.clone())
                        .await?;
                }
            }
        }
        Ok(())
    }
    pub(super) async fn load(
        &self,
        scope: &RuntimeScope,
        reference: &str,
    ) -> Result<Option<Job>, BoxError> {
        let row = self
            .directory
            .read::<Job>(&key(scope, "jobs", reference)?)
            .await?;
        if let Some(row) = &row {
            row.value.validate(scope, reference)?;
        }
        Ok(row.map(|v| v.value))
    }
    pub(super) async fn answered(
        &self,
        scope: &RuntimeScope,
        gate: &str,
        recipient: &str,
        deadline: u64,
    ) -> Result<bool, BoxError> {
        let Some(reply) = self
            .directory
            .read::<Reply>(&key(scope, "replies", gate)?)
            .await?
        else {
            return Ok(false);
        };
        let reply = reply.value;
        if &reply.scope != scope
            || reply.gate_wake_ref != gate
            || reply.principal != recipient
            || reply.received_ms >= deadline
        {
            return Err("clarification reply correlation mismatch".into());
        }
        Ok(true)
    }
    pub(super) async fn save(&self, job: &Job) -> Result<(), BoxError> {
        let key = key(&job.scope, "jobs", &job.wake_ref)?;
        let old = self.directory.read::<Job>(&key).await?;
        self.directory
            .put(
                &key,
                job,
                old.map_or(PutMode::Create, |v| PutMode::Update(v.version)),
            )
            .await
    }
    async fn scope(&self) -> Result<RuntimeScope, BoxError> {
        let row = self
            .nexus
            .system_session()
            .read_control(DEFAULT_SPACE, "attention/config", None)
            .await?
            .ok_or("attention is not registered")?;
        Ok(
            serde_json::from_value::<anda_cognitive_nexus::attention::AttentionConfig>(row.value)?
                .scope,
        )
    }
    /// Trusted Rust host read. Public recipient-filtered inboxes use runtime_api.
    pub async fn status(&self, reference: &str) -> Result<Option<ActionStatus>, BoxError> {
        let scope = self.scope().await?;
        Ok(self.load(&scope, reference).await?.map(|j| j.status))
    }
    /// An answer is data, never an approval or a business execution permit.
    pub async fn respond(
        self: &Arc<Self>,
        reference: String,
        auth: AuthContext,
        response: ClarificationResponse,
    ) -> Result<(), BoxError> {
        let this = self.clone();
        self.tasks
            .run(async move {
                let _g = this.gate.lock().await;
                let scope = this.scope().await?;
                let session = this.session(&scope).await?;
                let job = this.confirm_parent(&session, &reference, &scope).await?;
                let p = job.prepared.as_ref().ok_or("clarification unavailable")?;
                if p.decision != "ask"
                    || p.recipient.as_deref() != Some(auth.principal_id.as_str())
                    || !principal_valid(&auth.principal_id)
                    || auth.auth_method.is_empty()
                    || auth.auth_strength == "none"
                    || !auth.delegation_chain.is_empty()
                    || response.event_key.is_empty()
                    || response.event_key.len() > 256
                    || response.answer.trim().is_empty()
                    || response.answer.len() > 16_384
                {
                    return Err("invalid authenticated clarification response".into());
                }
                // Recheck current visibility of the actual ask, even on replay.
                context::element(
                    &this.nexus.session(auth.clone()),
                    job.status
                        .decision_ref
                        .as_deref()
                        .ok_or("ask decision missing")?,
                    None,
                    this.bindings.limits.callbacks_ms,
                )
                .await?;
                let storage_key = key(&scope, "replies", &reference)?;
                if let Some(old) = this.directory.read::<Reply>(&storage_key).await? {
                    if old.value.scope == scope
                        && old.value.gate_wake_ref == reference
                        && old.value.principal == auth.principal_id
                        && old.value.response == response
                    {
                        return Ok(());
                    }
                    return Err("clarification response idempotency conflict".into());
                }
                if p.reply_deadline_ms
                    .is_none_or(|t| t <= anda_engine::unix_ms())
                {
                    return Err("clarification deadline passed; answer is not consent".into());
                }
                let reply = Reply {
                    scope,
                    gate_wake_ref: reference,
                    principal: auth.principal_id,
                    received_ms: anda_engine::unix_ms(),
                    response,
                };
                this.directory
                    .put(&storage_key, &reply, PutMode::Create)
                    .await
            })
            .await
    }
    /// Explicit operator retry of retained work. It does not bypass current
    /// native authority, dispatch lookup, generation, dependency or fence checks.
    pub async fn retry(self: &Arc<Self>, reference: String) -> Result<(), BoxError> {
        let this = self.clone();
        self.tasks
            .run(async move {
                let _g = this.gate.lock().await;
                if !this.automatic {
                    return Err("isolated host cannot retry external work".into());
                }
                let scope = this.scope().await?;
                let session = this.session(&scope).await?;
                let wake = session.read_wake(DEFAULT_SPACE, &reference).await?;
                if wake.pins != this.pins()
                    || matches!(
                        wake.state,
                        WakeState::Completed { .. } | WakeState::Cancelled { .. }
                    )
                {
                    return Err("work cannot be retried under this configuration/state".into());
                }
                let mut job = this
                    .load(&scope, &reference)
                    .await?
                    .ok_or("no retained action work")?;
                job.failures = 0;
                job.status.operator_retries = job
                    .status
                    .operator_retries
                    .checked_add(1)
                    .ok_or("operator retry counter exhausted")?;
                job.status.attempts = 0;
                job.rounds = 0;
                job.status.state = "pending".into();
                job.status.next_run_ms = None;
                this.save(&job).await
            })
            .await
    }

    pub(crate) async fn process(
        self: &Arc<Self>,
        wake: WakeRecord,
    ) -> Result<ActionStatus, BoxError> {
        let this = self.clone();
        self.tasks
            .run(async move { this.process_owned(wake).await })
            .await
    }

    async fn process_owned(&self, wake: WakeRecord) -> Result<ActionStatus, BoxError> {
        let _g = self.gate.lock().await;
        if !self.automatic {
            return Err("isolated host cannot run actions".into());
        }
        if !self.enabled().await? {
            return Ok(ActionStatus {
                wake_ref: wake.wake_ref,
                state: "disabled".into(),
                reason: Some("actions_disabled_by_host".into()),
                ..Default::default()
            });
        }
        if wake.pins != self.pins() {
            return Err(
                "action binding/policy pins changed; re-arm or operator migration required".into(),
            );
        }
        let mut job = self
            .load(&wake.scope, &wake.wake_ref)
            .await?
            .unwrap_or_else(|| Job::new(&wake, 0));
        if job.pins != wake.pins {
            return Err("action journal configuration mismatch".into());
        }
        if job
            .status
            .next_run_ms
            .is_some_and(|t| t > anda_engine::unix_ms())
        {
            return Ok(job.status);
        }
        if let Err(e) = self.drive(&wake, &mut job).await {
            job.failures = job.failures.saturating_add(1);
            job.status.reason = Some(e.to_string().chars().take(1024).collect());
            job.status.state = if job.failures > self.bindings.limits.max_retries {
                "blocked"
            } else {
                "retrying"
            }
            .into();
            job.status.next_run_ms = Some(
                anda_engine::unix_ms()
                    + self
                        .bindings
                        .limits
                        .retry_ms
                        .saturating_mul(1u64 << job.failures.min(6)),
            );
            // Retain failed prepared writes for exact receipt recovery.
            // Cancellation and failures never fabricate a silence decision.
            if job.status.state == "blocked" {
                job.status.next_run_ms = None;
                if let Ok(session) = self.session(&wake.scope).await
                    && let Ok(current) = session.read_wake(DEFAULT_SPACE, &wake.wake_ref).await
                    && matches!(&current.state,WakeState::Running{lease} if lease.owner==session.auth().principal_id && lease.expires_at_ms>anda_engine::unix_ms())
                {
                    let _ = self
                        .block(&session, &current, &mut job, "binding_unavailable")
                        .await;
                }
            }
        }
        self.save(&job).await?;
        Ok(job.status)
    }

    pub(super) async fn authorize(&self, request: &ActionRequest) -> Result<(), BoxError> {
        self.bounded(self.bindings.policy.authorize(request))
            .await?;
        self.bounded(self.executor(&request.kind)?.authorize(request))
            .await
    }
    pub(super) fn executor(&self, kind: &ActionKind) -> Result<&Arc<dyn ActionExecutor>, BoxError> {
        match kind {
            ActionKind::Business => self.bindings.business.as_ref(),
            ActionKind::DeliverClarification => {
                self.bindings.clarification.as_ref().map(|c| &c.executor)
            }
        }
        .ok_or_else(|| "action binding unavailable".into())
    }
    pub(super) async fn deduplicate(
        &self,
        session: &Session,
        request: &ActionRequest,
    ) -> Result<Option<String>, BoxError> {
        let Some(id) = &request.context.deduplication_key else {
            return Ok(None);
        };
        let key = key(&request.scope, "dedup", id)?;
        let digest = content_digest(
            &json!({"kind":request.kind,"payload":request.payload,"context":request.context}),
        )?;
        if let Some(old) = self.directory.read::<Dedup>(&key).await? {
            let old = old.value;
            if old.scope != request.scope
                || old.pins != request.pins
                || old.request_digest != digest
            {
                return Ok(Some("deduplication_identity_conflict".into()));
            }
            if old.owner == request.gate_wake_ref {
                return Ok(None);
            }
            let parent = self
                .confirm_parent(session, &old.owner, &request.scope)
                .await;
            let original = session.read_wake(DEFAULT_SPACE, &old.owner).await?;
            let source = context::element(
                session,
                &original.fire.watch_ref,
                None,
                self.bindings.limits.callbacks_ms,
            )
            .await?;
            if source["attributes"]["status"] != "fired"
                || source["facets"][format!("{PROFILE}WatchState")]["arm_generation"]
                    != original.fire.arm_generation
            {
                return Ok(Some(format!(
                    "deduplication_owner_generation_changed:{}",
                    old.owner
                )));
            }
            return Ok(Some(match parent {
                Ok(parent)
                    if parent
                        .prepared
                        .as_ref()
                        .is_some_and(|p| p.decision == "act") =>
                {
                    format!("duplicate_committed_operation:{}", old.owner)
                }
                _ => format!("deduplication_owner_unresolved:{}", old.owner),
            }));
        }
        self.directory
            .put(
                &key,
                &Dedup {
                    scope: request.scope.clone(),
                    pins: request.pins.clone(),
                    owner: request.gate_wake_ref.clone(),
                    request_digest: digest,
                },
                PutMode::Create,
            )
            .await?;
        Ok(None)
    }

    fn resume_condition(&self, wake: &WakeRecord) -> Json {
        json!({"format":FORMAT,"kind":"operator_or_dispatch_reconciled","scope":wake.scope,"wake":wake.wake_ref})
    }
    fn install_resume(&self, wake: &WakeRecord) -> Result<String, BoxError> {
        let condition = self.resume_condition(wake);
        let key = content_digest(&condition)?;
        let mut registered = self
            .resume_bindings
            .lock()
            .map_err(|_| "resume registration lock poisoned")?;
        if registered.contains(&key) {
            return Ok(key);
        }
        let key = self.nexus.register_wake_resume_verifier(
            condition,
            self.bindings.policy_pin.clone(),
            Arc::new(Resume {
                directory: self.directory.clone(),
                scope: wake.scope.clone(),
                reference: wake.wake_ref.clone(),
                nexus: Arc::downgrade(&self.nexus),
                timeout: Duration::from_millis(self.bindings.limits.callbacks_ms),
            }),
        )?;
        registered.insert(key.clone());
        Ok(key)
    }
    pub(super) async fn block(
        &self,
        session: &Session,
        wake: &WakeRecord,
        job: &mut Job,
        reason: &str,
    ) -> Result<(), BoxError> {
        let digest = self.install_resume(wake)?;
        job.status.state = "blocked".into();
        job.status.next_run_ms = None;
        self.save(job).await?;
        session
            .block_wake(
                DEFAULT_SPACE,
                &wake.wake_ref,
                wake.version,
                wake.fence,
                WakeRetry {
                    reason: reason.into(),
                    resume: WakeResume::OnChange {
                        condition_digest: digest,
                    },
                },
            )
            .await?;
        Ok(())
    }
    pub(super) async fn acquire(
        &self,
        session: &Session,
        wake: &mut WakeRecord,
        job: &Job,
    ) -> Result<(), BoxError> {
        if matches!(wake.state, WakeState::Blocked { .. }) {
            self.install_resume(wake)?;
            session
                .resume_wake(DEFAULT_SPACE, &wake.wake_ref, wake.version, wake.fence)
                .await?;
            *wake = session.read_wake(DEFAULT_SPACE, &wake.wake_ref).await?;
        }
        if matches!(&wake.state,WakeState::Running{lease} if lease.owner==session.auth().principal_id && lease.expires_at_ms>anda_engine::unix_ms()+self.bindings.limits.callbacks_ms*4)
        {
            return Ok(());
        }
        let expiry = crate::kip::timestamp(anda_engine::unix_ms() + self.bindings.limits.lease_ms);
        let result = if matches!(&wake.state,WakeState::Running{lease} if lease.owner==session.auth().principal_id && lease.expires_at_ms>anda_engine::unix_ms())
        {
            session
                .renew_wake(
                    DEFAULT_SPACE,
                    &wake.wake_ref,
                    wake.version,
                    wake.fence,
                    &expiry,
                )
                .await?
        } else {
            session
                .claim_wake(
                    DEFAULT_SPACE,
                    &wake.wake_ref,
                    wake.version,
                    wake.fence,
                    &expiry,
                )
                .await?
        };
        *wake = serde_json::from_value(result["wake"].clone())?;
        let _ = job;
        Ok(())
    }
}