anda_brain 0.13.1

🧠 Anda Brain (大脑) — Autonomous Graph Memory for AI Agents
Documentation
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
//! Offline, read-only protocol documentation. All bytes are compiled in; a
//! document ID is a registry key, never a path or URL to open.
use anda_core::{BoxError, FunctionDefinition, Json, Resource, Tool, ToolOutput};
use anda_engine::context::BaseCtx;
use serde::Deserialize;
use serde_json::json;
use std::sync::LazyLock;

const VERSION: &str = "0.14.0";
const PAGE_BYTES: usize = 8192;

pub(crate) const INSTRUCTIONS: &str = "# Embedded KIP references\n\
The complete KIP 2.0 syntax, role cards, Cognitive Memory Profile and this deployment's policy \
are already in system context. This host preloads the full syntax for every call, including \
routine operations; no reference-tool call is needed before writing your first command. \
Construct commands using the supplied KIP 2.0 syntax, not SQL, Cypher or legacy KIP syntax. \
Markdown links and bare document paths in these texts are source citations, not readable files. \
Use kip_reference for additional protocol details; it requires no filesystem or network access. \
Call with document=\"index\", section=null, offset=0 to discover available documents. \
Use document=\"syntax\" with section=\"kql\", \"kml\", \"meta\" or \"envelope\" for common topics. \
For any Markdown document, section=\"index\" lists exact heading names; pass one as section to read it. \
section=null reads the document. Follow next_offset within the same document and section until null \
when you need the rest; a page is not the whole reference. Unknown documents or sections are unavailable, \
never inferred from a link. Translations and unlisted background links are source citations only. \
Reference text describes the protocol, not this host's permissions or enabled capabilities; \
the listed tools and host capability constraints still govern every operation.";

struct Document {
    id: &'static str,
    source: &'static str,
    content: &'static str,
}

macro_rules! doc {
    ($id:literal, $source:literal, $content:expr) => {
        Document {
            id: $id,
            source: $source,
            content: $content,
        }
    };
}

static DOCUMENTS: LazyLock<Vec<Document>> = LazyLock::new(|| {
    vec![
        doc!("syntax", "KIPSyntax.md", anda_kip::KIP_SYNTAX),
        doc!(
            "profile",
            "profiles/CognitiveMemoryProfile-2.0.md",
            anda_kip::COGNITIVE_MEMORY_PROFILE
        ),
        doc!("recall", "brain/KIPRecall.md", anda_kip::KIP_RECALL_CARD),
        doc!(
            "formation",
            "brain/KIPFormation.md",
            anda_kip::KIP_FORMATION_CARD
        ),
        doc!(
            "maintenance",
            "brain/KIPMaintenance.md",
            anda_kip::KIP_MAINTENANCE_CARD
        ),
        doc!(
            "consistency",
            "Cognitive-Consistency.md (KIP-2.0-Cognitive-Consistency.md)",
            anda_kip::COGNITIVE_CONSISTENCY
        ),
        doc!(
            "memory-interface",
            "Memory-Interface.md (KIP-2.0-Memory-Interface.md)",
            anda_kip::MEMORY_INTERFACE
        ),
        doc!(
            "memory-interface-card",
            "brain/MemoryInterface.md",
            anda_kip::MEMORY_AGENT_CARD
        ),
        doc!(
            "brain-runtime",
            "brain/Brain-Runtime.md (brain/KIP-2.0-Brain-Runtime.md)",
            anda_kip::BRAIN_RUNTIME
        ),
        doc!(
            "validated-learning",
            "brain/Validated-Learning.md (brain/KIP-2.0-Validated-Learning.md)",
            anda_kip::VALIDATED_LEARNING
        ),
        doc!(
            "specification",
            "SPECIFICATION.md (KIP-2.0-SPECIFICATION.md)",
            include_str!("../assets/kip-reference/SPECIFICATION.md")
        ),
        doc!(
            "invariants",
            "Invariants.md (KIP-2.0-Invariants.md)",
            include_str!("../assets/kip-reference/Invariants.md")
        ),
        doc!(
            "experience-learning",
            "brain/ExperienceLearningArchitecture.md",
            include_str!("../assets/kip-reference/brain/ExperienceLearningArchitecture.md")
        ),
        doc!(
            "grammar-kql",
            "grammar/KQL.ebnf (KIP-2.0-KQL.ebnf)",
            include_str!("../assets/kip-reference/grammar/KQL.ebnf")
        ),
        doc!(
            "grammar-kml",
            "grammar/KML.ebnf (KIP-2.0-KML.ebnf)",
            include_str!("../assets/kip-reference/grammar/KML.ebnf")
        ),
        doc!(
            "grammar-meta",
            "grammar/META.ebnf (KIP-2.0-META.ebnf)",
            include_str!("../assets/kip-reference/grammar/META.ebnf")
        ),
        doc!(
            "schema-request",
            "schemas/kip-request.schema.json",
            include_str!("../assets/kip-reference/schemas/kip-request.schema.json")
        ),
        doc!(
            "schema-response",
            "schemas/kip-response.schema.json",
            include_str!("../assets/kip-reference/schemas/kip-response.schema.json")
        ),
        doc!(
            "schema-common",
            "schemas/kip-common.schema.json",
            anda_kip::COMMON_SCHEMA
        ),
        doc!(
            "schema-projection",
            "schemas/kip-projection.schema.json",
            anda_kip::PROJECTION_SCHEMA
        ),
        doc!(
            "schema-memory",
            "schemas/kip-memory.schema.json",
            anda_kip::MEMORY_SCHEMA
        ),
        doc!(
            "schema-records",
            "schemas/kip-cognitive-records.schema.json",
            anda_kip::COGNITIVE_RECORDS_SCHEMA
        ),
        doc!(
            "schema-element",
            "schemas/kip-element.schema.json",
            anda_kip::ELEMENT_SCHEMA
        ),
        doc!(
            "schema-package",
            "schemas/kip-schema-package.schema.json",
            anda_kip::SCHEMA_PACKAGE_SCHEMA
        ),
    ]
});

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct ReferenceArgs {
    document: String,
    section: Option<String>,
    #[serde(default)]
    offset: usize,
}

/// (byte offset, heading level, exact heading text), excluding fenced examples.
fn headings(text: &str) -> Vec<(usize, usize, &str)> {
    let mut result = Vec::new();
    let mut offset = 0;
    let mut fence: Option<(char, usize)> = None;
    for line in text.split_inclusive('\n') {
        let trimmed = line.trim();
        let marker = trimmed.chars().next().unwrap_or(' ');
        let run = trimmed.chars().take_while(|&c| c == marker).count();
        if let Some((character, length)) = fence {
            if marker == character && run >= length && trimmed[run..].trim().is_empty() {
                fence = None;
            }
        } else if matches!(marker, '`' | '~') && run >= 3 {
            fence = Some((marker, run));
        } else if marker == '#' && run <= 6 && trimmed.as_bytes().get(run) == Some(&b' ') {
            result.push((offset, run, trimmed[run..].trim()));
        }
        offset += line.len();
    }
    result
}

fn section_text<'a>(content: &'a str, section: &str) -> Result<&'a str, BoxError> {
    let headings = headings(content);
    let matches: Vec<_> = headings
        .iter()
        .enumerate()
        .filter(|(_, h)| h.2 == section)
        .collect();
    if matches.len() != 1 {
        return Err(
            "unknown or ambiguous reference section; use section=index for exact headings".into(),
        );
    }
    let (index, &(start, level, _)) = matches[0];
    let end = headings[index + 1..]
        .iter()
        .find(|h| h.1 <= level)
        .map_or(content.len(), |h| h.0);
    Ok(&content[start..end])
}

fn page(text: &str, offset: usize) -> Result<(&str, Option<usize>), BoxError> {
    if offset > text.len() || !text.is_char_boundary(offset) {
        return Err("reference offset must be an in-range UTF-8 byte boundary".into());
    }
    let mut end = offset.saturating_add(PAGE_BYTES).min(text.len());
    while !text.is_char_boundary(end) {
        end -= 1;
    }
    Ok((&text[offset..end], (end < text.len()).then_some(end)))
}

#[derive(Clone)]
pub(crate) struct KipReferenceTool;

impl KipReferenceTool {
    pub const NAME: &'static str = "kip_reference";

    pub fn execute(args: ReferenceArgs) -> Result<Json, BoxError> {
        if args.document.len() > 64 || args.section.as_ref().is_some_and(|s| s.len() > 512) {
            return Err("reference identifier too long".into());
        }
        let generated;
        let (source, content) = if args.document == "index" {
            if args.section.is_some() {
                return Err("document index requires section=null".into());
            }
            generated = DOCUMENTS
                .iter()
                .map(|d| format!("{}: {}\n", d.id, d.source))
                .collect::<String>();
            ("embedded document catalogue", generated.as_str())
        } else {
            let doc = DOCUMENTS
                .iter()
                .find(|d| d.id == args.document)
                .ok_or("unknown reference document; use document=index")?;
            let section = match (doc.id, args.section.as_deref()) {
                ("syntax", Some("kql")) => Some("2. KQL — Read"),
                ("syntax", Some("kml")) => Some("3. KML — Write"),
                ("syntax", Some("meta")) => Some("4. META — Ground, Verify, Inspect"),
                ("syntax", Some("envelope")) => Some("5. Runtime Envelope"),
                (_, section) => section,
            };
            let content = match section {
                None => doc.content,
                Some("index") => {
                    generated = headings(doc.content)
                        .iter()
                        .map(|h| format!("{}\n", h.2))
                        .collect::<String>();
                    generated.as_str()
                }
                Some(section) => section_text(doc.content, section)?,
            };
            (doc.source, content)
        };
        let (text, next_offset) = page(content, args.offset)?;
        Ok(
            json!({"document":args.document, "section":args.section, "source":source,
            "crate_version":VERSION, "protocol":"KIP 2.0", "offset":args.offset,
            "total_bytes":content.len(), "next_offset":next_offset, "content":text}),
        )
    }
}

static DEFINITION: LazyLock<FunctionDefinition> = LazyLock::new(|| {
    serde_json::from_value(json!({
    "name":KipReferenceTool::NAME,
    "description":"Read compiled-in, version-pinned KIP documentation without file/network access or state changes. document=index lists IDs. section=index lists exact Markdown headings; section=null reads the whole document. syntax also accepts kql/kml/meta/envelope. Each page contains at most 8192 UTF-8 bytes of text; follow next_offset with the same document and section. Reference availability grants no execution permission.",
    "parameters":{"type":"object","additionalProperties":false,"properties":{
        "document":{"type":"string","description":"Exact registry ID; use index to discover IDs. No paths or URLs."},
        "section":{"type":["string","null"],"description":"Exact heading, syntax topic alias, index, or null."},
        "offset":{"type":"integer","minimum":0,"description":"UTF-8 byte offset within the selected text, initially 0; then use next_offset."}
    },"required":["document","section","offset"]}
})).unwrap()
});

impl Tool<BaseCtx> for KipReferenceTool {
    type Args = ReferenceArgs;
    type Output = Json;
    fn name(&self) -> String {
        Self::NAME.into()
    }
    fn description(&self) -> String {
        DEFINITION.description.clone()
    }
    fn definition(&self) -> FunctionDefinition {
        DEFINITION.clone()
    }
    async fn call(
        &self,
        _ctx: BaseCtx,
        args: ReferenceArgs,
        _resources: Vec<Resource>,
    ) -> Result<ToolOutput<Json>, BoxError> {
        Ok(ToolOutput::new(Self::execute(args)?))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use sha2::{Digest, Sha256};

    #[test]
    fn embedded_assets_match_the_pinned_release_manifest() {
        let manifest: Json =
            serde_json::from_str(include_str!("../assets/kip-reference/manifest.json")).unwrap();
        assert_eq!(manifest["version"], VERSION);
        for (source, expected) in manifest["sha256"].as_object().unwrap() {
            let document = DOCUMENTS
                .iter()
                .find(|d| d.source.split(" (").next() == Some(source.as_str()))
                .unwrap();
            assert_eq!(
                Sha256::digest(document.content.as_bytes())
                    .iter()
                    .map(|byte| format!("{byte:02x}"))
                    .collect::<String>(),
                expected.as_str().unwrap(),
                "{source}"
            );
        }
    }

    #[test]
    fn compiled_references_are_available_without_source_files() {
        for doc in DOCUMENTS.iter() {
            let mut offset = 0;
            let mut complete = String::new();
            loop {
                let result = KipReferenceTool::execute(ReferenceArgs {
                    document: doc.id.into(),
                    section: None,
                    offset,
                })
                .unwrap();
                let content = result["content"].as_str().unwrap();
                assert!(content.len() <= PAGE_BYTES);
                complete.push_str(content);
                match result["next_offset"].as_u64() {
                    Some(next) => {
                        assert!(next as usize > offset);
                        offset = next as usize;
                    }
                    None => break,
                }
            }
            assert_eq!(complete, doc.content, "{}", doc.id);
        }
    }

    #[test]
    fn required_references_and_topics_are_registered() {
        for id in [
            "syntax",
            "profile",
            "specification",
            "consistency",
            "invariants",
            "experience-learning",
            "grammar-kql",
            "grammar-kml",
            "grammar-meta",
            "schema-request",
            "schema-response",
            "memory-interface",
            "brain-runtime",
            "validated-learning",
            "schema-common",
        ] {
            assert!(DOCUMENTS.iter().any(|d| d.id == id));
        }
        for section in ["kql", "kml", "meta", "envelope"] {
            let result = KipReferenceTool::execute(ReferenceArgs {
                document: "syntax".into(),
                section: Some(section.into()),
                offset: 0,
            })
            .unwrap();
            assert!(!result["content"].as_str().unwrap().is_empty());
        }
        let spec = DOCUMENTS.iter().find(|d| d.id == "specification").unwrap();
        for (_, _, heading) in headings(spec.content) {
            assert!(section_text(spec.content, heading).is_ok(), "{heading}");
        }
    }

    #[test]
    fn sections_ignore_code_fences_and_include_subsections() {
        let text = "# First\n```text\n# Fake\n```\n## Child\nyes\n# Next\nno\n";
        assert_eq!(
            section_text(text, "First").unwrap(),
            "# First\n```text\n# Fake\n```\n## Child\nyes\n"
        );
        assert!(section_text(text, "Fake").is_err());
        assert_eq!(section_text(text, "Child").unwrap(), "## Child\nyes\n");
    }

    #[test]
    fn invalid_identifiers_and_offsets_fail_closed() {
        for document in ["../SPECIFICATION.md", "https://example.com", "unknown"] {
            assert!(
                KipReferenceTool::execute(ReferenceArgs {
                    document: document.into(),
                    section: None,
                    offset: 0
                })
                .is_err()
            );
        }
        assert!(section_text(anda_kip::KIP_SYNTAX, "unknown").is_err());
        assert!(page("🧠", 1).is_err());
        assert!(page("🧠", usize::MAX).is_err());
        let text = "🧠".repeat(PAGE_BYTES);
        assert_eq!(page(&text, 0).unwrap().1, Some(PAGE_BYTES));
        assert!(
            serde_json::from_value::<ReferenceArgs>(
                json!({"document":"syntax","operation":"arm_watch"})
            )
            .is_err()
        );
    }
}