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,305 @@
1
+ /**
2
+ * Citation checking: does a memory point at a file that is still there?
3
+ *
4
+ * The most damaging kind of wrong memory is the one that is specific and
5
+ * checkable: "the report is at `output/market_report.json`", "run
6
+ * `scripts/start_sap_test.bat`". Both were true when written, both were later
7
+ * deleted, and a future session acting on them wastes a turn or, worse, concludes
8
+ * that working code is broken. Measured on a real store, 16 of 242 cited paths
9
+ * could not be found in any known checkout.
10
+ *
11
+ * The check is deliberately conservative. A relative path is only reported when
12
+ * its parent directory IS found but the file is not — that is a strong signal of
13
+ * "this moved or was deleted". When the parent is missing too, the memory may
14
+ * simply be describing another checkout, and guessing would produce noise that
15
+ * trains the reader to ignore the flag.
16
+ *
17
+ * @module dsh-cross-memory/citations
18
+ */
19
+ import { existsSync, readFileSync, statSync } from 'node:fs';
20
+ import { dirname, isAbsolute, join, relative } from 'node:path';
21
+ /** The default probe: the real filesystem. */
22
+ export const fileProbe = (path) => existsSync(path);
23
+ /**
24
+ * Relative paths a body cites.
25
+ *
26
+ * Only things with a directory component and a file extension are considered:
27
+ * a bare `index.js` names a file in a dozen packages, so it cannot be checked,
28
+ * and prose like `and/or` should never be mistaken for a path.
29
+ *
30
+ * @param text - the entry body.
31
+ * @returns unique relative paths, in first-seen order.
32
+ */
33
+ export function extractCitations(text) {
34
+ const pattern = /\b([A-Za-z0-9_.-]+\/[A-Za-z0-9_./-]*\.[A-Za-z0-9]{1,6})\b/gu;
35
+ const found = [];
36
+ const seen = new Set();
37
+ for (const match of text.matchAll(pattern)) {
38
+ const raw = match[1];
39
+ if (raw === undefined)
40
+ continue;
41
+ // A URL's path is not a repository path. The scheme itself is outside the
42
+ // match, so look at what precedes it.
43
+ const before = text.slice(Math.max(0, (match.index ?? 0) - 3), match.index ?? 0);
44
+ if (before.includes('//'))
45
+ continue;
46
+ const cleaned = raw.replace(/^\.\//u, '').replace(/\/+/gu, '/');
47
+ if (cleaned.startsWith('/') || cleaned.includes('://'))
48
+ continue;
49
+ // A trailing segment that is a hidden directory (`app/.tools`) is not a file
50
+ // path, and the check can only ask whether files exist.
51
+ const last = cleaned.split('/').at(-1) ?? '';
52
+ if (last.startsWith('.'))
53
+ continue;
54
+ // Reject slash-joined lists of same-extension files (`config.ts/index.ts`).
55
+ const parts = cleaned.split('/');
56
+ if (parts.length > 1 && parts.every((part) => /\.(ts|js|mjs|py)$/u.test(part)))
57
+ continue;
58
+ if (seen.has(cleaned))
59
+ continue;
60
+ seen.add(cleaned);
61
+ found.push(cleaned);
62
+ }
63
+ return found;
64
+ }
65
+ /** Whether any known root contains this relative path. */
66
+ function presentUnder(roots, relativePath, probe) {
67
+ return roots.some((root) => probe(join(root, relativePath)));
68
+ }
69
+ /**
70
+ * Whether any known root contains a directory with this name at this depth.
71
+ *
72
+ * Used only to decide whether a missing file is *evidence* of staleness: the
73
+ * parent chain existing somewhere while the file does not is the signal.
74
+ *
75
+ * @param roots - checkout roots to search.
76
+ * @param relativePath - the cited path, parent included.
77
+ * @param probe - filesystem probe.
78
+ * @returns true when the parent directory exists under some root.
79
+ */
80
+ function parentExists(roots, relativePath, probe) {
81
+ const parent = dirname(relativePath);
82
+ if (parent === '.' || parent.length === 0)
83
+ return false;
84
+ return roots.some((root) => probe(join(root, parent)));
85
+ }
86
+ /**
87
+ * Citations in one body that no longer resolve under any known root.
88
+ *
89
+ * @param body - the entry body.
90
+ * @param roots - checkout roots (project roots, the harness home, package dirs).
91
+ * @param probe - filesystem probe, for tests.
92
+ * @param options - extra rules; `isIgnored` suppresses a path the repository
93
+ * itself generates (see {@link isIgnoredPath}).
94
+ * @returns the paths that look deleted or moved.
95
+ */
96
+ export function missingCitations(body, roots, probe = fileProbe, options = {}) {
97
+ if (roots.length === 0)
98
+ return [];
99
+ const missing = [];
100
+ for (const citation of extractCitations(body)) {
101
+ if (presentUnder(roots, citation, probe))
102
+ continue;
103
+ if (!parentExists(roots, citation, probe))
104
+ continue;
105
+ // A generated file is not a stale citation. Measured on a real store, three
106
+ // of twelve flagged memories named runtime artifacts (`state/run.json`, a
107
+ // cache database, a synced local-state file) that are absent between runs by
108
+ // design; a warning that cries wolf is a warning the reader learns to skip.
109
+ if (options.isIgnored?.(citation) === true)
110
+ continue;
111
+ missing.push(citation);
112
+ }
113
+ return missing;
114
+ }
115
+ /**
116
+ * Parse one `.gitignore` body.
117
+ *
118
+ * Only the subset that decides "is this path generated" is supported: comments,
119
+ * blank lines, negation, a leading or embedded slash for anchoring, a trailing
120
+ * slash for directories, and `*` / `**` / `?` globs. Character classes and
121
+ * escaped leading `#`/`!` are treated literally, which can only make the check
122
+ * miss an exclusion — never invent one.
123
+ *
124
+ * @param text - the file's contents.
125
+ * @returns the rules, in file order (git gives the last match the last word).
126
+ */
127
+ export function parseGitignore(text) {
128
+ const rules = [];
129
+ for (const raw of text.split(/\r?\n/u)) {
130
+ const line = raw.trim();
131
+ if (line.length === 0 || line.startsWith('#'))
132
+ continue;
133
+ const negated = line.startsWith('!');
134
+ let body = negated ? line.slice(1).trim() : line;
135
+ if (body.length === 0)
136
+ continue;
137
+ const directory = body.endsWith('/');
138
+ if (directory)
139
+ body = body.slice(0, -1);
140
+ // A slash anywhere but the end anchors the pattern to the ignore file.
141
+ const anchored = body.startsWith('/') || body.includes('/');
142
+ if (body.startsWith('/'))
143
+ body = body.slice(1);
144
+ if (body.length === 0)
145
+ continue;
146
+ rules.push({ pattern: body, negated, anchored, directory });
147
+ }
148
+ return rules;
149
+ }
150
+ /**
151
+ * Read the ignore rules that govern paths relative to one root.
152
+ *
153
+ * Deliberately one file: the root's own `.gitignore`. Parent directories and
154
+ * global excludes are not consulted, so a path covered only by those is still
155
+ * reported — the conservative direction for a check whose job is to warn.
156
+ *
157
+ * @param root - repository root.
158
+ * @returns the rules, or an empty list when there is no readable `.gitignore`.
159
+ */
160
+ export function readGitignore(root) {
161
+ try {
162
+ return parseGitignore(readFileSync(join(root, '.gitignore'), 'utf8'));
163
+ }
164
+ catch {
165
+ return [];
166
+ }
167
+ }
168
+ /** Translate one gitignore pattern into a whole-string regular expression. */
169
+ function ignorePatternToRegExp(pattern) {
170
+ let source = '';
171
+ for (let index = 0; index < pattern.length; index += 1) {
172
+ const char = pattern[index] ?? '';
173
+ if (char === '*') {
174
+ if (pattern[index + 1] === '*') {
175
+ index += 1;
176
+ if (pattern[index + 1] === '/') {
177
+ source += '(?:.*/)?';
178
+ index += 1;
179
+ }
180
+ else if (source.endsWith('/')) {
181
+ source = `${source.slice(0, -1)}(?:/.*)?`;
182
+ }
183
+ else {
184
+ source += '.*';
185
+ }
186
+ continue;
187
+ }
188
+ source += '[^/]*';
189
+ continue;
190
+ }
191
+ if (char === '?') {
192
+ source += '[^/]';
193
+ continue;
194
+ }
195
+ source += char.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&');
196
+ }
197
+ return new RegExp(`^${source}$`, 'u');
198
+ }
199
+ /** Whether one rule matches a path, given its segments. */
200
+ function ruleMatches(rule, path, segments) {
201
+ const regex = ignorePatternToRegExp(rule.pattern);
202
+ if (rule.anchored) {
203
+ if (!rule.directory)
204
+ return regex.test(path);
205
+ // Every directory on the way down is a candidate, never the file itself.
206
+ for (let depth = 1; depth < segments.length; depth += 1) {
207
+ if (regex.test(segments.slice(0, depth).join('/')))
208
+ return true;
209
+ }
210
+ return false;
211
+ }
212
+ // Unanchored patterns name one component and match it at any depth.
213
+ const last = segments.length - 1;
214
+ for (let index = 0; index < segments.length; index += 1) {
215
+ if (rule.directory && index === last)
216
+ continue;
217
+ if (regex.test(segments[index] ?? ''))
218
+ return true;
219
+ }
220
+ return false;
221
+ }
222
+ /**
223
+ * Whether a repository's own ignore rules cover this relative path.
224
+ *
225
+ * The last matching rule decides, exactly as git does, so a `!` re-inclusion is
226
+ * honoured.
227
+ *
228
+ * @param relativePath - the cited path, relative to the root the rules came from.
229
+ * @param rules - rules from {@link parseGitignore} / {@link readGitignore}.
230
+ * @returns true when the path is one the repository generates.
231
+ */
232
+ export function isIgnoredPath(relativePath, rules) {
233
+ if (rules.length === 0)
234
+ return false;
235
+ const path = relativePath.replace(/\\/gu, '/').replace(/^\.\//u, '').replace(/^\/+/u, '');
236
+ if (path.length === 0)
237
+ return false;
238
+ const segments = path.split('/');
239
+ let ignored = false;
240
+ for (const rule of rules) {
241
+ if (ruleMatches(rule, path, segments))
242
+ ignored = !rule.negated;
243
+ }
244
+ return ignored;
245
+ }
246
+ /**
247
+ * A predicate that suppresses generated paths under one root.
248
+ *
249
+ * @param root - repository root whose `.gitignore` applies.
250
+ * @returns a predicate for {@link missingCitations}.
251
+ */
252
+ export function ignoredUnder(root) {
253
+ const rules = readGitignore(root);
254
+ return (relativePath) => isIgnoredPath(relativePath, rules);
255
+ }
256
+ /**
257
+ * Roots worth checking a project's memories against.
258
+ *
259
+ * A memory cites files relative to the repository it is about, so its own project
260
+ * root comes first; the harness home covers memories about profiles and
261
+ * settings, and one level of `node_modules` covers citations that name installed
262
+ * package sources.
263
+ *
264
+ * @param projectRoot - the owning workspace root, when there is one.
265
+ * @param extra - further roots the caller knows about (e.g. the harness home).
266
+ * @returns roots to search, most specific first.
267
+ */
268
+ export function citationRoots(projectRoot, extra = []) {
269
+ const roots = [];
270
+ if (projectRoot !== undefined && projectRoot.length > 0)
271
+ roots.push(projectRoot);
272
+ for (const root of extra)
273
+ if (root.length > 0 && !roots.includes(root))
274
+ roots.push(root);
275
+ return roots;
276
+ }
277
+ /**
278
+ * The marker appended to a summary bullet whose citations no longer resolve.
279
+ *
280
+ * @param missing - the paths that could not be found.
281
+ * @returns one short clause, or an empty string.
282
+ */
283
+ export function citationWarning(missing) {
284
+ if (missing.length === 0)
285
+ return '';
286
+ const shown = missing.slice(0, 2).join('、');
287
+ const more = missing.length > 2 ? ` 等 ${missing.length} 处` : '';
288
+ return `⚠ 引用的 ${shown}${more} 已不存在,以实测为准`;
289
+ }
290
+ /** Whether a path is inside a root (used by callers that filter roots). */
291
+ export function isInside(root, path) {
292
+ if (!isAbsolute(path))
293
+ return false;
294
+ const rel = relative(root, path);
295
+ return rel.length > 0 && !rel.startsWith('..') && !isAbsolute(rel);
296
+ }
297
+ /** Whether a path is an existing directory. */
298
+ export function isDirectory(path) {
299
+ try {
300
+ return statSync(path).isDirectory();
301
+ }
302
+ catch {
303
+ return false;
304
+ }
305
+ }