mempal-agent-memory 0.9.0

Reusable agent-memory domain types and anchor helpers for mempal.
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
use super::anchor;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SourceType {
    Project,
    Conversation,
    Manual,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MemoryKind {
    Evidence,
    Knowledge,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MemoryDomain {
    Project,
    Agent,
    Skill,
    Global,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AnchorKind {
    Global,
    Repo,
    Worktree,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Provenance {
    Runtime,
    Research,
    Human,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum KnowledgeTier {
    Qi,
    Shu,
    DaoRen,
    DaoTian,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum KnowledgeStatus {
    Candidate,
    Promoted,
    Canonical,
    Demoted,
    Retired,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TriggerHints {
    pub intent_tags: Vec<String>,
    pub workflow_bias: Vec<String>,
    pub tool_needs: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum KnowledgeEvidenceRole {
    Supporting,
    Verification,
    Counterexample,
    Teaching,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum KnowledgeEventType {
    Created,
    Promoted,
    Demoted,
    Retired,
    Linked,
    Unlinked,
    Updated,
    PublishedAnchor,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RuntimeAdoptionTrack {
    RuntimeAdoption,
    CardContext,
    CardEmbedding,
    Evaluator,
    ResearchAdapter,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RuntimeAdoptionSignal {
    Used,
    Accepted,
    Rejected,
    Miss,
    Rollback,
    Contradiction,
    Neutral,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RuntimeAdoptionEvent {
    pub id: String,
    pub track: RuntimeAdoptionTrack,
    pub signal: RuntimeAdoptionSignal,
    pub feature: String,
    pub query: Option<String>,
    pub context_hash: Option<String>,
    pub card_id: Option<String>,
    pub evaluator_id: Option<String>,
    pub research_report_id: Option<String>,
    pub note: Option<String>,
    pub metadata: Option<serde_json::Value>,
    pub created_at: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct RuntimeAdoptionFilter {
    pub track: Option<RuntimeAdoptionTrack>,
    pub feature: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct KnowledgeCard {
    pub id: String,
    pub statement: String,
    pub content: String,
    pub tier: KnowledgeTier,
    pub status: KnowledgeStatus,
    pub domain: MemoryDomain,
    pub field: String,
    pub anchor_kind: AnchorKind,
    pub anchor_id: String,
    pub parent_anchor_id: Option<String>,
    pub scope_constraints: Option<String>,
    pub trigger_hints: Option<TriggerHints>,
    pub created_at: String,
    pub updated_at: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct KnowledgeCardFilter {
    pub tier: Option<KnowledgeTier>,
    pub status: Option<KnowledgeStatus>,
    pub domain: Option<MemoryDomain>,
    pub field: Option<String>,
    pub anchor_kind: Option<AnchorKind>,
    pub anchor_id: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct KnowledgeEvidenceLink {
    pub id: String,
    pub card_id: String,
    pub evidence_drawer_id: String,
    pub role: KnowledgeEvidenceRole,
    pub note: Option<String>,
    pub created_at: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct KnowledgeCardEvent {
    pub id: String,
    pub card_id: String,
    pub event_type: KnowledgeEventType,
    pub from_status: Option<KnowledgeStatus>,
    pub to_status: Option<KnowledgeStatus>,
    pub reason: String,
    pub actor: Option<String>,
    pub metadata: Option<serde_json::Value>,
    pub created_at: String,
}

#[derive(Debug, Clone, Copy)]
pub struct BootstrapIdentityParts<'a> {
    pub memory_kind: &'a MemoryKind,
    pub domain: &'a MemoryDomain,
    pub field: &'a str,
    pub anchor_kind: &'a AnchorKind,
    pub anchor_id: &'a str,
    pub parent_anchor_id: Option<&'a str>,
    pub provenance: Option<&'a Provenance>,
    pub statement: Option<&'a str>,
    pub tier: Option<&'a KnowledgeTier>,
    pub status: Option<&'a KnowledgeStatus>,
    pub supporting_refs: &'a [String],
    pub counterexample_refs: &'a [String],
    pub teaching_refs: &'a [String],
    pub verification_refs: &'a [String],
    pub scope_constraints: Option<&'a str>,
    pub trigger_hints: Option<&'a TriggerHints>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct TunnelEndpoint {
    pub wing: String,
    pub room: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExplicitTunnel {
    pub id: String,
    pub left: TunnelEndpoint,
    pub right: TunnelEndpoint,
    pub label: String,
    pub created_at: String,
    pub created_by: Option<String>,
    pub deleted_at: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TunnelFollowResult {
    pub endpoint: TunnelEndpoint,
    pub via_tunnel_id: String,
    pub hop: u8,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReindexSource {
    pub source_file: Option<String>,
    pub wing: String,
    pub room: Option<String>,
    pub drawer_count: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Drawer {
    pub id: String,
    pub content: String,
    pub wing: String,
    pub room: Option<String>,
    pub source_file: Option<String>,
    pub source_type: SourceType,
    pub added_at: String,
    pub chunk_index: Option<i64>,
    #[serde(default = "default_normalize_version")]
    pub normalize_version: u32,
    /// Importance ranking (0-5). Higher = more important for wake-up context.
    #[serde(default)]
    pub importance: i32,
    pub memory_kind: MemoryKind,
    pub domain: MemoryDomain,
    pub field: String,
    pub anchor_kind: AnchorKind,
    pub anchor_id: String,
    pub parent_anchor_id: Option<String>,
    pub provenance: Option<Provenance>,
    pub statement: Option<String>,
    pub tier: Option<KnowledgeTier>,
    pub status: Option<KnowledgeStatus>,
    #[serde(default)]
    pub supporting_refs: Vec<String>,
    #[serde(default)]
    pub counterexample_refs: Vec<String>,
    #[serde(default)]
    pub teaching_refs: Vec<String>,
    #[serde(default)]
    pub verification_refs: Vec<String>,
    pub scope_constraints: Option<String>,
    pub trigger_hints: Option<TriggerHints>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BootstrapEvidenceArgs {
    pub id: String,
    pub content: String,
    pub wing: String,
    pub room: Option<String>,
    pub source_file: Option<String>,
    pub source_type: SourceType,
    pub added_at: String,
    pub chunk_index: Option<i64>,
    pub importance: i32,
}

impl Drawer {
    pub fn new_bootstrap_evidence(args: BootstrapEvidenceArgs) -> Self {
        let defaults = anchor::bootstrap_defaults(&args.source_type);
        Self {
            id: args.id,
            content: args.content,
            wing: args.wing,
            room: args.room,
            source_file: args.source_file,
            source_type: args.source_type,
            added_at: args.added_at,
            chunk_index: args.chunk_index,
            normalize_version: default_normalize_version(),
            importance: args.importance,
            memory_kind: MemoryKind::Evidence,
            domain: MemoryDomain::Project,
            field: defaults.field,
            anchor_kind: defaults.anchor_kind,
            anchor_id: defaults.anchor_id,
            parent_anchor_id: defaults.parent_anchor_id,
            provenance: Some(defaults.provenance),
            statement: None,
            tier: None,
            status: None,
            supporting_refs: Vec::new(),
            counterexample_refs: Vec::new(),
            teaching_refs: Vec::new(),
            verification_refs: Vec::new(),
            scope_constraints: None,
            trigger_hints: None,
        }
    }
}

fn default_normalize_version() -> u32 {
    1
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Triple {
    pub id: String,
    pub subject: String,
    pub predicate: String,
    pub object: String,
    pub valid_from: Option<String>,
    pub valid_to: Option<String>,
    pub confidence: f64,
    pub source_drawer: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TaxonomyEntry {
    pub wing: String,
    pub room: String,
    pub display_name: Option<String>,
    pub keywords: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TripleStats {
    pub total: i64,
    pub active: i64,
    pub expired: i64,
    pub entities: i64,
    pub top_predicates: Vec<(String, i64)>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RouteDecision {
    pub wing: Option<String>,
    pub room: Option<String>,
    pub confidence: f32,
    pub reason: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NeighborChunk {
    pub drawer_id: String,
    pub content: String,
    pub chunk_index: u32,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChunkNeighbors {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prev: Option<NeighborChunk>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next: Option<NeighborChunk>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SearchResult {
    pub drawer_id: String,
    pub content: String,
    pub wing: String,
    pub room: Option<String>,
    pub source_file: String,
    pub memory_kind: MemoryKind,
    pub domain: MemoryDomain,
    pub field: String,
    pub statement: Option<String>,
    pub tier: Option<KnowledgeTier>,
    pub status: Option<KnowledgeStatus>,
    pub anchor_kind: AnchorKind,
    pub anchor_id: String,
    pub parent_anchor_id: Option<String>,
    pub similarity: f32,
    pub route: RouteDecision,
    #[serde(skip)]
    pub chunk_index: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub neighbors: Option<ChunkNeighbors>,
    /// Other wings that share this result's room (tunnel hints).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tunnel_hints: Vec<String>,
}