@avantf/dsh-mem 0.6.4 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/README.md +19 -10
  2. package/lib/client.js +1665 -3466
  3. package/lib/dsh-build.json +6 -5
  4. package/lib/engine/graph-recall/in-memory.d.ts +93 -0
  5. package/lib/engine/graph-recall/index.d.ts +22 -0
  6. package/lib/engine/graph-recall/traverse.d.ts +119 -0
  7. package/lib/engine/graph-recall/types.d.ts +188 -0
  8. package/lib/engine/mem/config/config_files.d.ts +1 -1
  9. package/lib/engine/mem/config/loader.d.ts +2 -3
  10. package/lib/engine/mem/config/paths.d.ts +2 -13
  11. package/lib/engine/mem/db/dao/contradictions.d.ts +4 -22
  12. package/lib/engine/mem/db/dao/entities.d.ts +80 -7
  13. package/lib/engine/mem/db/dao/fact_sources.d.ts +3 -13
  14. package/lib/engine/mem/db/dao/facts.d.ts +311 -56
  15. package/lib/engine/mem/db/dao/triples.d.ts +1 -26
  16. package/lib/engine/mem/db/graph.d.ts +249 -0
  17. package/lib/engine/mem/db/journal_notes.d.ts +35 -0
  18. package/lib/engine/mem/db/migrations.d.ts +9 -0
  19. package/lib/engine/mem/db/port.d.ts +22 -3
  20. package/lib/engine/mem/db/schema.d.ts +119 -0
  21. package/lib/engine/mem/db/tokenizer.d.ts +9 -0
  22. package/lib/engine/mem/dbdrivers/fake/driver.d.ts +75 -0
  23. package/lib/engine/mem/dbdrivers/fake/index.d.ts +7 -0
  24. package/lib/engine/mem/dbdrivers/fake/surface.d.ts +34 -0
  25. package/lib/engine/mem/dbdrivers/fake/transport.d.ts +61 -0
  26. package/lib/engine/mem/dbdrivers/sqlite/connection.d.ts +14 -0
  27. package/lib/engine/mem/dbdrivers/sqlite/contradictions.d.ts +35 -0
  28. package/lib/engine/mem/dbdrivers/sqlite/dao/index.d.ts +16 -0
  29. package/lib/engine/mem/dbdrivers/sqlite/driver.d.ts +54 -0
  30. package/lib/engine/mem/dbdrivers/sqlite/facts.d.ts +117 -0
  31. package/lib/engine/mem/dbdrivers/sqlite/index.d.ts +6 -0
  32. package/lib/engine/mem/dbdrivers/sqlite/migrations.d.ts +15 -0
  33. package/lib/engine/mem/dbdrivers/sqlite/relations.d.ts +62 -0
  34. package/lib/engine/mem/dbdrivers/sqlite/search.d.ts +56 -0
  35. package/lib/engine/mem/dbdrivers/sqlite/sources.d.ts +37 -0
  36. package/lib/engine/mem/defaults.d.ts +3 -4
  37. package/lib/engine/mem/dispatch.d.ts +7 -32
  38. package/lib/engine/mem/entities/extract.d.ts +44 -47
  39. package/lib/engine/mem/eval/derived_runner.d.ts +6 -1
  40. package/lib/engine/mem/eval/relation_gold.d.ts +251 -0
  41. package/lib/engine/mem/eval/write_metrics.d.ts +1 -1
  42. package/lib/engine/mem/facts_io.d.ts +328 -0
  43. package/lib/engine/mem/index.d.ts +10 -2
  44. package/lib/engine/mem/interfaces/capabilities.d.ts +30 -0
  45. package/lib/engine/mem/interfaces/common.d.ts +11 -0
  46. package/lib/engine/mem/interfaces/errors.d.ts +65 -0
  47. package/lib/engine/mem/interfaces/index.d.ts +18 -0
  48. package/lib/engine/mem/interfaces/migrations.d.ts +21 -0
  49. package/lib/engine/mem/interfaces/repositories.d.ts +471 -0
  50. package/lib/engine/mem/interfaces/search_index.d.ts +113 -0
  51. package/lib/engine/mem/interfaces/storage_backend.d.ts +84 -0
  52. package/lib/engine/mem/interfaces/storage_driver.d.ts +43 -0
  53. package/lib/engine/mem/interfaces/version.d.ts +34 -0
  54. package/lib/engine/mem/lifecycle/contradiction.d.ts +2 -2
  55. package/lib/engine/mem/maintenance_paths.d.ts +41 -0
  56. package/lib/engine/mem/modelBootstrap.d.ts +1 -1
  57. package/lib/engine/mem/relations/confidence.d.ts +39 -0
  58. package/lib/engine/mem/relations/extract.d.ts +128 -0
  59. package/lib/engine/mem/relations/index.d.ts +32 -0
  60. package/lib/engine/mem/relations/name-quality.d.ts +78 -0
  61. package/lib/engine/mem/relations/normalize.d.ts +275 -0
  62. package/lib/engine/mem/relations/patterns.d.ts +67 -0
  63. package/lib/engine/mem/relations/statement.d.ts +88 -0
  64. package/lib/engine/mem/relations/types.d.ts +79 -0
  65. package/lib/engine/mem/runtime.d.ts +117 -90
  66. package/lib/engine/mem/storage/create.d.ts +42 -0
  67. package/lib/engine/mem/storage/errors.d.ts +65 -0
  68. package/lib/engine/mem/storage/index.d.ts +15 -0
  69. package/lib/engine/mem/storage/ports.d.ts +472 -0
  70. package/lib/engine/mem/storage/registry.d.ts +37 -0
  71. package/lib/engine/mem/storage/sqlite.d.ts +28 -0
  72. package/lib/engine/mem/store/common.d.ts +1 -1
  73. package/lib/engine/mem/store/entity_leg.d.ts +59 -2
  74. package/lib/engine/mem/store/entity_sweep.d.ts +4 -3
  75. package/lib/engine/mem/store/explicit_relations.d.ts +52 -0
  76. package/lib/engine/mem/store/facts_restore.d.ts +124 -0
  77. package/lib/engine/mem/store/floors.d.ts +4 -1
  78. package/lib/engine/mem/store/graph_reader.d.ts +146 -0
  79. package/lib/engine/mem/store/hybrid.d.ts +50 -13
  80. package/lib/engine/mem/store/kinds.d.ts +47 -0
  81. package/lib/engine/mem/store/legs.d.ts +24 -29
  82. package/lib/engine/mem/store/lexical.d.ts +36 -45
  83. package/lib/engine/mem/store/memory.d.ts +486 -82
  84. package/lib/engine/mem/store/recall_policy.d.ts +49 -0
  85. package/lib/engine/mem/store/relation_backfill.d.ts +210 -0
  86. package/lib/engine/mem/store/relation_planner.d.ts +191 -0
  87. package/lib/engine/mem/store/secret_guard.d.ts +0 -1
  88. package/lib/engine/mem/store/self_query.d.ts +4 -2
  89. package/lib/engine/mem/store/time_filter.d.ts +121 -0
  90. package/lib/engine/mem/store/titles.d.ts +52 -0
  91. package/lib/engine/mem/store/vector_repair.d.ts +44 -54
  92. package/lib/engine/mem-contract/config.d.ts +44 -38
  93. package/lib/engine/mem-contract/text.d.ts +1 -1
  94. package/lib/engine/mem-contract/tools.d.ts +184 -117
  95. package/lib/engine/mem-contract/types.d.ts +364 -288
  96. package/lib/index.js +13898 -11500
  97. package/lib/interface-version.json +1 -1
  98. package/lib/types/client/index.d.ts +2 -2
  99. package/lib/types/client/index.d.ts.map +1 -1
  100. package/lib/types/client/paging.d.ts +3 -12
  101. package/lib/types/client/paging.d.ts.map +1 -1
  102. package/lib/types/client/summarize.d.ts.map +1 -1
  103. package/lib/types/command.d.ts +61 -0
  104. package/lib/types/command.d.ts.map +1 -0
  105. package/lib/types/envinit-bootstrap.d.ts +1 -1
  106. package/lib/types/envinit.d.ts +2 -6
  107. package/lib/types/envinit.d.ts.map +1 -1
  108. package/lib/types/hints.d.ts +28 -24
  109. package/lib/types/hints.d.ts.map +1 -1
  110. package/lib/types/index.d.ts +21 -40
  111. package/lib/types/index.d.ts.map +1 -1
  112. package/lib/types/inject.d.ts +3 -2
  113. package/lib/types/inject.d.ts.map +1 -1
  114. package/lib/types/interface_gate.d.ts +9 -9
  115. package/lib/types/prompt.d.ts +17 -41
  116. package/lib/types/prompt.d.ts.map +1 -1
  117. package/lib/types/provision.d.ts +3 -0
  118. package/lib/types/provision.d.ts.map +1 -1
  119. package/lib/types/remote.d.ts +23 -9
  120. package/lib/types/remote.d.ts.map +1 -1
  121. package/lib/types/render.d.ts +3 -3
  122. package/lib/types/tool_schema.d.ts +4 -0
  123. package/lib/types/tool_schema.d.ts.map +1 -1
  124. package/lib/types/vectorMigration.d.ts +5 -7
  125. package/lib/types/vectorMigration.d.ts.map +1 -1
  126. package/package.json +16 -15
  127. package/lib/engine/mem/config/domains.d.ts +0 -20
  128. package/lib/engine/mem/db/dao/chunks.d.ts +0 -231
  129. package/lib/engine/mem/db/dao/documents.d.ts +0 -51
  130. package/lib/engine/mem/db/hash.d.ts +0 -2
  131. package/lib/engine/mem/db/knowledge.d.ts +0 -17
  132. package/lib/engine/mem/eval/index.d.ts +0 -4
  133. package/lib/engine/mem/git.d.ts +0 -73
  134. package/lib/engine/mem/router.d.ts +0 -51
  135. package/lib/engine/mem/store/doc_files.d.ts +0 -125
  136. package/lib/engine/mem/store/document_text.d.ts +0 -47
  137. package/lib/engine/mem/store/ingest_guard.d.ts +0 -25
  138. package/lib/engine/mem/store/knowledge.d.ts +0 -598
  139. package/lib/engine/mem/store/source_picker.d.ts +0 -43
  140. package/lib/engine/mem-retrieval/adapters/local_reranker.d.ts +0 -52
  141. package/lib/engine/mem-retrieval/adapters/none_reranker.d.ts +0 -10
  142. package/lib/engine/mem-retrieval/rerank.d.ts +0 -13
  143. package/lib/types/client/domains.d.ts +0 -53
  144. package/lib/types/client/domains.d.ts.map +0 -1
  145. package/lib/types/client/ingest.d.ts +0 -24
  146. package/lib/types/client/ingest.d.ts.map +0 -1
  147. package/lib/types/open.d.ts +0 -77
  148. package/lib/types/open.d.ts.map +0 -1
  149. package/lib/types/reconcile.d.ts +0 -64
  150. package/lib/types/reconcile.d.ts.map +0 -1
@@ -1,11 +1,23 @@
1
- import type { AdminRequest, KbAddRequest, KbConflictReport, KbRequest, QueryRequest, RecallRequest, RecallResult, RememberRequest, VectorsFixReport } from '../mem-contract/index.js';
1
+ import type { AdminRequest, RecallRequest, RememberRequest, VectorsFixReport } from '../mem-contract/index.js';
2
2
  import { type AvantfLogger, type StatsSummary } from '../mem-contract/index.js';
3
3
  import { type SemanticBackend } from '../mem-retrieval/index.js';
4
4
  import { type LoadedConfig } from './config/loader.js';
5
5
  import { type Db } from './db/conn.js';
6
- import { type RelevanceHit } from './store/lexical.js';
7
6
  import { MemoryStore } from './store/memory.js';
8
- import { KnowledgeStore } from './store/knowledge.js';
7
+ import { type FactsExportCodec, type ImportReport, type MergeReport } from './facts_io.js';
8
+ import type { RestoreReport } from './store/facts_restore.js';
9
+ /**
10
+ * The write rules a MODEL-FACING boundary adds on top of the contract (JOURNAL_TITLE_SEARCH §2.3).
11
+ *
12
+ * There is exactly one today: "a journal add must carry a title" cannot be expressed in zod's
13
+ * discriminated union without wrapping a branch in `superRefine`, which would break the JSON-Schema /
14
+ * DSH-parameter derivation (both read the branch's `.shape`). So it is enforced at the tool handler
15
+ * (`runToolSpec` → `dispatchToolKey`) and passed down as a store rule. It is deliberately NOT a
16
+ * store-wide invariant: old journals, old exports, the CLI and scripts all write untitled facts.
17
+ */
18
+ export interface RememberWriteRules {
19
+ requireJournalTitle?: boolean;
20
+ }
9
21
  export interface RuntimeOptions {
10
22
  dataHome?: string;
11
23
  memoryDbPath?: string;
@@ -35,6 +47,22 @@ export interface RuntimeOptions {
35
47
  readonly tools?: string;
36
48
  readonly models?: string;
37
49
  };
50
+ /**
51
+ * Skip the adapters' eager constructor warm even when the caller passes no {@link managedRoots}:
52
+ * the caller owns the warm schedule and will warm explicitly.
53
+ *
54
+ * There are exactly TWO ways to get a deferred warm, and both are requests of the same thing:
55
+ *
56
+ * - {@link managedRoots} — the DSH plugin's path. The framework owns the model root and may still
57
+ * be installing into it, so the backends must not warm themselves (that would race the install
58
+ * and fetch a second copy).
59
+ * - `deferWarm: true` — the CLI's path. The flag exists because the CLI is one-shot and its
60
+ * READ-ONLY commands (`stats`, `list`, `export`, …) must not load or download the model at all;
61
+ * it warms explicitly, and only from `needsModels` (the command table in `cli/src/index.ts`).
62
+ *
63
+ * A first `encode()` still warms lazily, so deferring never costs the commands that do encode.
64
+ */
65
+ deferWarm?: boolean;
38
66
  }
39
67
  /**
40
68
  * The awaited result of a store method — what a dispatch overload promises.
@@ -66,15 +94,34 @@ export type DispatchError = {
66
94
  */
67
95
  export type AdminStatsResult = StatsSummary;
68
96
  /**
69
- * The payload of the four hits-returning recall actions, which one overload groups because a
70
- * caller may hold them as a union (`action: 'chain' | 'reason'`).
71
- *
72
- * Grouping them is a CLAIM — that `probe`, `chain` and `reason` all answer like `search` — so it is
73
- * proven rather than assumed: a type-level check asserts that each store method's result equals this
74
- * one, and a divergence fails there instead of the grouped overload quietly lying about it. (Only
75
- * `probe` holds by construction, because it calls `memory.search`.)
97
+ * `admin export`: the file THIS call wrote and its container. Deliberately carries NO document body
98
+ * — the model gets a receipt, not the library (the file is the artifact; whoever needs the facts
99
+ * reads it with the CLI or imports it back).
76
100
  */
77
- export type RecallHitsResult = StoreResult<MemoryStore, 'search'>;
101
+ export interface AdminExportReport {
102
+ file: string;
103
+ codec: FactsExportCodec;
104
+ bytes: number;
105
+ plain_bytes: number;
106
+ counts: {
107
+ active: number;
108
+ archived: number;
109
+ };
110
+ /** True when an existing file was replaced (the atomic write is the same either way). */
111
+ overwritten: boolean;
112
+ }
113
+ /** `admin restore_snapshot`: the engine's restore report plus the container it came from. */
114
+ export type AdminRestoreSnapshotReport = RestoreReport & {
115
+ file: string;
116
+ codec: FactsExportCodec;
117
+ };
118
+ /**
119
+ * The payload of `mem_recall.search`: the two-stage result (an exact page or a fuzzy shortlist).
120
+ * Named from the store method that produces it, so a change there cannot leave this alias lying.
121
+ */
122
+ export type RecallHitsResult = StoreResult<MemoryStore, 'searchMemory'>;
123
+ /** The payload of `mem_recall.titles`: an exact page or a fuzzy shortlist of title candidates. */
124
+ export type RecallTitlesResult = StoreResult<MemoryStore, 'titles'>;
78
125
  /**
79
126
  * `remember` dispatches to five store operations whose RESULTS differ: `add`/`update` return the
80
127
  * written fact, `remove` a flag, the feedback actions the new counter (or `null`). One plain
@@ -85,148 +132,128 @@ export type RecallHitsResult = StoreResult<MemoryStore, 'search'>;
85
132
  export type RememberDispatch = {
86
133
  (req: Extract<RememberRequest, {
87
134
  action: 'add';
88
- }>): Promise<StoreResult<MemoryStore, 'add'>>;
135
+ }>, rules?: RememberWriteRules): Promise<StoreResult<MemoryStore, 'add'>>;
89
136
  (req: Extract<RememberRequest, {
90
137
  action: 'update';
91
- }>): Promise<StoreResult<MemoryStore, 'update'>>;
138
+ }>, rules?: RememberWriteRules): Promise<StoreResult<MemoryStore, 'update'>>;
92
139
  (req: Extract<RememberRequest, {
93
140
  action: 'remove';
94
141
  }>): Promise<StoreResult<MemoryStore, 'remove'>>;
95
142
  (req: Extract<RememberRequest, {
96
143
  action: 'helpful' | 'unhelpful';
97
144
  }>): Promise<StoreResult<MemoryStore, 'helpful'>>;
98
- (req: RememberRequest): Promise<unknown>;
145
+ (req: RememberRequest, rules?: RememberWriteRules): Promise<unknown>;
99
146
  };
100
147
  /**
101
- * Same per-action idea as {@link RememberDispatch}. `ask` may also report a caller error, and
102
- * `related` answers with ENTITY COUNTS rather than hits (`memory.related`) — a distinction that
103
- * only showed up once this type existed, because the callers had been casting to whatever they
104
- * assumed.
148
+ * Same per-action idea as {@link RememberDispatch}, over the TWO public recall actions
149
+ * (docs/RECALL_INTERFACE.md §1): a content search answers hits (exact page or fuzzy shortlist), a
150
+ * title search answers candidates. Two different payload shapes, so two overloads.
105
151
  */
106
152
  export type RecallDispatch = {
107
153
  (req: Extract<RecallRequest, {
108
- action: 'search' | 'probe' | 'chain' | 'reason';
154
+ action: 'search';
109
155
  }>): Promise<RecallHitsResult>;
110
156
  (req: Extract<RecallRequest, {
111
- action: 'ask';
112
- }>): Promise<StoreResult<MemoryStore, 'ask'> | DispatchError>;
113
- (req: Extract<RecallRequest, {
114
- action: 'related';
115
- }>): Promise<StoreResult<MemoryStore, 'related'>>;
116
- (req: Extract<RecallRequest, {
117
- action: 'contradict';
118
- }>): Promise<StoreResult<MemoryStore, 'listContradictions'>>;
157
+ action: 'titles';
158
+ }>): Promise<RecallTitlesResult>;
119
159
  (req: RecallRequest): Promise<unknown>;
120
160
  };
121
161
  /**
122
- * `admin` is the widest dispatcher: counts, a fact page, a detail, flags, and five diagnostic
123
- * reports. `ReturnType<…>` keeps each member tied to the store method that actually produces it,
124
- * so a change there propagates instead of leaving callers with a cast they invented.
162
+ * `admin` is the widest dispatcher: counts, a fact page, a detail, flags, five diagnostic
163
+ * reports, and the four data-root maintenance actions (export / import / merge / restore_snapshot).
164
+ * `ReturnType<…>` keeps each member tied to the store method that actually produces it, so a change
165
+ * there propagates instead of leaving callers with a cast they invented.
125
166
  */
126
167
  export type AdminDispatch = {
127
168
  (req: Extract<AdminRequest, {
128
169
  action: 'stats';
129
- }>): AdminStatsResult;
170
+ }>): Promise<AdminStatsResult>;
130
171
  (req: Extract<AdminRequest, {
131
172
  action: 'list';
132
- }>): StoreResult<MemoryStore, 'list'>;
173
+ }>): Promise<StoreResult<MemoryStore, 'list'>>;
133
174
  (req: Extract<AdminRequest, {
134
175
  action: 'detail';
135
- }>): StoreResult<MemoryStore, 'get'> | DispatchError;
176
+ }>): Promise<StoreResult<MemoryStore, 'get'> | DispatchError>;
136
177
  (req: Extract<AdminRequest, {
137
178
  action: 'archive';
138
- }>): StoreResult<MemoryStore, 'archive'>;
179
+ }>): Promise<StoreResult<MemoryStore, 'archive'>>;
139
180
  (req: Extract<AdminRequest, {
140
181
  action: 'restore';
141
- }>): StoreResult<MemoryStore, 'restore'>;
182
+ }>): Promise<StoreResult<MemoryStore, 'restore'>>;
142
183
  (req: Extract<AdminRequest, {
143
184
  action: 'pin';
144
- }>): StoreResult<MemoryStore, 'pin'>;
185
+ }>): Promise<StoreResult<MemoryStore, 'pin'>>;
145
186
  (req: Extract<AdminRequest, {
146
187
  action: 'unpin';
147
- }>): StoreResult<MemoryStore, 'unpin'>;
188
+ }>): Promise<StoreResult<MemoryStore, 'unpin'>>;
148
189
  (req: Extract<AdminRequest, {
149
190
  action: 'trust_diagnose';
150
- }>): StoreResult<MemoryStore, 'trustDiagnose'>;
191
+ }>): Promise<StoreResult<MemoryStore, 'trustDiagnose'>>;
151
192
  (req: Extract<AdminRequest, {
152
193
  action: 'vectors_diagnose';
153
- }>): StoreResult<MemoryStore, 'vectorsDiagnose'>;
194
+ }>): Promise<StoreResult<MemoryStore, 'vectorsDiagnose'>>;
154
195
  (req: Extract<AdminRequest, {
155
196
  action: 'vectors_fix';
156
197
  }>): Promise<VectorsFixReport>;
157
198
  (req: Extract<AdminRequest, {
158
199
  action: 'contradict_check';
159
- }>): StoreResult<MemoryStore, 'checkContradictions'>;
200
+ }>): Promise<StoreResult<MemoryStore, 'checkContradictions'>>;
201
+ (req: Extract<AdminRequest, {
202
+ action: 'contradictions';
203
+ }>): Promise<StoreResult<MemoryStore, 'listContradictions'>>;
160
204
  (req: Extract<AdminRequest, {
161
205
  action: 'contradict_resolve';
162
- }>): StoreResult<MemoryStore, 'resolveContradiction'>;
206
+ }>): Promise<StoreResult<MemoryStore, 'resolveContradiction'>>;
163
207
  (req: Extract<AdminRequest, {
164
208
  action: 'maintenance';
165
- }>): StoreResult<MemoryStore, 'maintenance'>;
166
- (req: AdminRequest): unknown;
167
- };
168
- /**
169
- * `kb_manage`: ingest/import report a batch, list/detail the corpus, remove a flag, reindex a
170
- * plan. Members are pinned to the store methods that produce them (`ReturnType<…>`) so the
171
- * shapes cannot drift into "whatever the caller assumed".
172
- *
173
- * `ingest`/`import` may instead answer with a {@link KbConflictReport}: this face is the UI/CLI
174
- * REPLACE mode, where an unconfirmed collision writes nothing and comes back as a structured
175
- * list. The add-only face (`kbAdd`) never returns one — it refuses.
176
- */
177
- export type KbDispatch = {
178
- (req: Extract<KbRequest, {
179
- action: 'ingest';
180
- }>): Promise<StoreResult<KnowledgeStore, 'ingest'> | KbConflictReport | DispatchError>;
181
- (req: Extract<KbRequest, {
209
+ }>): Promise<StoreResult<MemoryStore, 'maintenance'>>;
210
+ (req: Extract<AdminRequest, {
211
+ action: 'export';
212
+ }>): Promise<AdminExportReport>;
213
+ /** Content-detected: a SQLite source answers with the db report, an export document with the JSON one. */
214
+ (req: Extract<AdminRequest, {
182
215
  action: 'import';
183
- }>): Promise<StoreResult<KnowledgeStore, 'importPaths'> | KbConflictReport>;
184
- (req: Extract<KbRequest, {
185
- action: 'list';
186
- }>): Promise<StoreResult<KnowledgeStore, 'list'>>;
187
- (req: Extract<KbRequest, {
188
- action: 'detail';
189
- }>): Promise<StoreResult<KnowledgeStore, 'detail'>>;
190
- (req: Extract<KbRequest, {
191
- action: 'remove';
192
- }>): Promise<StoreResult<KnowledgeStore, 'remove'>>;
193
- (req: Extract<KbRequest, {
194
- action: 'reindex';
195
- }>): Promise<StoreResult<KnowledgeStore, 'reindex'>>;
196
- (req: KbRequest): Promise<unknown>;
216
+ }>): Promise<ImportReport | MergeReport>;
217
+ (req: Extract<AdminRequest, {
218
+ action: 'merge';
219
+ }>): Promise<MergeReport>;
220
+ (req: Extract<AdminRequest, {
221
+ action: 'restore_snapshot';
222
+ }>): Promise<AdminRestoreSnapshotReport>;
223
+ (req: AdminRequest): unknown;
197
224
  };
198
225
  export interface AvantfRuntime {
199
226
  config: LoadedConfig;
200
227
  db: Db;
201
228
  memory: MemoryStore;
202
- knowledge: KnowledgeStore;
203
229
  /** Logger every startup/lifecycle line goes through. */
204
230
  logger: AvantfLogger;
205
231
  remember: RememberDispatch;
206
232
  recall: RecallDispatch;
207
233
  admin: AdminDispatch;
208
- kb: KbDispatch;
209
- /**
210
- * The ADD-ONLY face of the same shared ingestion entry (`kb_add`): same plan/classify/dispatch,
211
- * but a collision is refused instead of replaced. Kept as its own runtime method — not a field on
212
- * the request — so the model-facing `KB_ADD_TOOL.input` cannot even express "replace".
213
- */
214
- kbAdd(req: KbAddRequest): Promise<unknown>;
215
- query(req: QueryRequest): Promise<RecallResult>;
216
234
  /**
217
- * Plugin-internal, NOT a tool and not model-facing: does either store already hold something
218
- * relevant to `text`? A boolean (`store/lexical.ts`) because the one tool the hint names
219
- * (`kb_query`) retrieves from both stores, so "which one matched" changes no downstream decision.
220
- * Synchronous on purpose — the caller renders it into a prompt provider that cannot await
221
- * (DESIGN §12).
235
+ * The conditional hint's decision (DESIGN §12): would a memory retrieval return at least one hit
236
+ * for `text`? Implemented as `MemoryStore.search({ query: text, limit: 1, probe: true })` and
237
+ * answering `hits.length > 0` — there is no second threshold, no lexical short-circuit and no
238
+ * self-query special case here: retrieval already augments the query with its own rewrite.
239
+ *
240
+ * READ-ONLY is a contract: this runs on the user-message path and must never reinforce a fact,
241
+ * record retrieval/usage stats, warm/download a model, or mutate the vstore/DB. Never throws — a
242
+ * missing hint is harmless while a throwing `agent/pre-step` listener takes the step down.
222
243
  */
223
- relevance(text: string): RelevanceHit;
244
+ wouldFind(text: string): Promise<boolean>;
224
245
  /**
225
246
  * True once {@link shutdown} has run: the databases are closed and no background pass may start.
226
247
  * A gated startup job checks this (as well as the plugin's fiber) before touching the stores.
227
248
  */
228
249
  readonly closed: boolean;
229
- shutdown(): void;
250
+ shutdown(): Promise<void>;
230
251
  }
231
- /** Build the runtime: load config, open the memory DB, construct the stores. */
232
- export declare function buildRuntime(opts?: RuntimeOptions): AvantfRuntime;
252
+ /**
253
+ * Build the runtime: load config, open memory DB, construct the stores, run their open-time pass.
254
+ *
255
+ * ASYNC since DB-2: the store's startup pass (clock repair, lifecycle tick, vector-index reload) now
256
+ * awaits repository reads, so it cannot run inside a constructor. Making the open await it keeps the
257
+ * ordering every caller depends on — by the time `buildRuntime` resolves, the store is started.
258
+ */
259
+ export declare function buildRuntime(opts?: RuntimeOptions): Promise<AvantfRuntime>;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The storage composition root (DB-1; registry-based from DB-2/DB-3).
3
+ *
4
+ * One decision, made once: which driver serves this process. The configured name is resolved through
5
+ * the REGISTRY, so adding a storage is adding a driver package — never a new branch here
6
+ * (docs/DB_DRIVER_LAYOUT.md §一 rule "不要新增其他固定 backend 分支"). An unknown name fails
7
+ * explicitly and creates nothing; there is no silent SQLite fallback.
8
+ *
9
+ * The builtin drivers are imported below for their REGISTRATION side effect: this file is the
10
+ * factory, so the factory is where the builtins must be available, no matter which module graph the
11
+ * host loaded. `fake` is a builtin on purpose (DB-4, docs/DB_DRIVER_LAYOUT.md §五): it exists so the
12
+ * config switch and the async boundary can be exercised end to end without a second storage engine,
13
+ * which is why selecting it needs no import at the call site.
14
+ *
15
+ * @module storage/create
16
+ */
17
+ import type { StorageConfig } from '../../mem-contract/index.js';
18
+ import type { StorageBackend } from '../interfaces/storage_backend.js';
19
+ import '../dbdrivers/sqlite/index.js';
20
+ import '../dbdrivers/fake/index.js';
21
+ /** Raised when `storage.backend` (or `storage.driver`) names a driver this host does not have. */
22
+ export declare class UnknownStorageBackendError extends Error {
23
+ readonly backend: string;
24
+ constructor(backend: string);
25
+ }
26
+ export interface CreateStorageBackendOptions {
27
+ /**
28
+ * The configured contradiction threshold (`lifecycle.contradiction_threshold`), passed to the
29
+ * driver for its detector construction.
30
+ */
31
+ readonly contradictionThreshold?: number;
32
+ }
33
+ /**
34
+ * Open the selected driver.
35
+ *
36
+ * ASYNC because every driver's `open` is (DB-2): the SQLite driver resolves immediately, a remote one
37
+ * connects. Callers await it, so the host has one opening path.
38
+ *
39
+ * @throws {UnknownStorageBackendError} when the configured name has no registered driver.
40
+ * @throws {UnsupportedStorageContractError} when the driver's contract major is not supported.
41
+ */
42
+ export declare function createStorageBackend(config: StorageConfig, options?: CreateStorageBackendOptions): Promise<StorageBackend>;
@@ -0,0 +1,65 @@
1
+ /**
2
+ * The storage error boundary (DB-3).
3
+ *
4
+ * SQLite reports failures as a driver error object with a numeric `errcode`; PostgreSQL has SQLSTATE,
5
+ * an HTTP backend has status codes, and none of those belong above the port. The store has exactly
6
+ * three things it can usefully do with a failed read/write: retry (the backend is temporarily
7
+ * unavailable), report a conflict (a uniqueness/constraint rule refused the write), or fail (anything
8
+ * else). This module names those three, and `db/sqlite.ts` — the one place that sees a driver error —
9
+ * maps into them.
10
+ *
11
+ * WHAT THIS IS NOT: a replacement for the domain refusals. `TITLE_IMMUTABLE`, `RELATIONS_NEED_NOTE`
12
+ * and the other `Error`s the store throws are decisions about the caller's request, not backend
13
+ * failures; they stay plain and unchanged. Only errors that come out of the database layer are
14
+ * classified here.
15
+ *
16
+ * The messages are preserved verbatim (the classification is metadata), so every existing caller and
17
+ * test that asserts on a driver message keeps working.
18
+ *
19
+ * @module storage/errors
20
+ */
21
+ /** What a caller can do about a failed storage operation. */
22
+ export type StorageFailureKind = 'unavailable' | 'constraint' | 'corrupt' | 'unknown';
23
+ /** A database-layer failure, classified so the boundary does not leak a driver's error shape. */
24
+ export declare class StorageError extends Error {
25
+ readonly kind: StorageFailureKind;
26
+ constructor(message: string, kind: StorageFailureKind, options?: {
27
+ code?: string | number;
28
+ cause?: unknown;
29
+ });
30
+ /** The driver's own code, kept for a log line — never branched on above the adapter. */
31
+ readonly code?: string | number;
32
+ /** Whether the same call may succeed if repeated later (a lock timeout, a restarting server). */
33
+ get retryable(): boolean;
34
+ }
35
+ /** The backend is temporarily unable to serve (SQLite: `SQLITE_BUSY`/`SQLITE_LOCKED`). */
36
+ export declare class StorageUnavailableError extends StorageError {
37
+ constructor(message: string, options?: {
38
+ code?: string | number;
39
+ cause?: unknown;
40
+ });
41
+ }
42
+ /** The backend refused the write because of one of its integrity rules. */
43
+ export declare class StorageConstraintError extends StorageError {
44
+ constructor(message: string, options?: {
45
+ code?: string | number;
46
+ cause?: unknown;
47
+ });
48
+ }
49
+ /** The stored data cannot be read (SQLite: `SQLITE_CORRUPT`/`SQLITE_NOTADB`). */
50
+ export declare class StorageCorruptError extends StorageError {
51
+ constructor(message: string, options?: {
52
+ code?: string | number;
53
+ cause?: unknown;
54
+ });
55
+ }
56
+ /** Whether a caught value is already one of ours (a second mapping must not wrap it again). */
57
+ export declare function isStorageError(error: unknown): error is StorageError;
58
+ /**
59
+ * Classify a driver failure into the storage vocabulary.
60
+ *
61
+ * Takes `unknown` because that is what a `catch` binds; anything already classified is returned
62
+ * untouched (mapping is idempotent), and a non-error value becomes a generic `StorageError` carrying
63
+ * its string form, so callers never have to re-check.
64
+ */
65
+ export declare function mapStorageError(error: unknown): StorageError;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The storage boundary's public entry point (DB-1; registry factory since DB-2/DB-3).
3
+ *
4
+ * It re-exports the HAL CONTRACT (`interfaces/`) for the store and for external drivers, and the
5
+ * registry/factory that select a driver. It deliberately re-exports NO driver implementation: a
6
+ * caller that wants the SQLite backend imports `dbdrivers/sqlite` explicitly (turning a hidden
7
+ * dependency into a visible one), while `createStorageBackend` serves everyone else through the
8
+ * registry.
9
+ *
10
+ * @module storage
11
+ */
12
+ export * from '../interfaces/index.js';
13
+ export type { StorageConfig } from '../../mem-contract/index.js';
14
+ export { assertSupportedDriver, lookupStorageDriver, registerStorageDriver, storageDriverNames, UnsupportedStorageContractError, } from './registry.js';
15
+ export { createStorageBackend, UnknownStorageBackendError, type CreateStorageBackendOptions } from './create.js';