dsh-cross-memory 0.0.0-stage → 0.2.1

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 (152) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +253 -2
  3. package/cordis.patch.yml +27 -0
  4. package/docs/DETAIL.md +13 -0
  5. package/docs/DETAIL.zh.md +17 -0
  6. package/icon.svg +1 -0
  7. package/lib/backfill.js +273 -0
  8. package/lib/citations.js +305 -0
  9. package/lib/client.js +1660 -0
  10. package/lib/config.js +736 -0
  11. package/lib/consolidate.js +1119 -0
  12. package/lib/extract.js +570 -0
  13. package/lib/gate.js +137 -0
  14. package/lib/git-origin.js +224 -0
  15. package/lib/index.js +4482 -0
  16. package/lib/log.js +260 -0
  17. package/lib/loops.js +186 -0
  18. package/lib/notices.js +195 -0
  19. package/lib/notify.js +33 -0
  20. package/lib/persona.js +163 -0
  21. package/lib/query.js +93 -0
  22. package/lib/raw-recall.js +286 -0
  23. package/lib/reflection.js +291 -0
  24. package/lib/remote.js +490 -0
  25. package/lib/render.js +678 -0
  26. package/lib/retention.js +64 -0
  27. package/lib/schedule.js +245 -0
  28. package/lib/search.js +1068 -0
  29. package/lib/skills.js +220 -0
  30. package/lib/state.js +701 -0
  31. package/lib/storage.js +1424 -0
  32. package/lib/sync-runtime.js +268 -0
  33. package/lib/sync.js +620 -0
  34. package/lib/test/activity.test.js +74 -0
  35. package/lib/test/archive.test.js +182 -0
  36. package/lib/test/backfill.test.js +144 -0
  37. package/lib/test/citations.test.js +91 -0
  38. package/lib/test/client-wiring.test.js +134 -0
  39. package/lib/test/consolidate-job.test.js +95 -0
  40. package/lib/test/consolidate.test.js +430 -0
  41. package/lib/test/consolidation-selection.test.js +152 -0
  42. package/lib/test/dedupe-keys.test.js +230 -0
  43. package/lib/test/delta.test.js +93 -0
  44. package/lib/test/derived.test.js +110 -0
  45. package/lib/test/disputes-panel.test.js +341 -0
  46. package/lib/test/eval.test.js +171 -0
  47. package/lib/test/expand.test.js +164 -0
  48. package/lib/test/extract.test.js +1020 -0
  49. package/lib/test/git-origin.test.js +189 -0
  50. package/lib/test/injection.test.js +440 -0
  51. package/lib/test/kind-filter.test.js +49 -0
  52. package/lib/test/legacy-state.test.js +69 -0
  53. package/lib/test/log.test.js +156 -0
  54. package/lib/test/loops.test.js +86 -0
  55. package/lib/test/memory.test.js +855 -0
  56. package/lib/test/notices.test.js +142 -0
  57. package/lib/test/persona.test.js +86 -0
  58. package/lib/test/prefilter.test.js +73 -0
  59. package/lib/test/preset.test.js +77 -0
  60. package/lib/test/raw-recall.test.js +186 -0
  61. package/lib/test/recall.test.js +433 -0
  62. package/lib/test/reflection.test.js +64 -0
  63. package/lib/test/remote.test.js +238 -0
  64. package/lib/test/retention.test.js +117 -0
  65. package/lib/test/retrieval.test.js +142 -0
  66. package/lib/test/rotation.test.js +228 -0
  67. package/lib/test/schedule.test.js +112 -0
  68. package/lib/test/settings-card.test.js +58 -0
  69. package/lib/test/skills.test.js +129 -0
  70. package/lib/test/state.test.js +212 -0
  71. package/lib/test/subagent-contract.test.js +139 -0
  72. package/lib/test/sweep.test.js +159 -0
  73. package/lib/test/sync.test.js +314 -0
  74. package/lib/test/time.test.js +171 -0
  75. package/lib/time.js +303 -0
  76. package/lib/tool.js +330 -0
  77. package/lib/types/backfill.d.ts +96 -0
  78. package/lib/types/citations.d.ts +105 -0
  79. package/lib/types/config.d.ts +811 -0
  80. package/lib/types/consolidate.d.ts +742 -0
  81. package/lib/types/extract.d.ts +352 -0
  82. package/lib/types/gate.d.ts +83 -0
  83. package/lib/types/git-origin.d.ts +23 -0
  84. package/lib/types/index.d.ts +1316 -0
  85. package/lib/types/log.d.ts +144 -0
  86. package/lib/types/loops.d.ts +82 -0
  87. package/lib/types/notices.d.ts +117 -0
  88. package/lib/types/notify.d.ts +12 -0
  89. package/lib/types/persona.d.ts +76 -0
  90. package/lib/types/query.d.ts +45 -0
  91. package/lib/types/raw-recall.d.ts +84 -0
  92. package/lib/types/reflection.d.ts +122 -0
  93. package/lib/types/remote.d.ts +246 -0
  94. package/lib/types/render.d.ts +286 -0
  95. package/lib/types/retention.d.ts +66 -0
  96. package/lib/types/schedule.d.ts +116 -0
  97. package/lib/types/search.d.ts +583 -0
  98. package/lib/types/skills.d.ts +71 -0
  99. package/lib/types/state.d.ts +294 -0
  100. package/lib/types/storage.d.ts +458 -0
  101. package/lib/types/sync-runtime.d.ts +103 -0
  102. package/lib/types/sync.d.ts +222 -0
  103. package/lib/types/test/activity.test.d.ts +1 -0
  104. package/lib/types/test/archive.test.d.ts +1 -0
  105. package/lib/types/test/backfill.test.d.ts +1 -0
  106. package/lib/types/test/citations.test.d.ts +1 -0
  107. package/lib/types/test/client-wiring.test.d.ts +1 -0
  108. package/lib/types/test/consolidate-job.test.d.ts +1 -0
  109. package/lib/types/test/consolidate.test.d.ts +1 -0
  110. package/lib/types/test/consolidation-selection.test.d.ts +1 -0
  111. package/lib/types/test/dedupe-keys.test.d.ts +1 -0
  112. package/lib/types/test/delta.test.d.ts +1 -0
  113. package/lib/types/test/derived.test.d.ts +1 -0
  114. package/lib/types/test/disputes-panel.test.d.ts +1 -0
  115. package/lib/types/test/eval.test.d.ts +1 -0
  116. package/lib/types/test/expand.test.d.ts +1 -0
  117. package/lib/types/test/extract.test.d.ts +1 -0
  118. package/lib/types/test/git-origin.test.d.ts +1 -0
  119. package/lib/types/test/injection.test.d.ts +1 -0
  120. package/lib/types/test/kind-filter.test.d.ts +1 -0
  121. package/lib/types/test/legacy-state.test.d.ts +1 -0
  122. package/lib/types/test/log.test.d.ts +1 -0
  123. package/lib/types/test/loops.test.d.ts +1 -0
  124. package/lib/types/test/memory.test.d.ts +1 -0
  125. package/lib/types/test/notices.test.d.ts +1 -0
  126. package/lib/types/test/persona.test.d.ts +1 -0
  127. package/lib/types/test/prefilter.test.d.ts +1 -0
  128. package/lib/types/test/preset.test.d.ts +1 -0
  129. package/lib/types/test/raw-recall.test.d.ts +1 -0
  130. package/lib/types/test/recall.test.d.ts +1 -0
  131. package/lib/types/test/reflection.test.d.ts +1 -0
  132. package/lib/types/test/remote.test.d.ts +1 -0
  133. package/lib/types/test/retention.test.d.ts +1 -0
  134. package/lib/types/test/retrieval.test.d.ts +1 -0
  135. package/lib/types/test/rotation.test.d.ts +1 -0
  136. package/lib/types/test/schedule.test.d.ts +1 -0
  137. package/lib/types/test/settings-card.test.d.ts +1 -0
  138. package/lib/types/test/skills.test.d.ts +1 -0
  139. package/lib/types/test/state.test.d.ts +1 -0
  140. package/lib/types/test/subagent-contract.test.d.ts +1 -0
  141. package/lib/types/test/sweep.test.d.ts +1 -0
  142. package/lib/types/test/sync.test.d.ts +1 -0
  143. package/lib/types/test/time.test.d.ts +1 -0
  144. package/lib/types/time.d.ts +67 -0
  145. package/lib/types/tool.d.ts +9 -0
  146. package/lib/types/types.d.ts +374 -0
  147. package/lib/types/workspace.d.ts +20 -0
  148. package/lib/types.js +47 -0
  149. package/lib/workspace.js +65 -0
  150. package/locale/en.json +7 -0
  151. package/locale/zh.json +7 -0
  152. package/package.json +126 -4
@@ -0,0 +1,294 @@
1
+ /** One session's extraction progress. */
2
+ export interface SessionState {
3
+ /** Last surface message seq already mined (exclusive). */
4
+ lastSeq: number;
5
+ /** Unix epoch milliseconds of the last successful extraction. */
6
+ at: number;
7
+ /** Absolute workspace root recorded when the watermark was written. */
8
+ root?: string;
9
+ /** Whether this session has ever contributed memories. */
10
+ contributed?: boolean;
11
+ /** Unix epoch milliseconds of the session's last observed activity. */
12
+ activityAt?: number;
13
+ }
14
+ /** One pending consolidation job. */
15
+ export interface ConsolidateJob {
16
+ /** Job key; currently always the global scope name. */
17
+ key: string;
18
+ /** Unix epoch milliseconds when the job was enqueued. */
19
+ enqueuedAt: number;
20
+ /** Unix epoch milliseconds before which the job must not run. */
21
+ notBefore: number;
22
+ /**
23
+ * Absolute workspace root whose project scope this pass covers.
24
+ *
25
+ * Consolidation is process-level but project scope is per-workspace, so the
26
+ * job has to remember which workspace enqueued it: using whichever session
27
+ * happens to run the pass would consolidate a different project's memories.
28
+ */
29
+ root?: string;
30
+ /** Lease holder token, or `undefined` when unclaimed. */
31
+ lease?: string;
32
+ /** Unix epoch milliseconds the lease expires. */
33
+ leaseUntil?: number;
34
+ /** Remaining retries before the job is dropped. */
35
+ retries: number;
36
+ /** Last failure message, for diagnostics. */
37
+ lastError?: string;
38
+ }
39
+ /** The single background-pass pause a store records today. */
40
+ export declare const BACKGROUND_LIMIT = "background";
41
+ /**
42
+ * A recorded "stop spending quota" state.
43
+ *
44
+ * Codex gates background memory work on a provider-reported remaining-quota
45
+ * percentage. DSH exposes no such number, so the same protection is driven by the
46
+ * refusal itself: after a rate-limit or exhausted-quota error the plugin stops
47
+ * starting background passes until the wait elapses, doubling it per consecutive
48
+ * refusal.
49
+ */
50
+ export interface LimitState {
51
+ /** Consecutive refused passes. */
52
+ failures: number;
53
+ /** Unix epoch milliseconds before which no background pass may start. */
54
+ until: number;
55
+ /** Unix epoch milliseconds of the most recent refusal. */
56
+ at: number;
57
+ /** Provider wording, for diagnostics. */
58
+ reason?: string;
59
+ }
60
+ /**
61
+ * Durable state store.
62
+ *
63
+ * Every method is synchronous and internally consistent: SQLite serializes the
64
+ * writes, and the fallback map is single-process. Callers do not need locking.
65
+ */
66
+ export declare class StateStore {
67
+ readonly path: string;
68
+ private db;
69
+ /** Fallback when `node:sqlite` is unavailable: watermarks survive only in-process. */
70
+ private readonly sessions;
71
+ private readonly jobs;
72
+ private readonly usage;
73
+ /** Bookkeeping scalars: when the last sweep ran, and anything else a pass must remember. */
74
+ private readonly meta;
75
+ /** Per-session memory switches, when the SQL driver is unavailable. */
76
+ private readonly sessionModes;
77
+ /** Last injection per conversation, when the SQL driver is unavailable. */
78
+ private readonly injections;
79
+ private readonly limits;
80
+ /** Why the SQL driver is absent, for one diagnostic line. */
81
+ readonly degradedReason: string | undefined;
82
+ constructor(path: string);
83
+ /**
84
+ * Add columns a newer version introduced.
85
+ *
86
+ * `CREATE TABLE IF NOT EXISTS` is a no-op on an existing table, so a column
87
+ * added to the schema above would silently never appear on an upgraded store
88
+ * and every query naming it would fail. Each column is checked and added
89
+ * individually, which is safe to run on every open.
90
+ */
91
+ private migrate;
92
+ /** Whether SQLite is backing this store. */
93
+ get durable(): boolean;
94
+ /**
95
+ * The write-ahead-log checkpoint threshold in pages, as this connection sees it.
96
+ *
97
+ * Exposed because the value is per-connection and therefore invisible to any
98
+ * other handle: a fresh connection reports the compiled-in default and would
99
+ * happily "confirm" a policy this store does not use.
100
+ */
101
+ get walAutocheckpoint(): number;
102
+ /** Close the database handle; safe to call twice. */
103
+ close(): void;
104
+ /** Read one session's watermark. */
105
+ getSession(id: string): SessionState | undefined;
106
+ /** Write one session's watermark. */
107
+ putSession(id: string, state: SessionState): void;
108
+ /** Record activity for a session without touching its watermark. */
109
+ touchSession(id: string, at?: number): void;
110
+ /** Every session that has a watermark, newest activity first. */
111
+ listSessions(): {
112
+ id: string;
113
+ state: SessionState;
114
+ }[];
115
+ /**
116
+ * What one conversation was already shown, and when.
117
+ *
118
+ * The ids are the entries the model has already seen in this conversation;
119
+ * `at` is the moment of the last injection. A delta pass compares entry
120
+ * `updated` stamps against `at`, so both are needed.
121
+ *
122
+ * @param id - session id.
123
+ * @returns the record, or `undefined` when the conversation was never injected.
124
+ */
125
+ getInjection(id: string): {
126
+ at: number;
127
+ ids: readonly string[];
128
+ } | undefined;
129
+ /** Record that a conversation was shown these entries at `at`. */
130
+ putInjection(id: string, record: {
131
+ at: number;
132
+ ids: readonly string[];
133
+ }): void;
134
+ /**
135
+ * Number of sessions this store knows about.
136
+ *
137
+ * Every row counts, including ones that only ever recorded activity or a
138
+ * memory switch, so this answers "has this store seen the session" — which is
139
+ * what the legacy-import guard needs. {@link minedCount} answers "was it
140
+ * mined", which is what a diagnostic wants.
141
+ */
142
+ sessionCount(): number;
143
+ /**
144
+ * Number of sessions that were actually mined.
145
+ *
146
+ * The distinction is not cosmetic: a store can accumulate dozens of activity
147
+ * rows while extraction has never once completed, and a single "sessions"
148
+ * number reports that as a busy, healthy plugin.
149
+ */
150
+ minedCount(): number;
151
+ /** Read one job. */
152
+ getJob(key: string): ConsolidateJob | undefined;
153
+ /** Insert or replace one job. */
154
+ putJob(job: ConsolidateJob): void;
155
+ /** Delete one job. */
156
+ deleteJob(key: string): void;
157
+ /**
158
+ * Claim one job for `leaseMs`, or return `undefined` when another holder owns
159
+ * a live lease or the job's backoff has not elapsed. The current holder may
160
+ * renew its own lease.
161
+ * @param key - job key.
162
+ * @param token - this worker's opaque lease token.
163
+ * @param leaseMs - how long the lease is valid.
164
+ * @param now - injected clock.
165
+ * @returns the claimed job, or `undefined` when it is not available.
166
+ */
167
+ claimJob(key: string, token: string, leaseMs: number, now?: number): ConsolidateJob | undefined;
168
+ /**
169
+ * Drop one job's backoff and last failure, leaving the job queued.
170
+ *
171
+ * Called when a person forces a pass. Without it a manual `/memories
172
+ * consolidate` returned "nothing to consolidate" for hours after a failure had
173
+ * already been fixed — the command was refused by a backoff meant for the
174
+ * background retry, and the message named three causes none of which was the
175
+ * real one.
176
+ *
177
+ * @param key - job key.
178
+ */
179
+ clearJobBackoff(key: string): void;
180
+ /** Record one usage hit for a memory entry. */
181
+ bumpUsage(scope: string, id: string, now?: number): {
182
+ uses: number;
183
+ lastUsedAt: number;
184
+ };
185
+ /** Read one entry's usage counters. */
186
+ getUsage(scope: string, id: string): {
187
+ uses: number;
188
+ lastUsedAt: number;
189
+ };
190
+ /**
191
+ * Every usage row, for the summary ranking pass.
192
+ *
193
+ * `surfacedAt` is the authoritative "was this ever listed in an injected
194
+ * block" clock. The entry file mirrors it for human readers, but the mirror
195
+ * can be stale (it is refreshed at most hourly, and entries written before
196
+ * this table existed never got a row): selection decisions read THIS.
197
+ */
198
+ listUsage(): {
199
+ scope: string;
200
+ id: string;
201
+ uses: number;
202
+ lastUsedAt: number;
203
+ surfacedAt: number;
204
+ }[];
205
+ /**
206
+ * Record that an entry was listed in an injected block.
207
+ *
208
+ * Surfacing is a weaker signal than a read — the model saw the memory but did
209
+ * not open it — so it deliberately does not inflate `uses`. It exists because
210
+ * retention would otherwise evict the memories that work too well to need a
211
+ * search: an entry the model read straight out of the summary would look
212
+ * unused, and the better it is, the faster it would expire.
213
+ *
214
+ * @param scope - the entry's scope.
215
+ * @param id - the entry's id.
216
+ * @param now - injected clock.
217
+ * @returns the recorded timestamp.
218
+ */
219
+ bumpSurfaced(scope: string, id: string, now?: number): number;
220
+ /**
221
+ * Record that a consolidation pass reviewed these entries.
222
+ *
223
+ * The mark is what stops a review from starving: selection prefers entries it
224
+ * has never reviewed, then the ones reviewed longest ago, so a memory cannot
225
+ * sit unseen forever behind a permanently-newest first page.
226
+ *
227
+ * @param refs - the scope/id pairs the pass considered.
228
+ * @param now - clock to record.
229
+ */
230
+ markConsolidated(refs: readonly {
231
+ scope: string;
232
+ id: string;
233
+ }[], now?: number): void;
234
+ /** Every usage row with all four timestamps, for the retention pass. */
235
+ retentionRows(): {
236
+ scope: string;
237
+ id: string;
238
+ uses: number;
239
+ lastUsedAt: number;
240
+ surfacedAt: number;
241
+ consolidatedAt: number;
242
+ }[];
243
+ /** Read one bookkeeping value, if it was ever written. */
244
+ getMeta(key: string): string | undefined;
245
+ /** Write one bookkeeping value. */
246
+ putMeta(key: string, value: string): void;
247
+ /** One session's memory switch; `on` unless it was turned off. */
248
+ getSessionMode(id: string): 'on' | 'off';
249
+ /** Turn one session's memory off, or back on. */
250
+ setSessionMode(id: string, mode: 'on' | 'off', at?: number): void;
251
+ /** The recorded background pause, if any. */
252
+ getLimit(id?: string): LimitState | undefined;
253
+ /**
254
+ * Whether background passes are paused right now.
255
+ * @param now - injected clock.
256
+ * @param id - which pause to read.
257
+ * @returns true while the recorded wait has not elapsed.
258
+ */
259
+ isLimited(now?: number, id?: string): boolean;
260
+ /**
261
+ * Record one provider refusal and extend the wait.
262
+ *
263
+ * The wait doubles per consecutive refusal, capped, so a provider that keeps
264
+ * refusing costs one failed call per interval instead of one per idle timer.
265
+ *
266
+ * @param reason - provider wording, for diagnostics.
267
+ * @param baseMs - wait after the first refusal.
268
+ * @param maxMs - ceiling for the doubling.
269
+ * @param now - injected clock.
270
+ * @param id - which pause to write.
271
+ * @returns the state after this refusal.
272
+ */
273
+ noteLimitFailure(reason: string, baseMs: number, maxMs: number, now?: number, id?: string): LimitState;
274
+ /** Forget the pause after a pass succeeds. */
275
+ clearLimit(id?: string): void;
276
+ }
277
+ /**
278
+ * Resolve the state database path inside one memory store. */
279
+ export declare function statePath(memoriesDir: string): string;
280
+ /** The pre-SQLite watermark file, imported once and then removed. */
281
+ export declare const LEGACY_STATE_FILE = "extract-state.json";
282
+ /**
283
+ * Import and remove a pre-SQLite `extract-state.json`.
284
+ *
285
+ * The file only ever held extraction watermarks. Importing them matters: a
286
+ * session with no watermark is re-mined, which spends quota re-reading a
287
+ * conversation that was already processed. The import is skipped when the
288
+ * database already holds sessions, so it can never overwrite newer state.
289
+ *
290
+ * @param store - the state store to import into.
291
+ * @param memoriesDir - directory the legacy file lives in.
292
+ * @returns how many watermarks were imported, or `undefined` when there was nothing to do.
293
+ */
294
+ export declare function importLegacyState(store: StateStore, memoriesDir: string): Promise<number | undefined>;