atheneum 0.2.1

Agent coordination graph database - episodic and semantic memory for multi-agent workflows
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
//! Tests for Envoy-Atheneum Bridge API
//! Tests are written FIRST (TDD) and will fail until implementation is complete.
//!
//! Phase 1: atheneum core APIs
//! - Discovery entity (agent findings: symbols, CFG, issues, patterns)
//! - Handoff entity (context transfer between agents)
//! - Knowledge Query (aggregate discoveries + handoffs + token savings)

use atheneum::graph::{AtheneumGraph, EntityType};
use serde_json::json;

// ============================================================================
// Discovery API Tests
// ============================================================================

#[test]
fn test_store_discovery_symbol() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    let discovery_id = graph
        .store_discovery(
            "claude1",
            "symbol",
            "http_handler",
            json!({
                "file_path": "src/http.rs",
                "line": 42,
                "kind": "function",
                "signature": "pub fn http_handler(req: Request) -> Response"
            }),
        )
        .expect("Failed to store discovery");

    assert!(discovery_id > 0, "Discovery ID should be positive");

    let discovery = graph
        .get_entity(discovery_id)
        .expect("Failed to retrieve discovery");

    assert_eq!(discovery.kind, EntityType::Discovery.as_str());
    assert_eq!(discovery.name, "claude1: http_handler");
}

#[test]
fn test_store_discovery_cfg() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    let discovery_id = graph
        .store_discovery(
            "claude2",
            "cfg",
            "process_request",
            json!({
                "file_path": "src/http.rs",
                "function": "process_request",
                "cyclomatic_complexity": 8,
                "has_loop": true,
                "branches": 4
            }),
        )
        .expect("Failed to store discovery");

    let discovery = graph
        .get_entity(discovery_id)
        .expect("Failed to retrieve discovery");

    let data = discovery.data.as_object().expect("Data should be object");
    assert_eq!(data["discovery_type"], "cfg");
    assert_eq!(data["target"], "process_request");
}

#[test]
fn test_query_discoveries_by_target() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    // Agent A discovers a symbol
    graph
        .store_discovery(
            "claude1",
            "symbol",
            "http_handler",
            json!({"file": "src/http.rs", "line": 42}),
        )
        .expect("Failed to store discovery A");

    // Agent B also discovers the same symbol (from a different angle)
    graph
        .store_discovery(
            "claude2",
            "cfg",
            "http_handler",
            json!({"complexity": 5, "file": "src/http.rs"}),
        )
        .expect("Failed to store discovery B");

    // Query for all discoveries about http_handler
    let discoveries = graph
        .query_discoveries("http_handler")
        .expect("Failed to query discoveries");

    assert_eq!(discoveries.len(), 2, "Should find 2 discoveries");

    // Verify both agents are represented
    let agents: Vec<_> = discoveries
        .iter()
        .filter_map(|d| d.data.get("agent"))
        .map(|a| a.as_str())
        .collect();

    assert!(agents.contains(&Some("claude1")), "Should include claude1");
    assert!(agents.contains(&Some("claude2")), "Should include claude2");
}

#[test]
fn test_query_discoveries_empty() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    let discoveries = graph
        .query_discoveries("nonexistent")
        .expect("Failed to query");

    assert!(
        discoveries.is_empty(),
        "Should return empty for unknown target"
    );
}

#[test]
fn test_store_discovery_creates_provenance() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    let discovery_id = graph
        .store_discovery(
            "hermes",
            "pattern",
            "clone-escape-hatch",
            json!({"description": "Excessive .clone() calls"}),
        )
        .expect("Failed to store discovery");

    // Check that the discovery has provenance metadata
    let discovery = graph
        .get_entity(discovery_id)
        .expect("Failed to retrieve discovery");

    let data = discovery.data.as_object().expect("Data should be object");
    assert!(data.contains_key("agent"), "Should have agent field");
    assert!(data.contains_key("timestamp"), "Should have timestamp");
    assert_eq!(data["agent"], "hermes");
}

// ============================================================================
// Handoff API Tests
// ============================================================================

#[test]
fn test_store_handoff() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    let manifest = json!({
        "task": "implement auth",
        "files_analyzed": ["src/auth.rs", "src/user.rs"],
        "discoveries": 5,
        "token_budget_used": 45000,
        "token_budget_remaining": 45000,
        "next_steps": ["Add JWT validation", "Write tests"]
    });

    let handoff_id = graph
        .store_handoff("claude1", "claude2", manifest)
        .expect("Failed to store handoff");

    assert!(handoff_id > 0, "Handoff ID should be positive");

    let handoff = graph
        .get_entity(handoff_id)
        .expect("Failed to retrieve handoff");
    assert_eq!(handoff.kind, EntityType::Handoff.as_str());
    assert_eq!(handoff.name, "claude1 -> claude2");

    let data = handoff.data.as_object().expect("Data should be object");
    assert_eq!(data["from_agent"], "claude1");
    assert_eq!(data["to_agent"], "claude2");
    assert!(data.contains_key("manifest"));
}

#[test]
fn test_get_pending_handoff() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    // Create a handoff for claude2
    let manifest = json!({"task": "fix bug", "context": "analyzed"});
    graph
        .store_handoff("claude1", "claude2", manifest)
        .expect("Failed to store handoff");

    // claude2 checks for pending handoffs
    let pending = graph
        .get_pending_handoff("claude2")
        .expect("Failed to get pending handoff");

    assert!(pending.is_some(), "Should have a pending handoff");

    let handoff = pending.unwrap();
    assert_eq!(handoff.data["from_agent"], "claude1");
    assert_eq!(handoff.data["to_agent"], "claude2");
}

#[test]
fn test_get_pending_handoff_none() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    // Create a handoff for claude3, not claude2
    graph
        .store_handoff("claude1", "claude3", json!({}))
        .expect("Failed to store handoff");

    // claude2 checks for pending handoffs
    let pending = graph
        .get_pending_handoff("claude2")
        .expect("Failed to get pending handoff");

    assert!(
        pending.is_none(),
        "Should have no pending handoffs for claude2"
    );
}

#[test]
fn test_get_pending_handoff_returns_most_recent() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    // Create multiple handoffs for claude2
    graph
        .store_handoff("claude1", "claude2", json!({"seq": 1}))
        .expect("Failed to store handoff 1");

    graph
        .store_handoff("hermes", "claude2", json!({"seq": 2}))
        .expect("Failed to store handoff 2");

    let pending = graph
        .get_pending_handoff("claude2")
        .expect("Failed to get pending handoff");

    assert!(pending.is_some(), "Should have a pending handoff");

    // Should return the most recent (highest ID)
    assert_eq!(pending.unwrap().data["manifest"]["seq"], 2);
}

#[test]
fn test_handoff_marked_claimed() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    let handoff_id = graph
        .store_handoff("claude1", "claude2", json!({"task": "test"}))
        .expect("Failed to store handoff");

    // Mark as claimed
    graph
        .mark_handoff_claimed(handoff_id)
        .expect("Failed to mark handoff");

    // Should no longer appear in pending
    let pending = graph
        .get_pending_handoff("claude2")
        .expect("Failed to get pending handoff");

    assert!(pending.is_none(), "Claimed handoff should not be pending");
}

// ============================================================================
// Knowledge Query API Tests
// ============================================================================

#[test]
fn test_query_knowledge_aggregates_discoveries() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    // Multiple agents discover the same symbol
    graph
        .store_discovery(
            "claude1",
            "symbol",
            "http_handler",
            json!({"file": "src/http.rs", "lines": 50}),
        )
        .expect("Failed to store discovery 1");

    graph
        .store_discovery("claude2", "cfg", "http_handler", json!({"complexity": 8}))
        .expect("Failed to store discovery 2");

    graph
        .store_discovery(
            "claude3",
            "issue",
            "http_handler",
            json!({"issue": "missing error handling"}),
        )
        .expect("Failed to store discovery 3");

    let knowledge = graph
        .query_knowledge("http_handler")
        .expect("Failed to query knowledge");

    assert_eq!(knowledge["target"], "http_handler");
    assert_eq!(knowledge["discovery_count"], 3);

    let discoveries = knowledge["discoveries"].as_array().unwrap();
    assert_eq!(discoveries.len(), 3);
}

#[test]
fn test_query_knowledge_includes_handoffs() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    // A handoff mentioning the target
    graph
        .store_handoff(
            "claude1",
            "claude2",
            json!({
                "task": "fix http_handler",
                "files_analyzed": ["src/http.rs"],
                "token_budget_used": 30000
            }),
        )
        .expect("Failed to store handoff");

    let knowledge = graph
        .query_knowledge("http_handler")
        .expect("Failed to query knowledge");

    // Handoff should be included because it mentions the target in the task
    let handoffs = knowledge["handoffs"].as_array().unwrap();
    assert_eq!(handoffs.len(), 1);
    assert_eq!(handoffs[0]["data"]["from_agent"], "claude1");
}

#[test]
fn test_query_knowledge_calculates_token_savings() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    // Simulate 3 agents each reading a 15K token file
    // Without knowledge sharing: 45K tokens
    // With knowledge sharing: 15K (first agent) + 2.5K (summary) * 2 = 20K tokens

    graph
        .store_discovery(
            "claude1",
            "symbol",
            "large_file.rs",
            json!({"file": "src/large_file.rs", "token_count": 15000}),
        )
        .expect("Failed to store discovery 1");

    graph
        .store_discovery(
            "claude2",
            "cfg",
            "large_file.rs",
            json!({"file": "src/large_file.rs", "token_count": 15000}),
        )
        .expect("Failed to store discovery 2");

    graph
        .store_discovery(
            "claude3",
            "issue",
            "large_file.rs",
            json!({"file": "src/large_file.rs", "token_count": 15000}),
        )
        .expect("Failed to store discovery 3");

    let knowledge = graph
        .query_knowledge("large_file.rs")
        .expect("Failed to query knowledge");

    let savings = knowledge["token_savings"].as_object().unwrap();
    assert!(savings["without_sharing"].as_i64().unwrap() > 0);
    assert!(savings["with_sharing"].as_i64().unwrap() > 0);
    assert!(savings["saved"].as_i64().unwrap() > 0);
    assert!(savings["percentage_reduction"].as_f64().unwrap() > 0.0);
}

#[test]
fn test_query_knowledge_empty_target() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    let knowledge = graph
        .query_knowledge("unknown")
        .expect("Failed to query knowledge");

    assert_eq!(knowledge["target"], "unknown");
    assert_eq!(knowledge["discovery_count"], 0);
    assert!(knowledge["discoveries"].as_array().unwrap().is_empty());
}

#[test]
fn test_query_knowledge_includes_metadata() {
    let graph = AtheneumGraph::open_in_memory().expect("Failed to create graph");

    graph
        .store_discovery(
            "claude1",
            "symbol",
            "test_target",
            json!({"file": "test.rs"}),
        )
        .expect("Failed to store discovery");

    let knowledge = graph
        .query_knowledge("test_target")
        .expect("Failed to query knowledge");

    let obj = knowledge.as_object().expect("Knowledge should be object");
    assert!(obj.contains_key("queried_at"));
    assert!(obj.contains_key("total_entities"));
}