dsh-cross-memory 0.0.0-stage → 0.2.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 (152) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +247 -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 +1550 -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 +4475 -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 +112 -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 +455 -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 +314 -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 +140 -0
  57. package/lib/test/persona.test.js +73 -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 +227 -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 +43 -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 +233 -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
package/lib/skills.js ADDED
@@ -0,0 +1,220 @@
1
+ /**
2
+ * Skill drafts and their promotion into the harness's skill roots.
3
+ *
4
+ * Consolidation can notice that several memories describe one repeatable
5
+ * procedure. Writing that straight into `$DSH_HOME/skills` would silently grow
6
+ * the model's skill catalog from a background pass, so the draft lands in the
7
+ * memory store instead and a human promotes it:
8
+ *
9
+ * ```text
10
+ * $DSH_HOME/memories/skills/<name>/SKILL.md # draft, not yet loaded by DSH
11
+ * $DSH_HOME/skills/<name>/SKILL.md # promoted, now in the catalog
12
+ * ```
13
+ *
14
+ * Promotion is explicit (`/memories promote <name>`) and idempotent: the file
15
+ * written is exactly the shape `@deepseek-ai/dsh-skill-filesystem` parses
16
+ * (`name` + `description` frontmatter in a per-skill directory).
17
+ *
18
+ * @module dsh-cross-memory/skills
19
+ */
20
+ import { mkdir, readFile, readdir, rename, rm, stat, writeFile } from 'node:fs/promises';
21
+ import { dirname, join } from 'node:path';
22
+ // One YAML scalar encoder for the whole package: the skill frontmatter and the
23
+ // session evidence notes must both survive descriptions a model wrote.
24
+ import { yamlScalar } from './storage.js';
25
+ /** The skill-name grammar the harness loader accepts. */
26
+ const SKILL_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
27
+ /** Whether a filesystem error means "absent". */
28
+ function isMissing(error) {
29
+ return typeof error === 'object' && error !== null && 'code' in error
30
+ && (error.code === 'ENOENT' || error.code === 'ENOTDIR');
31
+ }
32
+ /** Whether one path exists. */
33
+ async function exists(path) {
34
+ try {
35
+ await stat(path);
36
+ return true;
37
+ }
38
+ catch (error) {
39
+ if (isMissing(error))
40
+ return false;
41
+ throw error;
42
+ }
43
+ }
44
+ /** Normalize a proposed name into the harness grammar, or `undefined`. */
45
+ export function normalizeSkillName(value) {
46
+ const name = value.toLowerCase().replace(/[^a-z0-9]+/gu, '-').replace(/^-+|-+$/gu, '').slice(0, 64);
47
+ return SKILL_NAME.test(name) ? name : undefined;
48
+ }
49
+ /**
50
+ * Render one draft as the markdown the harness loader reads.
51
+ * @param draft - the proposed skill.
52
+ * @returns a `SKILL.md` body with the required frontmatter.
53
+ */
54
+ export function renderSkillFile(draft) {
55
+ return [
56
+ '---',
57
+ `name: ${draft.name}`,
58
+ `description: ${yamlScalar(draft.description)}`,
59
+ '---',
60
+ '',
61
+ ...draft.steps.map((step, index) => `${index + 1}. ${step}`),
62
+ '',
63
+ ].join('\n');
64
+ }
65
+ /** Where skill drafts live inside the memory store. */
66
+ export function draftRoot(memoriesDir) {
67
+ return join(memoriesDir, 'skills');
68
+ }
69
+ /** Where the harness discovers promoted skills. */
70
+ export function promotionRoot(dshHome) {
71
+ return join(dshHome, 'skills');
72
+ }
73
+ /**
74
+ * Write or replace one skill draft in the memory store.
75
+ *
76
+ * The write is atomic at the file level (a temp file renamed over the target),
77
+ * so a crash cannot leave a half-written `SKILL.md` that the loader would then
78
+ * try to parse.
79
+ *
80
+ * @param memoriesDir - the memory store root.
81
+ * @param draft - the skill to stage.
82
+ * @returns the absolute path of the staged file.
83
+ */
84
+ export async function writeDraft(memoriesDir, draft) {
85
+ const name = normalizeSkillName(draft.name);
86
+ if (name === undefined)
87
+ throw new Error(`dsh-cross-memory: invalid skill name ${JSON.stringify(draft.name)}`);
88
+ const dir = join(draftRoot(memoriesDir), name);
89
+ const path = join(dir, 'SKILL.md');
90
+ await mkdir(dir, { recursive: true });
91
+ const temp = `${path}.${process.pid}.${Date.now().toString(36)}.tmp`;
92
+ await writeFile(temp, renderSkillFile({ ...draft, name }), 'utf8');
93
+ await rename(temp, path);
94
+ return path;
95
+ }
96
+ /**
97
+ * List the staged drafts that still need a human.
98
+ *
99
+ * `promote` copies rather than moves, so the draft outlives the promotion on
100
+ * purpose — it is the provenance. The cost was the notice: the banner kept
101
+ * counting promoted skills as pending, and on a real store it read "4 pending"
102
+ * when three of them had been live for a week. A draft whose promoted copy is
103
+ * byte-identical is not waiting for anybody, so it is not listed.
104
+ *
105
+ * @param memoriesDir - the memory store root.
106
+ * @param dshHome - the harness home; when given, already-promoted drafts are skipped.
107
+ * @returns the drafts still pending, name-sorted.
108
+ */
109
+ export async function listDrafts(memoriesDir, dshHome) {
110
+ const root = draftRoot(memoriesDir);
111
+ const promoted = dshHome === undefined ? undefined : promotionRoot(dshHome);
112
+ let names;
113
+ try {
114
+ const dirents = await readdir(root, { withFileTypes: true });
115
+ names = dirents.filter((dirent) => dirent.isDirectory()).map((dirent) => dirent.name).sort();
116
+ }
117
+ catch (error) {
118
+ if (isMissing(error))
119
+ return [];
120
+ throw error;
121
+ }
122
+ const staged = [];
123
+ for (const name of names) {
124
+ const path = join(root, name, 'SKILL.md');
125
+ if (!(await exists(path)))
126
+ continue;
127
+ const text = await readFile(path, 'utf8');
128
+ if (promoted !== undefined) {
129
+ const live = await readFile(join(promoted, name, 'SKILL.md'), 'utf8').catch(() => undefined);
130
+ if (live === text)
131
+ continue;
132
+ }
133
+ staged.push({ name, description: descriptionOf(text), path });
134
+ }
135
+ return staged;
136
+ }
137
+ /**
138
+ * Read the description out of one `SKILL.md` frontmatter block.
139
+ *
140
+ * Drafts written by this module carry a double-quoted scalar (see
141
+ * {@link yamlScalar}); a hand-edited file may carry a plain one, so both are
142
+ * accepted and the quotes are stripped only when they wrap the whole value.
143
+ */
144
+ function descriptionOf(text) {
145
+ const end = text.indexOf('\n---', 3);
146
+ const header = end < 0 ? '' : text.slice(3, end);
147
+ for (const line of header.split('\n')) {
148
+ const separator = line.indexOf(':');
149
+ if (separator < 0)
150
+ continue;
151
+ if (line.slice(0, separator).trim().toLowerCase() !== 'description')
152
+ continue;
153
+ const raw = line.slice(separator + 1).trim();
154
+ if (raw.length >= 2 && raw.startsWith('"') && raw.endsWith('"')) {
155
+ return raw.slice(1, -1)
156
+ .replace(/\\n/gu, '\n')
157
+ .replace(/\\t/gu, '\t')
158
+ .replace(/\\"/gu, '"')
159
+ .replace(/\\\\/gu, '\\');
160
+ }
161
+ return raw;
162
+ }
163
+ return '';
164
+ }
165
+ /**
166
+ * Copy one staged draft into the harness's skill root.
167
+ *
168
+ * Copies rather than moves, so the memory store keeps the provenance and the
169
+ * promoted file is an independent artifact a human can edit. Re-promoting an
170
+ * unchanged draft is a no-op, which is what makes it a safe retry.
171
+ *
172
+ * It REFUSES one case: overwriting a promoted copy that is newer than the draft.
173
+ * A draft is staged once and then forgotten while the promoted skill keeps being
174
+ * edited, so "re-promote" is the one action that can silently roll a live skill
175
+ * back — measured on a real store, one draft was four days older than its
176
+ * promoted copy and still said "must restart the host 2.0.17" where the live
177
+ * skill said "2.0.18-beta.1 can hot-mount".
178
+ *
179
+ * @param memoriesDir - the memory store root.
180
+ * @param dshHome - the harness home whose `skills/` the loader scans.
181
+ * @param name - the draft to promote.
182
+ * @returns the absolute path of the promoted skill, or `undefined` when no such draft exists.
183
+ * @throws when the promoted copy is newer than the draft it would replace.
184
+ */
185
+ export async function promote(memoriesDir, dshHome, name) {
186
+ const normalized = normalizeSkillName(name);
187
+ if (normalized === undefined)
188
+ return undefined;
189
+ const source = join(draftRoot(memoriesDir), normalized, 'SKILL.md');
190
+ if (!(await exists(source)))
191
+ return undefined;
192
+ const text = await readFile(source, 'utf8');
193
+ const target = join(promotionRoot(dshHome), normalized, 'SKILL.md');
194
+ const live = await readFile(target, 'utf8').catch(() => undefined);
195
+ if (live === text)
196
+ return target;
197
+ if (live !== undefined) {
198
+ const [sourceTime, liveTime] = await Promise.all([
199
+ stat(source).then((info) => info.mtimeMs).catch(() => 0),
200
+ stat(target).then((info) => info.mtimeMs).catch(() => 0),
201
+ ]);
202
+ if (liveTime > sourceTime) {
203
+ throw new Error(`dsh-cross-memory: refusing to overwrite \`${normalized}\`: the promoted copy is newer than the staged draft, and re-promoting would roll the live skill back. Run /memories discard ${normalized} instead.`);
204
+ }
205
+ }
206
+ await mkdir(dirname(target), { recursive: true });
207
+ await writeFile(target, text, 'utf8');
208
+ return target;
209
+ }
210
+ /** Remove one staged draft. */
211
+ export async function discardDraft(memoriesDir, name) {
212
+ const normalized = normalizeSkillName(name);
213
+ if (normalized === undefined)
214
+ return false;
215
+ const dir = join(draftRoot(memoriesDir), normalized);
216
+ if (!(await exists(dir)))
217
+ return false;
218
+ await rm(dir, { recursive: true, force: true });
219
+ return true;
220
+ }