lwc 0.17.10

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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
use crate::{
    artifacts,
    config::{self, GraphSetting},
    error::{AppError, Result},
    graph::{GraphPage, related},
    tokenize::{joined_index_terms, tokenize_for_query},
};
use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd};
use rusqlite::{
    Connection, ErrorCode, MAIN_DB, OpenFlags, OptionalExtension, Transaction, TransactionBehavior,
    backup::{Backup, StepResult},
    ffi, params,
};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
#[cfg(unix)]
use std::os::unix::fs::OpenOptionsExt;
use std::{
    collections::{BTreeMap, BTreeSet},
    ffi::CStr,
    fs,
    io::{BufReader, BufWriter, Seek, Write},
    ops::Range,
    path::{Path, PathBuf},
    sync::atomic::{AtomicU64, Ordering},
    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};

const COMPOUND_WIKI_VERSION: i32 = 6;
const PAGE_PROVENANCE_VERSION: i32 = 7;
const SOURCE_PATH_REVISIONS_VERSION: i32 = 8;
const RETRIEVAL_WEIGHTING_VERSION: i32 = 9;
const CHANGESETS_VERSION: i32 = 10;
const EXTERNAL_GRAPH_VERSION: i32 = 12;
const TAGS_VERSION: i32 = 13;
const TEMPORAL_MEMORY_VERSION: i32 = 14;
const AGENT_STATE_VERSION: i32 = 15;
const TODO_FEATURES_VERSION: i32 = 16;
const STRUCTURED_SPAN_INDEX_VERSION: i32 = 17;
const USER_VERSION: i32 = STRUCTURED_SPAN_INDEX_VERSION;
const CHANGESET_FREEZE_KEY: &str = "changeset_frozen";
const SEARCH_INDEX_VERSION: i32 = 4;
const INGEST_WORKFLOW_VERSION: i32 = 5;
const TOKENIZER_ID: &str = "cjk-bigram@1/bounded-terms";
const SOURCE_GROUNDED: &str = "source-grounded";
const EXPLICIT_PROVENANCE: [&str; 3] = ["user-provided", "agent-observed", "hypothesis"];
const BUSY_TIMEOUT: Duration = Duration::from_secs(10);
const TIMESTAMP_SQL: &str = "STRFTIME('%Y-%m-%dT%H:%M:%fZ', 'now')";
const TITLE_WEIGHT: f64 = 32.0;
const PATH_WEIGHT: f64 = 16.0;
const GENERIC_WEIGHT: f64 = 8.0;
const GRAPH_MATCH_WEIGHT: f64 = 0.25;
const GRAPH_HUB_WEIGHT: f64 = 4.0;
const MANUAL_WEIGHT: f64 = 2.0;
const FEEDBACK_WEIGHT: f64 = 1.5;
static SOURCE_STAGE_COUNTER: AtomicU64 = AtomicU64::new(0);
type MigrationProgress<'a> = dyn FnMut(usize, usize, &str) -> Result<()> + 'a;
pub const DEFAULT_SCHEMA: &str = r#"# Wiki Schema

## Page types

- `entity`: named people, organizations, products, systems, and datasets.
- `concept`: ideas, techniques, patterns, and phenomena.
- `source`: one traceable summary for each ingested source.
- `query`: durable answers and open questions.
- `comparison`: side-by-side analysis.
- `synthesis`: cross-cutting conclusions.

## Content contract (MUST)

- Preserve the semantic integrity of `title`, `summary`, `kind`, provenance, and links.
- Keep one logical main title. The body may omit H1 or repeat the canonical title once.
- Every page declares provenance. Cite source IDs or use `user-provided`, `agent-observed`, or `hypothesis`.
- Use `[[stable-slug]]` for cross-references.

## Writing guidance (SHOULD)

- Address one primary reader question and put the current conclusion, definition, or status first.
- Use descriptive headings and separate current knowledge from history.
- Read an existing page before replacing it and preserve still-valid knowledge.
- Record contradictions instead of silently choosing one source.
- Before completing ingest, update affected shared pages or record why the source needs no derived-page update.

## Author freedom (MAY)

- Use any section names, order, and Markdown constructs. Short pages may omit H2 headings.
- Never add empty sections merely to satisfy a template. Existing free-form pages remain valid.

## Optional questions

- `entity` / `concept`: What is it, what are its boundaries, and what is true now?
- `source`: What does it claim, how strong is the evidence, and which shared pages does it support? Treat summaries as navigation, not a substitute for shared knowledge.
- `query`: What is the current answer, its evidence, and its unknowns?
- `comparison` / `synthesis`: What agrees, conflicts, differs, or follows from the evidence?
"#;
pub const DEFAULT_PURPOSE: &str = r#"# Project Purpose

## Goal

Build a persistent, traceable wiki from curated project sources.

## Key questions

1. What should this wiki help its users understand or decide?
2. Which sources and claims are authoritative?

## Scope

Keep project knowledge here; put reusable cross-project knowledge in the global wiki.
"#;
const LINT_ISSUES_SQL: &str = r#"
WITH issues(code, page, target, message) AS (
    SELECT
        'missing_schema', NULL, NULL, 'schema has not been set'
    WHERE NOT EXISTS (
        SELECT 1 FROM meta WHERE key = 'schema' AND TRIM(value) <> ''
    )
    UNION ALL
    SELECT
        'missing_summary', slug, NULL, 'page summary is missing'
    FROM pages
    WHERE summary IS NULL OR TRIM(summary) = ''
    UNION ALL
    SELECT
        'untitled_source', NULL, CAST(id AS TEXT), 'source title is missing'
    FROM sources
    WHERE title IS NULL OR TRIM(title) = ''
    UNION ALL
    SELECT
        'shallow_ingest',
        NULL,
        CAST(ij.source_id AS TEXT),
        'completed ingest has no cited non-source page and no explicit reason'
    FROM ingest_jobs ij
    WHERE ij.status = 'completed'
      AND (ij.no_derived_pages_reason IS NULL OR TRIM(ij.no_derived_pages_reason) = '')
      AND NOT EXISTS (
          SELECT 1
          FROM page_sources ps
          JOIN pages p ON p.slug = ps.page_slug
          WHERE ps.source_id = ij.source_id
            AND LOWER(COALESCE(p.kind, '')) <> 'source'
      )
    UNION ALL
    SELECT
        'uncited_page', p.slug, NULL,
        'page has neither cited sources nor explicit provenance'
    FROM pages p
    WHERE NOT EXISTS (
        SELECT 1 FROM page_sources ps WHERE ps.page_slug = p.slug
    )
      AND NOT EXISTS (
        SELECT 1 FROM page_provenance pp WHERE pp.page_slug = p.slug
    )
    UNION ALL
    SELECT
        'orphan_page', p.slug, NULL, 'page has no inbound wikilinks'
    FROM pages p
    LEFT JOIN links l ON l.to_slug = p.slug
    WHERE l.to_slug IS NULL
    UNION ALL
    SELECT
        'dangling_link', l.from_slug, l.to_slug, 'wikilink target does not exist'
    FROM links l
    LEFT JOIN pages p ON p.slug = l.to_slug
    WHERE p.slug IS NULL
    UNION ALL
    SELECT
        'search_index_duplicate',
        doc_type || ':' || identifier,
        NULL,
        'search index contains duplicate rows for one document'
    FROM search_fts
    GROUP BY doc_type, identifier
    HAVING COUNT(*) > 1
    UNION ALL
    SELECT
        'search_index_orphan',
        f.doc_type || ':' || f.identifier,
        NULL,
        'search index row has no matching document'
    FROM search_fts f
    LEFT JOIN pages p
      ON f.doc_type = 'page' AND p.slug = f.identifier
    LEFT JOIN sources s
      ON f.doc_type = 'source' AND s.id = CAST(f.identifier AS INTEGER)
    WHERE (f.doc_type = 'page' AND p.slug IS NULL)
       OR (f.doc_type = 'source' AND s.id IS NULL)
       OR f.doc_type NOT IN ('page', 'source')
    UNION ALL
    SELECT
        'search_index_missing',
        'page:' || identifier,
        NULL,
        'document is missing from the search index'
    FROM (
        SELECT slug AS identifier FROM pages
        EXCEPT
        SELECT identifier FROM search_fts WHERE doc_type = 'page'
    )
    UNION ALL
    SELECT
        'search_index_missing',
        'source:' || identifier,
        NULL,
        'document is missing from the search index'
    FROM (
        SELECT CAST(id AS TEXT) AS identifier FROM sources
        EXCEPT
        SELECT identifier FROM search_fts WHERE doc_type = 'source'
    )
    UNION ALL
    SELECT
        'retrieval_weight_orphan',
        CASE WHEN r.target_type = 'page' THEN r.target_identifier END,
        r.target_type || ':' || r.target_identifier,
        'retrieval weight target does not exist'
    FROM retrieval_weights r
    WHERE (r.target_type = 'page' AND NOT EXISTS (
              SELECT 1 FROM pages p WHERE p.slug = r.target_identifier
          ))
       OR (r.target_type = 'source' AND NOT EXISTS (
              SELECT 1 FROM sources s WHERE CAST(s.id AS TEXT) = r.target_identifier
          ))
    UNION ALL
    SELECT
        'retrieval_feedback_orphan',
        CASE WHEN r.target_type = 'page' THEN r.target_identifier END,
        r.target_type || ':' || r.target_identifier || ':' || SUBSTR(r.query_fingerprint, 1, 12),
        'retrieval feedback target does not exist'
    FROM retrieval_feedback r
    WHERE (r.target_type = 'page' AND NOT EXISTS (
              SELECT 1 FROM pages p WHERE p.slug = r.target_identifier
          ))
       OR (r.target_type = 'source' AND NOT EXISTS (
              SELECT 1 FROM sources s WHERE CAST(s.id AS TEXT) = r.target_identifier
          ))
)
"#;

#[derive(Debug)]
pub struct Store {
    scope: String,
    database: PathBuf,
    conn: Connection,
}

struct TemporaryDatabase(PathBuf);

impl Drop for TemporaryDatabase {
    fn drop(&mut self) {
        let _ = fs::remove_file(&self.0);
        let _ = fs::remove_file(self.0.with_extension("db-shm"));
        let _ = fs::remove_file(self.0.with_extension("db-wal"));
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct SourceAddInput {
    pub title: Option<String>,
    pub origin: String,
    pub tracked_path: Option<String>,
    pub content: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct PagePutInput {
    pub slug: String,
    pub title: String,
    pub kind: Option<String>,
    pub summary: Option<String>,
    pub body: String,
    pub source_ids: Vec<i64>,
    pub provenance: Vec<String>,
}

#[derive(Debug)]
struct PageMutationBase {
    content_fingerprint: String,
    version_fingerprint: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SchemaResponse {
    pub scope: String,
    pub database: String,
    pub schema: Option<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct PurposeResponse {
    pub scope: String,
    pub database: String,
    pub purpose: Option<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SourceRecord {
    pub id: i64,
    pub title: Option<String>,
    pub origin: String,
    pub content_hash: String,
    pub content: String,
    pub created_at: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SourceSummary {
    pub id: i64,
    pub title: Option<String>,
    pub origin: String,
    pub content_hash: String,
    pub bytes: i64,
    pub created_at: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SourceAddResponse {
    pub scope: String,
    pub database: String,
    pub source: SourceSummary,
    pub created: bool,
    pub graph: GraphMutationSummary,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct GraphMutationSummary {
    pub invalidated_semantic_relations: usize,
    pub engine: String,
    pub status: String,
    pub document_duration_ms: u64,
    pub queue_duration_ms: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub work: Option<Value>,
}

impl GraphMutationSummary {
    fn with_work(mut self, work: Option<Value>) -> Self {
        self.work = work;
        self
    }
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SourceRemoveResponse {
    pub scope: String,
    pub database: String,
    pub source_id: i64,
    pub removed: bool,
    pub removed_path_revisions: usize,
    pub untracked_paths: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub graph_work: Option<Value>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SourceListResponse {
    pub scope: String,
    pub database: String,
    pub sources: Vec<SourceSummary>,
    pub limit: usize,
    pub offset: usize,
    pub has_more: bool,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SourceStatusTarget {
    pub requested_source_id: i64,
    pub tracked_path: String,
    pub head_source_id: i64,
    pub head_revision: i64,
    pub head_content_hash: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SourceStatusTargets {
    pub targets: Vec<SourceStatusTarget>,
    pub untracked_source_ids: Vec<i64>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SourceShowResponse {
    pub scope: String,
    pub database: String,
    pub source: SourceRecord,
    pub window: SourceWindow,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SourceWindow {
    pub offset_chars: usize,
    pub returned_chars: usize,
    pub total_chars: usize,
    pub next_offset_chars: Option<usize>,
    pub has_more: bool,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct PageRecord {
    pub slug: String,
    pub title: String,
    pub kind: Option<String>,
    pub summary: Option<String>,
    pub body: String,
    pub source_ids: Vec<i64>,
    pub provenance: Vec<String>,
    pub links: Vec<String>,
    pub created_at: String,
    pub updated_at: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct PageSummary {
    pub slug: String,
    pub title: String,
    pub kind: Option<String>,
    pub summary: Option<String>,
    pub provenance: Vec<String>,
    pub updated_at: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct PagePutResponse {
    pub scope: String,
    pub database: String,
    pub page: PageWriteRecord,
    pub created: bool,
    pub graph: GraphMutationSummary,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct PageRemoveResponse {
    pub scope: String,
    pub database: String,
    pub slug: String,
    pub removed: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub graph_work: Option<Value>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct PageWriteRecord {
    pub slug: String,
    pub title: String,
    pub kind: Option<String>,
    pub summary: Option<String>,
    pub source_ids: Vec<i64>,
    pub provenance: Vec<String>,
    pub links: Vec<String>,
    pub created_at: String,
    pub updated_at: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct PageListResponse {
    pub scope: String,
    pub database: String,
    pub pages: Vec<PageSummary>,
    pub limit: usize,
    pub offset: usize,
    pub has_more: bool,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct PageShowResponse {
    pub scope: String,
    pub database: String,
    pub page: PageRecord,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct TagPageIdentity {
    pub scope: String,
    pub tag: String,
    pub page_slug: String,
    pub priority: i32,
    pub reason: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct TaggedPage {
    pub scope: String,
    pub tag: String,
    pub priority: i32,
    pub reason: String,
    pub ordinal: usize,
    pub page: PageRecord,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct TagAutoloadPolicy {
    pub scope: String,
    pub name: String,
    pub priority: i32,
    pub limit: usize,
    pub max_chars: usize,
    pub reason: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct PageLinksResponse {
    pub scope: String,
    pub database: String,
    pub page: String,
    pub outgoing: Vec<String>,
    pub backlinks: Vec<String>,
    pub missing: Vec<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SourceRefsResponse {
    pub scope: String,
    pub database: String,
    pub source: SourceSummary,
    pub pages: Vec<PageSummary>,
    pub limit: usize,
    pub offset: usize,
    pub has_more: bool,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct SearchResult {
    pub scope: String,
    #[serde(rename = "type")]
    pub result_type: String,
    pub identifier: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub document: Option<SearchDocumentRef>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub span: Option<SearchSpanRef>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fused_score: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub matches: Option<Vec<SearchMatch>>,
    pub title: Option<String>,
    pub kind: Option<String>,
    pub summary: Option<String>,
    pub provenance: Option<Vec<String>>,
    pub snippet: String,
    pub rank: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub explanation: Option<SearchExplanation>,
    #[serde(skip)]
    paired_source_ids: Vec<i64>,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct SearchMatch {
    #[serde(rename = "type")]
    pub result_type: String,
    pub identifier: String,
    pub snippet: String,
    pub rank: f64,
    pub fused_score: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub span: Option<SearchSpanRef>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SearchDocumentRef {
    #[serde(rename = "type")]
    pub document_type: String,
    pub identifier: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SearchSpanRef {
    pub parent_identifier: String,
    pub ordinal: usize,
    pub byte_start: usize,
    pub byte_end: usize,
    pub content_fingerprint: String,
    pub segmenter_version: u32,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct SearchExplanation {
    pub base_rank: f64,
    pub signals: SearchSignals,
    pub contributions: SearchContributions,
    pub graph_seeds: Vec<GraphSeedEvidence>,
    pub final_rank: f64,
}

#[derive(Debug, Clone, Default, Serialize, PartialEq)]
pub struct SearchSignals {
    pub title_match: f64,
    pub path_match: f64,
    pub generic_marker: f64,
    pub graph_match: f64,
    pub graph_hub_penalty: f64,
    pub manual_adjustment: f64,
    pub feedback_adjustment: f64,
}

#[derive(Debug, Clone, Default, Serialize, PartialEq)]
pub struct SearchContributions {
    pub title: f64,
    pub path: f64,
    pub generic: f64,
    pub graph: f64,
    pub manual: f64,
    pub feedback: f64,
}

impl SearchContributions {
    fn total(&self) -> f64 {
        self.title + self.path + self.generic + self.graph + self.manual + self.feedback
    }
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct GraphSeedEvidence {
    pub slug: String,
    pub raw_score: f64,
    pub contribution: f64,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct SearchResponse {
    pub results: Vec<SearchResult>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SpanRecord {
    pub identifier: String,
    #[serde(rename = "type")]
    pub span_type: String,
    pub document: SearchDocumentRef,
    pub parent_identifier: String,
    pub ordinal: usize,
    pub byte_start: usize,
    pub byte_end: usize,
    pub content_fingerprint: String,
    pub segmenter_version: u32,
    pub text: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SpanGetResponse {
    pub scope: String,
    pub database: String,
    pub span: SpanRecord,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct SpanExpandResponse {
    pub scope: String,
    pub database: String,
    pub span: SpanRecord,
    pub parent: Option<SpanRecord>,
    pub siblings: Vec<SpanRecord>,
    pub children: Vec<SpanRecord>,
    pub children_truncated: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchMode {
    Auto,
    Page,
    Source,
    All,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchGranularity {
    Document,
    Passage,
    Sentence,
    All,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchGrouping {
    None,
    Document,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SearchOptions {
    pub mode: SearchMode,
    pub granularity: SearchGranularity,
    pub grouping: SearchGrouping,
    pub kinds: Vec<String>,
    pub explain: bool,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct RetrievalAdjustment {
    pub target_type: String,
    pub target_identifier: String,
    pub provenance: String,
    pub weight: i32,
    pub reason: String,
    pub updated_at: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct RetrievalWeightResponse {
    pub scope: String,
    pub database: String,
    pub adjustment: RetrievalAdjustment,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct RetrievalWeightListResponse {
    pub scope: String,
    pub database: String,
    pub adjustments: Vec<RetrievalAdjustment>,
    pub effective: Option<RetrievalAdjustment>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct RetrievalClearResponse {
    pub scope: String,
    pub database: String,
    pub removed: bool,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct RetrievalFeedbackResponse {
    pub scope: String,
    pub database: String,
    pub query_fingerprint: String,
    pub target_type: String,
    pub target_identifier: String,
    pub provenance: String,
    pub signal: String,
    pub reason: String,
    pub updated_at: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct OperationRecord {
    pub id: i64,
    pub action: String,
    pub target: String,
    pub detail: Value,
    pub created_at: String,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct StoreIdentity {
    pub store_id: String,
    pub revision: String,
    pub operation_id: i64,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ChangesetDraftState {
    pub id: String,
    pub name: String,
    pub status: String,
    pub base_revision: String,
    pub base_operation_id: i64,
    pub begin_operation_id: i64,
    pub draft_revision: String,
    pub draft_operation_id: i64,
    pub staged_operation_count: usize,
    pub action_counts: BTreeMap<String, usize>,
    pub operations: Vec<OperationRecord>,
    pub created_at: String,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub(crate) struct DetachedChangesetIntent {
    pub(crate) version: u32,
    pub(crate) origin_changeset_id: String,
    pub(crate) name: String,
    pub(crate) actions: Vec<DetachedChangesetAction>,
    pub(crate) sources: Vec<DetachedSourceIntent>,
    pub(crate) pages: Vec<DetachedPageIntent>,
    pub(crate) tags: Vec<DetachedTagIntent>,
    pub(crate) meta: Vec<DetachedMetaIntent>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub(crate) enum DetachedChangesetAction {
    SourceAdd {
        content_hash: String,
    },
    Ingest {
        action: String,
        content_hash: String,
    },
    PagePut {
        slug: String,
    },
    PageRemove {
        slug: String,
    },
    Tag {
        action: String,
        name: String,
    },
    MetaSet {
        key: String,
    },
    Search,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub(crate) struct DetachedSourceIntent {
    pub(crate) content_hash: String,
    pub(crate) title: Option<String>,
    pub(crate) origin: Option<String>,
    pub(crate) structural_navigation: bool,
    pub(crate) base_fingerprint: String,
    pub(crate) content_required: bool,
    #[serde(default)]
    pub(crate) ingest: DetachedIngestIntent,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub(crate) struct DetachedIngestIntent {
    pub(crate) status: String,
    pub(crate) attempts: i64,
    pub(crate) analysis: Option<String>,
    pub(crate) no_derived_pages_reason: Option<String>,
}

impl Default for DetachedIngestIntent {
    fn default() -> Self {
        Self {
            status: "pending".into(),
            attempts: 0,
            analysis: None,
            no_derived_pages_reason: None,
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub(crate) struct DetachedPageIntent {
    pub(crate) slug: String,
    pub(crate) base_fingerprint: String,
    pub(crate) after: Option<DetachedPageAfterImage>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub(crate) struct DetachedPageAfterImage {
    pub(crate) title: String,
    pub(crate) kind: Option<String>,
    pub(crate) summary: Option<String>,
    pub(crate) body: String,
    pub(crate) source_hashes: Vec<String>,
    pub(crate) provenance: Vec<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub(crate) struct DetachedTagIntent {
    pub(crate) name: String,
    pub(crate) base_fingerprint: String,
    pub(crate) after: Option<DetachedTagAfterImage>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub(crate) struct DetachedTagAfterImage {
    pub(crate) autoload: bool,
    pub(crate) autoload_priority: i32,
    pub(crate) autoload_limit: i64,
    pub(crate) autoload_max_chars: i64,
    pub(crate) reason: String,
    pub(crate) memberships: Vec<DetachedTagMembership>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub(crate) struct DetachedTagMembership {
    pub(crate) page_slug: String,
    pub(crate) priority: i32,
    pub(crate) reason: String,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub(crate) struct DetachedMetaIntent {
    pub(crate) key: String,
    pub(crate) base_fingerprint: String,
    pub(crate) value: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ChangesetSyncReplayState {
    pub(crate) complete: bool,
    pub(crate) items: BTreeMap<String, String>,
}

pub(crate) type DetachedChangesetStoreExport =
    (DetachedChangesetIntent, Vec<(i64, String, u64)>);

#[derive(Debug, Clone)]
pub struct ChangesetPublishInput {
    pub id: String,
    pub name: String,
    pub store_id: String,
    pub base_revision: String,
    pub draft_revision: String,
    pub draft_operation_id: i64,
    pub staged_operation_count: usize,
    pub checkpoint: String,
    pub lint_issues: usize,
    pub lint_override_reason: Option<String>,
    pub graph_documents: Vec<(String, String)>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ChangesetCommitState {
    pub changeset_id: String,
    pub name: String,
    pub base_revision: String,
    pub post_revision: String,
    pub checkpoint: String,
    pub staged_operation_count: usize,
    pub lint_issues: usize,
    pub locked_publish_ms: u64,
    pub source_id_remap: Vec<SparseSourceIdRemap>,
    pub graph_documents: Vec<(String, String)>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChangesetHistoryState {
    pub id: String,
    pub name: String,
    pub status: String,
    pub base_revision: String,
    pub base_operation_id: i64,
    pub begin_operation_id: i64,
    pub pre_commit_checkpoint: Option<String>,
    pub post_revision: Option<String>,
    pub created_at: String,
    pub committed_at: Option<String>,
    pub rolled_back_at: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ChangesetRollbackInput {
    pub history: ChangesetHistoryState,
    pub store_id: String,
    pub pre_rollback_checkpoint: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ChangesetRollbackState {
    pub changeset_id: String,
    pub name: String,
    pub rollback_revision: String,
    pub checkpoint: String,
    pub locked_rollback_ms: u64,
    pub graph_documents: Vec<(String, String)>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct SparsePageSnapshot {
    slug: String,
    title: String,
    kind: Option<String>,
    summary: Option<String>,
    body: String,
    structural_navigation: bool,
    source_ids: Vec<i64>,
    provenance: Vec<String>,
    links: Vec<String>,
    created_at: String,
    updated_at: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct SparsePageInverse {
    slug: String,
    before: Option<SparsePageSnapshot>,
    after_fingerprint: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    after: Option<SparsePageSnapshot>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    inbound_links: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct SparseMetaInverse {
    key: String,
    before: String,
    after_fingerprint: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct SparseIngestSnapshot {
    status: String,
    attempts: i64,
    analysis: Option<String>,
    last_error: Option<String>,
    no_derived_pages_reason: Option<String>,
    updated_at: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct SparseSourcePathSnapshot {
    tracked_path: String,
    revision: i64,
    observed_at: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct SparseTrackedPathRevision {
    revision: i64,
    source_id: i64,
    observed_at: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct SparseTrackedPathInverse {
    tracked_path: String,
    before: Vec<SparseTrackedPathRevision>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    after: Vec<SparseTrackedPathRevision>,
    after_fingerprint: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SparseSourceIdRemap {
    pub draft_id: i64,
    pub live_id: i64,
    pub created: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct SparseSourceSnapshot {
    id: i64,
    content_hash: String,
    title: Option<String>,
    origin: String,
    content: String,
    structural_navigation: bool,
    created_at: String,
    ingest: SparseIngestSnapshot,
    paths: Vec<SparseSourcePathSnapshot>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct SparseSourceInverse {
    source_id: i64,
    before: Option<SparseSourceSnapshot>,
    after_fingerprint: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct TagPolicySnapshot {
    name: String,
    autoload: bool,
    autoload_priority: i32,
    autoload_limit: i64,
    autoload_max_chars: i64,
    reason: String,
    updated_at: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct PageTagSnapshot {
    tag_name: String,
    page_slug: String,
    priority: i32,
    reason: String,
    created_at: String,
    updated_at: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct SparseTagSnapshot {
    policy: Option<TagPolicySnapshot>,
    memberships: Vec<PageTagSnapshot>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct SparseTagInverse {
    name: String,
    before: SparseTagSnapshot,
    after_fingerprint: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct SparseInversePayload {
    version: u32,
    changeset_id: String,
    store_id: String,
    pages: Vec<SparsePageInverse>,
    #[serde(default)]
    meta: Vec<SparseMetaInverse>,
    #[serde(default)]
    sources: Vec<SparseSourceInverse>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    source_paths: Vec<SparseTrackedPathInverse>,
    #[serde(default)]
    tags: Vec<SparseTagInverse>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct SparseInverseEnvelope {
    payload: SparseInversePayload,
    checksum: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ContextStore {
    pub scope: String,
    pub database: String,
    pub schema: Option<String>,
    pub purpose: Option<String>,
    pub pages: Vec<PageSummary>,
    pub recent_operations: Vec<OperationRecord>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct IngestJobSummary {
    pub source_id: i64,
    pub status: String,
    pub attempts: i64,
    pub last_error: Option<String>,
    pub no_derived_pages_reason: Option<String>,
    pub updated_at: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct IngestWork {
    pub source: SourceRecord,
    pub source_window: SourceWindow,
    pub status: String,
    pub attempts: i64,
    pub analysis: Option<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct IngestPacket {
    pub scope: String,
    pub database: String,
    pub job: Option<IngestWork>,
    pub schema: Option<String>,
    pub purpose: Option<String>,
    pub pages: Vec<PageSummary>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct IngestListResponse {
    pub scope: String,
    pub database: String,
    pub jobs: Vec<IngestJobSummary>,
    pub limit: usize,
    pub offset: usize,
    pub has_more: bool,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct IngestMutationResponse {
    pub scope: String,
    pub database: String,
    pub job: IngestJobSummary,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub integration: Option<IngestIntegration>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct IngestIntegration {
    pub source_summary_pages: usize,
    pub derived_pages: usize,
    pub no_derived_pages_reason: Option<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct RelatedPageResponse {
    pub slug: String,
    pub title: String,
    pub kind: Option<String>,
    pub direct_links: usize,
    pub shared_sources: usize,
    pub adamic_adar: f64,
    pub type_affinity: f64,
    pub score: f64,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct GraphRelatedResponse {
    pub scope: String,
    pub database: String,
    pub seed: String,
    pub related: Vec<RelatedPageResponse>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ReindexResponse {
    pub scope: String,
    pub database: String,
    pub sources: usize,
    pub pages: usize,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct MaterializeResponse {
    pub scope: String,
    pub database: String,
    pub files: Vec<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct CompactResponse {
    pub scope: String,
    pub database: String,
    pub busy: bool,
    pub log_frames: i64,
    pub checkpointed_frames: i64,
    pub before_bytes: u64,
    pub after_bytes: u64,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct CheckpointRecord {
    pub name: String,
    pub path: String,
    pub bytes: u64,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct CheckpointResponse {
    pub scope: String,
    pub database: String,
    pub checkpoint: String,
    pub path: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub safety_checkpoint: Option<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct CheckpointListResponse {
    pub scope: String,
    pub database: String,
    pub checkpoints: Vec<CheckpointRecord>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "lowercase")]
pub enum LintSeverity {
    Error,
    Warning,
    Info,
}

impl LintSeverity {
    fn is_blocking(&self) -> bool {
        matches!(self, Self::Error)
    }
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct LintIssue {
    pub severity: LintSeverity,
    pub code: String,
    pub page: Option<String>,
    pub target: Option<String>,
    pub message: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct LintResponse {
    pub scope: String,
    pub database: String,
    pub issues: Vec<LintIssue>,
    pub counts: BTreeMap<String, usize>,
    pub total: usize,
    pub blocking_total: usize,
    pub advisory_total: usize,
    pub limit: usize,
    pub offset: usize,
    pub has_more: bool,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct LogResponse {
    pub scope: String,
    pub database: String,
    pub operations: Vec<OperationRecord>,
}