dsh-continual-evolve 0.10.6 → 0.11.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 (64) hide show
  1. package/README.md +24 -31
  2. package/README.zh.md +17 -21
  3. package/lib/approval.d.ts +2 -1
  4. package/lib/audit-log.d.ts +19 -0
  5. package/lib/audit-log.js +52 -0
  6. package/lib/benchmark-command.d.ts +1 -1
  7. package/lib/benchmark-command.js +1 -1
  8. package/lib/command-util.d.ts +70 -0
  9. package/lib/command-util.js +123 -0
  10. package/lib/command.d.ts +7 -43
  11. package/lib/command.js +24 -458
  12. package/lib/copy.d.ts +4 -41
  13. package/lib/copy.js +0 -102
  14. package/lib/declines.d.ts +2 -0
  15. package/lib/declines.js +4 -0
  16. package/lib/evolve-event.js +2 -1
  17. package/lib/extraction-phase.d.ts +132 -0
  18. package/lib/extraction-phase.js +236 -0
  19. package/lib/index.d.ts +26 -42
  20. package/lib/index.js +19 -33
  21. package/lib/inject.d.ts +13 -119
  22. package/lib/inject.js +12 -260
  23. package/lib/injection-caps.d.ts +4 -0
  24. package/lib/injection-caps.js +4 -0
  25. package/lib/injection-directory.d.ts +29 -0
  26. package/lib/injection-directory.js +78 -0
  27. package/lib/injection-rank.d.ts +85 -0
  28. package/lib/injection-rank.js +161 -0
  29. package/lib/listener.d.ts +45 -0
  30. package/lib/listener.js +199 -0
  31. package/lib/maintenance-commands.d.ts +23 -0
  32. package/lib/maintenance-commands.js +136 -0
  33. package/lib/memory-agent.d.ts +8 -44
  34. package/lib/memory-agent.js +8 -276
  35. package/lib/memory-apply.d.ts +44 -0
  36. package/lib/memory-apply.js +202 -0
  37. package/lib/memory-manifest.d.ts +16 -0
  38. package/lib/memory-manifest.js +70 -0
  39. package/lib/mount-command.js +1 -1
  40. package/lib/notify.d.ts +9 -20
  41. package/lib/notify.js +0 -40
  42. package/lib/plan-command.d.ts +12 -0
  43. package/lib/plan-command.js +38 -0
  44. package/lib/prefix-cache.d.ts +2 -1
  45. package/lib/review-scheduler.d.ts +1 -1
  46. package/lib/review-scheduler.js +2 -10
  47. package/lib/runtime-commands.d.ts +14 -0
  48. package/lib/runtime-commands.js +115 -0
  49. package/lib/service.d.ts +16 -2
  50. package/lib/service.js +74 -1
  51. package/lib/store.d.ts +1 -2
  52. package/lib/store.js +2 -2
  53. package/lib/tool.js +1 -1
  54. package/lib/turn-snapshot.d.ts +7 -1
  55. package/lib/turn-snapshot.js +14 -0
  56. package/lib/types.d.ts +10 -0
  57. package/lib/types.js +6 -0
  58. package/lib/wrapup-candidates.d.ts +127 -0
  59. package/lib/wrapup-candidates.js +161 -0
  60. package/lib/wrapup-proposals.d.ts +101 -0
  61. package/lib/wrapup-proposals.js +281 -0
  62. package/lib/wrapup.d.ts +8 -212
  63. package/lib/wrapup.js +4 -438
  64. package/package.json +1 -1
package/lib/wrapup.js CHANGED
@@ -1,444 +1,10 @@
1
- import { ARCHIVED_AT_KEY, PROMOTED_AT_KEY, PROMOTED_TO_KEY, SOURCE_SEQS_KEY, SOURCE_SESSION_KEY, SOURCED_FROM_KEY, VALENCE_NEGATIVE_KEY, isArchived } from "./types.js";
2
- import { MEMORY_TYPE_KEY, isMemoryType } from "./types.js";
3
- import { extractJsonObject } from "./plan.js";
4
- import { compactText } from "./render.js";
5
1
  import { asLlmSessionId, streamText } from "./llm-text.js";
6
- import { getUsageCount, loadUsage } from "./usage.js";
7
2
  import { createTokenUsageObserver } from "./token-usage.js";
8
3
  import { recordLanguageInstruction, resolveRecordLanguage } from "./record-language.js";
9
- import { recencyScore } from "./inject.js";
10
- import { DEFAULT_PROMOTION_POLICY, mostSimilarGlobalEntry, projectScopedReason, secretLeakReason } from "./promotion.js";
11
- export function candidateKey(kind, id) {
12
- return `${kind}:${id}`;
13
- }
14
- /** Lowercase, punctuation-stripped title used for cheap coverage matching. */
15
- function normalizeKey(value) {
16
- return value.toLowerCase().replace(/[^a-z0-9\u3400-\u9fff]+/g, "").trim();
17
- }
18
- /**
19
- * Deterministic global-coverage check (STRONG signal): the global store
20
- * already covers a topic when it holds a title that normalizes equal to, or
21
- * (beyond a length floor) contains, the candidate's normalized title.
22
- * Archived global entries count too — the topic was already judged
23
- * cross-session; a local duplicate would only re-sediment it.
24
- *
25
- * The bare same-id case is deliberately NOT coverage: ids are slugs derived
26
- * from titles, so a real collision is usually caught by the title check
27
- * below. A same-id entry with a wildly different title is a weak signal — the
28
- * caller routes it through {@link globalHintsFor} for the assessor to judge
29
- * against the actual global title (real case: local `memory` "用户产品愿景与
30
- * 收入需求(本会话)" vs global `memory` "用户画像(持续更新)").
31
- */
32
- export function globalCoverageDetected(globalState, kind, entry) {
33
- const records = globalState.entries[kind];
34
- const title = normalizeKey(entry.title);
35
- if (title.length === 0)
36
- return false;
37
- for (const other of Object.values(records)) {
38
- const otherTitle = normalizeKey(other.title);
39
- if (otherTitle.length === 0)
40
- continue;
41
- if (otherTitle === title)
42
- return true;
43
- if (title.length >= 4 && otherTitle.length >= 4 && (title.includes(otherTitle) || otherTitle.includes(title))) {
44
- return true;
45
- }
46
- }
47
- return false;
48
- }
49
- /**
50
- * The actual global entries that touch the same topic as a local candidate:
51
- * same id (regardless of title — the weak collision signal that is NOT
52
- * coverage on its own), equal normalized title, or title overlap. The raw ids
53
- * and titles let the assessor judge enrichment against real global content
54
- * (does the global copy already hold what the local one adds?) rather than a
55
- * bare boolean. Bounded: a handful of best matches, never the whole store.
56
- */
57
- export function globalHintsFor(globalState, kind, entry) {
58
- const records = globalState.entries[kind];
59
- const title = normalizeKey(entry.title);
60
- const hints = [];
61
- for (const other of Object.values(records)) {
62
- const otherTitle = normalizeKey(other.title);
63
- if (otherTitle.length === 0 && other.id !== entry.id)
64
- continue;
65
- const matches = other.id === entry.id ||
66
- (otherTitle.length > 0 && (otherTitle === title || (title.length >= 4 && otherTitle.length >= 4 && (title.includes(otherTitle) || otherTitle.includes(title)))));
67
- if (matches) {
68
- hints.push({ id: other.id, title: other.title });
69
- }
70
- }
71
- return hints;
72
- }
73
- /**
74
- * The auditable local candidates of a session: every non-archived local
75
- * entry that has not already been promoted (a promoted entry's lifecycle is
76
- * finished — the global copy is the live one). Each carries its
77
- * `coveredGlobally` flag so the assessor never wastes a promote on a topic
78
- * the global store already owns.
79
- */
80
- /** Staleness threshold: entries with recency below this AND zero usage are stale. */
81
- const STALE_RECENCY_THRESHOLD = 0.1;
82
- export function listLocalCandidates(state, globalState, baseDir) {
83
- const usage = baseDir ? loadUsage(baseDir) : undefined;
84
- const now = Date.now();
85
- const candidates = [];
86
- for (const kind of Object.keys(state.entries)) {
87
- for (const entry of Object.values(state.entries[kind])) {
88
- if (entry.scope !== "local")
89
- continue;
90
- if (isArchived(entry))
91
- continue;
92
- if (typeof entry.metadata[PROMOTED_TO_KEY] === "string")
93
- continue;
94
- const injectionCount = usage ? getUsageCount(usage, kind, entry.id) : 0;
95
- const stale = injectionCount === 0 && recencyScore(entry, now) < STALE_RECENCY_THRESHOLD;
96
- const rawNegative = entry.metadata[VALENCE_NEGATIVE_KEY];
97
- const negativeCount = typeof rawNegative === "number" && Number.isFinite(rawNegative) && rawNegative > 0 ? rawNegative : 0;
98
- candidates.push({
99
- kind,
100
- id: entry.id,
101
- title: entry.title,
102
- content: entry.content,
103
- path: entry.path,
104
- version: entry.version,
105
- metadata: entry.metadata,
106
- coveredGlobally: globalCoverageDetected(globalState, kind, entry),
107
- globalHints: globalHintsFor(globalState, kind, entry),
108
- injectionCount,
109
- stale,
110
- negativeCount,
111
- });
112
- }
113
- }
114
- return candidates;
115
- }
116
- /**
117
- * Parse and validate the model's assessment JSON. Defense is mechanical:
118
- * keys outside the candidate list are dropped, verdicts outside the enum
119
- * collapse to "keep", and candidates the model omitted default to "keep" —
120
- * a malformed reply can never change an entry's fate by itself.
121
- *
122
- * Split promotion (verdict "archive" with a `promote` sub-object): the
123
- * sub-object is accepted ONLY on archive verdicts and ONLY when both cleaned
124
- * title and content are non-empty strings — a dropped/malformed sub-object
125
- * silently degrades to a plain archive (the entry is never half-promoted).
126
- */
127
- export function parseWrapupAssessment(text, candidates) {
128
- const value = extractJsonObject(text);
129
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
130
- throw new Error("wrap-up assessment JSON must be an object");
131
- }
132
- const record = value;
133
- const allowed = new Set(candidates.map((candidate) => candidateKey(candidate.kind, candidate.id)));
134
- const items = [];
135
- if (Array.isArray(record["items"])) {
136
- for (const raw of record["items"]) {
137
- if (typeof raw !== "object" || raw === null)
138
- continue;
139
- const item = raw;
140
- const key = typeof item["key"] === "string" ? item["key"] : "";
141
- if (!allowed.has(key))
142
- continue;
143
- const verdict = item["verdict"] === "promote" || item["verdict"] === "archive" ? item["verdict"] : "keep";
144
- const built = { key, verdict, reason: typeof item["reason"] === "string" ? item["reason"] : "" };
145
- if (item["contradicted"] === true) {
146
- built.contradicted = true;
147
- }
148
- if (verdict === "archive" && typeof item["promote"] === "object" && item["promote"] !== null) {
149
- const sub = item["promote"];
150
- const subTitle = typeof sub["title"] === "string" ? sub["title"].trim() : "";
151
- const subContent = typeof sub["content"] === "string" ? sub["content"].trim() : "";
152
- if (subTitle.length > 0 && subContent.length > 0) {
153
- built.promote = { title: subTitle, content: subContent };
154
- }
155
- }
156
- items.push(built);
157
- }
158
- }
159
- for (const candidate of candidates) {
160
- const key = candidateKey(candidate.kind, candidate.id);
161
- if (!items.some((item) => item.key === key)) {
162
- items.push({ key, verdict: "keep", reason: "not mentioned by the assessor" });
163
- }
164
- }
165
- return { items, rationale: typeof record["rationale"] === "string" ? record["rationale"] : "" };
166
- }
167
- /**
168
- * Apply-time deterministic guard: re-check every promote verdict against the
169
- * global store right before it lands. The LLM classification may be stale
170
- * (a gate ran while assessing) or wrong; this ensures a promote never writes
171
- * a duplicate, project-scoped, or too-thin global entry. Pure and unit-tested.
172
- *
173
- * Guards (2026-08-22 promotion policy):
174
- * - audited candidate list + title coverage (pre-existing),
175
- * - project-scoped content markers (absolute paths / session ids) — the
176
- * global store is shared across projects and must stay portable,
177
- * - thin content below the policy floor (framing outweighs the fact),
178
- * - near-duplicate of an existing global entry by content overlap.
179
- */
180
- export function filterPromotable(items, globalState, candidates, policy = DEFAULT_PROMOTION_POLICY) {
181
- const byKey = new Map(candidates.map((candidate) => [candidateKey(candidate.kind, candidate.id), candidate]));
182
- const promotable = [];
183
- const skipped = [];
184
- for (const item of items) {
185
- if (item.verdict !== "promote")
186
- continue;
187
- const candidate = byKey.get(item.key);
188
- if (!candidate) {
189
- skipped.push({ key: item.key, reason: "not in the audited candidate list" });
190
- continue;
191
- }
192
- if (candidate.coveredGlobally || globalCoverageDetected(globalState, candidate.kind, candidate)) {
193
- skipped.push({ key: item.key, reason: "already covered globally" });
194
- continue;
195
- }
196
- if (candidate.kind === "memory" && !isMemoryType(candidate.metadata[MEMORY_TYPE_KEY])) {
197
- skipped.push({
198
- key: item.key,
199
- reason: `memory has no recall type (metadata.${MEMORY_TYPE_KEY} one of user|feedback|project|reference) — update the local entry to classify it first, then promote`,
200
- });
201
- continue;
202
- }
203
- const scoped = projectScopedReason(`${candidate.title}\n${candidate.content}`, policy);
204
- if (scoped) {
205
- skipped.push({ key: item.key, reason: scoped });
206
- continue;
207
- }
208
- // Metadata screens too: wholePromoteProposals copies candidate.metadata
209
- // verbatim into the global entry, so a credential planted in local
210
- // metadata must not ride the promotion path into the shared store
211
- // (review audit 2026-08-28 B2).
212
- const secret = secretLeakReason([candidate.title, candidate.content, JSON.stringify(candidate.metadata ?? {})].join("\n"));
213
- if (secret) {
214
- skipped.push({ key: item.key, reason: secret });
215
- continue;
216
- }
217
- if (candidate.content.length < policy.minPromoteChars) {
218
- skipped.push({
219
- key: item.key,
220
- reason: `too thin to promote (${candidate.content.length} < ${policy.minPromoteChars} chars) — keep local or merge`,
221
- });
222
- continue;
223
- }
224
- const similar = mostSimilarGlobalEntry(globalState, candidate.kind, candidate.title, candidate.content, policy);
225
- if (similar) {
226
- skipped.push({
227
- key: item.key,
228
- reason: `near-duplicate of global ${candidate.kind}:${similar.id} "${similar.title}" (overlap ${similar.score.toFixed(2)}) — update that entry instead`,
229
- });
230
- continue;
231
- }
232
- promotable.push(item);
233
- }
234
- return { promotable, skipped };
235
- }
236
- /**
237
- * The symmetric archive guard. `filterPromotable` is one-directional: it
238
- * stops the model from WRITING duplicate global entries, but nothing stopped
239
- * an unfounded ARCHIVE from hiding content that was actually only local.
240
- * Guard criteria: an archive needs user confirmation when it is NOT covered
241
- * globally AND the entry carries a real distillation source (sourceSeqs /
242
- * sourceSession — i.e. it was distilled from actual user messages, so it
243
- * may hold reusable value). Operational/empty entries archive silently as
244
- * before. Split archives (archive + promote sub-object) skip this check:
245
- * their promotion already crosses a human approval gate, so the archive is
246
- * the completion of an approved action, not a silent burial.
247
- */
248
- export function needsArchiveReview(item, candidate) {
249
- if (item.verdict !== "archive")
250
- return false;
251
- if (item.promote)
252
- return false;
253
- if (candidate.coveredGlobally)
254
- return false;
255
- const seqs = candidate.metadata[SOURCE_SEQS_KEY];
256
- const session = candidate.metadata[SOURCE_SESSION_KEY];
257
- return (Array.isArray(seqs) && seqs.length > 0) || (typeof session === "string" && session.length > 0);
258
- }
259
- /** Partition archive items into silent vs review-required (see needsArchiveReview). */
260
- export function splitArchiveGuards(items, candidates) {
261
- const byKey = new Map(candidates.map((candidate) => [candidateKey(candidate.kind, candidate.id), candidate]));
262
- const silent = [];
263
- const review = [];
264
- for (const item of items) {
265
- if (item.verdict !== "archive")
266
- continue;
267
- const candidate = byKey.get(item.key);
268
- if (candidate && needsArchiveReview(item, candidate)) {
269
- review.push(item);
270
- }
271
- else {
272
- silent.push(item);
273
- }
274
- }
275
- return { silent, review };
276
- }
277
- /**
278
- * Apply-time guard for a split promotion (archive + promote sub-object):
279
- * the cleaned payload must pass the same promotion policy as a whole
280
- * promote — no global coverage duplicate, no project-scoped content, not
281
- * too thin, no near-duplicate global entry. A blocked split is dropped (the
282
- * entry still archives plain) rather than half-promoting a redundancy.
283
- */
284
- export function splitPromoteBlocked(item, globalState, kind, policy = DEFAULT_PROMOTION_POLICY, candidateMetadata) {
285
- if (!item.promote)
286
- return "no split payload";
287
- if (kind === "memory" && !isMemoryType(candidateMetadata?.[MEMORY_TYPE_KEY])) {
288
- return `split promotion needs the source entry classified (metadata.${MEMORY_TYPE_KEY} one of user|feedback|project|reference) — update it first`;
289
- }
290
- if (globalCoverageDetected(globalState, kind, { id: "", title: item.promote.title })) {
291
- return "split promotion duplicates a globally covered topic";
292
- }
293
- const scoped = projectScopedReason(`${item.promote.title}\n${item.promote.content}`, policy);
294
- if (scoped)
295
- return `split promotion is ${scoped}`;
296
- const secret = secretLeakReason([item.promote.title, item.promote.content].join("\n"));
297
- if (secret)
298
- return `split promotion blocked: ${secret}`;
299
- if (item.promote.content.length < policy.minPromoteChars) {
300
- return `split promotion too thin (${item.promote.content.length} < ${policy.minPromoteChars} chars)`;
301
- }
302
- const similar = mostSimilarGlobalEntry(globalState, kind, item.promote.title, item.promote.content, policy);
303
- if (similar) {
304
- return `split promotion near-duplicates global ${kind}:${similar.id} "${similar.title}" (overlap ${similar.score.toFixed(2)})`;
305
- }
306
- return undefined;
307
- }
308
- /**
309
- * Shared proposal builders for a WHOLE promotion — used by both the
310
- * `/evolve wrapup` command and the gate's local-fate dimension so the two
311
- * paths apply IDENTICAL edits (global create + local retirement stamp).
312
- *
313
- * The local stamp is a factory: the `promotedTo` id is only known after the
314
- * global create lands (validation may slugify the id), so the caller applies
315
- * the global proposal first and stamps the local copy with the created id.
316
- */
317
- export function wholePromoteProposals(item, candidate, sessionId) {
318
- const now = new Date().toISOString();
319
- return {
320
- global: {
321
- summary: `wrapup: promote local ${item.key} to the global store`,
322
- rationale: item.reason,
323
- expectedOutcome: `The entry is now visible to every session via the global store (sourcedFromLocal=${sessionId}:${candidate.id}).`,
324
- edits: [
325
- {
326
- action: "create",
327
- kind: candidate.kind,
328
- id: candidate.id,
329
- title: candidate.title,
330
- content: candidate.content,
331
- path: candidate.path,
332
- metadata: {
333
- ...candidate.metadata,
334
- [SOURCED_FROM_KEY]: `${sessionId}:${candidate.id}`,
335
- [PROMOTED_AT_KEY]: now,
336
- },
337
- },
338
- ],
339
- },
340
- localStamp: (createdId) => ({
341
- summary: `wrapup: stamp local ${item.key} as promoted to ${createdId} and retire it from injection`,
342
- rationale: item.reason,
343
- expectedOutcome: `The local copy keeps its data but stops being injected; the global copy is the live one.`,
344
- edits: [
345
- {
346
- action: "update",
347
- kind: candidate.kind,
348
- id: candidate.id,
349
- title: candidate.title,
350
- content: candidate.content,
351
- metadata: {
352
- ...candidate.metadata,
353
- [PROMOTED_TO_KEY]: createdId,
354
- [PROMOTED_AT_KEY]: now,
355
- [ARCHIVED_AT_KEY]: now,
356
- },
357
- },
358
- ],
359
- }),
360
- };
361
- }
362
- /**
363
- * Shared proposal builders for a SPLIT promotion (A-form): archive a mixed
364
- * local entry but promote ONLY the cleaned durable part the model extracted.
365
- * Same usage contract as {@link wholePromoteProposals}: apply the global
366
- * create, then stamp the original local entry with the created id.
367
- */
368
- export function splitPromoteProposals(item, candidate, sessionId) {
369
- if (!item.promote)
370
- throw new Error("split promote proposals require a promote payload");
371
- const now = new Date().toISOString();
372
- return {
373
- global: {
374
- summary: `wrapup: split — promote cleaned part of ${item.key} to the global store`,
375
- rationale: item.reason,
376
- expectedOutcome: `Only the durable part becomes visible globally; the snapshot half stays archived with the original.`,
377
- edits: [
378
- {
379
- action: "create",
380
- kind: candidate.kind,
381
- id: candidate.id,
382
- title: item.promote.title,
383
- content: item.promote.content,
384
- path: candidate.path,
385
- metadata: {
386
- ...candidate.metadata,
387
- [SOURCED_FROM_KEY]: `${sessionId}:${candidate.id}`,
388
- [PROMOTED_AT_KEY]: now,
389
- },
390
- },
391
- ],
392
- },
393
- localStamp: (createdId) => ({
394
- summary: `wrapup: split — archive original ${item.key}, stamped as promoted to ${createdId}`,
395
- rationale: item.reason,
396
- expectedOutcome: `The original leaves injection (data kept, restorable); the cleaned global copy is the live one.`,
397
- edits: [
398
- {
399
- action: "update",
400
- kind: candidate.kind,
401
- id: candidate.id,
402
- title: candidate.title,
403
- content: candidate.content,
404
- metadata: {
405
- ...candidate.metadata,
406
- [PROMOTED_TO_KEY]: createdId,
407
- [PROMOTED_AT_KEY]: now,
408
- [ARCHIVED_AT_KEY]: now,
409
- },
410
- },
411
- ],
412
- }),
413
- };
414
- }
415
- /**
416
- * Valence stamp (P1 效价反馈): a keep-verdict item the assessor marked
417
- * contradicted gets its negative-valence counter bumped — the entry stays
418
- * live but sinks in injection ranking and the next assessment prefers
419
- * archiving it. Promotes pass untouched (the human-approved global copy is
420
- * the live truth); archives don't need the stamp (they already leave
421
- * injection). Undefined when nothing applies.
422
- */
423
- export function valenceStampProposal(item, candidate) {
424
- if (item.verdict !== "keep" || !item.contradicted)
425
- return undefined;
426
- return {
427
- summary: `wrapup: stamp contradicted valence on ${item.key} (${candidate.negativeCount + 1})`,
428
- rationale: item.reason,
429
- expectedOutcome: "The entry's negative-valence counter rises; injection downranks it and the next assessment prefers archiving it.",
430
- edits: [
431
- {
432
- action: "update",
433
- kind: candidate.kind,
434
- id: candidate.id,
435
- title: candidate.title,
436
- content: candidate.content,
437
- metadata: { ...candidate.metadata, [VALENCE_NEGATIVE_KEY]: candidate.negativeCount + 1 },
438
- },
439
- ],
440
- };
441
- }
4
+ import { compactText } from "./render.js";
5
+ import { candidateKey, parseWrapupAssessment } from "./wrapup-candidates.js";
6
+ export * from "./wrapup-candidates.js";
7
+ export * from "./wrapup-proposals.js";
442
8
  export const WRAPUP_ASSESS_SYSTEM_PROMPT = `You are the /evolve session wrap-up assessor.
443
9
  A session is ending and its local harness entries need a fate. Classify each
444
10
  listed entry exactly once:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-continual-evolve",
3
- "version": "0.10.6",
3
+ "version": "0.11.0",
4
4
  "description": "Continual self-evolution plugin for DeepSeek Harness: versioned, auditable, rollback-safe harness state (prompt notes, memories, skills, subagent specs) refined from session trajectories.",
5
5
  "license": "MIT",
6
6
  "repository": {