axiomsync 1.0.0

Core data-processing engine for AxiomSync local retrieval runtime.
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
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
use chrono::Utc;
use rusqlite::{Connection, OptionalExtension, params};

use crate::error::{AxiomError, Result};
use crate::models::{OmV2MigrationReport, QueueEventStatus, ReconcileRunStatus};
use crate::om::{OM_PROTOCOL_VERSION, OmOriginType, OmRecord, resolve_canonical_thread_id};

use super::SqliteStateStore;

const OM_V2_MIGRATION_APPLIED_AT_KEY: &str = "om_v2_one_shot_migration_applied_at";
const OM_V2_REQUIRED_EPISODIC_REV: &str = "53dfe97bc7df8e32dbee5f7b2be862a6da9171c5";

const MIGRATION_SCHEMA_SQL: &str = r"
    PRAGMA journal_mode = WAL;
    PRAGMA foreign_keys = ON;
    CREATE TABLE IF NOT EXISTS index_state (
        uri TEXT PRIMARY KEY,
        content_hash TEXT NOT NULL,
        mtime INTEGER NOT NULL,
        indexed_at TEXT NOT NULL,
        status TEXT NOT NULL
    );

    CREATE TABLE IF NOT EXISTS outbox (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        event_type TEXT NOT NULL,
        uri TEXT NOT NULL,
        payload_json TEXT NOT NULL,
        created_at TEXT NOT NULL,
        attempt_count INTEGER NOT NULL DEFAULT 0,
        status TEXT NOT NULL CHECK(status IN ('new', 'processing', 'done', 'dead_letter')),
        next_attempt_at TEXT NOT NULL,
        lane TEXT NOT NULL
    );

    CREATE TABLE IF NOT EXISTS queue_checkpoint (
        worker_name TEXT PRIMARY KEY,
        last_event_id INTEGER NOT NULL,
        updated_at TEXT NOT NULL
    );

    CREATE TABLE IF NOT EXISTS reconcile_runs (
        run_id TEXT PRIMARY KEY,
        started_at TEXT NOT NULL,
        ended_at TEXT,
        drift_count INTEGER NOT NULL DEFAULT 0,
        status TEXT NOT NULL CHECK(status IN ('running', 'dry_run', 'success', 'failed'))
    );

    CREATE TABLE IF NOT EXISTS trace_index (
        trace_id TEXT PRIMARY KEY,
        uri TEXT NOT NULL,
        request_type TEXT NOT NULL,
        query TEXT NOT NULL,
        target_uri TEXT,
        created_at TEXT NOT NULL
    );

    CREATE INDEX IF NOT EXISTS idx_trace_index_created_at
    ON trace_index(created_at DESC);

    CREATE TABLE IF NOT EXISTS system_kv (
        key TEXT PRIMARY KEY,
        value TEXT NOT NULL,
        updated_at TEXT NOT NULL
    );

    CREATE TABLE IF NOT EXISTS search_docs (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        uri TEXT NOT NULL UNIQUE,
        parent_uri TEXT,
        is_leaf INTEGER NOT NULL,
        context_type TEXT NOT NULL,
        name TEXT NOT NULL,
        abstract_text TEXT NOT NULL,
        content TEXT NOT NULL,
        tags_text TEXT NOT NULL,
        mime TEXT,
        updated_at TEXT NOT NULL,
        depth INTEGER NOT NULL
    );

    CREATE TABLE IF NOT EXISTS search_doc_tags (
        doc_id INTEGER NOT NULL,
        tag TEXT NOT NULL,
        PRIMARY KEY (doc_id, tag),
        FOREIGN KEY (doc_id) REFERENCES search_docs(id) ON DELETE CASCADE
    );

    CREATE TABLE IF NOT EXISTS om_records (
        id TEXT PRIMARY KEY,
        scope TEXT NOT NULL CHECK(scope IN ('session', 'thread', 'resource')),
        scope_key TEXT NOT NULL UNIQUE,
        session_id TEXT,
        thread_id TEXT,
        resource_id TEXT,
        generation_count INTEGER NOT NULL DEFAULT 0,
        last_applied_outbox_event_id INTEGER,
        origin_type TEXT NOT NULL CHECK(origin_type IN ('initial', 'reflection')),
        active_observations TEXT NOT NULL DEFAULT '',
        observation_token_count INTEGER NOT NULL DEFAULT 0,
        pending_message_tokens INTEGER NOT NULL DEFAULT 0,
        last_observed_at TEXT,
        current_task TEXT,
        suggested_response TEXT,
        last_activated_message_ids_json TEXT NOT NULL DEFAULT '[]',
        observer_trigger_count_total INTEGER NOT NULL DEFAULT 0,
        reflector_trigger_count_total INTEGER NOT NULL DEFAULT 0,
        is_observing INTEGER NOT NULL DEFAULT 0,
        is_reflecting INTEGER NOT NULL DEFAULT 0,
        is_buffering_observation INTEGER NOT NULL DEFAULT 0,
        is_buffering_reflection INTEGER NOT NULL DEFAULT 0,
        last_buffered_at_tokens INTEGER NOT NULL DEFAULT 0,
        last_buffered_at_time TEXT,
        buffered_reflection TEXT,
        buffered_reflection_tokens INTEGER,
        buffered_reflection_input_tokens INTEGER,
        reflected_observation_line_count INTEGER,
        created_at TEXT NOT NULL,
        updated_at TEXT NOT NULL
    );

    CREATE TABLE IF NOT EXISTS om_observation_chunks (
        id TEXT PRIMARY KEY,
        record_id TEXT NOT NULL,
        seq INTEGER NOT NULL,
        cycle_id TEXT NOT NULL,
        observations TEXT NOT NULL,
        token_count INTEGER NOT NULL,
        message_tokens INTEGER NOT NULL,
        message_ids_json TEXT NOT NULL,
        last_observed_at TEXT NOT NULL,
        created_at TEXT NOT NULL,
        FOREIGN KEY (record_id) REFERENCES om_records(id) ON DELETE CASCADE,
        UNIQUE(record_id, seq)
    );

    CREATE TABLE IF NOT EXISTS om_observer_applied_events (
        outbox_event_id INTEGER PRIMARY KEY,
        scope_key TEXT NOT NULL,
        generation_count INTEGER NOT NULL,
        created_at TEXT NOT NULL
    );

    CREATE TABLE IF NOT EXISTS om_scope_sessions (
        scope_key TEXT NOT NULL,
        session_id TEXT NOT NULL,
        updated_at TEXT NOT NULL,
        PRIMARY KEY(scope_key, session_id)
    );

    CREATE TABLE IF NOT EXISTS om_thread_states (
        scope_key TEXT NOT NULL,
        thread_id TEXT NOT NULL,
        last_observed_at TEXT,
        current_task TEXT,
        suggested_response TEXT,
        updated_at TEXT NOT NULL,
        PRIMARY KEY(scope_key, thread_id)
    );

    CREATE TABLE IF NOT EXISTS om_entries (
        entry_id TEXT PRIMARY KEY,
        scope_key TEXT NOT NULL,
        canonical_thread_id TEXT NOT NULL,
        priority TEXT NOT NULL CHECK(priority IN ('high', 'medium', 'low')),
        text TEXT NOT NULL,
        source_message_ids_json TEXT NOT NULL,
        origin_kind TEXT NOT NULL CHECK(origin_kind IN ('observation', 'reflection', 'migration')),
        created_at TEXT NOT NULL,
        superseded_by TEXT
    );

    CREATE TABLE IF NOT EXISTS om_reflection_events (
        event_id TEXT PRIMARY KEY,
        scope_key TEXT NOT NULL,
        covers_entry_ids_json TEXT NOT NULL,
        reflection_entry_id TEXT NOT NULL,
        created_at TEXT NOT NULL
    );

    CREATE TABLE IF NOT EXISTS om_continuation_state (
        scope_key TEXT NOT NULL,
        canonical_thread_id TEXT NOT NULL,
        current_task TEXT,
        suggested_response TEXT,
        confidence REAL NOT NULL DEFAULT 0.0,
        source_kind TEXT NOT NULL,
        updated_at TEXT NOT NULL,
        PRIMARY KEY(scope_key, canonical_thread_id)
    );

    CREATE TABLE IF NOT EXISTS om_protocol_meta (
        id INTEGER PRIMARY KEY CHECK(id = 1),
        protocol_version TEXT NOT NULL,
        episodic_rev TEXT NOT NULL,
        migrated_at TEXT NOT NULL,
        updated_at TEXT NOT NULL
    );

    CREATE TABLE IF NOT EXISTS om_runtime_metrics (
        id INTEGER PRIMARY KEY CHECK (id = 1),
        reflect_apply_attempts_total INTEGER NOT NULL DEFAULT 0,
        reflect_apply_applied_total INTEGER NOT NULL DEFAULT 0,
        reflect_apply_stale_generation_total INTEGER NOT NULL DEFAULT 0,
        reflect_apply_idempotent_total INTEGER NOT NULL DEFAULT 0,
        reflect_apply_latency_ms_total INTEGER NOT NULL DEFAULT 0,
        reflect_apply_latency_ms_max INTEGER NOT NULL DEFAULT 0,
        updated_at TEXT NOT NULL
    );

    CREATE TABLE IF NOT EXISTS memory_promotion_checkpoints (
        session_id TEXT NOT NULL,
        checkpoint_id TEXT NOT NULL,
        request_hash TEXT NOT NULL,
        request_json TEXT NOT NULL,
        phase TEXT NOT NULL CHECK(phase IN ('pending', 'applying', 'applied')),
        result_json TEXT,
        applied_at TEXT,
        attempt_count INTEGER NOT NULL DEFAULT 0,
        updated_at TEXT NOT NULL,
        PRIMARY KEY (session_id, checkpoint_id)
    );

    CREATE INDEX IF NOT EXISTS idx_search_docs_uri ON search_docs(uri);
    CREATE INDEX IF NOT EXISTS idx_search_docs_parent_uri ON search_docs(parent_uri);
    CREATE INDEX IF NOT EXISTS idx_search_docs_mime ON search_docs(mime);
    CREATE INDEX IF NOT EXISTS idx_search_doc_tags_tag ON search_doc_tags(tag);
    CREATE INDEX IF NOT EXISTS idx_om_records_updated_at ON om_records(updated_at);
    CREATE INDEX IF NOT EXISTS idx_om_records_scope_session ON om_records(scope, session_id);
    CREATE INDEX IF NOT EXISTS idx_om_chunks_record_created_at ON om_observation_chunks(record_id, created_at);
    CREATE INDEX IF NOT EXISTS idx_om_observer_applied_scope_generation
    ON om_observer_applied_events(scope_key, generation_count, outbox_event_id);
    CREATE INDEX IF NOT EXISTS idx_om_scope_sessions_scope_updated_at
    ON om_scope_sessions(scope_key, updated_at DESC);
    CREATE INDEX IF NOT EXISTS idx_om_thread_states_scope_updated_at
    ON om_thread_states(scope_key, updated_at DESC);
    CREATE INDEX IF NOT EXISTS idx_om_entries_scope_created_at
    ON om_entries(scope_key, created_at DESC);
    CREATE INDEX IF NOT EXISTS idx_om_entries_scope_thread
    ON om_entries(scope_key, canonical_thread_id);
    CREATE INDEX IF NOT EXISTS idx_om_continuation_scope_updated_at
    ON om_continuation_state(scope_key, updated_at DESC);
    CREATE INDEX IF NOT EXISTS idx_om_reflection_events_scope_created_at
    ON om_reflection_events(scope_key, created_at DESC);
    CREATE INDEX IF NOT EXISTS idx_memory_promotion_checkpoints_session
    ON memory_promotion_checkpoints(session_id, updated_at DESC);
";

impl SqliteStateStore {
    pub fn migrate(&self) -> Result<()> {
        let conn = self
            .conn
            .lock()
            .map_err(|_| AxiomError::mutex_poisoned("sqlite"))?;
        conn.execute_batch(MIGRATION_SCHEMA_SQL)?;
        ensure_required_column(
            &conn,
            "outbox",
            "next_attempt_at",
            "unsupported outbox schema: next_attempt_at is missing; reset workspace state database",
        )?;
        ensure_required_column(
            &conn,
            "outbox",
            "lane",
            "unsupported outbox schema: lane is missing; reset workspace state database",
        )?;
        ensure_required_column(
            &conn,
            "om_records",
            "last_activated_message_ids_json",
            "unsupported om_records schema: last_activated_message_ids_json is missing; reset workspace state database",
        )?;
        ensure_required_column(
            &conn,
            "om_records",
            "current_task",
            "unsupported om_records schema: current_task is missing; reset workspace state database",
        )?;
        ensure_required_column(
            &conn,
            "om_records",
            "suggested_response",
            "unsupported om_records schema: suggested_response is missing; reset workspace state database",
        )?;
        ensure_required_column(
            &conn,
            "om_records",
            "observer_trigger_count_total",
            "unsupported om_records schema: observer_trigger_count_total is missing; reset workspace state database",
        )?;
        ensure_required_column(
            &conn,
            "om_records",
            "reflector_trigger_count_total",
            "unsupported om_records schema: reflector_trigger_count_total is missing; reset workspace state database",
        )?;
        ensure_required_column(
            &conn,
            "om_records",
            "buffered_reflection_tokens",
            "unsupported om_records schema: buffered_reflection_tokens is missing; reset workspace state database",
        )?;
        ensure_required_column(
            &conn,
            "om_records",
            "buffered_reflection_input_tokens",
            "unsupported om_records schema: buffered_reflection_input_tokens is missing; reset workspace state database",
        )?;
        ensure_required_column(
            &conn,
            "om_records",
            "reflected_observation_line_count",
            "unsupported om_records schema: reflected_observation_line_count is missing; reset workspace state database",
        )?;
        ensure_required_column(
            &conn,
            "reconcile_runs",
            "status",
            "unsupported reconcile_runs schema: status is missing; reset workspace state database",
        )?;
        normalize_status_column(&conn, "outbox", "status")?;
        validate_status_domain(
            &conn,
            "outbox",
            "status",
            &outbox_status_domain_values(),
            "unsupported outbox schema",
        )?;
        normalize_status_column(&conn, "reconcile_runs", "status")?;
        validate_status_domain(
            &conn,
            "reconcile_runs",
            "status",
            &reconcile_run_status_domain_values(),
            "unsupported reconcile_runs schema",
        )?;
        conn.execute("DROP TABLE IF EXISTS search_docs_fts", [])?;
        drop(conn);
        Ok(())
    }

    pub fn om_v2_migration_dry_run(&self) -> Result<OmV2MigrationReport> {
        self.run_om_v2_one_shot_migration(true)
    }

    pub fn apply_om_v2_one_shot_migration(&self) -> Result<OmV2MigrationReport> {
        self.run_om_v2_one_shot_migration(false)
    }

    fn run_om_v2_one_shot_migration(&self, dry_run: bool) -> Result<OmV2MigrationReport> {
        let records = self.list_om_records()?;
        let mut report = OmV2MigrationReport {
            dry_run,
            already_applied: false,
            records_scanned: records.len(),
            entries_planned: 0,
            continuation_planned: 0,
            entries_upserted: 0,
            continuation_upserted: 0,
            protocol_meta_updated: false,
            integrity_ok: false,
            protocol_version: OM_PROTOCOL_VERSION.to_string(),
            episodic_rev: OM_V2_REQUIRED_EPISODIC_REV.to_string(),
            issues: Vec::new(),
        };

        for record in &records {
            if !record.active_observations.trim().is_empty() {
                report.entries_planned = report.entries_planned.saturating_add(1);
            }
            if has_continuation_candidate(record) {
                report.continuation_planned = report.continuation_planned.saturating_add(1);
            }
        }

        let meta_matches = self.with_conn(|conn| {
            let meta = read_om_protocol_meta(conn)?;
            Ok(meta.is_some_and(|(protocol_version, episodic_rev)| {
                protocol_version == OM_PROTOCOL_VERSION
                    && episodic_rev == OM_V2_REQUIRED_EPISODIC_REV
            }))
        })?;

        if dry_run {
            report.protocol_meta_updated = !meta_matches;
            report.integrity_ok = true;
            return Ok(report);
        }

        if meta_matches
            && self
                .get_system_value(OM_V2_MIGRATION_APPLIED_AT_KEY)?
                .is_some()
        {
            report.already_applied = true;
            report.integrity_ok = true;
            return Ok(report);
        }

        self.with_tx(|tx| {
            for record in &records {
                if !record.active_observations.trim().is_empty() {
                    let entry_id = entry_id_for_record(record);
                    let source_message_ids_json = serde_json::to_string(&record.last_activated_message_ids)?;
                    tx.execute(
                        r"
                        INSERT INTO om_entries(
                            entry_id, scope_key, canonical_thread_id, priority, text,
                            source_message_ids_json, origin_kind, created_at, superseded_by
                        )
                        VALUES(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, NULL)
                        ON CONFLICT(entry_id) DO UPDATE SET
                            scope_key = excluded.scope_key,
                            canonical_thread_id = excluded.canonical_thread_id,
                            priority = excluded.priority,
                            text = excluded.text,
                            source_message_ids_json = excluded.source_message_ids_json,
                            origin_kind = excluded.origin_kind,
                            created_at = excluded.created_at,
                            superseded_by = NULL
                        ",
                        params![
                            entry_id,
                            record.scope_key,
                            canonical_thread_id_for_record(record),
                            "medium",
                            record.active_observations.trim(),
                            source_message_ids_json,
                            entry_origin_kind(record.origin_type),
                            record.created_at.to_rfc3339(),
                        ],
                    )?;
                    report.entries_upserted = report.entries_upserted.saturating_add(1);
                }

                let current_task = normalize_optional_text(record.current_task.as_deref());
                let suggested_response =
                    normalize_optional_text(record.suggested_response.as_deref());
                if current_task.is_none() && suggested_response.is_none() {
                    continue;
                }

                tx.execute(
                    r"
                    INSERT INTO om_continuation_state(
                        scope_key, canonical_thread_id, current_task, suggested_response,
                        confidence, source_kind, updated_at
                    )
                    VALUES(?1, ?2, ?3, ?4, ?5, 'migration', ?6)
                    ON CONFLICT(scope_key, canonical_thread_id) DO UPDATE SET
                        current_task = excluded.current_task,
                        suggested_response = excluded.suggested_response,
                        confidence = excluded.confidence,
                        source_kind = excluded.source_kind,
                        updated_at = excluded.updated_at
                    ",
                    params![
                        record.scope_key,
                        canonical_thread_id_for_record(record),
                        current_task,
                        suggested_response,
                        continuation_confidence(record.current_task.as_deref(), record.suggested_response.as_deref()),
                        record.updated_at.to_rfc3339(),
                    ],
                )?;
                report.continuation_upserted = report.continuation_upserted.saturating_add(1);
            }

            let now = Utc::now().to_rfc3339();
            tx.execute(
                r"
                INSERT INTO om_protocol_meta(id, protocol_version, episodic_rev, migrated_at, updated_at)
                VALUES(1, ?1, ?2, ?3, ?3)
                ON CONFLICT(id) DO UPDATE SET
                    protocol_version = excluded.protocol_version,
                    episodic_rev = excluded.episodic_rev,
                    updated_at = excluded.updated_at
                ",
                params![OM_PROTOCOL_VERSION, OM_V2_REQUIRED_EPISODIC_REV, now],
            )?;
            report.protocol_meta_updated = true;
            Ok(())
        })?;

        self.set_system_value(OM_V2_MIGRATION_APPLIED_AT_KEY, &Utc::now().to_rfc3339())?;

        let issues = self.validate_om_v2_migration_integrity(&records)?;
        report.integrity_ok = issues.is_empty();
        report.issues = issues;
        if !report.integrity_ok {
            return Err(AxiomError::Validation(format!(
                "om v2 one-shot migration integrity check failed: {}",
                report.issues.join("; ")
            )));
        }
        Ok(report)
    }

    fn validate_om_v2_migration_integrity(&self, records: &[OmRecord]) -> Result<Vec<String>> {
        self.with_conn(|conn| {
            let mut issues = Vec::<String>::new();

            let meta = read_om_protocol_meta(conn)?;
            match meta {
                Some((protocol_version, episodic_rev))
                    if protocol_version == OM_PROTOCOL_VERSION
                        && episodic_rev == OM_V2_REQUIRED_EPISODIC_REV => {}
                Some((protocol_version, episodic_rev)) => {
                    issues.push(format!(
                        "om_protocol_meta mismatch: protocol_version={protocol_version}, episodic_rev={episodic_rev}"
                    ));
                }
                None => issues.push("missing om_protocol_meta row".to_string()),
            }

            for record in records {
                if !record.active_observations.trim().is_empty() {
                    let exists = conn
                        .query_row(
                            "SELECT 1 FROM om_entries WHERE entry_id = ?1 LIMIT 1",
                            params![entry_id_for_record(record)],
                            |_| Ok(()),
                        )
                        .optional()?
                        .is_some();
                    if !exists {
                        issues.push(format!(
                            "missing om_entries row for scope_key={}",
                            record.scope_key
                        ));
                    }
                }

                if has_continuation_candidate(record) {
                    let exists = conn
                        .query_row(
                            "SELECT 1 FROM om_continuation_state WHERE scope_key = ?1 AND canonical_thread_id = ?2 LIMIT 1",
                            params![record.scope_key, canonical_thread_id_for_record(record)],
                            |_| Ok(()),
                        )
                        .optional()?
                        .is_some();
                    if !exists {
                        issues.push(format!(
                            "missing om_continuation_state row for scope_key={} canonical_thread_id={}",
                            record.scope_key,
                            canonical_thread_id_for_record(record)
                        ));
                    }
                }
            }

            Ok(issues)
        })
    }
}

fn read_om_protocol_meta(conn: &Connection) -> Result<Option<(String, String)>> {
    let row = conn
        .query_row(
            "SELECT protocol_version, episodic_rev FROM om_protocol_meta WHERE id = 1",
            [],
            |row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)),
        )
        .optional()?;
    Ok(row)
}

fn entry_id_for_record(record: &OmRecord) -> String {
    format!(
        "omv2:{}:{}:{}",
        record.scope_key,
        record.generation_count,
        record.created_at.timestamp_micros()
    )
}

fn canonical_thread_id_for_record(record: &OmRecord) -> String {
    let fallback = record
        .session_id
        .as_deref()
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .unwrap_or(record.scope_key.as_str());
    resolve_canonical_thread_id(
        record.scope,
        &record.scope_key,
        record.thread_id.as_deref(),
        record.session_id.as_deref(),
        fallback,
    )
}

const fn entry_origin_kind(origin: OmOriginType) -> &'static str {
    match origin {
        OmOriginType::Initial => "observation",
        OmOriginType::Reflection => "reflection",
    }
}

fn has_continuation_candidate(record: &OmRecord) -> bool {
    normalize_optional_text(record.current_task.as_deref()).is_some()
        || normalize_optional_text(record.suggested_response.as_deref()).is_some()
}

fn normalize_optional_text(raw: Option<&str>) -> Option<String> {
    raw.map(str::trim)
        .filter(|value| !value.is_empty())
        .map(ToString::to_string)
}

fn continuation_confidence(current_task: Option<&str>, suggested_response: Option<&str>) -> f64 {
    let has_current_task = normalize_optional_text(current_task).is_some();
    let has_suggested_response = normalize_optional_text(suggested_response).is_some();
    if has_current_task && has_suggested_response {
        0.92
    } else if has_current_task || has_suggested_response {
        0.82
    } else {
        0.0
    }
}

fn outbox_status_domain_values() -> [&'static str; 4] {
    [
        QueueEventStatus::New.as_str(),
        QueueEventStatus::Processing.as_str(),
        QueueEventStatus::Done.as_str(),
        QueueEventStatus::DeadLetter.as_str(),
    ]
}

fn reconcile_run_status_domain_values() -> [&'static str; 4] {
    [
        ReconcileRunStatus::Running.as_str(),
        ReconcileRunStatus::DryRun.as_str(),
        ReconcileRunStatus::Success.as_str(),
        ReconcileRunStatus::Failed.as_str(),
    ]
}

fn normalize_status_column(conn: &Connection, table: &str, column: &str) -> Result<()> {
    conn.execute(
        &format!(
            "UPDATE {table} SET {column} = lower(trim({column})) WHERE {column} <> lower(trim({column}))"
        ),
        [],
    )?;
    Ok(())
}

fn validate_status_domain(
    conn: &Connection,
    table: &str,
    column: &str,
    allowed: &[&str],
    error_prefix: &str,
) -> Result<()> {
    let mut stmt = conn.prepare(&format!("SELECT DISTINCT {column} FROM {table}"))?;
    let rows = stmt.query_map([], |row| row.get::<_, String>(0))?;
    let mut invalid = Vec::<String>::new();
    for row in rows {
        let value = row?.trim().to_ascii_lowercase();
        if !allowed.contains(&value.as_str()) {
            invalid.push(value);
        }
    }

    if invalid.is_empty() {
        return Ok(());
    }

    invalid.sort();
    invalid.dedup();
    Err(AxiomError::Validation(format!(
        "{error_prefix}: invalid status value(s): {}",
        invalid.join(", ")
    )))
}

fn has_column(conn: &Connection, table: &str, column: &str) -> Result<bool> {
    let mut stmt = conn.prepare(&format!("PRAGMA table_info({table})"))?;
    let rows = stmt.query_map([], |row| row.get::<_, String>(1))?;
    for row in rows {
        if row? == column {
            return Ok(true);
        }
    }
    Ok(false)
}

fn ensure_required_column(
    conn: &Connection,
    table: &str,
    column: &str,
    error_message: &'static str,
) -> Result<()> {
    if has_column(conn, table, column)? {
        Ok(())
    } else {
        Err(AxiomError::Validation(error_message.to_string()))
    }
}