lwc 0.17.11

Agent-driven proactive memory CLI for AI agents — autonomously recall, maintain, and evolve persistent, source-grounded knowledge across sessions.
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
fn elapsed_millis(started: Instant) -> u64 {
    u64::try_from(started.elapsed().as_millis()).unwrap_or(u64::MAX)
}

fn attached_store_identity(conn: &Connection) -> Result<StoreIdentity> {
    let (store_id, revision) = conn.query_row(
        "SELECT
            MAX(CASE WHEN key = 'store_id' THEN value END),
            MAX(CASE WHEN key = 'store_revision' THEN value END)
         FROM candidate.meta
         WHERE key IN ('store_id', 'store_revision')",
        [],
        |row| {
            Ok((
                row.get::<_, Option<String>>(0)?,
                row.get::<_, Option<String>>(1)?,
            ))
        },
    )?;
    let operation_id = conn.query_row(
        "SELECT COALESCE(MAX(id), 0) FROM candidate.operations",
        [],
        |row| row.get(0),
    )?;
    Ok(StoreIdentity {
        store_id: store_id.ok_or_else(|| {
            AppError::new("changeset_corrupt", "draft store_id metadata is missing")
        })?,
        revision: revision.ok_or_else(|| {
            AppError::new(
                "changeset_corrupt",
                "draft store_revision metadata is missing",
            )
        })?,
        operation_id,
    })
}

fn validate_changeset_table_inventory(conn: &Connection, schema: &str) -> Result<()> {
    const TABLES: [&str; 63] = [
        "agent_plan_tracks",
        "agent_todo_tracks",
        "changesets",
        "ingest_jobs",
        "links",
        "memory_changes",
        "memory_events",
        "memory_evidence",
        "memory_feedback",
        "memory_fragments",
        "memory_fts",
        "memory_fts_config",
        "memory_fts_content",
        "memory_fts_data",
        "memory_fts_docsize",
        "memory_fts_idx",
        "memory_hint_state",
        "memory_relations",
        "memory_state",
        "meta",
        "operations",
        "page_provenance",
        "page_sources",
        "page_tags",
        "pages",
        "plan_constraints",
        "plan_fts",
        "plan_fts_config",
        "plan_fts_content",
        "plan_fts_data",
        "plan_fts_docsize",
        "plan_fts_idx",
        "plan_history",
        "plan_steps",
        "plan_tags",
        "plans",
        "retrieval_feedback",
        "retrieval_weights",
        "search_fts",
        "search_fts_config",
        "search_fts_content",
        "search_fts_data",
        "search_fts_docsize",
        "search_fts_idx",
        "search_spans",
        "semantic_relations",
        "source_path_revisions",
        "sources",
        "span_fts",
        "span_fts_config",
        "span_fts_content",
        "span_fts_data",
        "span_fts_docsize",
        "span_fts_idx",
        "tags",
        "todo_fts",
        "todo_fts_config",
        "todo_fts_content",
        "todo_fts_data",
        "todo_fts_docsize",
        "todo_fts_idx",
        "todo_items",
        "todo_tags",
    ];
    let sql = format!(
        "SELECT name FROM {schema}.sqlite_schema
         WHERE type = 'table' AND name NOT LIKE 'sqlite_%'
         ORDER BY name"
    );
    let mut statement = conn.prepare(&sql)?;
    let actual = statement
        .query_map([], |row| row.get::<_, String>(0))?
        .collect::<rusqlite::Result<Vec<_>>>()?;
    if actual != TABLES {
        return Err(AppError::new(
            "changeset_corrupt",
            format!("{schema} Wiki table inventory does not match store format v{USER_VERSION}"),
        ));
    }
    Ok(())
}

type ChangedSearchDocuments = (Vec<(i64, Option<i64>)>, Vec<(String, Option<i64>)>);

fn changed_search_documents(
    conn: &Connection,
    source_schema: &str,
) -> Result<ChangedSearchDocuments> {
    if source_schema != "candidate" {
        return Err(AppError::new(
            "changeset_corrupt",
            "unsupported attached changeset schema",
        ));
    }
    let mut source_statement = conn.prepare(
        "WITH changed(id) AS (
             SELECT live.id FROM main.sources live
             WHERE NOT EXISTS (
                 SELECT 1 FROM candidate.sources draft
                 WHERE draft.id = live.id
                   AND draft.content_hash IS live.content_hash
                   AND draft.title IS live.title
                   AND draft.origin IS live.origin
                   AND draft.content IS live.content
                   AND draft.structural_navigation IS live.structural_navigation
                   AND draft.created_at IS live.created_at
             )
             UNION
             SELECT draft.id FROM candidate.sources draft
             WHERE NOT EXISTS (
                 SELECT 1 FROM main.sources live
                 WHERE live.id = draft.id
                   AND live.content_hash IS draft.content_hash
                   AND live.title IS draft.title
                   AND live.origin IS draft.origin
                   AND live.content IS draft.content
                   AND live.structural_navigation IS draft.structural_navigation
                   AND live.created_at IS draft.created_at
             )
         )
         SELECT changed.id,
                (SELECT rowid FROM candidate.search_fts
                 WHERE doc_type = 'source'
                   AND identifier = CAST(changed.id AS TEXT)
                 LIMIT 1)
         FROM changed ORDER BY changed.id",
    )?;
    let sources = source_statement
        .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?
        .collect::<rusqlite::Result<Vec<_>>>()?;

    let mut page_statement = conn.prepare(
        "WITH changed(slug) AS (
             SELECT live.slug FROM main.pages live
             WHERE NOT EXISTS (
                 SELECT 1 FROM candidate.pages draft
                 WHERE draft.slug = live.slug
                   AND draft.title IS live.title
                   AND draft.kind IS live.kind
                   AND draft.summary IS live.summary
                   AND draft.body IS live.body
                   AND draft.structural_navigation IS live.structural_navigation
                   AND draft.created_at IS live.created_at
                   AND draft.updated_at IS live.updated_at
             )
             UNION
             SELECT draft.slug FROM candidate.pages draft
             WHERE NOT EXISTS (
                 SELECT 1 FROM main.pages live
                 WHERE live.slug = draft.slug
                   AND live.title IS draft.title
                   AND live.kind IS draft.kind
                   AND live.summary IS draft.summary
                   AND live.body IS draft.body
                   AND live.structural_navigation IS draft.structural_navigation
                   AND live.created_at IS draft.created_at
                   AND live.updated_at IS draft.updated_at
             )
         )
         SELECT changed.slug,
                (SELECT rowid FROM candidate.search_fts
                 WHERE doc_type = 'page' AND identifier = changed.slug
                 LIMIT 1)
         FROM changed ORDER BY changed.slug",
    )?;
    let pages = page_statement
        .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?
        .collect::<rusqlite::Result<Vec<_>>>()?;
    Ok((sources, pages))
}

fn refresh_changed_search_documents(
    tx: &Transaction<'_>,
    (sources, pages): ChangedSearchDocuments,
) -> Result<()> {
    for (source_id, _) in &sources {
        tx.execute(
            "DELETE FROM search_fts WHERE doc_type = 'source' AND identifier = ?1",
            params![source_id.to_string()],
        )?;
    }
    for (slug, _) in &pages {
        tx.execute(
            "DELETE FROM search_fts WHERE doc_type = 'page' AND identifier = ?1",
            params![slug],
        )?;
    }

    for (source_id, rowid) in sources {
        let source = tx
            .query_row(
                "SELECT title, origin, content FROM sources WHERE id = ?1",
                params![source_id],
                |row| {
                    Ok((
                        row.get::<_, Option<String>>(0)?,
                        row.get::<_, String>(1)?,
                        row.get::<_, String>(2)?,
                    ))
                },
            )
            .optional()?;
        match (source, rowid) {
            (Some((title, origin, content)), Some(rowid)) => index_source(
                tx,
                Some(rowid),
                source_id,
                title.as_deref(),
                &origin,
                &content,
            )?,
            (None, None) => {}
            _ => {
                return Err(AppError::new(
                    "changeset_corrupt",
                    "candidate source and search index do not match",
                ));
            }
        }
    }
    for (slug, rowid) in pages {
        let page = tx
            .query_row(
                "SELECT title, summary, body FROM pages WHERE slug = ?1",
                params![&slug],
                |row| {
                    Ok((
                        row.get::<_, String>(0)?,
                        row.get::<_, Option<String>>(1)?,
                        row.get::<_, String>(2)?,
                    ))
                },
            )
            .optional()?;
        match (page, rowid) {
            (Some((title, summary, body)), Some(rowid)) => {
                index_page(tx, Some(rowid), &slug, &title, summary.as_deref(), &body)?;
            }
            (None, None) => {}
            _ => {
                return Err(AppError::new(
                    "changeset_corrupt",
                    "candidate page and search index do not match",
                ));
            }
        }
    }
    Ok(())
}

fn replace_main_from_attached(tx: &Transaction<'_>, source_schema: &str) -> Result<()> {
    if source_schema != "candidate" {
        return Err(AppError::new(
            "changeset_corrupt",
            "unsupported attached changeset schema",
        ));
    }
    tx.execute_batch(
        "DELETE FROM semantic_relations;
         DELETE FROM search_spans;
         DELETE FROM page_tags;
         DELETE FROM tags;
         DELETE FROM page_sources;
         DELETE FROM page_provenance;
         DELETE FROM links;
         DELETE FROM ingest_jobs;
         DELETE FROM source_path_revisions;
         DELETE FROM retrieval_weights;
         DELETE FROM retrieval_feedback;
         DELETE FROM changesets;
         DELETE FROM operations;
         DELETE FROM pages;
         DELETE FROM sources;
         DELETE FROM meta;

         INSERT INTO meta(key, value)
         SELECT key, value FROM candidate.meta;
         DELETE FROM meta WHERE key = 'changeset_frozen';
         INSERT INTO sources(
             id, content_hash, title, origin, content, structural_navigation, created_at
         ) SELECT
             id, content_hash, title, origin, content, structural_navigation, created_at
           FROM candidate.sources;
         INSERT INTO pages(
             slug, title, kind, summary, body, structural_navigation, created_at, updated_at
         ) SELECT
             slug, title, kind, summary, body, structural_navigation, created_at, updated_at
           FROM candidate.pages;",
    )
    .map_err(changeset_copy_error)?;
    changeset_test_fault("mid_copy")?;
    tx.execute_batch(
        "
         INSERT INTO page_sources(page_slug, source_id)
         SELECT page_slug, source_id FROM candidate.page_sources;
         INSERT INTO page_provenance(page_slug, provenance)
         SELECT page_slug, provenance FROM candidate.page_provenance;
         INSERT INTO tags(
             name, autoload, autoload_priority, autoload_limit,
             autoload_max_chars, reason, updated_at
         ) SELECT
             name, autoload, autoload_priority, autoload_limit,
             autoload_max_chars, reason, updated_at
           FROM candidate.tags;
         INSERT INTO page_tags(
             tag_name, page_slug, priority, reason, created_at, updated_at
         ) SELECT
             tag_name, page_slug, priority, reason, created_at, updated_at
           FROM candidate.page_tags;
         INSERT INTO links(from_slug, to_slug)
         SELECT from_slug, to_slug FROM candidate.links;
         INSERT INTO operations(id, action, target, detail_json, created_at)
         SELECT id, action, target, detail_json, created_at FROM candidate.operations;
         INSERT INTO ingest_jobs(
             source_id, status, attempts, analysis, last_error,
             no_derived_pages_reason, updated_at
         ) SELECT
             source_id, status, attempts, analysis, last_error,
             no_derived_pages_reason, updated_at
           FROM candidate.ingest_jobs;
         INSERT INTO source_path_revisions(tracked_path, revision, source_id, observed_at)
         SELECT tracked_path, revision, source_id, observed_at
           FROM candidate.source_path_revisions;
         INSERT INTO retrieval_weights(
             target_type, target_identifier, provenance, weight, reason, updated_at
         ) SELECT
             target_type, target_identifier, provenance, weight, reason, updated_at
           FROM candidate.retrieval_weights;
         INSERT INTO retrieval_feedback(
             query_fingerprint, target_type, target_identifier,
             provenance, signal, reason, updated_at
         ) SELECT
             query_fingerprint, target_type, target_identifier,
             provenance, signal, reason, updated_at
           FROM candidate.retrieval_feedback;
         INSERT INTO changesets(
             id, name, status, base_revision, base_operation_id, begin_operation_id,
             pre_commit_checkpoint, post_revision, created_at, committed_at, rolled_back_at
         ) SELECT
             id, name, status, base_revision, base_operation_id, begin_operation_id,
             pre_commit_checkpoint, post_revision, created_at, committed_at, rolled_back_at
           FROM candidate.changesets;
         INSERT INTO semantic_relations(
             id, relation_type, from_identifier, to_identifier,
             confidence, provenance, reason, source_ids_json, created_at, updated_at
         ) SELECT
             id, relation_type, from_identifier, to_identifier,
             confidence, provenance, reason, source_ids_json, created_at, updated_at
           FROM candidate.semantic_relations;
         INSERT INTO search_spans(
             span_id, span_type, document_type, document_identifier,
             parent_identifier, ordinal, byte_start, byte_end,
             content_fingerprint, segmenter_version, active
         ) SELECT
             span_id, span_type, document_type, document_identifier,
             parent_identifier, ordinal, byte_start, byte_end,
             content_fingerprint, segmenter_version, active
           FROM candidate.search_spans;",
    )
    .map_err(changeset_copy_error)?;
    rebuild_span_index(tx)?;
    Ok(())
}

fn rebuild_span_index(tx: &Transaction<'_>) -> Result<()> {
    tx.execute("DELETE FROM span_fts", [])?;
    let rows = {
        let mut statement = tx.prepare(
            "SELECT n.span_id, n.span_type, n.document_type,
                    n.document_identifier, n.byte_start, n.byte_end,
                    CASE n.document_type WHEN 'page' THEN p.body ELSE s.content END,
                    CASE n.document_type
                        WHEN 'page' THEN p.title
                        ELSE COALESCE(s.title, s.origin)
                    END AS title,
                    CASE n.document_type
                        WHEN 'page' THEN p.slug
                        ELSE s.origin
                    END AS path
             FROM search_spans n
             LEFT JOIN pages p
               ON n.document_type = 'page' AND p.slug = n.document_identifier
             LEFT JOIN sources s
               ON n.document_type = 'source'
              AND s.id = CAST(n.document_identifier AS INTEGER)
             WHERE n.active = 1
             ORDER BY n.document_type, n.document_identifier, n.span_type, n.ordinal, n.span_id",
        )?;
        statement
            .query_map([], |row| {
                Ok((
                    row.get::<_, String>(0)?,
                    row.get::<_, String>(1)?,
                    row.get::<_, String>(2)?,
                    row.get::<_, String>(3)?,
                    row.get::<_, i64>(4)?,
                    row.get::<_, i64>(5)?,
                    row.get::<_, String>(6)?,
                    row.get::<_, String>(7)?,
                    row.get::<_, String>(8)?,
                ))
            })?
            .collect::<rusqlite::Result<Vec<_>>>()?
    };
    let mut heading_paths = None;
    for (node_id, node_type, document_type, document_identifier, start, end, body, title, path) in
        rows
    {
        let start = usize::try_from(start)
            .map_err(|_| AppError::new("changeset_corrupt", "negative search span start"))?;
        let end = usize::try_from(end)
            .map_err(|_| AppError::new("changeset_corrupt", "negative search span end"))?;
        let label = body.get(start..end).ok_or_else(|| {
            AppError::new("changeset_corrupt", "search span has an invalid byte range")
        })?;
        let document_key = (document_type.clone(), document_identifier.clone());
        if heading_paths
            .as_ref()
            .is_none_or(|(indexed_document, _)| indexed_document != &document_key)
        {
            let mut ranges = BTreeMap::new();
            for passage in crate::segment::segment_document(&body)
                .map_err(segment_error)?
                .passages
            {
                let heading_path = passage.heading_path.join(" ");
                ranges.insert(
                    (passage.range.start, passage.range.end),
                    heading_path.clone(),
                );
                for sentence in passage.sentences {
                    ranges.insert(
                        (sentence.range.start, sentence.range.end),
                        heading_path.clone(),
                    );
                }
            }
            heading_paths = Some((document_key, ranges));
        }
        let heading_path = heading_paths
            .as_ref()
            .and_then(|(_, ranges)| ranges.get(&(start, end)))
            .cloned()
            .unwrap_or_default();
        tx.execute(
            "INSERT INTO span_fts(
                span_id, span_type, document_type, document_identifier,
                title_terms, path_terms, heading_terms, body_terms
             ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
            params![
                node_id,
                node_type,
                document_type,
                document_identifier,
                joined_terms(&title),
                joined_terms(&path),
                joined_terms(&heading_path),
                joined_terms(label),
            ],
        )?;
    }
    Ok(())
}

fn changeset_copy_error(error: rusqlite::Error) -> AppError {
    AppError::new(
        "changeset_corrupt",
        format!("changeset canonical copy failed: {error}"),
    )
}

fn wal_checkpoint_truncate(conn: &Connection, wait_for_readers: bool) -> Result<(i64, i64, i64)> {
    if wait_for_readers {
        return conn
            .query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |row| {
                Ok((row.get(0)?, row.get(1)?, row.get(2)?))
            })
            .map_err(Into::into);
    }
    conn.busy_timeout(Duration::ZERO)?;
    let checkpoint = conn.query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |row| {
        Ok((row.get(0)?, row.get(1)?, row.get(2)?))
    });
    conn.busy_timeout(BUSY_TIMEOUT)?;
    checkpoint.map_err(Into::into)
}

fn changeset_test_fault(point: &str) -> Result<()> {
    if std::env::var("LWC_TEST_CHANGESET_FAULT").as_deref() == Ok(point) {
        return Err(AppError::new(
            "changeset_test_fault",
            format!("injected changeset fault at {point}"),
        ));
    }
    Ok(())
}

fn changeset_test_crash(point: &str) {
    if std::env::var("LWC_TEST_CHANGESET_FAULT").as_deref() == Ok(format!("crash:{point}").as_str())
    {
        std::process::abort();
    }
}

fn validate_database_integrity(conn: &Connection) -> Result<()> {
    let mut foreign_keys = conn.prepare("PRAGMA foreign_key_check")?;
    if foreign_keys.query([])?.next()?.is_some() {
        return Err(AppError::new(
            "changeset_corrupt",
            "changeset database violates a foreign key",
        ));
    }
    let integrity: String = conn.query_row("PRAGMA integrity_check", [], |row| row.get(0))?;
    if integrity != "ok" {
        return Err(AppError::new(
            "changeset_corrupt",
            format!("changeset database failed integrity_check: {integrity}"),
        ));
    }
    Ok(())
}

fn record_operation(
    tx: &Transaction<'_>,
    action: &str,
    target: &str,
    detail: &Value,
) -> Result<String> {
    if tx
        .query_row(
            "SELECT 1 FROM meta WHERE key = ?1 LIMIT 1",
            params![CHANGESET_FREEZE_KEY],
            |_| Ok(()),
        )
        .optional()?
        .is_some()
    {
        return Err(AppError::new(
            "changeset_frozen",
            "changeset is frozen for commit; retry commit or discard it instead of staging more writes",
        ));
    }
    let detail_json = serde_json::to_string(detail)
        .map_err(|error| AppError::new("json_error", error.to_string()))?;
    tx.execute(
        "INSERT INTO operations(action, target, detail_json) VALUES (?1, ?2, ?3)",
        params![action, target, detail_json],
    )?;
    let revision: String =
        tx.query_row("SELECT LOWER(HEX(RANDOMBLOB(32)))", [], |row| row.get(0))?;
    let updated = tx.execute(
        "UPDATE meta SET value = ?1 WHERE key = 'store_revision'",
        params![&revision],
    )?;
    if updated != 1 {
        return Err(AppError::new(
            "corrupt_store",
            "wiki store_revision metadata is missing",
        ));
    }
    Ok(revision)
}

fn read_source_record(row: &rusqlite::Row<'_>) -> rusqlite::Result<SourceRecord> {
    Ok(SourceRecord {
        id: row.get(0)?,
        title: row.get(1)?,
        origin: row.get(2)?,
        content_hash: row.get(3)?,
        content: row.get(4)?,
        created_at: row.get(5)?,
    })
}

fn window_source(
    mut source: SourceRecord,
    offset_chars: usize,
    max_chars: Option<usize>,
) -> Result<(SourceRecord, SourceWindow)> {
    if max_chars == Some(0) {
        return Err(AppError::new(
            "invalid_limit",
            "max-chars must be greater than zero",
        ));
    }
    let total_chars = source.content.chars().count();
    if offset_chars > total_chars {
        return Err(AppError::new(
            "invalid_offset",
            format!("offset-chars {offset_chars} exceeds source length {total_chars}"),
        ));
    }
    let requested = max_chars.unwrap_or_else(|| total_chars.saturating_sub(offset_chars));
    let content = source
        .content
        .chars()
        .skip(offset_chars)
        .take(requested)
        .collect::<String>();
    let returned_chars = content.chars().count();
    let consumed = offset_chars.saturating_add(returned_chars);
    let has_more = consumed < total_chars;
    source.content = content;
    Ok((
        source,
        SourceWindow {
            offset_chars,
            returned_chars,
            total_chars,
            next_offset_chars: has_more.then_some(consumed),
            has_more,
        },
    ))
}

fn read_source_summary(row: &rusqlite::Row<'_>) -> rusqlite::Result<SourceSummary> {
    Ok(SourceSummary {
        id: row.get(0)?,
        title: row.get(1)?,
        origin: row.get(2)?,
        content_hash: row.get(3)?,
        bytes: row.get(4)?,
        created_at: row.get(5)?,
    })
}

fn read_source_status_target(row: &rusqlite::Row<'_>) -> rusqlite::Result<SourceStatusTarget> {
    Ok(SourceStatusTarget {
        requested_source_id: row.get(0)?,
        tracked_path: row.get(1)?,
        head_source_id: row.get(2)?,
        head_revision: row.get(3)?,
        head_content_hash: row.get(4)?,
    })
}

fn read_ingest_job_summary(row: &rusqlite::Row<'_>) -> rusqlite::Result<IngestJobSummary> {
    Ok(IngestJobSummary {
        source_id: row.get(0)?,
        status: row.get(1)?,
        attempts: row.get(2)?,
        last_error: row.get(3)?,
        no_derived_pages_reason: row.get(4)?,
        updated_at: row.get(5)?,
    })
}

fn read_operation_record(row: &rusqlite::Row<'_>) -> rusqlite::Result<OperationRecord> {
    let detail_json: String = row.get(3)?;
    let detail = serde_json::from_str(&detail_json).map_err(|error| {
        rusqlite::Error::FromSqlConversionFailure(3, rusqlite::types::Type::Text, Box::new(error))
    })?;
    Ok(OperationRecord {
        id: row.get(0)?,
        action: row.get(1)?,
        target: row.get(2)?,
        detail,
        created_at: row.get(4)?,
    })
}

fn read_changeset_history(row: &rusqlite::Row<'_>) -> rusqlite::Result<ChangesetHistoryState> {
    Ok(ChangesetHistoryState {
        id: row.get(0)?,
        name: row.get(1)?,
        status: row.get(2)?,
        base_revision: row.get(3)?,
        base_operation_id: row.get(4)?,
        begin_operation_id: row.get(5)?,
        pre_commit_checkpoint: row.get(6)?,
        post_revision: row.get(7)?,
        created_at: row.get(8)?,
        committed_at: row.get(9)?,
        rolled_back_at: row.get(10)?,
    })
}

fn read_retrieval_adjustment(row: &rusqlite::Row<'_>) -> rusqlite::Result<RetrievalAdjustment> {
    Ok(RetrievalAdjustment {
        target_type: row.get(0)?,
        target_identifier: row.get(1)?,
        provenance: row.get(2)?,
        weight: row.get(3)?,
        reason: row.get(4)?,
        updated_at: row.get(5)?,
    })
}

fn validate_retrieval_weight(weight: i32) -> Result<()> {
    if matches!(weight, -2 | -1 | 1 | 2) {
        Ok(())
    } else {
        Err(AppError::new(
            "invalid_weight",
            "weight must be one of -2, -1, 1, or 2",
        ))
    }
}

fn validate_retrieval_provenance(provenance: &str) -> Result<()> {
    if matches!(provenance, "user-provided" | "agent-observed") {
        Ok(())
    } else {
        Err(AppError::new(
            "invalid_provenance",
            "retrieval provenance must be user-provided or agent-observed",
        ))
    }
}

fn validate_nonempty_reason(reason: &str) -> Result<()> {
    if reason.trim().is_empty() {
        Err(AppError::new(
            "invalid_input",
            "retrieval adjustment reason must not be empty",
        ))
    } else {
        Ok(())
    }
}

fn normalize_retrieval_target(
    conn: &Connection,
    target_type: &str,
    identifier: &str,
) -> Result<String> {
    match target_type {
        "page" => {
            let exists = conn
                .query_row(
                    "SELECT 1 FROM pages WHERE slug = ?1",
                    params![identifier],
                    |_| Ok(()),
                )
                .optional()?
                .is_some();
            if exists {
                Ok(identifier.to_string())
            } else {
                Err(AppError::new(
                    "page_not_found",
                    format!("page not found: {identifier}"),
                ))
            }
        }
        "source" => {
            let source_id = identifier.parse::<i64>().map_err(|_| {
                AppError::new(
                    "invalid_input",
                    format!("source identifier must be an integer: {identifier}"),
                )
            })?;
            let exists = conn
                .query_row(
                    "SELECT 1 FROM sources WHERE id = ?1",
                    params![source_id],
                    |_| Ok(()),
                )
                .optional()?
                .is_some();
            if exists {
                Ok(source_id.to_string())
            } else {
                Err(AppError::new(
                    "source_not_found",
                    format!("source not found: {source_id}"),
                ))
            }
        }
        _ => Err(AppError::new(
            "invalid_input",
            "retrieval target type must be page or source",
        )),
    }
}

fn validate_ingest_status(status: &str) -> Result<()> {
    if matches!(
        status,
        "pending" | "analyzing" | "generating" | "completed" | "failed"
    ) {
        Ok(())
    } else {
        Err(AppError::new(
            "invalid_ingest_status",
            format!("unsupported ingest status: {status}"),
        ))
    }
}

fn require_ingest_state(tx: &Transaction<'_>, source_id: i64, allowed: &[&str]) -> Result<()> {
    let status = tx
        .query_row(
            "SELECT status FROM ingest_jobs WHERE source_id = ?1",
            params![source_id],
            |row| row.get::<_, String>(0),
        )
        .optional()?
        .ok_or_else(|| {
            AppError::new(
                "ingest_job_not_found",
                format!("ingest job not found for source {source_id}"),
            )
        })?;
    if allowed.contains(&status.as_str()) {
        Ok(())
    } else {
        Err(AppError::new(
            "invalid_ingest_state",
            format!(
                "source {source_id} is {status}; expected {}",
                allowed.join(" or ")
            ),
        ))
    }
}

fn claim_ingest_job(tx: &Transaction<'_>, source_id: i64) -> Result<()> {
    let updated = tx.execute(
        &format!(
            "UPDATE ingest_jobs
             SET status = 'analyzing',
                 attempts = attempts + 1,
                 last_error = NULL,
                 updated_at = {TIMESTAMP_SQL}
             WHERE source_id = ?1 AND status = 'pending'"
        ),
        params![source_id],
    )?;
    if updated != 1 {
        return Err(AppError::new(
            "invalid_ingest_state",
            format!("source {source_id} is not pending"),
        ));
    }
    record_operation(tx, "ingest_claim", &source_id.to_string(), &json!({})).map(|_| ())
}

fn load_page_mutation_base(conn: &Connection, slug: &str) -> Result<Option<PageMutationBase>> {
    let Some((title, kind, summary, body, structural_navigation, updated_at)) = conn
        .query_row(
            "SELECT title, kind, summary, body, structural_navigation, updated_at
             FROM pages WHERE slug = ?1",
            [slug],
            |row| {
                Ok((
                    row.get::<_, String>(0)?,
                    row.get::<_, Option<String>>(1)?,
                    row.get::<_, Option<String>>(2)?,
                    row.get::<_, String>(3)?,
                    row.get::<_, bool>(4)?,
                    row.get::<_, String>(5)?,
                ))
            },
        )
        .optional()?
    else {
        return Ok(None);
    };
    let source_ids = {
        let mut statement = conn.prepare(
            "SELECT source_id FROM page_sources WHERE page_slug = ?1 ORDER BY source_id",
        )?;
        statement
            .query_map([slug], |row| row.get::<_, i64>(0))?
            .collect::<rusqlite::Result<Vec<_>>>()?
    };
    let provenance = {
        let mut statement = conn.prepare(
            "SELECT provenance FROM page_provenance
             WHERE page_slug = ?1
             ORDER BY CASE provenance
                 WHEN 'user-provided' THEN 0
                 WHEN 'agent-observed' THEN 1
                 WHEN 'hypothesis' THEN 2
                 ELSE 3 END",
        )?;
        statement
            .query_map([slug], |row| row.get::<_, String>(0))?
            .collect::<rusqlite::Result<Vec<_>>>()?
    };
    let links = {
        let mut statement =
            conn.prepare("SELECT to_slug FROM links WHERE from_slug = ?1 ORDER BY to_slug")?;
        statement
            .query_map([slug], |row| row.get::<_, String>(0))?
            .collect::<rusqlite::Result<Vec<_>>>()?
    };
    let content_fingerprint = page_content_fingerprint(
        &title,
        kind.as_deref(),
        summary.as_deref(),
        &body,
        structural_navigation,
        &source_ids,
        &provenance,
        &links,
    );
    let version_fingerprint = hash_content(&format!("{content_fingerprint}\0{updated_at}"));
    Ok(Some(PageMutationBase {
        content_fingerprint,
        version_fingerprint,
    }))
}