-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclock_quorum.rs
More file actions
616 lines (585 loc) · 20.3 KB
/
Copy pathclock_quorum.rs
File metadata and controls
616 lines (585 loc) · 20.3 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#[path = "../support/mod.rs"]
mod support;
use cortex_daemon::clockwork::{
parse_query_frame, rebuild_clock_projections, record_used_with, reject_used_with, ClockTarget,
};
use cortex_daemon::handlers::recall::{execute_unified_recall, RecallContext};
use cortex_daemon::handlers::store::store_decision_with_ttl;
use cortex_tests::support::{solo_state, team_state};
use serde_json::{json, Value};
use std::fs;
use support::{
daemon_spawn_test_guard, read_token, request_json, reserve_port, shutdown_daemon, spawn_daemon,
unique_temp_dir, wait_for_exit, wait_for_health,
};
const AGENT: &str = "cqr-agent";
async fn store_owned(
state: &cortex_daemon::state::RuntimeState,
text: &str,
owner_id: Option<i64>,
) -> (Value, i64) {
store_owned_with_confidence(state, text, owner_id, 0.9).await
}
async fn store_owned_with_confidence(
state: &cortex_daemon::state::RuntimeState,
text: &str,
owner_id: Option<i64>,
confidence: f64,
) -> (Value, i64) {
let mut conn = state.db.lock().await;
let (entry, id) = store_decision_with_ttl(
&mut conn,
text,
None,
Some("decision".into()),
AGENT.into(),
Some(confidence),
None,
owner_id,
)
.unwrap_or_else(|err| panic!("store {text:?}: {err}"));
let id = id
.or_else(|| entry.get("id").and_then(|v| v.as_i64()))
.expect("stored id");
(entry, id)
}
async fn store_text(state: &cortex_daemon::state::RuntimeState, text: &str) -> i64 {
store_owned(state, text, None).await.1
}
async fn recall_results(
state: &cortex_daemon::state::RuntimeState,
query: &str,
ctx: &RecallContext,
) -> Vec<Value> {
let payload = execute_unified_recall(state, query, 320, 8, AGENT, ctx, None)
.await
.unwrap_or_else(|err| panic!("recall {query:?}: {err}"));
payload["results"]
.as_array()
.unwrap_or_else(|| panic!("results missing: {payload}"))
.clone()
}
fn excerpts(results: &[Value]) -> Vec<String> {
results
.iter()
.filter_map(|item| item["excerpt"].as_str().map(str::to_string))
.collect()
}
fn why_of(results: &[Value], excerpt: &str) -> Value {
results
.iter()
.find(|item| item["excerpt"].as_str() == Some(excerpt))
.and_then(|item| item.get("why").cloned())
.unwrap_or_else(|| panic!("missing why for {excerpt:?} in {results:?}"))
}
#[test]
fn contract_1_empty_home_no_models_dir() {
let _guard = daemon_spawn_test_guard();
let home_dir = unique_temp_dir("cqr-empty-home");
fs::create_dir_all(&home_dir).expect("home");
let home = home_dir.to_string_lossy().to_string();
let port = reserve_port();
let mut daemon = spawn_daemon(&home, port);
wait_for_health(port, &mut daemon);
let token = read_token(&home_dir);
let health = request_json(port, "GET", "/health", Some(&token), None)
.unwrap_or_else(|e| panic!("health: {e}"));
assert_eq!(health.status, 200, "health {}", health.body);
assert_eq!(health.body["retrieval"]["engine"], json!("clock-quorum"));
assert_eq!(health.body["retrieval"]["modelFree"], json!(true));
assert!(
!home_dir.join("models").exists(),
"models dir must not be created: {:?}",
home_dir.join("models")
);
let stored = request_json(
port,
"POST",
"/store",
Some(&token),
Some(json!({"decision":"CQR_EMPTY_HOME_TOKEN_9f2a persist exact recall","source_agent":AGENT,"confidence":0.9})),
)
.unwrap_or_else(|e| panic!("store: {e}"));
assert_eq!(stored.status, 200, "{}", stored.body);
let recalled = request_json(
port,
"GET",
"/recall?q=CQR_EMPTY_HOME_TOKEN_9f2a&k=5&budget=320",
Some(&token),
None,
)
.unwrap_or_else(|e| panic!("recall: {e}"));
assert_eq!(recalled.status, 200, "{}", recalled.body);
let hits = excerpts(recalled.body["results"].as_array().unwrap_or(&vec![]));
assert!(
hits.iter().any(|h| h.contains("CQR_EMPTY_HOME_TOKEN_9f2a")),
"exact recall failed: {hits:?} body {}",
recalled.body
);
assert!(
!home_dir.join("models").exists(),
"store/recall must not create models dir"
);
shutdown_daemon(port, &home_dir);
wait_for_exit(&mut daemon, std::time::Duration::from_secs(10));
}
#[tokio::test]
async fn contract_2_exact_path_excludes_neighbor() {
let state = solo_state();
let intended = "payments charge retry lives in src/payments/charge.rs::retry_charge";
let neighbor = "ui spinner retry lives in src/ui/spinner.rs::retry_render";
store_text(&state, intended).await;
store_text(&state, neighbor).await;
let mut ctx = RecallContext::solo();
ctx.paths.push("src/payments/charge.rs".into());
let results = recall_results(&state, "retry_charge src/payments/charge.rs", &ctx).await;
let got = excerpts(&results);
assert_eq!(
got.first().map(String::as_str),
Some(intended),
"exact path must win, got {got:?}"
);
assert!(
!got.iter().any(|e| e == neighbor),
"ui neighbor must be excluded, got {got:?}"
);
}
#[tokio::test]
async fn contract_3_alias_login_system() {
let state = solo_state();
let canonical = "The auth service issues session cookies after OAuth microservice handshake";
let distractor = "Generic OAuth library tokens should not be cached in redis";
store_text(&state, canonical).await;
store_text(&state, distractor).await;
let results = recall_results(&state, "login system", &RecallContext::solo()).await;
let got = excerpts(&results);
assert!(
got.iter().any(|e| e == canonical),
"alias recall must return canonical auth fact, got {got:?}"
);
assert!(
!got.first().is_some_and(|e| e == distractor),
"generic OAuth library fact must not win, got {got:?}"
);
let why = why_of(&results, canonical);
let why_s = why.to_string().to_ascii_lowercase();
assert!(
why_s.contains("auth")
|| why_s.contains("oauth")
|| why_s.contains("entity")
|| why_s.contains("login"),
"why must name alias/entity evidence: {why}"
);
}
#[tokio::test]
async fn contract_4_multihop_jake_postgres() {
let state = solo_state();
let first = "Jake proposed the DB move";
let second = "the DB move is the Postgres migration";
store_text(&state, first).await;
store_text(&state, second).await;
let results = recall_results(&state, "what did Jake propose", &RecallContext::solo()).await;
let got = excerpts(&results);
assert!(
got.iter().any(|e| e == second),
"multi-hop must admit Postgres decision, got {got:?}"
);
let why = why_of(&results, second);
let why_s = why.to_string();
assert!(
why_s.contains("observed_with") || why_s.contains("links"),
"why must show relation path: {why}"
);
}
#[tokio::test]
async fn contract_5_current_truth_and_as_of() {
let state = solo_state();
let old = "Always use Redis for caching in payments TEMPORALCQR across all deployments";
let new = "Never use Redis for caching in payments TEMPORALCQR across all deployments";
let (old_entry, _) = store_owned_with_confidence(&state, old, None, 0.65).await;
let old_from = old_entry["validFrom"]
.as_str()
.expect("old validFrom")
.to_string();
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
let (new_entry, _) = store_owned_with_confidence(&state, new, None, 0.99).await;
assert_eq!(
new_entry["classification"],
json!("CONTRADICTS"),
"replacement must supersede, {new_entry}"
);
let current = recall_results(&state, "TEMPORALCQR Redis caching", &RecallContext::solo()).await;
let current_excerpts = excerpts(¤t);
assert!(
current_excerpts.iter().any(|e| e == new),
"current recall must return the replacement, got {current_excerpts:?}"
);
assert!(
!current_excerpts.iter().any(|e| e == old),
"current recall must hide the closed fact, got {current_excerpts:?}"
);
let mut historical_ctx = RecallContext::solo();
historical_ctx.as_of = Some(old_from.clone());
let historical = recall_results(&state, "TEMPORALCQR Redis caching", &historical_ctx).await;
let historical_excerpts = excerpts(&historical);
assert!(
historical_excerpts.iter().any(|e| e == old),
"as-of must recover the first fact, got {historical_excerpts:?}"
);
let why = why_of(&historical, old);
let why_s = why.to_string().to_ascii_lowercase();
assert!(
why_s.contains("valid") || why_s.contains("filter") || why_s.contains("supersed"),
"historical why must name validity/supersession: {why}"
);
}
#[test]
fn contract_6_rollback_hides_later_store() {
let _guard = daemon_spawn_test_guard();
let home_dir = unique_temp_dir("cqr-rollback");
fs::create_dir_all(&home_dir).expect("home");
let home = home_dir.to_string_lossy().to_string();
let port = reserve_port();
let mut daemon = spawn_daemon(&home, port);
wait_for_health(port, &mut daemon);
let token = read_token(&home_dir);
let store = |decision: &str| {
let resp = request_json(
port,
"POST",
"/store",
Some(&token),
Some(json!({"decision":decision,"source_agent":AGENT,"confidence":0.9})),
)
.unwrap_or_else(|e| panic!("store {decision:?}: {e}"));
assert_eq!(resp.status, 200, "{}", resp.body);
resp.body
};
let a = store("We chose sqlite WAL journaling CQRHISTALPHA for the ledger");
let version_a = a["entry"]["versionId"].as_i64().expect("version A");
let _b = store("Billing exports move to parquet snapshots CQRHISTBETA nightly");
let rb = request_json(
port,
"POST",
"/rollback",
Some(&token),
Some(json!({"to": version_a})),
)
.expect("rollback");
assert_eq!(rb.status, 200, "{}", rb.body);
let recalled = request_json(
port,
"GET",
"/recall?q=CQRHISTBETA%20parquet&k=10",
Some(&token),
None,
)
.expect("recall B");
let got = excerpts(recalled.body["results"].as_array().unwrap_or(&vec![]));
for excerpt in &got {
assert!(
!excerpt.contains("CQRHISTBETA"),
"rolled-back token must not surface, got {got:?}"
);
}
shutdown_daemon(port, &home_dir);
wait_for_exit(&mut daemon, std::time::Duration::from_secs(10));
}
#[tokio::test]
async fn contract_7_task_path_context() {
let state = solo_state();
let payments = "reviewed scar: timeout retry in src/payments/charge.rs";
let ui = "reviewed scar: spinner flicker in src/ui/spinner.rs";
store_text(&state, payments).await;
store_text(&state, ui).await;
let mut pay_ctx = RecallContext::solo();
pay_ctx.paths.push("src/payments/**".into());
let pay_hits = excerpts(&recall_results(&state, "reviewed scar timeout", &pay_ctx).await);
assert!(
pay_hits.iter().any(|e| e == payments),
"payments path must admit payments scar, got {pay_hits:?}"
);
assert!(
!pay_hits.iter().any(|e| e == ui),
"payments path must exclude UI scar, got {pay_hits:?}"
);
let why = why_of(
&recall_results(&state, "reviewed scar timeout", &pay_ctx).await,
payments,
);
let why_s = why.to_string().to_ascii_lowercase();
assert!(
why_s.contains("task") || why_s.contains("path") || why_s.contains("hard"),
"why must name the task/path clock: {why}"
);
let mut ui_ctx = RecallContext::solo();
ui_ctx.paths.push("src/ui/**".into());
let ui_hits = excerpts(&recall_results(&state, "reviewed scar spinner", &ui_ctx).await);
assert!(
ui_hits.iter().any(|e| e == ui),
"UI path must admit UI scar, got {ui_hits:?}"
);
assert!(
!ui_hits.iter().any(|e| e == payments),
"UI path must exclude payments scar, got {ui_hits:?}"
);
}
#[tokio::test]
async fn contract_8_negative_neighbor() {
let state = solo_state();
let keeper = "billing invoices use Stripe Billing API in payments";
let neighbor = "design review used stripe patterns in the UI kit";
store_text(&state, keeper).await;
store_text(&state, neighbor).await;
let results = recall_results(&state, "Stripe Billing API", &RecallContext::solo()).await;
let got = excerpts(&results);
assert!(
got.iter().any(|e| e == keeper),
"keeper must return, got {got:?}"
);
assert!(
!got.iter().any(|e| e == neighbor),
"weak neighbor must not be admitted, got {got:?}"
);
}
#[tokio::test]
async fn contract_9_feedback_used_with() {
let state = solo_state();
let first = "Jake confirmed the payments retry budget PAYRETRYCQR";
let second = "Postgres WAL is required for the ledger PGWALCQR";
let first_id = store_text(&state, first).await;
let second_id = store_text(&state, second).await;
{
let conn = state.db.lock().await;
record_used_with(
&conn,
&ClockTarget {
target_type: "decision".into(),
target_id: first_id,
},
&ClockTarget {
target_type: "decision".into(),
target_id: second_id,
},
None,
)
.expect("record used_with");
}
let linked = recall_results(&state, "PAYRETRYCQR", &RecallContext::solo()).await;
assert!(
excerpts(&linked).iter().any(|e| e == second),
"used_with must admit the paired result, got {:?}",
excerpts(&linked)
);
let why = why_of(&linked, second);
assert!(
why.to_string().contains("used_with"),
"why must name used_with: {why}"
);
{
let conn = state.db.lock().await;
reject_used_with(
&conn,
&ClockTarget {
target_type: "decision".into(),
target_id: first_id,
},
&ClockTarget {
target_type: "decision".into(),
target_id: second_id,
},
)
.expect("reject used_with");
}
let after = recall_results(&state, "PAYRETRYCQR", &RecallContext::solo()).await;
assert!(
!excerpts(&after).iter().any(|e| e == second),
"rejected used_with must suppress the pair, got {:?}",
excerpts(&after)
);
}
#[tokio::test]
async fn contract_10_determinism() {
let state = solo_state();
store_text(
&state,
"deterministic clock why CQRDET token for byte equality",
)
.await;
let a = execute_unified_recall(
&state,
"CQRDET token",
320,
5,
AGENT,
&RecallContext::solo(),
None,
)
.await
.unwrap();
let b = execute_unified_recall(
&state,
"CQRDET token",
320,
5,
AGENT,
&RecallContext::solo(),
None,
)
.await
.unwrap();
let wa = serde_json::to_vec(&a["results"]).expect("a");
let wb = serde_json::to_vec(&b["results"]).expect("b");
assert_eq!(wa, wb, "byte-identical results+why required\n{a}\n{b}");
}
#[tokio::test]
async fn contract_11_acl_hides_other_owner_private_row() {
let state = team_state(1);
let secret = "other-owner private token ACLPRIVCQR must never leak";
store_owned(&state, secret, Some(2)).await;
let mut ctx = RecallContext::from_state(&state);
ctx.caller_id = Some(1);
let results = recall_results(&state, "ACLPRIVCQR", &ctx).await;
assert!(
results.is_empty() || excerpts(&results).iter().all(|e| !e.contains("ACLPRIVCQR")),
"private other-owner row must not appear as candidate/result/why: {results:?}"
);
}
#[tokio::test]
async fn contract_12_honest_miss() {
let state = solo_state();
store_text(
&state,
"The office snack policy prefers salted almonds on Fridays",
)
.await;
let results = recall_results(
&state,
"how should we authenticate the payments webhook",
&RecallContext::solo(),
)
.await;
assert!(
results.is_empty() || excerpts(&results).iter().all(|e| !e.contains("almonds")),
"honest miss must not return snack policy for auth query: {results:?}"
);
}
#[tokio::test]
async fn contract_13_rebuild_matches_fresh_projection() {
let state = solo_state();
store_text(&state, "rebuild projection unique CQRREBUILD token").await;
{
let conn = state.db.lock().await;
conn.execute_batch(
"DELETE FROM clock_anchor_evidence; DELETE FROM clock_links; DELETE FROM clock_anchors;",
)
.expect("clear projections");
let n = rebuild_clock_projections(&conn, 32).expect("rebuild");
assert!(n >= 1, "rebuild should project stored decisions, got {n}");
}
let results = recall_results(&state, "CQRREBUILD token", &RecallContext::solo()).await;
assert!(
excerpts(&results).iter().any(|e| e.contains("CQRREBUILD")),
"rebuilt projections must serve recall: {results:?}"
);
}
#[tokio::test]
async fn contract_14_current_truth_caching_nl() {
let state = solo_state();
let old = "We are using Redis for caching";
let current = "We are not using Redis for caching, we moved off Redis to rediska last sprint";
store_text(&state, old).await;
store_owned_with_confidence(&state, current, None, 0.95).await;
{
let conn = state.db.lock().await;
conn.execute(
"UPDATE decisions SET status = 'superseded', updated_at = datetime('now') WHERE decision = ?1 AND status = 'active'",
rusqlite::params![old],
)
.expect("supersede old caching fact");
}
let frame = parse_query_frame(
"what do we use for caching",
None,
None,
None,
Vec::new(),
Vec::new(),
None,
None,
);
let results =
recall_results(&state, "what do we use for caching", &RecallContext::solo()).await;
let got = excerpts(&results);
assert!(
got.iter().any(|e| e == current),
"NL caching query must return current fact; terms={:?} quotes={:?} got={got:?} payload={results:?}",
frame.terms,
frame.quoted_phrases
);
assert!(
!got.iter().any(|e| e == old),
"superseded caching fact must stay hidden, got {got:?}"
);
}
#[tokio::test]
async fn contract_15_morph_cache_nl() {
let state = solo_state();
let stored = "Redis is our cache layer in payments CACHENL15";
store_text(&state, stored).await;
let results =
recall_results(&state, "what do we use for caching", &RecallContext::solo()).await;
let got = excerpts(&results);
assert!(
got.iter().any(|e| e == stored),
"cache↔caching morphology must admit the stored fact, got {got:?}"
);
}
#[tokio::test]
async fn contract_16_webhook_paraphrase() {
let state = solo_state();
let stored = "HMAC verifies Stripe webhooks for payments AUTHWH16";
store_text(&state, stored).await;
let results = recall_results(
&state,
"how should we authenticate the payments webhook",
&RecallContext::solo(),
)
.await;
let got = excerpts(&results);
assert!(
got.iter().any(|e| e == stored),
"webhook/payments paraphrase must admit the HMAC fact, got {got:?}"
);
}
#[tokio::test]
async fn contract_17_vague_auth_query() {
let state = solo_state();
let stored = "The auth service issues session cookies after OAuth microservice handshake";
store_text(&state, stored).await;
let results = recall_results(&state, "how does auth work", &RecallContext::solo()).await;
let got = excerpts(&results);
assert!(
got.iter().any(|e| e == stored),
"cluster expansion of auth must recover the login/OAuth fact, got {got:?}"
);
}
#[tokio::test]
async fn contract_18_honest_miss_still_holds() {
let state = solo_state();
store_text(
&state,
"The office snack policy prefers salted almonds on Fridays",
)
.await;
let results = recall_results(
&state,
"how should we authenticate the payments webhook",
&RecallContext::solo(),
)
.await;
assert!(
results.is_empty() || excerpts(&results).iter().all(|e| !e.contains("almonds")),
"bridge expansion must not admit snack policy: {results:?}"
);
}