dsh-memory-bond 0.0.0-stage → 1.0.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 (126) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/LICENSE +21 -0
  3. package/README.md +181 -2
  4. package/README.zh-CN.md +180 -0
  5. package/SECURITY.md +29 -0
  6. package/client/memory-icon.svg +21 -0
  7. package/cordis.patch.yml +3 -0
  8. package/dist/audit.js +286 -0
  9. package/dist/automatic.js +16 -0
  10. package/dist/cache.js +40 -0
  11. package/dist/client.js +2035 -0
  12. package/dist/command-scope.js +123 -0
  13. package/dist/commands.js +156 -0
  14. package/dist/consolidation-store.js +357 -0
  15. package/dist/consolidation.js +182 -0
  16. package/dist/domain.js +245 -0
  17. package/dist/encoder.js +138 -0
  18. package/dist/export-files.js +161 -0
  19. package/dist/file-import-store.js +120 -0
  20. package/dist/file-import.js +265 -0
  21. package/dist/foreground-attempts.js +91 -0
  22. package/dist/guidance.js +242 -0
  23. package/dist/host.js +19 -0
  24. package/dist/index-policy.js +191 -0
  25. package/dist/index.js +349 -0
  26. package/dist/knowledge-hooks.js +398 -0
  27. package/dist/knowledge.js +359 -0
  28. package/dist/maintenance-episodes.js +73 -0
  29. package/dist/maintenance-import.js +92 -0
  30. package/dist/maintenance-store.js +738 -0
  31. package/dist/maintenance.js +376 -0
  32. package/dist/model-review.js +16 -0
  33. package/dist/personal.js +60 -0
  34. package/dist/policy.js +382 -0
  35. package/dist/prompt-copy.js +93 -0
  36. package/dist/proposal-receipts.js +70 -0
  37. package/dist/proposal-review.js +656 -0
  38. package/dist/propose-contract.js +38 -0
  39. package/dist/question-answers.js +176 -0
  40. package/dist/retrieval.js +126 -0
  41. package/dist/scope-narrowing.js +49 -0
  42. package/dist/search-index.js +164 -0
  43. package/dist/search-words.js +5 -0
  44. package/dist/semantic.js +157 -0
  45. package/dist/service.js +1473 -0
  46. package/dist/sharing.js +289 -0
  47. package/dist/sources.js +149 -0
  48. package/dist/store.js +1578 -0
  49. package/dist/tool-experience.js +87 -0
  50. package/dist/turn-review-store.js +159 -0
  51. package/dist/turn-review.js +459 -0
  52. package/dist/ui.js +346 -0
  53. package/dist/vector-index.js +232 -0
  54. package/docs/architecture.zh-CN.md +52 -0
  55. package/docs/benchmark.zh-CN.md +60 -0
  56. package/docs/benchmarks/repository-tasks.json +5355 -0
  57. package/docs/images/bond-memory-principle-en.svg +644 -0
  58. package/docs/images/bond-memory-principle-zh.svg +644 -0
  59. package/docs/images/bond-memory-principle.drawio +644 -0
  60. package/docs/images/memory-settings-en.png +0 -0
  61. package/docs/images/memory-settings-zh.png +0 -0
  62. package/docs/images/memory-turn-recalled-en.png +0 -0
  63. package/docs/images/memory-turn-recalled-zh.png +0 -0
  64. package/docs/images/memory-turn-saved-en.png +0 -0
  65. package/docs/images/memory-turn-saved-zh.png +0 -0
  66. package/docs/media/memory-bond-name-en.gif +0 -0
  67. package/docs/media/memory-bond-name-en.mp4 +0 -0
  68. package/docs/media/memory-bond-name-zh.gif +0 -0
  69. package/docs/media/memory-bond-name-zh.mp4 +0 -0
  70. package/locale/detail.en.md +13 -0
  71. package/locale/detail.zh.md +13 -0
  72. package/locale/en.json +345 -0
  73. package/locale/zh.json +345 -0
  74. package/native/embeddings/NOTICE.md +12 -0
  75. package/package.json +117 -4
  76. package/scripts/benchmark-bond.mjs +72 -0
  77. package/scripts/benchmark-conditions.mjs +144 -0
  78. package/scripts/benchmark-example.mjs +37 -0
  79. package/scripts/benchmark-scale.mjs +119 -0
  80. package/scripts/benchmark-scenarios.mjs +57 -0
  81. package/scripts/benchmark-tasks.mjs +244 -0
  82. package/src/audit.ts +286 -0
  83. package/src/automatic.ts +21 -0
  84. package/src/cache.ts +38 -0
  85. package/src/command-scope.ts +125 -0
  86. package/src/commands.ts +159 -0
  87. package/src/consolidation-store.ts +350 -0
  88. package/src/consolidation.ts +176 -0
  89. package/src/domain.ts +254 -0
  90. package/src/encoder.ts +142 -0
  91. package/src/export-files.ts +178 -0
  92. package/src/file-import-store.ts +139 -0
  93. package/src/file-import.ts +256 -0
  94. package/src/foreground-attempts.ts +97 -0
  95. package/src/guidance.ts +265 -0
  96. package/src/host.ts +119 -0
  97. package/src/index-policy.ts +231 -0
  98. package/src/index.ts +333 -0
  99. package/src/knowledge-hooks.ts +456 -0
  100. package/src/knowledge.ts +432 -0
  101. package/src/maintenance-episodes.ts +76 -0
  102. package/src/maintenance-import.ts +107 -0
  103. package/src/maintenance-store.ts +868 -0
  104. package/src/maintenance.ts +380 -0
  105. package/src/model-review.ts +27 -0
  106. package/src/personal.ts +68 -0
  107. package/src/policy.ts +508 -0
  108. package/src/prompt-copy.ts +99 -0
  109. package/src/proposal-receipts.ts +59 -0
  110. package/src/proposal-review.ts +614 -0
  111. package/src/propose-contract.ts +39 -0
  112. package/src/question-answers.ts +181 -0
  113. package/src/retrieval.ts +133 -0
  114. package/src/scope-narrowing.ts +58 -0
  115. package/src/search-index.ts +177 -0
  116. package/src/search-words.ts +6 -0
  117. package/src/semantic.ts +178 -0
  118. package/src/service.ts +1488 -0
  119. package/src/sharing.ts +349 -0
  120. package/src/sources.ts +150 -0
  121. package/src/store.ts +1829 -0
  122. package/src/tool-experience.ts +93 -0
  123. package/src/turn-review-store.ts +158 -0
  124. package/src/turn-review.ts +418 -0
  125. package/src/ui.ts +385 -0
  126. package/src/vector-index.ts +228 -0
@@ -0,0 +1,178 @@
1
+ import { skipsAutomaticMemory } from './automatic.ts'
2
+ import { isDeepStrictEqual } from 'node:util'
3
+ import { normalizeLabel, semanticSlot, validLabel } from './personal.ts'
4
+ import type { ProposalFields } from './policy.ts'
5
+ import { StoreError, type DraftItem, type Ref, type Rule } from './store.ts'
6
+
7
+ export interface SemanticCandidate extends ProposalFields {
8
+ scope: 'global' | 'project'
9
+ kind: 'fact' | 'preference' | 'rule'
10
+ topic: string
11
+ subject?: string
12
+ usage: 'proactive' | 'on_demand'
13
+ sensitive: boolean
14
+ keywords: string[]
15
+ replaces?: Ref
16
+ }
17
+
18
+ const object = (v: unknown): v is Record<string, unknown> => !!v && typeof v === 'object' && !Array.isArray(v)
19
+ const semanticFields = new Set([
20
+ 'subject', 'usage', 'scope', 'kind', 'topic', 'sensitive', 'correction', 'keywords', 'replaces',
21
+ 'action', 'when', 'exceptions', 'paths', 'user_quote', 'conflicts_with_repo_guidance',
22
+ ])
23
+
24
+ export function candidateRows(input: unknown): unknown[] {
25
+ if (!object(input) || Object.keys(input).length !== 1 ||
26
+ !Array.isArray(input.items) || !input.items.length || input.items.length > 32)
27
+ throw new StoreError('invalid_input')
28
+ return input.items
29
+ }
30
+
31
+ export function parseCandidate(row: unknown): SemanticCandidate {
32
+ if (!object(row)) throw new StoreError('invalid_input')
33
+ if (Object.keys(row).some(k => !semanticFields.has(k)) ||
34
+ (row.scope !== 'global' && row.scope !== 'project') ||
35
+ (row.kind !== 'fact' && row.kind !== 'preference' && row.kind !== 'rule') ||
36
+ !validLabel(row.topic) || (row.subject !== undefined && !validLabel(row.subject)) ||
37
+ (row.usage !== undefined && row.usage !== 'proactive' && row.usage !== 'on_demand') ||
38
+ typeof row.user_quote !== 'string' || !row.user_quote.trim() || row.user_quote.length > 480 ||
39
+ typeof row.action !== 'string' || !row.action.trim() || row.action.length > 480 ||
40
+ [row.sensitive, row.correction, row.conflicts_with_repo_guidance].some(v => v !== undefined && typeof v !== 'boolean') ||
41
+ [row.when, row.exceptions].some(v => v !== undefined && (typeof v !== 'string' || v.length > 480)) ||
42
+ (row.paths !== undefined && (!Array.isArray(row.paths) || row.paths.length > 64 || row.paths.some(v => typeof v !== 'string' || v.length > 1024))) ||
43
+ (row.keywords !== undefined && (!Array.isArray(row.keywords) || row.keywords.length > 8 ||
44
+ row.keywords.some(v => typeof v !== 'string' || !v.trim() || v.length > 40 || /[\p{Cc}]/u.test(v)))) ||
45
+ (row.replaces !== undefined && (!object(row.replaces) || Object.keys(row.replaces).some(key => key !== 'id' && key !== 'rev') ||
46
+ typeof row.replaces.id !== 'string' || !row.replaces.id.trim() ||
47
+ !Number.isSafeInteger(row.replaces.rev) || Number(row.replaces.rev) < 1)))
48
+ throw new StoreError('invalid_input')
49
+ return {
50
+ ...(row.subject !== undefined && { subject: row.subject.trim() }),
51
+ usage: row.kind !== 'fact' && row.usage === 'proactive' && !row.sensitive ? 'proactive' : 'on_demand',
52
+ scope: row.scope, kind: row.kind, topic: row.topic.trim(), user_quote: row.user_quote, action: row.action.trim(),
53
+ when: (row.when as string | undefined) ?? '', exceptions: (row.exceptions as string | undefined) ?? '',
54
+ paths: (row.paths as string[] | undefined) ?? [],
55
+ // Older models included this flag. Repository decisions come from the verified instruction snapshot.
56
+ conflicts_with_repo_guidance: false,
57
+ sensitive: row.sensitive === true,
58
+ keywords: [...new Set((row.keywords as string[] | undefined)?.map(k => k.trim()) ?? [])],
59
+ ...(row.replaces ? { replaces: { id: row.replaces.id as string, rev: Number(row.replaces.rev) } } : {}),
60
+ }
61
+ }
62
+
63
+ export function parseCandidates(input: unknown) {
64
+ return candidateRows(input).map(parseCandidate)
65
+ }
66
+
67
+ export function skipsCandidate(row: SemanticCandidate) {
68
+ return skipsAutomaticMemory([row.user_quote, row.action, row.when, row.exceptions, row.topic, row.subject, ...row.keywords, ...row.paths].join(' '))
69
+ }
70
+
71
+ export function semanticDraft(row: SemanticCandidate, draft: DraftItem, previous?: Rule): DraftItem {
72
+ const sensitive = row.sensitive || previous?.sensitive === true
73
+ return {
74
+ ...draft, kind: row.kind, semanticTopic: previous?.semanticTopic ?? row.topic,
75
+ ...(row.subject && { subject: previous?.subject ?? row.subject }),
76
+ slot: previous?.slot ?? semanticSlot(row.scope, row.topic, row.subject, draft),
77
+ keywords: row.keywords, sensitive,
78
+ usage: sensitive ? 'on_demand' : previous?.usageOverride ? previous.usage : row.usage,
79
+ ...(previous?.usageOverride && { usageOverride: true }),
80
+ ...(row.scope === 'global' && { hard: undefined, guidance: undefined }),
81
+ }
82
+ }
83
+
84
+ // The model supplies the applicable occasion. Only independent, evidenced scopes may
85
+ // coexist under one topic; an omitted qualifier cannot establish a separate context.
86
+ export function separateContexts(a: DraftItem, b: DraftItem) {
87
+ const named = (item: DraftItem) => item.when && item.user_quote.includes(item.when)
88
+ if (a.when !== b.when && named(a) && named(b)) return true
89
+ return a.paths.length > 0 && b.paths.length > 0 && a.paths.every(p => b.paths.every(q =>
90
+ p !== '.' && q !== '.' && p !== q && !p.startsWith(q + '/') && !q.startsWith(p + '/')))
91
+ }
92
+
93
+ // Normalize semantically equivalent wording only after the reviewer saw both
94
+ // sets of qualifiers. Adding or dropping a condition still reaches the store as
95
+ // a scope change. Paths keep their filesystem-based validation in prepareItems.
96
+ export function reviewedQualifiers(row: Pick<SemanticCandidate, 'when' | 'exceptions'>,
97
+ target: { when?: string; exceptions?: string } | undefined, equivalent: unknown) {
98
+ const { when, exceptions } = row
99
+ if (!target || equivalent !== true) return { when, exceptions }
100
+ return {
101
+ when: when && target.when ? target.when : when,
102
+ exceptions: exceptions && target.exceptions ? target.exceptions : exceptions,
103
+ }
104
+ }
105
+
106
+ const sameContext = (a: DraftItem, b: DraftItem) =>
107
+ a.when === b.when && a.exceptions === b.exceptions && isDeepStrictEqual(a.paths, b.paths)
108
+
109
+ // A newly assessed action can revise a confirmed action under the same repository authority.
110
+ function sameGuidanceForRevision(previous: unknown, next: unknown) {
111
+ if (isDeepStrictEqual(previous, next)) return true
112
+ if (!object(previous) || !object(next) || previous.status !== 'clear' || next.status !== 'clear') return false
113
+ const { assessmentKey: oldActionKey, ...oldAuthority } = previous
114
+ const { assessmentKey: newActionKey, ...newAuthority } = next
115
+ return typeof newActionKey === 'string' && isDeepStrictEqual(oldAuthority, newAuthority)
116
+ }
117
+
118
+ export function sameSemanticContent(a: DraftItem, b: DraftItem) {
119
+ return sameContext(a, b) && a.action === b.action && a.kind === b.kind &&
120
+ (a.usage ?? 'on_demand') === (b.usage ?? 'on_demand') && Boolean(a.sensitive) === Boolean(b.sensitive) &&
121
+ isDeepStrictEqual(a.hard, b.hard) && isDeepStrictEqual(a.guidance, b.guidance) &&
122
+ isDeepStrictEqual(a.localException, b.localException)
123
+ }
124
+
125
+ /** An ID selects an existing entry inside its authorized domain. Supplied labels
126
+ * can confirm its identity but cannot redirect the reference to a different topic. */
127
+ export function targetMismatch(row: SemanticCandidate, target: Rule) {
128
+ if (!target.semanticTopic || ![target.semanticTopic, ...(target.keywords ?? [])]
129
+ .some(label => normalizeLabel(label) === normalizeLabel(row.topic))) return 'topic_mismatch'
130
+ if (row.subject !== undefined && normalizeLabel(row.subject) !== normalizeLabel(target.subject ?? '')) return 'subject_mismatch'
131
+ if (target.kind !== row.kind) return 'kind_mismatch'
132
+ }
133
+
134
+ type SemanticPlan = { item: DraftItem; previous?: Rule } | { reason: string }
135
+
136
+ export function planSemantic(row: SemanticCandidate, prepared: DraftItem, matches: Rule[],
137
+ target: Rule | undefined, background: boolean, hasVerifiedSelection = false): SemanticPlan {
138
+ const applicable = matches.filter(head => sameContext(head, prepared))
139
+ if (target && !sameContext(target, prepared)) return { reason: 'context_mismatch' }
140
+ if (applicable.length > 1) return { reason: 'ambiguous_topic' }
141
+ const previous = target ?? applicable[0]
142
+ if (!previous && matches.some(head => !separateContexts(head, prepared))) return { reason: 'replacement_required' }
143
+ const canonical = previous ?? matches[0]
144
+ const item = semanticDraft(canonical ? { ...row, topic: canonical.semanticTopic!, subject: canonical.subject } : row,
145
+ prepared, previous)
146
+
147
+ if (previous && !row.replaces && !item.sensitive) item.usage = previous.usage
148
+ if (previous && sameSemanticContent(previous, item)) {
149
+ // Identical observations preserve both the existing revision and its evidence.
150
+ item.keywords = previous.keywords
151
+ return { item, previous }
152
+ }
153
+ if (previous && !target) return { reason: 'replacement_required' }
154
+ // This is source integrity, not an entailment test. The semantic extractor
155
+ // selects the asserted current value, speaker and qualifiers from the full input.
156
+ if (!row.user_quote.includes(row.action) && !hasVerifiedSelection) return { reason: 'insufficient_evidence' }
157
+
158
+ if (previous && background && (previous.channel !== 'automatic' || previous.pinned ||
159
+ previous.quoteType !== 'user_quote' || previous.usageOverride || previous.localException ||
160
+ (previous.usage ?? 'on_demand') !== (item.usage ?? 'on_demand') ||
161
+ !sameGuidanceForRevision(previous.guidance, item.guidance) ||
162
+ !isDeepStrictEqual((item.hard as {branch?: string} | undefined)?.branch,
163
+ (previous.hard as {branch?: string} | undefined)?.branch)))
164
+ return { reason: 'protected_revision' }
165
+
166
+ if (previous) {
167
+ // Retain the topic's aliases across updates. Avoid indexing the obsolete value.
168
+ item.keywords = [...new Set([...(previous.keywords ?? []).filter(k => !previous.action.includes(k) || item.action.includes(k)),
169
+ ...row.keywords])].slice(0, 8)
170
+ } else if (canonical) {
171
+ item.keywords = [...new Set([...(canonical.keywords ?? []), ...row.keywords])].slice(0, 8)
172
+ if (matches.some(head => head.sensitive)) {
173
+ item.sensitive = true
174
+ item.usage = 'on_demand'
175
+ }
176
+ }
177
+ return { item, previous }
178
+ }