eidetic-engine 0.18.0

Durable, local-first, explainable memory for coding agents.
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
use std::path::PathBuf;

use super::*;
use crate::config::WORKSPACE_MARKER;
use crate::core::backup::{
    BackupCreateOptions, BackupRestoreOptions, BackupVerifyOptions, create_backup, hash_bytes,
    restore_backup_to_side_path, restore_backup_to_side_path_with_verification_hook, verify_backup,
    work_history_error,
};
use crate::db::{OutcomeEvidenceSource, StoredFeedbackQuarantine, StoredLearningObservation};
use crate::models::{MemoryId, RedactionLevel, WorkspaceId};
use uuid::Uuid;

type TestResult = Result<(), String>;
type SignalState = (
    Vec<StoredLearningObservation>,
    Vec<StoredFeedbackQuarantine>,
    Vec<StoredOutcomeEvidence>,
);

struct Fixture {
    root: tempfile::TempDir,
    database: PathBuf,
    options: BackupRestoreOptions,
}

fn workspace_id() -> String {
    WorkspaceId::from_uuid(Uuid::from_u128(1)).to_string()
}
fn quarantine_id(n: usize) -> String {
    format!("fq_{n:026}")
}

fn read_state(db: &DbConnection) -> Result<SignalState, String> {
    let ws = workspace_id();
    Ok((
        db.list_learning_observations(&ws, None)
            .map_err(|e| e.to_string())?,
        db.list_feedback_quarantine(&ws, None)
            .map_err(|e| e.to_string())?,
        db.list_outcome_evidence_for_recovery(&ws)
            .map_err(|e| e.to_string())?,
    ))
}

fn fixture(level: RedactionLevel) -> Result<Fixture, String> {
    let (root, workspace, database) =
        crate::core::backup::tests::fixture().map_err(|e| e.message())?;
    let db = DbConnection::open_file(&database).map_err(|e| e.to_string())?;
    let ws = workspace_id();
    let memory_id = MemoryId::from_uuid(Uuid::from_u128(2)).to_string();
    let time = "2026-09-01T00:00:00Z";
    db.insert_learning_observation_for_recovery(&StoredLearningObservation {
        id: "lobs_recovery_signal".to_owned(),
        workspace_id: ws.clone(),
        observation_kind: "experiment_observe".to_owned(),
        source_type: "experiment".to_owned(),
        source_id: Some("api_key=signal-source-private-canary".to_owned()),
        target_type: "memory".to_owned(),
        target_id: memory_id.clone(),
        topic: Some("release".to_owned()),
        signal: "helpful".to_owned(),
        evidence_json: Some("{\"note\":\"measured result\"}".to_owned()),
        observed_at: time.to_owned(),
        created_at: time.to_owned(),
    })
    .map_err(|e| e.to_string())?;
    for n in 0..2 {
        let mut row = StoredFeedbackQuarantine {
            id: quarantine_id(n),
            workspace_id: ws.clone(),
            source_id: "api_key=signal-quarantine-private-canary".to_owned(),
            target_type: "memory".to_owned(),
            target_id: memory_id.clone(),
            signal: "harmful".to_owned(),
            weight: 0.5,
            source_type: "outcome_observed".to_owned(),
            proposed_event_id: Some(format!("fb_{:026}", 100 + n)),
            recorded_at: time.to_owned(),
            reason: "Requires explicit review".to_owned(),
            event_reason: Some("Observed failure".to_owned()),
            evidence_json: None,
            session_id: None,
            raw_event_hash: String::new(),
            status: "pending".to_owned(),
            reviewed_at: None,
            reviewed_by: None,
            released_feedback_event_id: None,
        };
        row.raw_event_hash = if n == 0 {
            quarantine_payload_hash(&row)
                .map_err(|e| e.message())?
                .ok_or("missing event id")?
        } else {
            hash_bytes(b"invalid original payload hash")
        };
        db.insert_feedback_quarantine_for_recovery(&row)
            .map_err(|e| e.to_string())?;
    }
    for (n, source) in [
        OutcomeEvidenceSource::VerifierSuccess,
        OutcomeEvidenceSource::ExplicitHuman,
    ]
    .into_iter()
    .enumerate()
    {
        let mut row = StoredOutcomeEvidence {
            workspace_id: ws.clone(),
            source,
            evidence_family: source.evidence_family().to_owned(),
            signal_direction: source.default_direction().unwrap_or("negative").to_owned(),
            base_weight_milli: source.base_weight_milli(),
            evidence_ref: format!("untracked-observation-{n}"),
            agent_id: Some("historical-agent".to_owned()),
            task_id: Some("historical-task".to_owned()),
            run_id: Some("historical-run".to_owned()),
            observed_at: time.to_owned(),
            provenance_hash: String::new(),
            created_at: time.to_owned(),
        };
        row.provenance_hash = row.computed_provenance_hash();
        db.insert_outcome_evidence_for_recovery(&row)
            .map_err(|e| e.to_string())?;
    }
    let state = read_state(&db)?;
    assert_eq!((state.0.len(), state.1.len(), state.2.len()), (1, 2, 2));
    db.close().map_err(|e| e.to_string())?;
    let backup = create_backup(&BackupCreateOptions {
        workspace_path: workspace.clone(),
        database_path: Some(database.clone()),
        output_dir: None,
        label: None,
        redaction_level: level,
        include_derived: false,
        include_graph_cache: false,
        dry_run: false,
    })
    .map_err(|e| e.message())?;
    let options = BackupRestoreOptions {
        workspace_path: workspace,
        backup_path: PathBuf::from(backup.backup_path),
        side_path: root.path().join("restored"),
        restore_graph_cache: false,
        dry_run: false,
    };
    Ok(Fixture {
        root,
        database,
        options,
    })
}

fn execute(db: &DbConnection, sql: &str) -> TestResult {
    db.execute_raw(sql).map(|_| ()).map_err(|e| e.to_string())
}

fn assert_refused(table: &str, mutate: impl FnOnce(&DbConnection) -> TestResult) -> TestResult {
    let fixture = fixture(RedactionLevel::None)?;
    let source = DbConnection::open_file(&fixture.database).map_err(|e| e.to_string())?;
    let original = read_state(&source)?;
    source.close().map_err(|e| e.to_string())?;
    let error = restore_backup_to_side_path_with_verification_hook(&fixture.options, |path| {
        let db = DbConnection::open_file(path).map_err(work_history_error)?;
        let before = read_state(&db).map_err(work_history_error)?;
        let tables = db.list_user_tables().map_err(work_history_error)?;
        let counts = tables
            .iter()
            .map(|t| db.count_table_rows(t))
            .collect::<Result<Vec<_>, _>>()
            .map_err(work_history_error)?;
        mutate(&db).map_err(work_history_error)?;
        assert_ne!(
            read_state(&db).map_err(work_history_error)?,
            before,
            "mutation must change actual readable rows"
        );
        let after_counts = tables
            .iter()
            .map(|t| db.count_table_rows(t))
            .collect::<Result<Vec<_>, _>>()
            .map_err(work_history_error)?;
        assert_eq!(
            counts, after_counts,
            "a count-only fence must not detect this mutation"
        );
        db.close().map_err(work_history_error)
    })
    .err()
    .ok_or("published altered learning evidence")?;
    assert!(
        error
            .message()
            .contains(&format!("Restored durable content differs for {table}")),
        "{}",
        error.message()
    );
    assert!(!error.message().contains("PRIVATE_SENTINEL"));
    assert!(!fixture.options.side_path.join(WORKSPACE_MARKER).exists());
    let source = DbConnection::open_file(&fixture.database).map_err(|e| e.to_string())?;
    assert_eq!(read_state(&source)?, original);
    assert_eq!(
        source
            .count_table_rows("feedback_events")
            .map_err(|e| e.to_string())?,
        0
    );
    source.close().map_err(|e| e.to_string())?;
    assert_eq!(
        verify_backup(&BackupVerifyOptions {
            workspace_path: fixture.options.workspace_path,
            backup_path: fixture.options.backup_path,
        })
        .map_err(|e| e.message())?
        .status,
        "verified"
    );
    Ok(())
}

fn rewrite_quarantine_payload(db: &DbConnection, invalid_source: bool) -> TestResult {
    let id = quarantine_id(usize::from(invalid_source));
    let mut row = db
        .list_feedback_quarantine(&workspace_id(), None)
        .map_err(|e| e.to_string())?
        .into_iter()
        .find(|row| row.id == id)
        .ok_or("missing quarantine")?;
    if !invalid_source {
        row.weight = 0.75;
    }
    let hash = quarantine_payload_hash(&row)
        .map_err(|e| e.message())?
        .ok_or("missing event")?;
    execute(
        db,
        &format!(
            "UPDATE feedback_quarantine SET weight = {}, raw_event_hash = '{hash}' WHERE id = '{id}'",
            row.weight
        ),
    )?;
    let actual = db
        .list_feedback_quarantine(&workspace_id(), None)
        .map_err(|e| e.to_string())?
        .into_iter()
        .find(|row| row.id == id)
        .ok_or("missing changed quarantine")?;
    assert_eq!(
        quarantine_payload_hash(&actual)
            .map_err(|e| e.message())?
            .as_deref(),
        Some(actual.raw_event_hash.as_str())
    );
    Ok(())
}

fn rewrite_outcome(
    db: &DbConnection,
    change: impl FnOnce(&mut StoredOutcomeEvidence),
) -> TestResult {
    let mut row = db
        .list_outcome_evidence_for_recovery(&workspace_id())
        .map_err(|e| e.to_string())?
        .into_iter()
        .find(|row| row.source == OutcomeEvidenceSource::VerifierSuccess)
        .ok_or("missing verifier result")?;
    let original_ref = row.evidence_ref.clone();
    let original_source = row.source.as_str();
    change(&mut row);
    row.provenance_hash = row.computed_provenance_hash();
    execute(
        db,
        &format!(
            "UPDATE outcome_evidence_rows SET source_kind = '{}', evidence_family = '{}', base_weight_milli = {}, evidence_ref = '{}', observed_at = '{}', provenance_hash = '{}' WHERE source_kind = '{original_source}' AND evidence_ref = '{original_ref}'",
            row.source.as_str(),
            row.evidence_family,
            row.base_weight_milli,
            row.evidence_ref,
            row.observed_at,
            row.provenance_hash
        ),
    )?;
    let actual = db
        .list_outcome_evidence_for_recovery(&workspace_id())
        .map_err(|e| e.to_string())?
        .into_iter()
        .find(|actual| actual.evidence_ref == row.evidence_ref && actual.source == row.source)
        .ok_or("missing changed outcome")?;
    assert_eq!(actual, row);
    assert_eq!(actual.provenance_hash, actual.computed_provenance_hash());
    assert_eq!(actual.base_weight_milli, actual.source.base_weight_milli());
    Ok(())
}

#[test]
fn signal_fence_rejects_observation_direction_change() -> TestResult {
    assert_refused("learning_observations", |db| {
        execute(db, "UPDATE learning_observations SET signal = 'harmful'")
    })
}

#[test]
fn signal_fence_rejects_fabricated_feedback_review() -> TestResult {
    assert_refused("feedback_quarantine", |db| {
        execute(
            db,
            "UPDATE feedback_quarantine SET status = 'rejected', reviewed_at = '2026-09-02T00:00:00Z', reviewed_by = 'PRIVATE_SENTINEL'",
        )
    })
}

#[test]
fn signal_fence_rejects_self_consistent_feedback_weight_change() -> TestResult {
    assert_refused("feedback_quarantine", |db| {
        rewrite_quarantine_payload(db, false)
    })
}

#[test]
fn signal_fence_rejects_repairing_invalid_source_into_releasable_feedback() -> TestResult {
    assert_refused("feedback_quarantine", |db| {
        rewrite_quarantine_payload(db, true)
    })
}

#[test]
fn signal_fence_rejects_self_consistent_escalation_to_human_authority() -> TestResult {
    assert_refused("outcome_evidence_rows", |db| {
        rewrite_outcome(db, |row| {
            row.source = OutcomeEvidenceSource::ExplicitHuman;
            row.evidence_family = row.source.evidence_family().to_owned();
            row.base_weight_milli = row.source.base_weight_milli();
        })
    })
}

#[test]
fn signal_fence_rejects_self_consistent_evidence_reattribution() -> TestResult {
    assert_refused("outcome_evidence_rows", |db| {
        rewrite_outcome(db, |row| row.evidence_ref = "PRIVATE_SENTINEL".to_owned())
    })
}

#[test]
fn signal_fence_rejects_self_consistent_outcome_chronology_change() -> TestResult {
    assert_refused("outcome_evidence_rows", |db| {
        rewrite_outcome(db, |row| {
            row.observed_at = "2026-08-31T00:00:00Z".to_owned()
        })
    })
}

#[test]
fn learning_signals_survive_rebackup_without_reapplying_or_validating_feedback() -> TestResult {
    for level in [RedactionLevel::None, RedactionLevel::Full] {
        let fixture = fixture(level)?;
        let first = restore_backup_to_side_path(&fixture.options).map_err(|e| e.message())?;
        let db =
            DbConnection::open_file(&first.restored_database_path).map_err(|e| e.to_string())?;
        let expected = read_state(&db)?;
        assert_eq!(
            (expected.0.len(), expected.1.len(), expected.2.len()),
            (1, 2, 2)
        );
        for row in &expected.1 {
            assert_eq!(row.status, "pending");
            assert!(row.reviewed_at.is_none());
            assert!(row.released_feedback_event_id.is_none());
            assert_eq!(
                quarantine_payload_hash(row)
                    .map_err(|e| e.message())?
                    .as_deref()
                    == Some(row.raw_event_hash.as_str()),
                row.id == quarantine_id(0)
            );
        }
        assert_eq!(
            db.count_table_rows("feedback_events")
                .map_err(|e| e.to_string())?,
            0
        );
        db.close().map_err(|e| e.to_string())?;
        let backup = create_backup(&BackupCreateOptions {
            workspace_path: fixture.options.side_path.clone(),
            database_path: Some(PathBuf::from(&first.restored_database_path)),
            output_dir: None,
            label: None,
            redaction_level: level,
            include_derived: false,
            include_graph_cache: false,
            dry_run: false,
        })
        .map_err(|e| e.message())?;
        let second = restore_backup_to_side_path(&BackupRestoreOptions {
            workspace_path: fixture.options.side_path,
            backup_path: PathBuf::from(backup.backup_path),
            side_path: fixture.root.path().join("second"),
            restore_graph_cache: false,
            dry_run: false,
        })
        .map_err(|e| e.message())?;
        let db =
            DbConnection::open_file(&second.restored_database_path).map_err(|e| e.to_string())?;
        assert_eq!(
            read_state(&db)?,
            expected,
            "observation identity, invalid source hashes, authority and chronology must remain exact"
        );
        assert_eq!(
            db.count_table_rows("feedback_events")
                .map_err(|e| e.to_string())?,
            0
        );
        db.close().map_err(|e| e.to_string())?;
    }
    Ok(())
}