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
//! Authorized document snapshots and source-derived reads.

use super::*;

impl WikiService {
    /// Read counterpart of [`WikiService::search_view`]: denials read as
    /// NotFound, exactly like the scoped HTTP path.
    pub async fn read_view(
        &self,
        input: WikiReadInput,
        view: Option<&[String]>,
    ) -> Result<WikiReadOutput, WikiError> {
        let access = WikiAccess {
            actor: String::new(),
            labels: view.map(<[String]>::to_vec),
        };
        let doc = self.doc_record(input.doc_id).await?;
        self.guard_doc(&doc, &access)?;
        self.read_document(&doc, input).await
    }

    /// Test convenience; production reads always resolve an authorized snapshot.
    #[cfg(test)]
    pub(super) async fn read(&self, input: WikiReadInput) -> Result<WikiReadOutput, WikiError> {
        let doc = self.doc_record(input.doc_id).await?;
        self.read_document(&doc, input).await
    }

    /// Authorization and version selection share this immutable document snapshot.
    pub(super) async fn read_document(
        &self,
        doc: &WikiDocRecord,
        input: WikiReadInput,
    ) -> Result<WikiReadOutput, WikiError> {
        let version_id = input.version.unwrap_or(doc.current_version);
        let version = self.published_version(doc, version_id).await?;
        let is_current = version_id == doc.current_version;
        let content = &version.content;

        let mut output = WikiReadOutput {
            doc_id: doc._id,
            version_id,
            is_current,
            title: doc.title.clone(),
            status: doc.status.clone(),
            checksum: version.checksum.clone(),
            size: version.size,
            toc: None,
            content: None,
            byte_range: None,
            truncated: false,
        };

        match input.selector {
            WikiSelector::Toc => output.toc = Some(outline::outline(content)),
            WikiSelector::Section { anchor } => {
                let layout = outline::outline(content);
                let entry = layout
                    .iter()
                    .find(|entry| entry.anchor == anchor)
                    .ok_or_else(|| {
                        WikiError::NotFound(format!("section anchor {anchor} not found"))
                    })?;
                let start = entry.byte_start as usize;
                let end = floor_char_boundary(
                    content,
                    (entry.byte_end as usize).min(start + MAX_READ_BYTES),
                );
                output.truncated = end < entry.byte_end as usize;
                output.content = Some(content[start..end].to_string());
                output.byte_range = Some((start as u64, end as u64));
            }
            WikiSelector::Range { start, end } => {
                if start > end {
                    return Err(WikiError::Invalid("range start exceeds end".into()));
                }
                let start = floor_char_boundary(content, start as usize);
                let mut end = floor_char_boundary(content, end as usize);
                // Bounded like Full: Range{0, u64::MAX} must not bypass the
                // read cap.
                if end - start > MAX_READ_BYTES {
                    end = floor_char_boundary(content, start + MAX_READ_BYTES);
                    output.truncated = true;
                }
                output.content = Some(content[start..end].to_string());
                output.byte_range = Some((start as u64, end as u64));
            }
            WikiSelector::Full => {
                let end = floor_char_boundary(content, MAX_READ_BYTES);
                output.truncated = end < content.len();
                output.content = Some(content[..end].to_string());
                output.byte_range = Some((0, end as u64));
            }
        }
        Ok(output)
    }

    /// Citation verification: recomputes the chunk checksum from the
    /// immutable version content. `Invalid` means the citation does not
    /// match stored content. `Superseded` reports the version that replaced
    /// the cited one. Mismatches are never evented: any anonymous caller
    /// could otherwise flood the audit log through `/wiki/verify`.
    #[cfg(test)]
    pub(super) async fn verify(
        &self,
        actor: String,
        input: WikiVerifyInput,
        now_ms: u64,
    ) -> Result<WikiVerifyOutput, WikiError> {
        self.verify_scoped(
            &WikiAccess {
                actor,
                labels: None,
            },
            input,
            now_ms,
        )
        .await
    }

    /// Resolves the verification target: URI form first, explicit fields
    /// otherwise.
    pub(super) fn verify_target(
        &self,
        input: &WikiVerifyInput,
    ) -> Result<(u64, u64, u64, u64), WikiError> {
        match &input.uri {
            Some(uri) => {
                let (space, doc_id, version_id, start, end) = parse_citation_uri(uri)
                    .ok_or_else(|| WikiError::Invalid(format!("malformed citation uri: {uri}")))?;
                if space != self.space_id {
                    return Err(WikiError::Invalid(format!(
                        "citation belongs to space {space}, not {}",
                        self.space_id
                    )));
                }
                Ok((doc_id, version_id, start, end))
            }
            None => {
                let (Some(doc_id), Some(version_id), Some((start, end))) =
                    (input.doc_id, input.version_id, input.byte_range)
                else {
                    return Err(WikiError::Invalid(
                        "either uri or doc_id+version_id+byte_range is required".into(),
                    ));
                };
                Ok((doc_id, version_id, start, end))
            }
        }
    }

    /// Verification core over preloaded records, so batch callers (the
    /// digest citation sample) can reuse one (doc, version) load across
    /// many facts instead of re-reading version content per fact.
    pub(super) async fn verify_resolved(
        &self,
        _actor: String,
        doc: &WikiDocRecord,
        version: &WikiVersionRecord,
        (start, end): (u64, u64),
        expected: Option<&str>,
        _now_ms: u64,
    ) -> Result<WikiVerifyOutput, WikiError> {
        if version.doc_id != doc._id {
            return Ok(verify_not_found());
        }
        let Some(text) = version.content.get(start as usize..end as usize) else {
            return Ok(WikiVerifyOutput {
                status: WikiVerifyStatus::Invalid,
                current_version: Some(doc.current_version),
                checksum: None,
                quote: None,
            });
        };

        let computed = chunk_checksum(&version.checksum, start as usize, end as usize, text);
        if let Some(expected) = expected
            && expected != computed
        {
            return Ok(WikiVerifyOutput {
                status: WikiVerifyStatus::Invalid,
                current_version: Some(doc.current_version),
                checksum: Some(computed),
                quote: None,
            });
        }

        Ok(WikiVerifyOutput {
            status: if version._id == doc.current_version {
                WikiVerifyStatus::Valid
            } else {
                WikiVerifyStatus::Superseded
            },
            current_version: Some(doc.current_version),
            checksum: Some(computed),
            quote: Some(quote_excerpt(text)),
        })
    }

    /// Test-only doc lookup; production paths authorize document snapshots.
    #[cfg(test)]
    pub(crate) async fn get_doc(&self, doc_id: u64) -> Result<WikiDocInfo, WikiError> {
        Ok(self.doc_record(doc_id).await?.into())
    }

    pub(super) async fn list_docs(
        &self,
        input: WikiListDocsInput,
    ) -> Result<WikiDocListOutput, WikiError> {
        self.list_docs_inner(input, None).await
    }

    pub(super) async fn list_docs_inner(
        &self,
        input: WikiListDocsInput,
        labels: Option<&[String]>,
    ) -> Result<WikiDocListOutput, WikiError> {
        let limit = input.limit.unwrap_or(20).clamp(1, 100);
        let cursor = self.cursor_or_max(&self.docs, &input.cursor)?;

        let mut filters: Vec<Box<Filter>> = vec![
            Box::new(Filter::Field((
                "_id".to_string(),
                RangeQuery::Lt(Fv::U64(cursor)),
            ))),
            // Hide initializing sentinels (crashed creates awaiting sweep).
            Box::new(Filter::Field((
                "current_version".to_string(),
                RangeQuery::Gt(Fv::U64(0)),
            ))),
        ];
        if let Some(namespace) = &input.namespace {
            filters.push(Box::new(Filter::Field((
                "namespace".to_string(),
                RangeQuery::Eq(Fv::Text(namespace.clone())),
            ))));
        }
        if let Some(status) = &input.status {
            filters.push(Box::new(Filter::Field((
                "status".to_string(),
                RangeQuery::Eq(Fv::Text(status.clone())),
            ))));
        }
        if let Some(tag) = &input.tag {
            filters.push(Box::new(Filter::Field((
                "tags".to_string(),
                RangeQuery::Eq(Fv::Text(tag.clone())),
            ))));
        }
        if let Some(labels) = labels {
            filters.push(Box::new(acl_filter(labels)));
        }

        let mut rows: Vec<WikiDocRecord> =
            query_last_as(&self.docs, Filter::And(filters), limit).await?;
        let next_cursor = page_cursor(&rows, limit, |doc| doc._id);
        rows.retain(|doc| {
            doc.current_version > 0
                && input
                    .namespace
                    .as_ref()
                    .is_none_or(|namespace| *namespace == doc.namespace)
                && input
                    .status
                    .as_ref()
                    .is_none_or(|status| *status == doc.status)
                && input.tag.as_ref().is_none_or(|tag| doc.tags.contains(tag))
                && labels.is_none_or(|labels| {
                    WikiAccess {
                        actor: String::new(),
                        labels: Some(labels.to_vec()),
                    }
                    .allows(&doc.acl_label)
                })
        });
        Ok(WikiDocListOutput {
            docs: rows.into_iter().map(Into::into).collect(),
            next_cursor,
        })
    }

    #[cfg(test)]
    pub(super) async fn list_versions(
        &self,
        doc_id: u64,
        cursor: Option<String>,
        limit: Option<usize>,
    ) -> Result<WikiVersionListOutput, WikiError> {
        let doc = self.doc_record(doc_id).await?;
        self.document_versions(&doc, cursor, limit).await
    }

    pub(super) fn guard_doc<'a>(
        &self,
        doc: &'a WikiDocRecord,
        access: &WikiAccess,
    ) -> Result<&'a WikiDocRecord, WikiError> {
        if access.allows(&doc.acl_label) {
            Ok(doc)
        } else {
            // NotFound, not Forbidden: restricted tokens must not learn that
            // a labeled document exists.
            Err(WikiError::NotFound(format!(
                "document {} not found",
                doc._id
            )))
        }
    }

    #[cfg(test)]
    pub async fn get_doc_scoped(
        &self,
        access: &WikiAccess,
        doc_id: u64,
    ) -> Result<WikiDocInfo, WikiError> {
        let doc = self.doc_record(doc_id).await?;
        self.guard_doc(&doc, access)?;
        Ok(doc.into())
    }

    pub async fn document_scoped(
        &self,
        access: &WikiAccess,
        doc_id: u64,
        now_ms: u64,
    ) -> Result<(WikiDocInfo, Vec<WikiTocEntry>), WikiError> {
        let doc = self.doc_record(doc_id).await?;
        self.guard_doc(&doc, access)?;
        let output = self
            .read_document(
                &doc,
                WikiReadInput {
                    doc_id,
                    version: None,
                    selector: WikiSelector::Toc,
                },
            )
            .await?;
        self.audit_read(access, &output, now_ms).await;
        Ok((doc.into(), output.toc.unwrap_or_default()))
    }

    pub async fn read_scoped(
        &self,
        access: &WikiAccess,
        input: WikiReadInput,
        now_ms: u64,
    ) -> Result<WikiReadOutput, WikiError> {
        let doc = self.doc_record(input.doc_id).await?;
        self.guard_doc(&doc, access)?;
        let output = self.read_document(&doc, input).await?;
        self.audit_read(access, &output, now_ms).await;
        Ok(output)
    }

    pub(super) async fn audit_read(
        &self,
        access: &WikiAccess,
        output: &WikiReadOutput,
        now_ms: u64,
    ) {
        if self.audit_reads() {
            let _ = self
                .audit_event(
                    EVENT_WIKI_READ,
                    Some(output.doc_id),
                    Some(output.version_id),
                    access.actor.clone(),
                    BTreeMap::from([(
                        "restricted".to_string(),
                        Json::from(access.labels.is_some()),
                    )]),
                    now_ms,
                )
                .await;
        }
    }

    pub async fn list_docs_scoped(
        &self,
        access: &WikiAccess,
        input: WikiListDocsInput,
    ) -> Result<WikiDocListOutput, WikiError> {
        self.list_docs_inner(input, access.labels.as_deref()).await
    }

    pub async fn list_versions_scoped(
        &self,
        access: &WikiAccess,
        doc_id: u64,
        cursor: Option<String>,
        limit: Option<usize>,
    ) -> Result<WikiVersionListOutput, WikiError> {
        let doc = self.doc_record(doc_id).await?;
        self.guard_doc(&doc, access)?;
        self.document_versions(&doc, cursor, limit).await
    }

    pub async fn verify_scoped(
        &self,
        access: &WikiAccess,
        input: WikiVerifyInput,
        now_ms: u64,
    ) -> Result<WikiVerifyOutput, WikiError> {
        let (doc_id, version_id, start, end) = self.verify_target(&input)?;
        let doc = match self.doc_record(doc_id).await {
            Ok(doc) if access.allows(&doc.acl_label) => doc,
            Ok(_) | Err(WikiError::NotFound(_)) => return Ok(verify_not_found()),
            Err(err) => return Err(err),
        };
        let version = match self.published_version(&doc, version_id).await {
            Ok(version) => version,
            Err(WikiError::NotFound(_)) => return Ok(verify_not_found()),
            Err(err) => return Err(err),
        };
        self.verify_resolved(
            access.actor.clone(),
            &doc,
            &version,
            (start, end),
            input.checksum.as_deref(),
            now_ms,
        )
        .await
    }
}