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/remote.js ADDED
@@ -0,0 +1,455 @@
1
+ /**
2
+ * Host→browser Remote surface for the Settings page.
3
+ *
4
+ * The Memories page's tunables card only edits this plugin's own config; a human
5
+ * also wants to see what is remembered. That needs the browser to call the host,
6
+ * and the harness already owns that transport: the Typert registry (`ctx.typert`)
7
+ * holds invocation descriptors, the API gateway serves them over the connection
8
+ * the shell already authenticated, and the browser half reaches them as
9
+ * `ctx.remote.<namespace>.<method>`.
10
+ *
11
+ * The manifest here is hand-written rather than generated. The generator emits
12
+ * the same shape from TypeScript decorators, but this package ships plain JS
13
+ * with no build-time Typert step, and the registry's own validation
14
+ * (`dsh-typert-registry`) accepts any contribution whose codecs carry a
15
+ * `typeSymbol` and a `create()` factory returning a schema with `parse()` — a
16
+ * live Zod schema is one such codec, a hand-written validator is another.
17
+ * (0.1.6-alpha.2 replaced the older `schema.parse` field with that factory: the
18
+ * registry now checks `typeof codec.create === 'function'` and the gateway
19
+ * materializes the schema with `codec.create().parse(value)`.) Dropping the
20
+ * generator therefore costs no wire capability and no dependency.
21
+ *
22
+ * `REMOTE_INVOCATION_DATA` is the single source of truth for the wire shape:
23
+ * the host expands it with validating parsers, and `scripts/build-client.mjs`
24
+ * serializes it into the browser bundle, which expands it with permissive ones.
25
+ * One list, so the two halves cannot drift.
26
+ *
27
+ * @module dsh-cross-memory/remote
28
+ */
29
+ import { existsSync } from 'node:fs';
30
+ import { join } from 'node:path';
31
+ import { browseMemories, searchMemories } from './search.js';
32
+ import { discardDraft, listDrafts, promote } from './skills.js';
33
+ import { MEMORY_KINDS, toMemoryKind } from './types.js';
34
+ /** npm package owning the Remote methods. */
35
+ export const REMOTE_PACKAGE = 'dsh-cross-memory';
36
+ /** Cordis service key the gateway resolves the receiver through. */
37
+ export const REMOTE_SERVICE = 'memories';
38
+ /** Wire namespace; the browser sees it as `ctx.remote.memories`. */
39
+ export const REMOTE_NAMESPACE = 'memories';
40
+ /**
41
+ * The wire contract.
42
+ *
43
+ * `project` names a project scope by slug (`overview` reports the slugs on
44
+ * disk); the empty string means "no project", which makes the project-scoped
45
+ * calls return nothing rather than fail.
46
+ */
47
+ export const REMOTE_INVOCATION_DATA = [
48
+ {
49
+ method: 'overview',
50
+ parameters: [],
51
+ result: 'overview',
52
+ },
53
+ {
54
+ method: 'list',
55
+ parameters: [
56
+ { name: 'project', wire: 'project', shape: 'text', optional: true },
57
+ { name: 'scope', wire: 'scope', shape: 'text', optional: true },
58
+ { name: 'query', wire: 'query', shape: 'text', optional: true },
59
+ { name: 'kind', wire: 'kind', shape: 'text', optional: true },
60
+ { name: 'limit', wire: 'limit', shape: 'number', optional: true },
61
+ ],
62
+ result: 'entries',
63
+ },
64
+ {
65
+ method: 'add',
66
+ parameters: [
67
+ { name: 'project', wire: 'project', shape: 'text', optional: true },
68
+ { name: 'scope', wire: 'scope', shape: 'text' },
69
+ { name: 'kind', wire: 'kind', shape: 'text', optional: true },
70
+ { name: 'title', wire: 'title', shape: 'text' },
71
+ { name: 'body', wire: 'body', shape: 'text' },
72
+ { name: 'tags', wire: 'tags', shape: 'text', optional: true },
73
+ ],
74
+ result: 'entry',
75
+ },
76
+ {
77
+ method: 'forget',
78
+ parameters: [
79
+ { name: 'project', wire: 'project', shape: 'text', optional: true },
80
+ { name: 'scope', wire: 'scope', shape: 'text' },
81
+ { name: 'id', wire: 'id', shape: 'text' },
82
+ ],
83
+ result: 'removed',
84
+ },
85
+ {
86
+ method: 'promoteSkill',
87
+ parameters: [{ name: 'name', wire: 'name', shape: 'text' }],
88
+ result: 'skill',
89
+ },
90
+ {
91
+ method: 'discardSkill',
92
+ parameters: [{ name: 'name', wire: 'name', shape: 'text' }],
93
+ result: 'discard',
94
+ },
95
+ {
96
+ method: 'disputes',
97
+ parameters: [],
98
+ result: 'disputes',
99
+ },
100
+ {
101
+ method: 'resolveDispute',
102
+ parameters: [
103
+ { name: 'id', wire: 'id', shape: 'text' },
104
+ { name: 'decision', wire: 'decision', shape: 'text' },
105
+ ],
106
+ result: 'text',
107
+ },
108
+ ];
109
+ /** Throw unless the value is a plain object. */
110
+ function asRecord(value, subject) {
111
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
112
+ throw new TypeError(`dsh-cross-memory: ${subject} must be an object`);
113
+ }
114
+ return value;
115
+ }
116
+ /** Throw unless the value is a string. */
117
+ function asText(value, subject) {
118
+ if (typeof value !== 'string')
119
+ throw new TypeError(`dsh-cross-memory: ${subject} must be a string`);
120
+ return value;
121
+ }
122
+ /** Throw unless the value is a finite number. */
123
+ function asCount(value, subject) {
124
+ if (typeof value !== 'number' || !Number.isFinite(value)) {
125
+ throw new TypeError(`dsh-cross-memory: ${subject} must be a number`);
126
+ }
127
+ return value;
128
+ }
129
+ /** Throw unless the value is a boolean. */
130
+ function asFlag(value, subject) {
131
+ if (typeof value !== 'boolean')
132
+ throw new TypeError(`dsh-cross-memory: ${subject} must be a boolean`);
133
+ return value;
134
+ }
135
+ /** Throw unless the value is an array. */
136
+ function asList(value, subject) {
137
+ if (!Array.isArray(value))
138
+ throw new TypeError(`dsh-cross-memory: ${subject} must be an array`);
139
+ return value;
140
+ }
141
+ /** Validate one stored entry as it crosses the wire. */
142
+ function parseEntryView(value) {
143
+ const view = asRecord(value, 'entry');
144
+ return {
145
+ id: asText(view.id, 'entry.id'),
146
+ scope: asText(view.scope, 'entry.scope'),
147
+ kind: asText(view.kind, 'entry.kind'),
148
+ title: asText(view.title, 'entry.title'),
149
+ body: asText(view.body, 'entry.body'),
150
+ tags: asList(view.tags, 'entry.tags').map((tag) => asText(tag, 'entry.tag')),
151
+ appliesTo: asText(view.appliesTo, 'entry.appliesTo'),
152
+ sourceSession: asText(view.sourceSession, 'entry.sourceSession'),
153
+ createdAt: asCount(view.createdAt, 'entry.createdAt'),
154
+ updatedAt: asCount(view.updatedAt, 'entry.updatedAt'),
155
+ uses: asCount(view.uses, 'entry.uses'),
156
+ lastUsedAt: asCount(view.lastUsedAt, 'entry.lastUsedAt'),
157
+ source: asText(view.source, 'entry.source'),
158
+ };
159
+ }
160
+ /** Validate one project row as it crosses the wire. */
161
+ function parseProjectView(value) {
162
+ const view = asRecord(value, 'project');
163
+ return {
164
+ slug: asText(view.slug, 'project.slug'),
165
+ name: asText(view.name, 'project.name'),
166
+ root: asText(view.root, 'project.root'),
167
+ count: asCount(view.count, 'project.count'),
168
+ updatedAt: asCount(view.updatedAt, 'project.updatedAt'),
169
+ };
170
+ }
171
+ /** Validate one staged draft as it crosses the wire. */
172
+ function parseDraftView(value) {
173
+ const view = asRecord(value, 'draft');
174
+ return {
175
+ name: asText(view.name, 'draft.name'),
176
+ description: asText(view.description, 'draft.description'),
177
+ promoted: asFlag(view.promoted, 'draft.promoted'),
178
+ };
179
+ }
180
+ /**
181
+ * Build the host-side codec for one shape.
182
+ *
183
+ * Parsing is real validation, not a passthrough: the gateway hands these
184
+ * functions untrusted values on the way in and this module's own output on the
185
+ * way out, and a malformed call should fail at the boundary rather than corrupt
186
+ * the store.
187
+ *
188
+ * @param shape - the shape to validate.
189
+ * @returns a strict codec whose `create()` returns that shape's parser.
190
+ */
191
+ function hostCodec(shape) {
192
+ const parse = (value) => {
193
+ switch (shape) {
194
+ case 'text':
195
+ return asText(value, 'argument');
196
+ case 'number':
197
+ return asCount(value, 'argument');
198
+ case 'overview': {
199
+ const view = asRecord(value, 'overview');
200
+ return {
201
+ storePath: asText(view.storePath, 'overview.storePath'),
202
+ globalCount: asCount(view.globalCount, 'overview.globalCount'),
203
+ projects: asList(view.projects, 'overview.projects').map(parseProjectView),
204
+ drafts: asList(view.drafts, 'overview.drafts').map(parseDraftView),
205
+ kinds: asList(view.kinds, 'overview.kinds').map((kind) => asText(kind, 'overview.kind')),
206
+ };
207
+ }
208
+ case 'entries': {
209
+ const view = asRecord(value, 'entries');
210
+ return {
211
+ entries: asList(view.entries, 'entries.entries').map(parseEntryView),
212
+ total: asCount(view.total, 'entries.total'),
213
+ };
214
+ }
215
+ case 'entry':
216
+ return parseEntryView(value);
217
+ case 'removed':
218
+ return { removed: asFlag(asRecord(value, 'removed').removed, 'removed.removed') };
219
+ case 'skill': {
220
+ const view = asRecord(value, 'skill');
221
+ return { name: asText(view.name, 'skill.name'), path: asText(view.path, 'skill.path') };
222
+ }
223
+ case 'discard': {
224
+ const view = asRecord(value, 'discard');
225
+ return { name: asText(view.name, 'discard.name'), removed: asFlag(view.removed, 'discard.removed') };
226
+ }
227
+ case 'disputes': {
228
+ const view = asRecord(value, 'disputes');
229
+ return {
230
+ disputes: asList(view.disputes, 'disputes.disputes').map((row) => {
231
+ const entry = asRecord(row, 'dispute');
232
+ return {
233
+ id: asText(entry.id, 'dispute.id'),
234
+ scope: asText(entry.scope, 'dispute.scope'),
235
+ action: asText(entry.action, 'dispute.action'),
236
+ title: asText(entry.title, 'dispute.title'),
237
+ reason: asText(entry.reason, 'dispute.reason'),
238
+ before: asText(entry.before, 'dispute.before'),
239
+ after: asText(entry.after, 'dispute.after'),
240
+ };
241
+ }),
242
+ };
243
+ }
244
+ /* v8 ignore next 2 -- the union is exhaustive; kept for the type checker. */
245
+ default:
246
+ throw new TypeError(`dsh-cross-memory: unknown shape ${String(shape)}`);
247
+ }
248
+ };
249
+ return { mode: 'strict', typeSymbol: `${REMOTE_PACKAGE}#${shape}`, create: () => ({ parse }) };
250
+ }
251
+ /**
252
+ * Expand the wire table into registry descriptors.
253
+ * @param data - the wire contract.
254
+ * @param codecOf - shape-to-codec factory (the host validates; the browser does not).
255
+ * @returns the invocation descriptors.
256
+ */
257
+ export function expandInvocations(data, codecOf = hostCodec) {
258
+ return data.map((invocation) => ({
259
+ id: `${REMOTE_PACKAGE}#${REMOTE_NAMESPACE}/${invocation.method}`,
260
+ service: REMOTE_SERVICE,
261
+ namespace: REMOTE_NAMESPACE,
262
+ method: invocation.method,
263
+ invocation: { kind: 'direct' },
264
+ parameters: invocation.parameters.map((parameter) => ({
265
+ name: parameter.name,
266
+ wire: parameter.wire,
267
+ source: 'json',
268
+ codec: codecOf(parameter.shape),
269
+ ...parameter.optional === true ? { acceptsUndefined: true } : {},
270
+ })),
271
+ result: codecOf(invocation.result),
272
+ }));
273
+ }
274
+ /** The host contribution, ready for `ctx.typert.register()`. */
275
+ export const REMOTE_CONTRIBUTION = {
276
+ package: REMOTE_PACKAGE,
277
+ face: 'host',
278
+ schemas: [],
279
+ invocations: expandInvocations(REMOTE_INVOCATION_DATA),
280
+ model: { services: [], events: [], objects: [] },
281
+ };
282
+ /** Project the stored entry onto its wire view. */
283
+ function entryView(entry) {
284
+ return {
285
+ id: entry.id,
286
+ scope: entry.scope,
287
+ kind: entry.kind,
288
+ title: entry.title,
289
+ body: entry.body,
290
+ tags: [...entry.tags],
291
+ appliesTo: entry.appliesTo ?? '',
292
+ sourceSession: entry.sourceSession ?? '',
293
+ createdAt: entry.createdAt,
294
+ updatedAt: entry.updatedAt,
295
+ uses: entry.uses,
296
+ lastUsedAt: entry.lastUsedAt,
297
+ source: entry.source,
298
+ };
299
+ }
300
+ /** Narrow a wire scope string to a scope, or `undefined` for "both". */
301
+ function scopeOf(value) {
302
+ return value === 'global' || value === 'project' ? value : undefined;
303
+ }
304
+ /** Narrow a wire kind string to a kind, or `undefined` for "every kind". */
305
+ function kindOf(value) {
306
+ return MEMORY_KINDS.includes(value) ? value : undefined;
307
+ }
308
+ /** Cap one page so a browser cannot ask for an unbounded read. */
309
+ const MAX_PAGE = 200;
310
+ /**
311
+ * Build the `memories` Remote service.
312
+ *
313
+ * Every method resolves its project root from the slug the browser passed, so a
314
+ * settings page can never reach a scope the store does not already know: an
315
+ * unknown slug is an empty result, never a path the caller supplied.
316
+ *
317
+ * @param host - store access and the harness home.
318
+ * @returns the service object plus its `typertRemote` binding.
319
+ */
320
+ export function createRemoteService(host) {
321
+ const { store, dshHome } = host;
322
+ /** Resolve a project slug to its descriptor, or `undefined`. */
323
+ const projectOf = async (slug) => {
324
+ const wanted = (slug ?? '').trim();
325
+ if (wanted.length === 0)
326
+ return undefined;
327
+ return await store.readProjectDescriptor(wanted);
328
+ };
329
+ /** Load the requested scopes for one project slug. */
330
+ const groupsOf = async (slug, scope) => {
331
+ const wanted = scopeOf(scope);
332
+ const groups = [];
333
+ if (wanted === undefined || wanted === 'global') {
334
+ const target = store.target('global', undefined);
335
+ groups.push({ scope: 'global', label: target.label, entries: await store.list('global', undefined) });
336
+ }
337
+ if (wanted === undefined || wanted === 'project') {
338
+ const project = await projectOf(slug);
339
+ if (project !== undefined) {
340
+ const target = store.target('project', project.root);
341
+ groups.push({ scope: 'project', label: target.label, entries: await store.list('project', project.root) });
342
+ }
343
+ }
344
+ return groups;
345
+ };
346
+ /** Whether one draft is already promoted into the harness skill root. */
347
+ const isPromoted = (name) => existsSync(join(dshHome, 'skills', name, 'SKILL.md'));
348
+ const service = {
349
+ async overview() {
350
+ const drafts = await listDrafts(store.memoriesDir);
351
+ const projects = [];
352
+ for (const slug of await store.listProjects()) {
353
+ const descriptor = await store.readProjectDescriptor(slug);
354
+ if (descriptor === undefined)
355
+ continue;
356
+ projects.push({
357
+ slug,
358
+ name: descriptor.name,
359
+ root: descriptor.root,
360
+ count: (await store.list('project', descriptor.root)).length,
361
+ updatedAt: descriptor.updatedAt,
362
+ });
363
+ }
364
+ return {
365
+ storePath: store.memoriesDir,
366
+ globalCount: (await store.list('global', undefined)).length,
367
+ projects,
368
+ drafts: drafts.map((draft) => ({
369
+ name: draft.name,
370
+ description: draft.description,
371
+ promoted: isPromoted(draft.name),
372
+ })),
373
+ kinds: [...MEMORY_KINDS],
374
+ };
375
+ },
376
+ async list(project, scope, query, kind, limit) {
377
+ const groups = await groupsOf(project, scope);
378
+ const page = Math.min(MAX_PAGE, Math.max(1, Math.trunc(limit ?? 50)));
379
+ const kinds = kindOf(kind);
380
+ const options = { limit: MAX_PAGE, ...kinds === undefined ? {} : { kinds: [kinds] } };
381
+ const text = (query ?? '').trim();
382
+ const hits = text.length > 0
383
+ ? searchMemories(groups, text, options)
384
+ : browseMemories(groups, options);
385
+ return { entries: hits.slice(0, page).map((hit) => entryView(hit.entry)), total: hits.length };
386
+ },
387
+ async add(project, scope, kind, title, body, tags) {
388
+ const target = scopeOf(scope) ?? 'global';
389
+ const descriptor = target === 'project' ? await projectOf(project) : undefined;
390
+ if (target === 'project' && descriptor === undefined) {
391
+ throw new Error('dsh-cross-memory: no project scope matches that slug; a project scope appears once a session has run in that workspace');
392
+ }
393
+ const heading = (title ?? '').trim();
394
+ const text = (body ?? '').trim();
395
+ if (heading.length === 0 || text.length === 0) {
396
+ throw new Error('dsh-cross-memory: a memory needs both a title and a body');
397
+ }
398
+ const keywords = (tags ?? '').split(',').map((tag) => tag.trim()).filter((tag) => tag.length > 0);
399
+ const result = await store.upsert({ scope: target, title: heading, body: text, tags: keywords, kind: toMemoryKind(kind) }, descriptor?.root, 'user');
400
+ return { entry: entryView(result.entry) };
401
+ },
402
+ async forget(project, scope, id) {
403
+ const target = scopeOf(scope) ?? 'global';
404
+ const descriptor = target === 'project' ? await projectOf(project) : undefined;
405
+ if (target === 'project' && descriptor === undefined)
406
+ return { removed: false };
407
+ return { removed: await store.remove(target, descriptor?.root, (id ?? '').trim()) };
408
+ },
409
+ async promoteSkill(name) {
410
+ const wanted = (name ?? '').trim();
411
+ const path = await promote(store.memoriesDir, dshHome, wanted);
412
+ if (path === undefined)
413
+ throw new Error(`dsh-cross-memory: no staged skill draft named ${JSON.stringify(wanted)}`);
414
+ return { name: wanted, path };
415
+ },
416
+ async discardSkill(name) {
417
+ const wanted = (name ?? '').trim();
418
+ return { name: wanted, removed: await discardDraft(store.memoriesDir, wanted) };
419
+ },
420
+ async disputes() {
421
+ if (host.disputes === undefined)
422
+ return { disputes: [] };
423
+ const rows = await host.disputes();
424
+ return {
425
+ disputes: rows.map((row) => ({
426
+ id: row.id,
427
+ scope: row.scope,
428
+ action: row.action,
429
+ title: row.title,
430
+ reason: row.reason,
431
+ // The page shows both versions; a decision about a rewrite is only
432
+ // meaningful next to what it is rewriting.
433
+ before: row.before,
434
+ after: row.after,
435
+ })),
436
+ };
437
+ },
438
+ async resolveDispute(id, decision) {
439
+ const wanted = (id ?? '').trim();
440
+ if (wanted.length === 0)
441
+ return 'No decision id was given.';
442
+ const action = decision === 'reject' ? 'reject' : 'accept';
443
+ if (host.resolveDispute === undefined)
444
+ return 'No decisions are waiting.';
445
+ return await host.resolveDispute(wanted, action) ?? 'That decision is no longer waiting.';
446
+ },
447
+ };
448
+ Object.defineProperty(service, 'typertRemote', {
449
+ configurable: false,
450
+ enumerable: false,
451
+ writable: false,
452
+ value: { service, serviceKey: REMOTE_SERVICE, namespace: REMOTE_NAMESPACE },
453
+ });
454
+ return service;
455
+ }