toon-memory 4.3.2 → 4.3.3
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.
- package/README.md +17 -11
- package/dist/cli/capture.js +3 -0
- package/dist/cli/setup.js +326 -134
- package/mcp/server.js +546 -125
- package/package.json +2 -2
- package/skills/toon-memory.md +3 -0
- package/src/cli/agents.ts +67 -0
- package/src/cli/commands.ts +54 -15
- package/src/cli/config-writer.ts +75 -10
- package/src/cli/hooks.ts +1 -1
- package/src/cli/types.ts +2 -2
- package/src/lib/evidence.ts +118 -0
- package/src/lib/graph.ts +12 -2
- package/src/lib/quality.ts +13 -3
- package/src/mcp/config.ts +10 -0
- package/src/mcp/consolidation.ts +65 -0
- package/src/mcp/tools.ts +164 -7
- package/src/mcp/vault.ts +176 -0
- package/src/viewer/html.ts +126 -80
- package/src/viewer/index.ts +27 -27
- package/src/viewer/logo-isologo.svg +16 -0
- package/src/viewer/logo.svg +49 -0
- package/src/viewer/types.ts +5 -5
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "toon-memory",
|
|
3
3
|
"mcpName": "io.github.LuiggiVal08/toon-memory",
|
|
4
|
-
"version": "4.3.
|
|
5
|
-
"description": "The Continuity Layer for AI Agents — an MCP memory server that preserves project knowledge, decisions, and conventions across sessions so every session starts where the last one ended. Offline, token-efficient, no LLM, no server; works with
|
|
4
|
+
"version": "4.3.3",
|
|
5
|
+
"description": "The Continuity Layer for AI Agents — an MCP memory server that preserves project knowledge, decisions, and conventions across sessions so every session starts where the last one ended. Offline, token-efficient, no LLM, no server; works with 20+ agents.",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"bin": {
|
|
8
8
|
"toon-memory": "./bin/toon-memory.js"
|
package/skills/toon-memory.md
CHANGED
|
@@ -54,6 +54,9 @@ A persistent memory system for AI coding agents. It remembers decisions, pattern
|
|
|
54
54
|
| `memory_merge_sessions` | Merge observations across parallel sessions |
|
|
55
55
|
| `memory_export_gist` | Export memory to a private GitHub Gist |
|
|
56
56
|
| `memory_import_gist` | Import memory from a GitHub Gist |
|
|
57
|
+
| `memory_secret` | Store/retrieve secrets in the encrypted vault (`secrets.toon`, AES-256-GCM) — store/get/list/forget |
|
|
58
|
+
| `memory_export_global` | Write project memory to the global file (`~/.toon-memory/memory/global.toon`) |
|
|
59
|
+
| `memory_import_global` | Merge cross-project conventions from the global file (one-shot, deterministic) |
|
|
57
60
|
| `memory_graph_path` | BFS shortest path between two entries |
|
|
58
61
|
| `memory_visualize` | Open interactive memory graph viewer |
|
|
59
62
|
| `memory_pin` | Pin entry with priority 1-5 for top recall placement |
|
package/src/cli/agents.ts
CHANGED
|
@@ -184,5 +184,72 @@ export function detectAgents(): Agent[] {
|
|
|
184
184
|
needsInstructions: false
|
|
185
185
|
})
|
|
186
186
|
|
|
187
|
+
agents.push({
|
|
188
|
+
name: "qwen",
|
|
189
|
+
global: join(HOME, ".qwen", "settings.json"),
|
|
190
|
+
local: join(projectRoot, ".qwen", "settings.json"),
|
|
191
|
+
mcpKey: "mcpServers",
|
|
192
|
+
format: "json",
|
|
193
|
+
needsHooks: true,
|
|
194
|
+
needsInstructions: true,
|
|
195
|
+
instructionFile: join(projectRoot, ".qwen", "AGENTS.md"),
|
|
196
|
+
captureJson: ["PostToolUse", "Stop"]
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
agents.push({
|
|
200
|
+
name: "kimi",
|
|
201
|
+
global: join(HOME, ".kimi", "mcp.json"),
|
|
202
|
+
mcpKey: "mcpServers",
|
|
203
|
+
format: "json",
|
|
204
|
+
needsHooks: false,
|
|
205
|
+
needsInstructions: false
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
agents.push({
|
|
209
|
+
name: "goose",
|
|
210
|
+
global: join(HOME, ".config", "goose", "config.yaml"),
|
|
211
|
+
mcpKey: "extensions",
|
|
212
|
+
format: "yaml",
|
|
213
|
+
needsHooks: false,
|
|
214
|
+
needsInstructions: false
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
agents.push({
|
|
218
|
+
name: "junie",
|
|
219
|
+
local: join(projectRoot, ".junie", "mcp", "mcp.json"),
|
|
220
|
+
mcpKey: "mcpServers",
|
|
221
|
+
format: "json",
|
|
222
|
+
needsHooks: false,
|
|
223
|
+
needsInstructions: false
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
agents.push({
|
|
227
|
+
name: "amp",
|
|
228
|
+
global: join(HOME, ".config", "amp", "settings.json"),
|
|
229
|
+
mcpKey: "amp.mcpServers",
|
|
230
|
+
format: "json",
|
|
231
|
+
needsHooks: false,
|
|
232
|
+
needsInstructions: false
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
agents.push({
|
|
236
|
+
name: "grok",
|
|
237
|
+
global: join(HOME, ".grok", "config.toml"),
|
|
238
|
+
local: join(projectRoot, ".grok", "config.toml"),
|
|
239
|
+
mcpKey: "mcp_servers",
|
|
240
|
+
format: "toml",
|
|
241
|
+
needsHooks: false,
|
|
242
|
+
needsInstructions: false
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
agents.push({
|
|
246
|
+
name: "trae",
|
|
247
|
+
local: join(projectRoot, ".trae", "mcp.json"),
|
|
248
|
+
mcpKey: "mcpServers",
|
|
249
|
+
format: "json",
|
|
250
|
+
needsHooks: false,
|
|
251
|
+
needsInstructions: false
|
|
252
|
+
})
|
|
253
|
+
|
|
187
254
|
return agents
|
|
188
255
|
}
|
package/src/cli/commands.ts
CHANGED
|
@@ -138,9 +138,11 @@ export function uninstall(): void {
|
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
const mcpKey = agent.mcpKey || "mcpServers"
|
|
141
|
+
const servers = config[mcpKey] ??
|
|
142
|
+
mcpKey.split(".").reduce((acc: any, k: string) => acc?.[k], config)
|
|
141
143
|
|
|
142
|
-
if (
|
|
143
|
-
delete
|
|
144
|
+
if (servers?.["toon-memory"]) {
|
|
145
|
+
delete servers["toon-memory"]
|
|
144
146
|
writeFileSync(configPath, JSON.stringify(config, null, 2))
|
|
145
147
|
console.log(` Removed MCP from ${agent.name} (${configPath})`)
|
|
146
148
|
}
|
|
@@ -148,13 +150,37 @@ export function uninstall(): void {
|
|
|
148
150
|
}
|
|
149
151
|
}
|
|
150
152
|
|
|
151
|
-
if (agent.format === "toml"
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
153
|
+
if (agent.format === "toml") {
|
|
154
|
+
const configs = [agent.global, agent.local].filter(Boolean) as string[]
|
|
155
|
+
const table = agent.mcpKey || "mcpServers"
|
|
156
|
+
|
|
157
|
+
for (const configPath of configs) {
|
|
158
|
+
if (!existsSync(configPath)) continue
|
|
159
|
+
try {
|
|
160
|
+
let content = readFileSync(configPath, "utf-8")
|
|
161
|
+
content = content.replace(new RegExp(`\\[${table}\\.toon-memory\\][\\s\\S]*?(?=\\n\\[|$)`), "").trim() + "\n"
|
|
162
|
+
writeFileSync(configPath, content)
|
|
163
|
+
console.log(` Removed MCP from ${agent.name} (${configPath})`)
|
|
164
|
+
} catch {}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (agent.format === "yaml") {
|
|
169
|
+
const configs = [agent.global, agent.local].filter(Boolean) as string[]
|
|
170
|
+
|
|
171
|
+
for (const configPath of configs) {
|
|
172
|
+
if (!existsSync(configPath)) continue
|
|
173
|
+
try {
|
|
174
|
+
let content = readFileSync(configPath, "utf-8")
|
|
175
|
+
if (!content.includes("toon-memory")) continue
|
|
176
|
+
content = content
|
|
177
|
+
.replace(/^extensions:\s*$/m, "")
|
|
178
|
+
.replace(/^ toon-memory:\n(?:(?:^ .*\n?)|(?:^ -.*\n?)|(?:^ \[.*\n?))*/gm, "")
|
|
179
|
+
.trim()
|
|
180
|
+
writeFileSync(configPath, content + "\n")
|
|
181
|
+
console.log(` Removed MCP from ${agent.name} (${configPath})`)
|
|
182
|
+
} catch {}
|
|
183
|
+
}
|
|
158
184
|
}
|
|
159
185
|
}
|
|
160
186
|
|
|
@@ -267,16 +293,29 @@ export function status(): void {
|
|
|
267
293
|
config = JSON.parse(readFileSync(configPath, "utf-8"))
|
|
268
294
|
}
|
|
269
295
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
296
|
+
const servers = config[agent.mcpKey] ??
|
|
297
|
+
agent.mcpKey.split(".").reduce((acc: any, k: string) => acc?.[k], config)
|
|
298
|
+
if (servers?.["toon-memory"]) configured = true
|
|
273
299
|
} catch {}
|
|
274
300
|
}
|
|
275
301
|
}
|
|
276
302
|
|
|
277
|
-
if (agent.format === "toml"
|
|
278
|
-
const
|
|
279
|
-
|
|
303
|
+
if (agent.format === "toml") {
|
|
304
|
+
const configs = [agent.global, agent.local].filter(Boolean) as string[]
|
|
305
|
+
for (const configPath of configs) {
|
|
306
|
+
if (!existsSync(configPath)) continue
|
|
307
|
+
const content = readFileSync(configPath, "utf-8")
|
|
308
|
+
if (content.includes("toon-memory")) configured = true
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (agent.format === "yaml") {
|
|
313
|
+
const configs = [agent.global, agent.local].filter(Boolean) as string[]
|
|
314
|
+
for (const configPath of configs) {
|
|
315
|
+
if (!existsSync(configPath)) continue
|
|
316
|
+
const content = readFileSync(configPath, "utf-8")
|
|
317
|
+
if (content.includes("toon-memory")) configured = true
|
|
318
|
+
}
|
|
280
319
|
}
|
|
281
320
|
|
|
282
321
|
const hasInstructions = agent.instructionFile ? existsSync(agent.instructionFile) : false
|
package/src/cli/config-writer.ts
CHANGED
|
@@ -18,6 +18,14 @@ function loadJSON(file: string): Record<string, any> | null {
|
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
/** Navigate (or create) a nested object path like "amp.mcpServers". */
|
|
22
|
+
function nested(obj: Record<string, any>, key: string): Record<string, any> {
|
|
23
|
+
return key.split(".").reduce((acc: Record<string, any>, part) => {
|
|
24
|
+
if (typeof acc[part] !== "object" || acc[part] === null) acc[part] = {}
|
|
25
|
+
return acc[part]
|
|
26
|
+
}, obj)
|
|
27
|
+
}
|
|
28
|
+
|
|
21
29
|
/**
|
|
22
30
|
* Install MCP server configuration for a JSON-format agent.
|
|
23
31
|
*/
|
|
@@ -39,21 +47,21 @@ export function installJSONConfig(agent: Agent, scope: string): void {
|
|
|
39
47
|
}
|
|
40
48
|
|
|
41
49
|
const mcpKey = agent.mcpKey || "mcpServers"
|
|
42
|
-
|
|
50
|
+
const servers = nested(config, mcpKey)
|
|
43
51
|
|
|
44
52
|
if (agent.name === "opencode") {
|
|
45
|
-
|
|
53
|
+
servers["toon-memory"] = {
|
|
46
54
|
enabled: true,
|
|
47
55
|
type: "local",
|
|
48
56
|
command: ["npx", "-y", "toon-memory", "mcp"]
|
|
49
57
|
}
|
|
50
58
|
} else if (agent.name === "kilocode") {
|
|
51
|
-
|
|
59
|
+
servers["toon-memory"] = {
|
|
52
60
|
type: "local",
|
|
53
61
|
command: ["npx", "-y", "toon-memory", "mcp"]
|
|
54
62
|
}
|
|
55
63
|
} else {
|
|
56
|
-
|
|
64
|
+
servers["toon-memory"] = {
|
|
57
65
|
command: "npx",
|
|
58
66
|
args: ["-y", "toon-memory", "mcp"]
|
|
59
67
|
}
|
|
@@ -64,21 +72,76 @@ export function installJSONConfig(agent: Agent, scope: string): void {
|
|
|
64
72
|
}
|
|
65
73
|
|
|
66
74
|
/**
|
|
67
|
-
* Install MCP server configuration for
|
|
75
|
+
* Install MCP server configuration for a TOML-format agent (Codex / Grok).
|
|
76
|
+
* The table name comes from agent.mcpKey so Grok's `mcp_servers` works too.
|
|
77
|
+
* Appends to an existing file instead of overwriting user settings.
|
|
68
78
|
*/
|
|
69
|
-
export function installTOMLConfig(agent: Agent): void {
|
|
70
|
-
const configPath = agent.local
|
|
79
|
+
export function installTOMLConfig(agent: Agent, scope: string): void {
|
|
80
|
+
const configPath = scope === "global" ? agent.global : agent.local
|
|
71
81
|
if (!configPath) return
|
|
72
82
|
|
|
73
83
|
const configDir = dirname(configPath)
|
|
74
84
|
if (!existsSync(configDir)) mkdirSync(configDir, { recursive: true })
|
|
75
85
|
|
|
76
|
-
const
|
|
86
|
+
const table = agent.mcpKey || "mcpServers"
|
|
87
|
+
const block = `[${table}.toon-memory]
|
|
77
88
|
command = "npx"
|
|
78
89
|
args = ["-y", "toon-memory", "mcp"]
|
|
79
90
|
`
|
|
80
91
|
|
|
81
|
-
|
|
92
|
+
let content = ""
|
|
93
|
+
if (existsSync(configPath)) {
|
|
94
|
+
content = readFileSync(configPath, "utf-8")
|
|
95
|
+
if (content.includes(`[${table}.toon-memory]`)) {
|
|
96
|
+
console.log(` MCP server already registered in ${configPath}`)
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
if (content && !content.endsWith("\n")) content += "\n"
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
writeFileSync(configPath, content + block)
|
|
103
|
+
console.log(` MCP server added to ${configPath}`)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Install MCP server configuration for Goose (YAML format).
|
|
108
|
+
* Configures a stdio extension under the top-level `extensions` key.
|
|
109
|
+
*/
|
|
110
|
+
export function installYAMLConfig(agent: Agent, scope: string): void {
|
|
111
|
+
const configPath = scope === "global" ? agent.global : agent.local
|
|
112
|
+
if (!configPath) {
|
|
113
|
+
console.log(` No ${scope} config path for ${agent.name}`)
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const configDir = dirname(configPath)
|
|
118
|
+
if (!existsSync(configDir)) mkdirSync(configDir, { recursive: true })
|
|
119
|
+
|
|
120
|
+
const entry = ` toon-memory:
|
|
121
|
+
type: stdio
|
|
122
|
+
cmd: npx
|
|
123
|
+
args: ["-y", "toon-memory", "mcp"]
|
|
124
|
+
`
|
|
125
|
+
|
|
126
|
+
let content = ""
|
|
127
|
+
if (existsSync(configPath)) {
|
|
128
|
+
content = readFileSync(configPath, "utf-8")
|
|
129
|
+
if (content.includes("toon-memory")) {
|
|
130
|
+
console.log(` MCP server already registered in ${configPath}`)
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (!content) {
|
|
136
|
+
content = `extensions:\n${entry}`
|
|
137
|
+
} else if (/^extensions:\s*$/m.test(content)) {
|
|
138
|
+
content = content.replace(/^extensions:\s*$/m, `extensions:\n${entry}`)
|
|
139
|
+
} else {
|
|
140
|
+
if (!content.endsWith("\n")) content += "\n"
|
|
141
|
+
content += `extensions:\n${entry}`
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
writeFileSync(configPath, content)
|
|
82
145
|
console.log(` MCP server added to ${configPath}`)
|
|
83
146
|
}
|
|
84
147
|
|
|
@@ -201,7 +264,9 @@ export function installMCPConfig(agent: Agent, scope: string): void {
|
|
|
201
264
|
}
|
|
202
265
|
|
|
203
266
|
if (agent.format === "toml") {
|
|
204
|
-
installTOMLConfig(agent)
|
|
267
|
+
installTOMLConfig(agent, scope)
|
|
268
|
+
} else if (agent.format === "yaml") {
|
|
269
|
+
installYAMLConfig(agent, scope)
|
|
205
270
|
} else if (agent.format === "jsonc") {
|
|
206
271
|
installZedConfig(agent)
|
|
207
272
|
} else if (agent.format === "continue") {
|
package/src/cli/hooks.ts
CHANGED
|
@@ -168,7 +168,7 @@ export function registerHookJSON(agent: Agent, hookPath: string): void {
|
|
|
168
168
|
return
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
-
if (agent.name === "claude" || agent.name === "gemini") {
|
|
171
|
+
if (agent.name === "claude" || agent.name === "gemini" || agent.name === "qwen") {
|
|
172
172
|
if (!config.hooks) config.hooks = {}
|
|
173
173
|
if (!config.hooks.SessionStart) config.hooks.SessionStart = []
|
|
174
174
|
if (!config.hooks.SessionStart.some((h: any) => h.command === hookPath)) {
|
package/src/cli/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/** Config format: "json" | "toml" | "jsonc" | "continue" | "openclaw" | "none" (instructions only) */
|
|
2
|
-
export type AgentFormat = "json" | "toml" | "jsonc" | "continue" | "openclaw" | "none"
|
|
1
|
+
/** Config format: "json" | "toml" | "jsonc" | "yaml" | "continue" | "openclaw" | "none" (instructions only) */
|
|
2
|
+
export type AgentFormat = "json" | "toml" | "jsonc" | "yaml" | "continue" | "openclaw" | "none"
|
|
3
3
|
|
|
4
4
|
/** Supported AI coding agent configuration */
|
|
5
5
|
export interface Agent {
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Evidence layer for memory writes (write-path intelligence).
|
|
3
|
+
*
|
|
4
|
+
* Deterministic, offline, no LLM. When an entry is saved it is annotated with
|
|
5
|
+
* one of three evidence levels that bias recall:
|
|
6
|
+
*
|
|
7
|
+
* - "conflict" — the new entry overlaps a `warning` or a critical/high
|
|
8
|
+
* decision already in memory. Surfaces (+0.15) so the agent
|
|
9
|
+
* re-checks before trusting the newer claim.
|
|
10
|
+
* - "verified" — the entry references a file that actually exists on disk
|
|
11
|
+
* (grounded in the repo). Small boost (+0.03).
|
|
12
|
+
* - "unverified" — neither. Slight penalty (−0.02).
|
|
13
|
+
*
|
|
14
|
+
* Contradiction detection is a Jaccard overlap against strong entries only
|
|
15
|
+
* (warnings and critical/high importance). It never blocks a write — the
|
|
16
|
+
* remember tool just warns so the agent can review.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { existsSync } from "fs"
|
|
20
|
+
import { tokenize } from "./utils"
|
|
21
|
+
import type { GraphEntry } from "./graph"
|
|
22
|
+
|
|
23
|
+
/** Evidence levels stored as the 20th TOON field. Empty = legacy entry or no file claim. */
|
|
24
|
+
export type Evidence = "" | "verified" | "unverified" | "conflict"
|
|
25
|
+
|
|
26
|
+
/** Ranking boost per evidence level. Empty/unknown = 0. */
|
|
27
|
+
export const EVIDENCE_BOOST: Record<string, number> = {
|
|
28
|
+
conflict: 0.15,
|
|
29
|
+
verified: 0.03,
|
|
30
|
+
unverified: -0.02,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Evidence of a potential contradiction with an existing strong entry. */
|
|
34
|
+
export interface Contradiction {
|
|
35
|
+
key: string
|
|
36
|
+
category: string
|
|
37
|
+
similarity: number
|
|
38
|
+
note: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Token-set Jaccard similarity (0..1) over key + content + tags. */
|
|
42
|
+
export function contentJaccard(a: string, b: string): number {
|
|
43
|
+
const sa = new Set(tokenize(a))
|
|
44
|
+
const sb = new Set(tokenize(b))
|
|
45
|
+
if (sa.size === 0 && sb.size === 0) return 1
|
|
46
|
+
if (sa.size === 0 || sb.size === 0) return 0
|
|
47
|
+
let inter = 0
|
|
48
|
+
for (const t of sa) if (sb.has(t)) inter++
|
|
49
|
+
const union = sa.size + sb.size - inter
|
|
50
|
+
return union > 0 ? inter / union : 0
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface Candidate {
|
|
54
|
+
key: string
|
|
55
|
+
content: string
|
|
56
|
+
category: string
|
|
57
|
+
tags: string[]
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Find entries that may contradict the candidate. Only "strong" entries are
|
|
62
|
+
* considered targets: warnings (negative memories) and critical/high
|
|
63
|
+
* importance decisions. Thresholds are looser for warnings (a landmine must
|
|
64
|
+
* surface) than for critical decisions (which need real overlap).
|
|
65
|
+
* Deterministic — sorted by similarity, capped at 3 hits.
|
|
66
|
+
*/
|
|
67
|
+
export function detectContradictions(
|
|
68
|
+
entries: GraphEntry[],
|
|
69
|
+
candidate: Candidate
|
|
70
|
+
): Contradiction[] {
|
|
71
|
+
const candText = `${candidate.key} ${candidate.content} ${candidate.tags.join(" ")}`
|
|
72
|
+
const hits: Contradiction[] = []
|
|
73
|
+
|
|
74
|
+
for (const e of entries) {
|
|
75
|
+
if (e.status === "obsolete" || e.status === "draft") continue
|
|
76
|
+
if (e.key === candidate.key) continue
|
|
77
|
+
const strong = e.category === "warning" || e.importance === "critical" || e.importance === "high"
|
|
78
|
+
if (!strong) continue
|
|
79
|
+
|
|
80
|
+
const sim = contentJaccard(candText, `${e.key} ${e.content} ${e.tags.join(" ")}`)
|
|
81
|
+
if (e.category === "warning" && sim < 0.22) continue
|
|
82
|
+
if (e.category !== "warning" && sim < 0.3) continue
|
|
83
|
+
|
|
84
|
+
hits.push({
|
|
85
|
+
key: e.key,
|
|
86
|
+
category: e.category,
|
|
87
|
+
similarity: Math.round(sim * 100),
|
|
88
|
+
note:
|
|
89
|
+
e.category === "warning"
|
|
90
|
+
? "overlaps a WARNING (negative memory) — possible landmine"
|
|
91
|
+
: "may contradict an existing critical/high decision",
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return hits.sort((a, b) => b.similarity - a.similarity).slice(0, 3)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Compute the evidence level for a candidate being saved.
|
|
100
|
+
* - "conflict" when it contradicts a strong entry,
|
|
101
|
+
* - "verified" when its referenced file exists on disk,
|
|
102
|
+
* - "unverified" when it claims a file that does not exist (broken reference),
|
|
103
|
+
* - "" (neutral) when no file is claimed.
|
|
104
|
+
*/
|
|
105
|
+
export function evidenceFor(
|
|
106
|
+
entries: GraphEntry[],
|
|
107
|
+
candidate: Candidate,
|
|
108
|
+
file?: string
|
|
109
|
+
): Evidence {
|
|
110
|
+
const conflicts = detectContradictions(entries, candidate)
|
|
111
|
+
if (conflicts.length > 0) return "conflict"
|
|
112
|
+
if (!file) return ""
|
|
113
|
+
const filePath = file.split(":")[0]
|
|
114
|
+
return existsSync(filePath) ? "verified" : "unverified"
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Ranking boost for an entry's stored evidence (empty/unknown = 0). */
|
|
118
|
+
export const evidenceBoost = (evidence: string): number => EVIDENCE_BOOST[evidence] ?? 0
|
package/src/lib/graph.ts
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
import { normalize, isExpiredLocal, tokenize, importance, importanceBoost, importanceRank, estimateTokens, parseToonLine } from "./utils"
|
|
18
18
|
import { expandSynonyms } from "./synonyms"
|
|
19
19
|
import { fuzzyMatch } from "./fuzzy"
|
|
20
|
+
import { evidenceBoost } from "./evidence"
|
|
20
21
|
|
|
21
22
|
export interface GraphEntry {
|
|
22
23
|
id: string
|
|
@@ -44,6 +45,8 @@ export interface GraphEntry {
|
|
|
44
45
|
supersededOn: string
|
|
45
46
|
/** Explicit importance level: "critical" | "high" | "medium" | "low". Empty = auto (recency+frequency). */
|
|
46
47
|
importance: string
|
|
48
|
+
/** Evidence annotation: "" | "verified" | "unverified" | "conflict" (write-path intelligence). Empty = legacy entry. */
|
|
49
|
+
evidence: string
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
/** Edge type labels used in typed links (`type:key`). */
|
|
@@ -104,7 +107,9 @@ export function parseEntries(data: string): GraphEntry[] {
|
|
|
104
107
|
const out: GraphEntry[] = []
|
|
105
108
|
for (const line of entryLines(data)) {
|
|
106
109
|
const parts = parseToonLine(line)
|
|
107
|
-
|
|
110
|
+
// Minimal legacy lines (id|category|key|...) are still valid entries;
|
|
111
|
+
// every optional field past `key` defaults gracefully below.
|
|
112
|
+
if (parts.length < 3) continue
|
|
108
113
|
const [id, category, key, content, file, tags, date, ttl, accessedRaw, linksRaw] =
|
|
109
114
|
parts
|
|
110
115
|
out.push({
|
|
@@ -131,6 +136,7 @@ export function parseEntries(data: string): GraphEntry[] {
|
|
|
131
136
|
status: parts.length > 16 && parts[16] ? parts[16] as "active" | "obsolete" | "resolved" | "draft" : "active",
|
|
132
137
|
supersededOn: parts.length > 17 ? parts[17] || "" : "",
|
|
133
138
|
importance: parts.length > 18 ? parts[18] || "" : "",
|
|
139
|
+
evidence: parts.length > 19 ? parts[19] || "" : "",
|
|
134
140
|
})
|
|
135
141
|
}
|
|
136
142
|
return out
|
|
@@ -423,6 +429,7 @@ export function buildReason(
|
|
|
423
429
|
}
|
|
424
430
|
parts.push(impScore >= 0.7 ? "importance HIGH" : impScore >= 0.4 ? "importance MED" : "importance LOW")
|
|
425
431
|
if (e.importance) parts.push(`explicit ${e.importance}`)
|
|
432
|
+
if (e.evidence) parts.push(`evidence ${e.evidence}`)
|
|
426
433
|
return parts.join(" · ")
|
|
427
434
|
}
|
|
428
435
|
|
|
@@ -550,6 +557,8 @@ export function graphRecallDetailed(
|
|
|
550
557
|
if (e.category === "warning") s += 0.2
|
|
551
558
|
// Explicit importance boost: critical/high entries surface first, low last.
|
|
552
559
|
s += importanceBoost(e.importance)
|
|
560
|
+
// Evidence bias: conflicts surface for review, verified is grounded, unverified sinks.
|
|
561
|
+
s += evidenceBoost(e.evidence)
|
|
553
562
|
s *= decay
|
|
554
563
|
return { e, s, priority: e.priority }
|
|
555
564
|
})
|
|
@@ -731,7 +740,8 @@ export function renderCompact(entries: GraphEntry[], opts: RenderCompactOpts = {
|
|
|
731
740
|
const statusInfo = e.status !== "active" ? ` · status: ${e.status}` : ""
|
|
732
741
|
const supersededInfo = e.supersededOn ? ` · superseded: ${e.supersededOn}` : ""
|
|
733
742
|
const impInfo = e.importance ? ` · importance: ${e.importance}` : ""
|
|
734
|
-
|
|
743
|
+
const evInfo = e.evidence ? ` · evidence: ${e.evidence}` : ""
|
|
744
|
+
block = `[${n}] ${e.category}/${e.key}${pin} (${e.id})\n ${e.content}\n File: ${e.file} | Tags: ${e.tags.join(";")} | Date: ${e.date}${ttlInfo}${accessInfo}${lastAccess}${originInfo}${scopeInfo}${statusInfo}${supersededInfo}${impInfo}${evInfo}${links}`
|
|
735
745
|
} else {
|
|
736
746
|
// "normal" budget (default)
|
|
737
747
|
let body = e.content
|
package/src/lib/quality.ts
CHANGED
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
import { normalize, isExpiredLocal, tokenize, importance, importanceBoost, importanceRank, isPrivate, estimateTokens, languageFamily, parseToonLine, toToonLine } from "./utils"
|
|
24
24
|
import { expandSynonyms } from "./synonyms"
|
|
25
25
|
import { fuzzyMatch } from "./fuzzy"
|
|
26
|
+
import { evidenceBoost } from "./evidence"
|
|
26
27
|
|
|
27
28
|
/**
|
|
28
29
|
* Quality score for an entry (0..1). Pure heuristics, no LLM.
|
|
@@ -185,7 +186,14 @@ export function mergeEntries(existingLine: string, newLine: string): string {
|
|
|
185
186
|
const newImportance = np.length > 18 ? np[18] || "" : ""
|
|
186
187
|
const importance = importanceRank(newImportance) >= importanceRank(existingImportance) ? newImportance : existingImportance
|
|
187
188
|
|
|
188
|
-
|
|
189
|
+
// Merge evidence (field 19): "conflict" wins, then any new non-empty value.
|
|
190
|
+
const existingEvidence = ep.length > 19 ? ep[19] || "" : ""
|
|
191
|
+
const newEvidence = np.length > 19 ? np[19] || "" : ""
|
|
192
|
+
const evidence = existingEvidence === "conflict" || newEvidence === "conflict"
|
|
193
|
+
? "conflict"
|
|
194
|
+
: newEvidence || existingEvidence
|
|
195
|
+
|
|
196
|
+
return toToonLine([id, category, key, content, file, mergedTags, date, ttl, String(accessed), mergedLinks, quality.toFixed(2), String(confidence), lastAccessed, String(priority), path_scope, origin, status, existingSupersededOn, importance, evidence])
|
|
189
197
|
}
|
|
190
198
|
|
|
191
199
|
/**
|
|
@@ -279,12 +287,14 @@ export function generateSmartRecall(
|
|
|
279
287
|
const warningBoost = e.category === "warning" ? 0.2 : 0
|
|
280
288
|
// Explicit importance boost: critical/high entries surface first, low last.
|
|
281
289
|
const impBoost = importanceBoost(e.importance)
|
|
290
|
+
// Evidence bias: conflicts surface for review, verified is grounded, unverified sinks.
|
|
291
|
+
const evBoost = evidenceBoost(e.evidence)
|
|
282
292
|
let combined: number
|
|
283
293
|
if (opts.rrf) {
|
|
284
|
-
combined = (fused?.get(e.key) ?? 0) - drift + sessionBias + langBoost + folderBoost + warningBoost + impBoost
|
|
294
|
+
combined = (fused?.get(e.key) ?? 0) - drift + sessionBias + langBoost + folderBoost + warningBoost + impBoost + evBoost
|
|
285
295
|
} else {
|
|
286
296
|
combined =
|
|
287
|
-
bm25Score + 0.3 * centScore + 0.3 * impScore + (matchesQuery ? 0.5 : 0) - drift + sessionBias + langBoost + folderBoost + warningBoost + impBoost
|
|
297
|
+
bm25Score + 0.3 * centScore + 0.3 * impScore + (matchesQuery ? 0.5 : 0) - drift + sessionBias + langBoost + folderBoost + warningBoost + impBoost + evBoost
|
|
288
298
|
}
|
|
289
299
|
return { entry: e, score: combined, matchesQuery, priority: e.priority, bm25Score }
|
|
290
300
|
})
|
package/src/mcp/config.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readFileSync, writeFileSync, existsSync, mkdirSync } from "fs"
|
|
2
2
|
import { join } from "path"
|
|
3
|
+
import { homedir } from "os"
|
|
3
4
|
|
|
4
5
|
/** Base directory for memory storage */
|
|
5
6
|
export const MEMORY_DIR = join(process.cwd(), ".toon-memory", "memory")
|
|
@@ -13,6 +14,15 @@ export const ARCHIVE_FILE = join(MEMORY_DIR, "archive.toon")
|
|
|
13
14
|
/** Observations log written by capture hooks (opt-in, separate from memory) */
|
|
14
15
|
export const OBSERVATIONS_FILE = join(MEMORY_DIR, "observations.toon")
|
|
15
16
|
|
|
17
|
+
/** Encrypted sidecar vault for secrets. Whole-file AES-256-GCM when TOON_MEMORY_KEY is set.
|
|
18
|
+
* Kept separate from data.toon so the main memory stays a readable, open format. */
|
|
19
|
+
export const SECRETS_FILE = join(MEMORY_DIR, "secrets.toon")
|
|
20
|
+
|
|
21
|
+
/** Optional global memory (cross-project conventions), merged on demand via memory_import_global.
|
|
22
|
+
* Overridable with TOON_MEMORY_GLOBAL_FILE for tests or custom locations. */
|
|
23
|
+
export const GLOBAL_FILE =
|
|
24
|
+
process.env.TOON_MEMORY_GLOBAL_FILE || join(homedir(), ".toon-memory", "memory", "global.toon")
|
|
25
|
+
|
|
16
26
|
/** Configuration file for encryption settings */
|
|
17
27
|
export const CONFIG_FILE = join(MEMORY_DIR, "config.json")
|
|
18
28
|
|
package/src/mcp/consolidation.ts
CHANGED
|
@@ -115,6 +115,71 @@ export function consolidateEntries(): { removed: number; kept: number; duplicate
|
|
|
115
115
|
return { removed: totalRemoved, kept: order.length, duplicates }
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Merge two TOON memory documents by key, preserving every field.
|
|
120
|
+
*
|
|
121
|
+
* Pure (no file I/O): local wins on ids; incoming entries merge into existing
|
|
122
|
+
* ones via mergeEntries (tags/links union, max confidence/priority, newest
|
|
123
|
+
* date/content). Used by memory_import_global — a one-shot, offline pull of
|
|
124
|
+
* cross-project conventions. Never a live dual-source recall, so the ranking
|
|
125
|
+
* pipeline is untouched.
|
|
126
|
+
*/
|
|
127
|
+
export function mergeMemoryFiles(
|
|
128
|
+
localData: string,
|
|
129
|
+
incomingData: string
|
|
130
|
+
): { data: string; added: number; updated: number } {
|
|
131
|
+
const isEntry = (l: string): boolean =>
|
|
132
|
+
l.startsWith(" ") && l.includes("|") && !l.startsWith(" summaries:")
|
|
133
|
+
|
|
134
|
+
const localLines = localData.split("\n")
|
|
135
|
+
const headerIdx = localLines.findIndex((l) => l.startsWith("entries[") || /^\[\d+\|]/.test(l))
|
|
136
|
+
|
|
137
|
+
let prefix: string[]
|
|
138
|
+
let summaries: string[]
|
|
139
|
+
let localEntryLines: string[]
|
|
140
|
+
|
|
141
|
+
if (headerIdx === -1) {
|
|
142
|
+
prefix = ["version: 1"]
|
|
143
|
+
summaries = []
|
|
144
|
+
localEntryLines = []
|
|
145
|
+
} else {
|
|
146
|
+
prefix = localLines.slice(0, headerIdx)
|
|
147
|
+
const body = localLines.slice(headerIdx + 1)
|
|
148
|
+
localEntryLines = body.filter(isEntry)
|
|
149
|
+
summaries = body.filter((l) => !isEntry(l))
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const merged = new Map<string, string>()
|
|
153
|
+
for (const line of localEntryLines) {
|
|
154
|
+
const parts = parseToonLine(line)
|
|
155
|
+
if (parts.length >= 3) merged.set(parts[2], line.trim())
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let added = 0
|
|
159
|
+
let updated = 0
|
|
160
|
+
for (const line of incomingData.split("\n").filter(isEntry)) {
|
|
161
|
+
const parts = parseToonLine(line)
|
|
162
|
+
// Minimal legacy lines (id|category|key|content) are valid entries too.
|
|
163
|
+
if (parts.length < 3) continue
|
|
164
|
+
const key = parts[2]
|
|
165
|
+
const existing = merged.get(key)
|
|
166
|
+
if (existing === undefined) {
|
|
167
|
+
merged.set(key, line.trim())
|
|
168
|
+
added++
|
|
169
|
+
} else {
|
|
170
|
+
merged.set(key, mergeEntries(existing, line.trim()))
|
|
171
|
+
updated++
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const header = `[${merged.size}|]{id|category|key|content|file|tags|date|ttl|accessed|links|quality|confidence|lastAccessed}:`
|
|
176
|
+
// Entries must keep the two-space TOON indent — parseEntries only reads
|
|
177
|
+
// lines starting with two spaces, so re-indent anything that lost it.
|
|
178
|
+
const indented = [...merged.values()].map((l) => (l.startsWith(" ") ? l : ` ${l}`))
|
|
179
|
+
const data = [...prefix, header, ...indented, ...summaries].join("\n")
|
|
180
|
+
return { data, added, updated }
|
|
181
|
+
}
|
|
182
|
+
|
|
118
183
|
/**
|
|
119
184
|
* Extract a (base, version) pair from a versioned key or content line.
|
|
120
185
|
* Matches "Use React 18", "React 18.2", "next 13.5.1", "vite ^5.0.0", etc.
|