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
//! # anda_kip — a Rust implementation of KIP 2.0
//!
//! **🧬 KIP (Knowledge Interaction Protocol)** is a cognitive state protocol
//! between an Agent and a persistent **Cognitive Nexus**. KIP 2.0 is not a
//! bigger KIP 1.x: it separates the things 1.x kept in one graph —
//!
//! ```text
//! meaning · belief · evidence · provenance · mnemonic state · retention · Governance · Schema
//! ```
//!
//! — because collapsing them is what lets a memory system confidently repeat
//! things nobody ever claimed. The single distinction the rest follows from:
//!
//! ```text
//! a Proposition existing ≠ the Proposition being true
//! ```
//!
//! A Proposition is a truth-neutral tuple. An Assertion is one actor's
//! commitment about it, with a stance, a mode, a confidence and its Evidence.
//! What is *currently believed* is projected from those, never stored.
//!
//! ## What is public
//!
//! `pub` means API. Each module is re-exported wholesale at the crate root,
//! so an item is either part of the contract this crate keeps or it is
//! `pub(crate)`; the nom combinators, the raw single-surface sub-parsers, the
//! validation gates behind [`Operation::parse`] and the semantic checks the
//! parsers apply are the latter. `tests/surface.rs` lists every public item
//! from the sources and compares the list with
//! `tests/fixtures/public_surface.txt`, so widening or narrowing the surface
//! is a change to that file, made on purpose.
//!
//! ## What this crate is
//!
//! The protocol half: parse, classify, validate. Everything that needs state —
//! Schema resolution, Governance, transactions, projection — belongs to an
//! engine behind the [`Executor`] trait.
//!
//! - [`ast`] — the executable AST, field-for-field compatible with the
//! reference toolkit `@ldclabs/kip-lang`, so a Rust engine and a TypeScript
//! one can be differentially tested against each other;
//! - [`parser`] — nom parsers for the three surfaces, enforcing the
//! schema-independent rules as they parse;
//! - [`semantics`] — the Core Package registries (§20.13) and the other rules
//! decidable without a Schema Environment;
//! - [`error`] — the Core Error Registry (§87) with categories and retry
//! classes;
//! - [`request`] — the runtime envelope (§71–§85);
//! - [`types`] — the Core data model (§6–§19);
//! - [`capsule`] — portable Cognitive Capsules (§37–§41);
//! - [`conformance`] — the profile names an implementation declares (§89);
//! - [`executor`] — the engine seam.
//!
//! ## Standards compliance
//!
//! Follows the official KIP 2.0 specification, a copy of which ships with this
//! crate as `SPECIFICATION.md`, alongside the LLM-facing `KIPSyntax.md`.
//!
//! **👉 [KIP Specification](https://github.com/ldclabs/KIP)**
//!
//! ## Quick start
//!
//! ```rust
//! use anda_kip::{Command, parse_kip};
//!
//! // Read raw claims — truth-neutral.
//! let read = parse_kip(
//! r#"
//! FIND(?a.asserted_by, ?a.confidence)
//! WHERE {
//! ?p (:alice, "timezone", ?tz)
//! ?a ASSERTION {proposition: ?p}
//! }
//! ORDER BY ?a.confidence DESC
//! LIMIT 10
//! "#,
//! )
//! .unwrap();
//! assert!(matches!(read, Command::Kql(_)));
//!
//! // Read what is currently believed — a Projection, not stored state.
//! let belief = parse_kip(
//! r#"FIND(?b) WHERE { ?b BELIEF (:alice, "timezone", ?tz) }"#,
//! )
//! .unwrap();
//! assert!(matches!(belief, Command::Kql(_)));
//!
//! // Record an attributed claim. `by` and `mode` have no safe default.
//! let write = parse_kip(
//! r#"ASSERT (:alice, "prefers", :dark_mode) {
//! by: :alice,
//! mode: "stated",
//! confidence: 0.9,
//! evidence: :msg
//! }"#,
//! )
//! .unwrap();
//! assert!(write.is_mutation());
//! ```
//!
//! Changing your mind never rewrites history:
//!
//! ```rust
//! use anda_kip::parse_kml;
//!
//! // Correcting a claim is a new Assertion plus supersession.
//! let revision = parse_kml(
//! r#"ASSERT ?new (:alice, "timezone", "+09:00") { by: :alice, mode: "stated" }
//! SUPERSEDING :old"#,
//! )
//! .unwrap();
//! assert_eq!(revision.clauses.len(), 3);
//!
//! // Rewriting the old one is rejected before it reaches an engine.
//! assert!(
//! parse_kml(
//! r#"UPDATE ?a SET FIELDS { confidence: 0.1 }
//! WHERE { ?a ASSERTION {id: "A-1"} }"#,
//! )
//! .is_err()
//! );
//! ```
// Works around a trait-solver regression in Rust 1.98.0: this lint asks "would
// the bound still hold without the `&`?", which re-enters selection with the
// borrow stripped, and on this crate that recursion does not terminate —
// `evaluate_predicate_recursively` → `enter_forall::<HostEffectPredicate>` →
// itself, until the process is ~19 GB and the machine gives up. On a GitHub
// runner the OOM takes the agent with it, so the job reports `exit code 143`
// with no diagnostic at all, which is how this cost an afternoon to find.
//
// Scoped to the crate that reproduces it, and to the one lint: 1.97.1 checks
// this crate in seconds, and every other Clippy lint still runs here. Plain
// `rustc` is unaffected — only Clippy re-runs selection this way. Remove the
// attribute once the toolchain no longer hangs; the test for that is simply
// `cargo clippy -p anda_kip --lib` on a newer stable.
use LazyLock;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use ;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
/// The KIP 2.0 syntax reference, condensed for a model to read in context.
pub static KIP_SYNTAX: &str = include_str!;
/// The Cognitive Memory Profile — the standard portable memory structures the
/// bundled prompts are written against.
///
/// [`SELF_INSTRUCTIONS`] and [`SYSTEM_INSTRUCTIONS`] both name this document
/// among the ones they assume are loaded, so a host wiring those prompts needs
/// a way to supply it. It is the Profile's prose half; the executable half is
/// the Schema Package artifact an engine installs.
pub static COGNITIVE_MEMORY_PROFILE: &str =
include_str!;
/// How an Agent should use KIP as its own memory protocol.
pub static SELF_INSTRUCTIONS: &str = include_str!;
/// What a KIP runtime owes its callers, from the execution and governance side.
pub static SYSTEM_INSTRUCTIONS: &str = include_str!;
/// The tool definition for the state-capable `execute_kip` entry point.
pub static KIP_FUNCTION_DEFINITION: =
new;
/// The tool definition for the read-only `execute_kip_readonly` entry point.
pub static KIP_READONLY_FUNCTION_DEFINITION: = new;
/// Protected cognitive runtime host contracts.