toon-memory 2.8.0 → 2.9.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.
- package/dist/cli/capture.js +41 -10
- package/dist/cli/session-start.js +6 -6
- package/dist/cli/setup.js +36 -33
- package/mcp/server.js +269 -252
- package/package.json +2 -2
- package/src/cli/agents.ts +8 -5
- package/src/cli/capture-toggle.ts +4 -4
- package/src/cli/capture.ts +5 -0
- package/src/cli/commands.ts +15 -15
- package/src/cli/constants.ts +1 -1
- package/src/cli/session-start.ts +6 -6
- package/src/cli/setup.ts +8 -8
- package/src/lib/context.ts +78 -57
- package/src/lib/quality.ts +16 -11
- package/src/mcp/archive.ts +5 -4
- package/src/mcp/config.ts +9 -1
- package/src/mcp/observations.ts +28 -1
- package/src/mcp/resources.ts +3 -3
- package/src/mcp/scoring.ts +3 -6
- package/src/mcp/tools.ts +179 -187
package/src/mcp/tools.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import type { McpServer } from "@modelcontextprotocol/server"
|
|
2
2
|
import { z } from "zod"
|
|
3
|
-
import { readFileSync, writeFileSync, existsSync } from "fs"
|
|
3
|
+
import { readFileSync, writeFileSync, existsSync, copyFileSync, mkdirSync, readdirSync } from "fs"
|
|
4
|
+
import { join, basename } from "path"
|
|
4
5
|
import { readMemory, writeMemory, safeWrite } from "./memory-io"
|
|
5
|
-
import { loadConfig, saveConfig, getKey, MEMORY_FILE, OBSERVATIONS_FILE, MAX_ENTRIES } from "./config"
|
|
6
|
-
import { generateId, parseTTL, isExpired, inferTags, parseRelativeDate
|
|
7
|
-
import {
|
|
6
|
+
import { loadConfig, saveConfig, getKey, MEMORY_FILE, OBSERVATIONS_FILE, MEMORY_DIR, MAX_ENTRIES, getMaxEntries, ARCHIVE_FILE } from "./config"
|
|
7
|
+
import { generateId, parseTTL, isExpired, inferTags, parseRelativeDate } from "./entries"
|
|
8
|
+
import { findRelatedEntries, bumpAccessed } from "./scoring"
|
|
8
9
|
import { readObservations } from "./observations"
|
|
9
10
|
import { archiveOldEntries } from "./archive"
|
|
10
11
|
import { consolidateEntries } from "./consolidation"
|
|
11
12
|
import { readUnderLock } from "../lib/lock"
|
|
12
13
|
import { encrypt, decrypt } from "./crypto"
|
|
13
|
-
import { graphRecallDetailed, renderCompact,
|
|
14
|
+
import { graphRecallDetailed, renderCompact, parseEntries } from "../lib/graph"
|
|
14
15
|
import { qualityScore, mergeEntries, generateSmartRecall } from "../lib/quality"
|
|
15
16
|
import { generateContextBrief, generateContextGenerate, generateContextDiff, generateContextFocus, generateContextHealth, generateContextExport } from "../lib/context"
|
|
16
17
|
import { coordinationView, resolveSessionId, currentBranch, SESSION_TTL_MS } from "../lib/sessions"
|
|
@@ -26,15 +27,15 @@ server.registerTool(
|
|
|
26
27
|
"memory_remember",
|
|
27
28
|
{
|
|
28
29
|
title: "Save to Memory",
|
|
29
|
-
description: "
|
|
30
|
+
description: "Save a fact to the project's persistent memory (decisions, patterns, bugs, knowledge). Persists between sessions.",
|
|
30
31
|
inputSchema: {
|
|
31
|
-
category: z.enum(["decision", "pattern", "bug", "knowledge"]).describe("
|
|
32
|
-
key: z.string().describe("
|
|
33
|
-
content: z.string().describe("
|
|
34
|
-
file: z.string().optional().default("").describe("
|
|
35
|
-
tags: z.string().optional().default("").describe("
|
|
36
|
-
ttl: z.string().optional().default("").describe("Time to live (
|
|
37
|
-
links: z.string().optional().default("").describe("
|
|
32
|
+
category: z.enum(["decision", "pattern", "bug", "knowledge"]).describe("Category of the fact"),
|
|
33
|
+
key: z.string().describe("Short title in kebab-case (e.g. risk-engine-priority)"),
|
|
34
|
+
content: z.string().describe("Detailed content of the fact"),
|
|
35
|
+
file: z.string().optional().default("").describe("Related file or line (e.g. spec.md:145)"),
|
|
36
|
+
tags: z.string().optional().default("").describe("Semicolon-separated tags (e.g. risk;spec)"),
|
|
37
|
+
ttl: z.string().optional().default("").describe("Time to live (e.g. 7d, 2026-07-17). Empty = no expiration"),
|
|
38
|
+
links: z.string().optional().default("").describe("Related entry keys, separated by space or ';' (e.g. risk-spec engine-arch). Builds graph edges."),
|
|
38
39
|
},
|
|
39
40
|
},
|
|
40
41
|
async ({ category, key, content, file, tags, ttl, links }) => {
|
|
@@ -71,15 +72,15 @@ server.registerTool(
|
|
|
71
72
|
? links.split(/[\s;]+/).filter(Boolean).join(" ")
|
|
72
73
|
: existingParts[9] || ""
|
|
73
74
|
let newEntry = `${entryId}|${category}|${key}|${content}|${file || ""}|${resolvedTags}|${date}|${resolvedTtl}|0|${resolvedLinks}`
|
|
74
|
-
let action = "
|
|
75
|
+
let action = "Saved"
|
|
75
76
|
let mergeInfo = ""
|
|
76
77
|
const tagsInferred = !tags && resolvedTags ? true : false
|
|
77
78
|
|
|
78
79
|
if (existingIdx !== -1) {
|
|
79
80
|
newEntry = mergeEntries(lines[existingIdx].trim(), newEntry)
|
|
80
81
|
lines[existingIdx] = ` ${newEntry}`
|
|
81
|
-
action = "
|
|
82
|
-
mergeInfo = "\
|
|
82
|
+
action = "Updated"
|
|
83
|
+
mergeInfo = "\nMerge: tags combined, date and links updated"
|
|
83
84
|
} else {
|
|
84
85
|
const quality = qualityScore(resolvedTags, resolvedLinks, content, date)
|
|
85
86
|
const confidence = 1.0
|
|
@@ -95,7 +96,7 @@ server.registerTool(
|
|
|
95
96
|
const headerMatch = lines[headerIdx].match(/\[(\d+)\|/)
|
|
96
97
|
const entryCount = headerMatch ? parseInt(headerMatch[1]) : 0
|
|
97
98
|
let archiveMsg = ""
|
|
98
|
-
if (entryCount >
|
|
99
|
+
if (entryCount > getMaxEntries()) {
|
|
99
100
|
const result = archiveOldEntries({ trimToMax: true })
|
|
100
101
|
if (result.archived > 0) {
|
|
101
102
|
archiveMsg = `\n📦 Auto-archived ${result.archived} low-importance entries (${result.kept} kept)`
|
|
@@ -103,13 +104,13 @@ server.registerTool(
|
|
|
103
104
|
}
|
|
104
105
|
|
|
105
106
|
const ttlMsg = resolvedTtl ? `\n⏰ TTL: ${resolvedTtl}` : ""
|
|
106
|
-
const inferredMsg = tagsInferred ? `\n🏷️
|
|
107
|
+
const inferredMsg = tagsInferred ? `\n🏷️ Inferred tags: ${resolvedTags}` : ""
|
|
107
108
|
|
|
108
109
|
const related = findRelatedEntries(`${key} ${content} ${resolvedTags}`, key, 3)
|
|
109
110
|
let relatedMsg = ""
|
|
110
111
|
if (related.length > 0) {
|
|
111
112
|
const items = related.map((r) => ` [${r.cat}] ${r.key} — ${r.content.slice(0, 80)}`).join("\n")
|
|
112
|
-
relatedMsg = `\n\n🔗
|
|
113
|
+
relatedMsg = `\n\n🔗 Related entries:\n${items}`
|
|
113
114
|
}
|
|
114
115
|
|
|
115
116
|
return {
|
|
@@ -124,103 +125,41 @@ server.registerTool(
|
|
|
124
125
|
"memory_recall",
|
|
125
126
|
{
|
|
126
127
|
title: "Search Memory",
|
|
127
|
-
description: "
|
|
128
|
+
description: "Search the project's persistent memory. Returns relevant entries. Use BEFORE reading files.",
|
|
128
129
|
inputSchema: {
|
|
129
|
-
query: z.string().describe("
|
|
130
|
-
category: z.string().optional().default("").describe("
|
|
131
|
-
from_date: z.string().optional().default("").describe("
|
|
132
|
-
to_date: z.string().optional().default("").describe("
|
|
133
|
-
mode: z.enum(["flat", "graph"]).optional().default("flat").describe("'flat' =
|
|
134
|
-
hops: z.number().optional().default(1).describe("
|
|
135
|
-
compact: z.boolean().optional().default(false).describe("
|
|
130
|
+
query: z.string().describe("Text to search for"),
|
|
131
|
+
category: z.string().optional().default("").describe("Filter by category (empty = all)"),
|
|
132
|
+
from_date: z.string().optional().default("").describe("Start date filter (YYYY-MM-DD)"),
|
|
133
|
+
to_date: z.string().optional().default("").describe("End date filter (YYYY-MM-DD)"),
|
|
134
|
+
mode: z.enum(["flat", "graph"]).optional().default("flat").describe("'flat' = keyword search (default). 'graph' = graph-based recall: expands the related-entry subgraph from matches (more precise, fewer tokens)."),
|
|
135
|
+
hops: z.number().optional().default(1).describe("Graph depth in 'graph' mode (1 or 2). Default 1."),
|
|
136
|
+
compact: z.boolean().optional().default(false).describe("Token-efficient output: numeric indices (1, 2), omits id/date/file (keeps tags), edges as '->2', truncates graph neighbors to a snippet. Does not mutate .toon file."),
|
|
136
137
|
},
|
|
137
138
|
},
|
|
138
139
|
async ({ query, category, from_date, to_date, mode, hops, compact }) => {
|
|
139
140
|
const data = readMemory()
|
|
140
|
-
const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|") && !l.startsWith(" summaries:"))
|
|
141
|
-
|
|
142
|
-
const queryTokens = normalize(query).split(" ").filter(Boolean)
|
|
143
141
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const [id, cat, key, content, file, tags, date, ttl, accessedRaw] = parts
|
|
150
|
-
const qualityRaw = parts[10] || ""
|
|
151
|
-
const confidenceRaw = parts[11] || ""
|
|
152
|
-
if (category && cat !== category) return null
|
|
153
|
-
if (from_date && date < from_date) return null
|
|
154
|
-
if (to_date && date > to_date) return null
|
|
155
|
-
if (ttl && isExpired(ttl)) return null
|
|
156
|
-
const searchStr = normalize(`${id} ${cat} ${key} ${content} ${file} ${tags} ${qualityRaw} ${confidenceRaw}`)
|
|
157
|
-
if (!queryTokens.every((token) => searchStr.includes(token))) return null
|
|
158
|
-
const accessed = accessedRaw ? parseInt(accessedRaw) || 0 : 0
|
|
159
|
-
return { id, cat, key, content, file, tags, date, accessed, qualityRaw, confidenceRaw }
|
|
160
|
-
})
|
|
161
|
-
.filter(Boolean) as Array<{ id: string; cat: string; key: string; content: string; file: string; tags: string; date: string; accessed: number; qualityRaw: string; confidenceRaw: string }>
|
|
162
|
-
|
|
163
|
-
if (results.length === 0) {
|
|
164
|
-
return { content: [{ type: "text" as const, text: `No se encontraron resultados para "${query}"` }] }
|
|
142
|
+
// Delegate to graphRecallDetailed for both flat and graph modes
|
|
143
|
+
// This avoids duplicating BM25 + ranking logic
|
|
144
|
+
const detail = graphRecallDetailed(data, query, { category, from_date, to_date, hops })
|
|
145
|
+
if (detail.entries.length === 0) {
|
|
146
|
+
return { content: [{ type: "text" as const, text: `No results found for "${query}"` }] }
|
|
165
147
|
}
|
|
166
|
-
|
|
167
|
-
if (
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (compact) {
|
|
174
|
-
const formatted = renderCompact(detail.entries, {
|
|
175
|
-
adjacency: detail.adjacency,
|
|
176
|
-
seeds: detail.seeds,
|
|
177
|
-
snippetLen: 90,
|
|
178
|
-
})
|
|
179
|
-
return { content: [{ type: "text" as const, text: formatted }] }
|
|
180
|
-
}
|
|
181
|
-
const formatted = detail.entries
|
|
182
|
-
.map((r) => {
|
|
183
|
-
const links = r.links.length ? `\n links: ${r.links.join(", ")}` : ""
|
|
184
|
-
return `[${r.category}] ${r.key} (${r.id})\n ${r.content}\n File: ${r.file} | Tags: ${r.tags.join(";")} | Date: ${r.date}${links}`
|
|
185
|
-
})
|
|
186
|
-
.join("\n\n")
|
|
148
|
+
bumpAccessed(detail.entries.map((e) => e.id))
|
|
149
|
+
if (compact) {
|
|
150
|
+
const formatted = renderCompact(detail.entries, {
|
|
151
|
+
adjacency: detail.adjacency,
|
|
152
|
+
seeds: detail.seeds,
|
|
153
|
+
snippetLen: 90,
|
|
154
|
+
})
|
|
187
155
|
return { content: [{ type: "text" as const, text: formatted }] }
|
|
188
156
|
}
|
|
189
|
-
|
|
190
|
-
const bm25 = bm25Scores(parseEntries(data), query)
|
|
191
|
-
|
|
192
|
-
const ranked = results
|
|
157
|
+
const formatted = detail.entries
|
|
193
158
|
.map((r) => {
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
const bm25Score = bm25.get(r.key) || 0
|
|
197
|
-
return { ...r, score: bm25Score + entryScore(r.date, r.accessed) + qualityBoost }
|
|
159
|
+
const links = r.links.length ? `\n links: ${r.links.join(", ")}` : ""
|
|
160
|
+
return `[${r.category}] ${r.key} (${r.id})\n ${r.content}\n File: ${r.file} | Tags: ${r.tags.join(";")} | Date: ${r.date}${links}`
|
|
198
161
|
})
|
|
199
|
-
.sort((a, b) => b.score - a.score)
|
|
200
|
-
|
|
201
|
-
bumpAccessed(ranked.map((r) => r.id))
|
|
202
|
-
|
|
203
|
-
if (compact) {
|
|
204
|
-
const entries = ranked.map((r) => ({
|
|
205
|
-
id: r.id,
|
|
206
|
-
category: r.cat,
|
|
207
|
-
key: r.key,
|
|
208
|
-
content: r.content,
|
|
209
|
-
file: r.file,
|
|
210
|
-
tags: r.tags ? r.tags.split(";").filter(Boolean) : [],
|
|
211
|
-
date: r.date,
|
|
212
|
-
ttl: "",
|
|
213
|
-
accessed: r.accessed,
|
|
214
|
-
links: [] as string[],
|
|
215
|
-
}))
|
|
216
|
-
const formatted = renderCompact(entries)
|
|
217
|
-
return { content: [{ type: "text" as const, text: formatted }] }
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
const formatted = ranked
|
|
221
|
-
.map((r) => `[${r.cat}] ${r.key} (${r.id})\n ${r.content}\n File: ${r.file} | Tags: ${r.tags} | Date: ${r.date}`)
|
|
222
162
|
.join("\n\n")
|
|
223
|
-
|
|
224
163
|
return { content: [{ type: "text" as const, text: formatted }] }
|
|
225
164
|
}
|
|
226
165
|
)
|
|
@@ -231,9 +170,9 @@ server.registerTool(
|
|
|
231
170
|
"memory_forget",
|
|
232
171
|
{
|
|
233
172
|
title: "Delete from Memory",
|
|
234
|
-
description: "
|
|
173
|
+
description: "Delete a memory entry by its key or id.",
|
|
235
174
|
inputSchema: {
|
|
236
|
-
key: z.string().describe("Key
|
|
175
|
+
key: z.string().describe("Key or id of the entry to delete"),
|
|
237
176
|
},
|
|
238
177
|
},
|
|
239
178
|
async ({ key }) => {
|
|
@@ -242,7 +181,7 @@ server.registerTool(
|
|
|
242
181
|
const headerIdx = lines.findIndex((l) => l.startsWith("entries[") || /^\[\d+\|]/.test(l))
|
|
243
182
|
|
|
244
183
|
if (headerIdx === -1) {
|
|
245
|
-
return { content: [{ type: "text" as const, text: "No
|
|
184
|
+
return { content: [{ type: "text" as const, text: "No entries in memory" }] }
|
|
246
185
|
}
|
|
247
186
|
|
|
248
187
|
const entryLines = lines.slice(headerIdx + 1).filter((l) => l.trim().length > 0 && !l.startsWith(" summaries:"))
|
|
@@ -261,12 +200,12 @@ server.registerTool(
|
|
|
261
200
|
|
|
262
201
|
if (removed === 0) {
|
|
263
202
|
return {
|
|
264
|
-
content: [{ type: "text" as const, text: `
|
|
203
|
+
content: [{ type: "text" as const, text: `"${key}" not found in memory.` }],
|
|
265
204
|
}
|
|
266
205
|
}
|
|
267
206
|
|
|
268
207
|
return {
|
|
269
|
-
content: [{ type: "text" as const, text: `"${key}"
|
|
208
|
+
content: [{ type: "text" as const, text: `"${key}" deleted. ${count - removed} entries remaining.` }],
|
|
270
209
|
}
|
|
271
210
|
}
|
|
272
211
|
)
|
|
@@ -277,7 +216,7 @@ server.registerTool(
|
|
|
277
216
|
"memory_stats",
|
|
278
217
|
{
|
|
279
218
|
title: "Memory Stats",
|
|
280
|
-
description: "
|
|
219
|
+
description: "Show project memory statistics.",
|
|
281
220
|
inputSchema: {},
|
|
282
221
|
},
|
|
283
222
|
async () => {
|
|
@@ -302,16 +241,16 @@ server.registerTool(
|
|
|
302
241
|
|
|
303
242
|
const summaryLines = data.split("\n").filter((l) => l.includes(":") && !l.startsWith(" ") && !l.startsWith("version") && !l.startsWith("entries") && !/^\[\d+\|]/.test(l))
|
|
304
243
|
const stats = [
|
|
305
|
-
`
|
|
306
|
-
`
|
|
244
|
+
`Total entries: ${entries.length}`,
|
|
245
|
+
`File summaries: ${summaryLines.length}`,
|
|
307
246
|
"",
|
|
308
|
-
"
|
|
247
|
+
"By category:",
|
|
309
248
|
...Object.entries(byCategory).map(([k, v]) => ` ${k}: ${v}`),
|
|
310
249
|
"",
|
|
311
|
-
`TTL: ${withTtl}
|
|
312
|
-
`
|
|
250
|
+
`TTL: ${withTtl} with expiration, ${expired} expired`,
|
|
251
|
+
`Average quality: ${avgQuality} (${withQuality} with score)`,
|
|
313
252
|
"",
|
|
314
|
-
|
|
253
|
+
`Last 5 entries:`,
|
|
315
254
|
...lines.slice(-5).map((l) => {
|
|
316
255
|
const parts = l.trim().split("|")
|
|
317
256
|
const ttlInfo = parts[7] ? ` | TTL: ${parts[7]}` : ""
|
|
@@ -330,10 +269,10 @@ server.registerTool(
|
|
|
330
269
|
"memory_diff",
|
|
331
270
|
{
|
|
332
271
|
title: "Memory Diff",
|
|
333
|
-
description: "
|
|
272
|
+
description: "Show what changed in memory since a date. Useful for seeing what was learned since the last session.",
|
|
334
273
|
inputSchema: {
|
|
335
|
-
since: z.string().describe("
|
|
336
|
-
type: z.enum(["all", "created", "updated"]).optional().default("all").describe("
|
|
274
|
+
since: z.string().describe("Show changes since (e.g. 24h, 7d, 2026-07-10)"),
|
|
275
|
+
type: z.enum(["all", "created", "updated"]).optional().default("all").describe("Filter by change type"),
|
|
337
276
|
},
|
|
338
277
|
},
|
|
339
278
|
async ({ since, type }) => {
|
|
@@ -356,16 +295,16 @@ server.registerTool(
|
|
|
356
295
|
.filter(Boolean)
|
|
357
296
|
|
|
358
297
|
if (results.length === 0) {
|
|
359
|
-
return { content: [{ type: "text" as const, text: `No
|
|
298
|
+
return { content: [{ type: "text" as const, text: `No changes since ${sinceDate}` }] }
|
|
360
299
|
}
|
|
361
300
|
|
|
362
301
|
const created = results.filter((r) => r!.changeType === "created")
|
|
363
302
|
const updated = results.filter((r) => r!.changeType === "updated")
|
|
364
303
|
|
|
365
|
-
const sections: string[] = [`📋
|
|
304
|
+
const sections: string[] = [`📋 Changes since ${sinceDate}:`, ""]
|
|
366
305
|
|
|
367
306
|
if (created.length > 0 && (type === "all" || type === "created")) {
|
|
368
|
-
sections.push(`➕
|
|
307
|
+
sections.push(`➕ New (${created.length}):`)
|
|
369
308
|
for (const r of created) {
|
|
370
309
|
sections.push(` [${r!.cat}] ${r!.key} (${r!.id})\n ${r!.content}`)
|
|
371
310
|
}
|
|
@@ -373,7 +312,7 @@ server.registerTool(
|
|
|
373
312
|
}
|
|
374
313
|
|
|
375
314
|
if (updated.length > 0 && (type === "all" || type === "updated")) {
|
|
376
|
-
sections.push(`✏️
|
|
315
|
+
sections.push(`✏️ Updated (${updated.length}):`)
|
|
377
316
|
for (const r of updated) {
|
|
378
317
|
sections.push(` [${r!.cat}] ${r!.key} (${r!.id}) — ${r!.date}`)
|
|
379
318
|
}
|
|
@@ -390,24 +329,24 @@ server.registerTool(
|
|
|
390
329
|
"memory_suggest",
|
|
391
330
|
{
|
|
392
331
|
title: "Suggest Related Memories",
|
|
393
|
-
description: "
|
|
332
|
+
description: "Suggest memory entries related to a given context. Useful for gathering context before a task.",
|
|
394
333
|
inputSchema: {
|
|
395
|
-
context: z.string().describe("
|
|
396
|
-
limit: z.number().optional().default(5).describe("
|
|
334
|
+
context: z.string().describe("Text or context to search for suggestions"),
|
|
335
|
+
limit: z.number().optional().default(5).describe("Maximum suggestions"),
|
|
397
336
|
},
|
|
398
337
|
},
|
|
399
338
|
async ({ context, limit }) => {
|
|
400
339
|
const related = findRelatedEntries(context, "", limit)
|
|
401
340
|
|
|
402
341
|
if (related.length === 0) {
|
|
403
|
-
return { content: [{ type: "text" as const, text: `No
|
|
342
|
+
return { content: [{ type: "text" as const, text: `No related entries found for "${context}"` }] }
|
|
404
343
|
}
|
|
405
344
|
|
|
406
345
|
const formatted = related
|
|
407
346
|
.map((r) => `[${r.cat}] ${r.key} (${r.id})\n ${r.content}\n File: ${r.file} | Tags: ${r.tags} | Date: ${r.date}`)
|
|
408
347
|
.join("\n\n")
|
|
409
348
|
|
|
410
|
-
return { content: [{ type: "text" as const, text: `🔍
|
|
349
|
+
return { content: [{ type: "text" as const, text: `🔍 Suggestions for "${context}":\n\n${formatted}` }] }
|
|
411
350
|
}
|
|
412
351
|
)
|
|
413
352
|
|
|
@@ -417,11 +356,11 @@ server.registerTool(
|
|
|
417
356
|
"memory_smart_recall",
|
|
418
357
|
{
|
|
419
358
|
title: "Smart Recall (Unified)",
|
|
420
|
-
description: "
|
|
359
|
+
description: "Unified retrieval: combines BM25 + graph + decay + quality in a single call. Use at the START of each task to get all relevant memory context.",
|
|
421
360
|
inputSchema: {
|
|
422
|
-
intent: z.string().describe("Describe
|
|
423
|
-
limit: z.number().optional().default(8).describe("
|
|
424
|
-
category: z.string().optional().default("").describe("
|
|
361
|
+
intent: z.string().describe("Describe what you need to know (e.g. 'database schema for backend')"),
|
|
362
|
+
limit: z.number().optional().default(8).describe("Maximum entries to return"),
|
|
363
|
+
category: z.string().optional().default("").describe("Filter by category (empty = all)"),
|
|
425
364
|
},
|
|
426
365
|
},
|
|
427
366
|
async ({ intent, limit, category }) => {
|
|
@@ -437,11 +376,11 @@ server.registerTool(
|
|
|
437
376
|
"memory_summary",
|
|
438
377
|
{
|
|
439
378
|
title: "File Summary",
|
|
440
|
-
description: "
|
|
379
|
+
description: "Save or retrieve a summary of a large file to save tokens.",
|
|
441
380
|
inputSchema: {
|
|
442
|
-
action: z.enum(["get", "set"]).describe("get
|
|
443
|
-
file: z.string().describe("
|
|
444
|
-
summary: z.string().optional().default("").describe("
|
|
381
|
+
action: z.enum(["get", "set"]).describe("get to read, set to save"),
|
|
382
|
+
file: z.string().describe("File path"),
|
|
383
|
+
summary: z.string().optional().default("").describe("File summary (only for set)"),
|
|
445
384
|
},
|
|
446
385
|
},
|
|
447
386
|
async ({ action, file, summary }) => {
|
|
@@ -451,13 +390,13 @@ server.registerTool(
|
|
|
451
390
|
const lines = data.split("\n")
|
|
452
391
|
const summaryIdx = lines.findIndex((l) => l.trim().startsWith("summaries:"))
|
|
453
392
|
if (summaryIdx === -1) {
|
|
454
|
-
return { content: [{ type: "text" as const, text: `No
|
|
393
|
+
return { content: [{ type: "text" as const, text: `No summaries saved for "${file}"` }] }
|
|
455
394
|
}
|
|
456
395
|
|
|
457
396
|
const summaryLines = lines.slice(summaryIdx + 1).filter((l) => l.includes(":"))
|
|
458
397
|
const match = summaryLines.find((l) => l.startsWith(` ${file}:`))
|
|
459
398
|
if (!match) {
|
|
460
|
-
return { content: [{ type: "text" as const, text: `No
|
|
399
|
+
return { content: [{ type: "text" as const, text: `No summary for "${file}"` }] }
|
|
461
400
|
}
|
|
462
401
|
|
|
463
402
|
const summaryText = match.replace(` ${file}: `, "")
|
|
@@ -484,7 +423,7 @@ server.registerTool(
|
|
|
484
423
|
lines.splice(summaryIdx + 1, lines.length - summaryIdx - 1, ...summaryLines)
|
|
485
424
|
writeMemory(lines.join("\n"))
|
|
486
425
|
return {
|
|
487
|
-
content: [{ type: "text" as const, text: `📝
|
|
426
|
+
content: [{ type: "text" as const, text: `📝 Summary saved for ${file}` }],
|
|
488
427
|
}
|
|
489
428
|
}
|
|
490
429
|
)
|
|
@@ -495,20 +434,20 @@ server.registerTool(
|
|
|
495
434
|
"memory_archive",
|
|
496
435
|
{
|
|
497
436
|
title: "Archive Old Entries",
|
|
498
|
-
description: "
|
|
437
|
+
description: "Move old entries (>30 days) to archive.toon to keep memory clean.",
|
|
499
438
|
inputSchema: {},
|
|
500
439
|
},
|
|
501
440
|
async () => {
|
|
502
441
|
const result = archiveOldEntries()
|
|
503
442
|
|
|
504
443
|
if (result.archived === 0) {
|
|
505
|
-
return { content: [{ type: "text" as const, text: "No
|
|
444
|
+
return { content: [{ type: "text" as const, text: "No old entries to archive" }] }
|
|
506
445
|
}
|
|
507
446
|
|
|
508
447
|
return {
|
|
509
448
|
content: [{
|
|
510
449
|
type: "text" as const,
|
|
511
|
-
text: `📦
|
|
450
|
+
text: `📦 Archived ${result.archived} old entries\n📋 ${result.kept} active entries remaining`
|
|
512
451
|
}],
|
|
513
452
|
}
|
|
514
453
|
}
|
|
@@ -520,19 +459,19 @@ server.registerTool(
|
|
|
520
459
|
"memory_encrypt",
|
|
521
460
|
{
|
|
522
461
|
title: "Enable Encryption",
|
|
523
|
-
description: "
|
|
462
|
+
description: "Enable AES-256-GCM encryption for memory. Requires TOON_MEMORY_KEY in environment.",
|
|
524
463
|
inputSchema: {},
|
|
525
464
|
},
|
|
526
465
|
async () => {
|
|
527
466
|
const config = loadConfig()
|
|
528
467
|
|
|
529
468
|
if (config.encrypted) {
|
|
530
|
-
return { content: [{ type: "text" as const, text: "
|
|
469
|
+
return { content: [{ type: "text" as const, text: "Encryption is already enabled" }] }
|
|
531
470
|
}
|
|
532
471
|
|
|
533
472
|
const key = getKey()
|
|
534
473
|
if (!key) {
|
|
535
|
-
return { content: [{ type: "text" as const, text: "❌
|
|
474
|
+
return { content: [{ type: "text" as const, text: "❌ Set TOON_MEMORY_KEY in environment before encrypting" }] }
|
|
536
475
|
}
|
|
537
476
|
|
|
538
477
|
const data = readUnderLock(MEMORY_FILE)
|
|
@@ -542,7 +481,7 @@ server.registerTool(
|
|
|
542
481
|
saveConfig({ encrypted: true })
|
|
543
482
|
|
|
544
483
|
return {
|
|
545
|
-
content: [{ type: "text" as const, text: "🔐
|
|
484
|
+
content: [{ type: "text" as const, text: "🔐 Encryption enabled" }],
|
|
546
485
|
}
|
|
547
486
|
}
|
|
548
487
|
)
|
|
@@ -553,21 +492,21 @@ server.registerTool(
|
|
|
553
492
|
"memory_decrypt",
|
|
554
493
|
{
|
|
555
494
|
title: "Disable Encryption",
|
|
556
|
-
description: "
|
|
495
|
+
description: "Disable encryption and decode memory.",
|
|
557
496
|
inputSchema: {
|
|
558
|
-
key: z.string().describe("
|
|
497
|
+
key: z.string().describe("Encryption key"),
|
|
559
498
|
},
|
|
560
499
|
},
|
|
561
500
|
async ({ key }) => {
|
|
562
501
|
const config = loadConfig()
|
|
563
502
|
|
|
564
503
|
if (!config.encrypted) {
|
|
565
|
-
return { content: [{ type: "text" as const, text: "
|
|
504
|
+
return { content: [{ type: "text" as const, text: "Encryption is not enabled" }] }
|
|
566
505
|
}
|
|
567
506
|
|
|
568
507
|
const resolvedKey = key || getKey() || ""
|
|
569
508
|
if (!resolvedKey) {
|
|
570
|
-
return { content: [{ type: "text" as const, text: "❌ No
|
|
509
|
+
return { content: [{ type: "text" as const, text: "❌ No key provided. Pass as argument or set in .env file" }] }
|
|
571
510
|
}
|
|
572
511
|
|
|
573
512
|
try {
|
|
@@ -578,10 +517,10 @@ server.registerTool(
|
|
|
578
517
|
saveConfig({ encrypted: false })
|
|
579
518
|
|
|
580
519
|
return {
|
|
581
|
-
content: [{ type: "text" as const, text: "🔓
|
|
520
|
+
content: [{ type: "text" as const, text: "🔓 Encryption disabled" }],
|
|
582
521
|
}
|
|
583
522
|
} catch {
|
|
584
|
-
return { content: [{ type: "text" as const, text: "❌
|
|
523
|
+
return { content: [{ type: "text" as const, text: "❌ Wrong key or corrupted data" }] }
|
|
585
524
|
}
|
|
586
525
|
}
|
|
587
526
|
)
|
|
@@ -592,12 +531,12 @@ server.registerTool(
|
|
|
592
531
|
"memory_captured",
|
|
593
532
|
{
|
|
594
533
|
title: "List Captured Activity",
|
|
595
|
-
description: "
|
|
534
|
+
description: "Show activity log captured automatically by hooks (only if capture is enabled). Useful for promoting observations to memory with memory_remember.",
|
|
596
535
|
inputSchema: {
|
|
597
|
-
limit: z.number().optional().default(20).describe("
|
|
598
|
-
tool: z.string().optional().default("").describe("
|
|
599
|
-
file: z.string().optional().default("").describe("
|
|
600
|
-
clear: z.boolean().optional().default(false).describe("
|
|
536
|
+
limit: z.number().optional().default(20).describe("Maximum observations to show"),
|
|
537
|
+
tool: z.string().optional().default("").describe("Filter by tool name"),
|
|
538
|
+
file: z.string().optional().default("").describe("Filter by file"),
|
|
539
|
+
clear: z.boolean().optional().default(false).describe("If true, clears the capture log"),
|
|
601
540
|
},
|
|
602
541
|
},
|
|
603
542
|
async ({ limit, tool, file, clear }) => {
|
|
@@ -605,7 +544,7 @@ server.registerTool(
|
|
|
605
544
|
if (existsSync(OBSERVATIONS_FILE)) {
|
|
606
545
|
writeFileSync(OBSERVATIONS_FILE, "version: 1\nobservations[0|]{ts|session|agent|branch|tool|hash|file|summary}:\n")
|
|
607
546
|
}
|
|
608
|
-
|
|
547
|
+
return { content: [{ type: "text" as const, text: "🧹 Capture log cleared" }] }
|
|
609
548
|
}
|
|
610
549
|
|
|
611
550
|
let obs = readObservations()
|
|
@@ -617,7 +556,7 @@ server.registerTool(
|
|
|
617
556
|
return {
|
|
618
557
|
content: [{
|
|
619
558
|
type: "text" as const,
|
|
620
|
-
text: "No
|
|
559
|
+
text: "No captured activity. Capture is disabled by default; enable with `toon-memory capture on`.",
|
|
621
560
|
}],
|
|
622
561
|
}
|
|
623
562
|
}
|
|
@@ -626,7 +565,7 @@ server.registerTool(
|
|
|
626
565
|
.map((o) => `[${o.ts}] ${o.agent}@${o.branch}/${o.tool}${o.file ? ` (${o.file})` : ""}\n ${o.summary}`)
|
|
627
566
|
.join("\n\n")
|
|
628
567
|
|
|
629
|
-
return { content: [{ type: "text" as const, text: `🔍
|
|
568
|
+
return { content: [{ type: "text" as const, text: `🔍 Captured activity (${obs.length}):\n\n${formatted}` }] }
|
|
630
569
|
}
|
|
631
570
|
)
|
|
632
571
|
|
|
@@ -636,18 +575,18 @@ server.registerTool(
|
|
|
636
575
|
"memory_consolidate",
|
|
637
576
|
{
|
|
638
577
|
title: "Consolidate Memory",
|
|
639
|
-
description: "
|
|
578
|
+
description: "Consolidate memory by removing entries with identical content (keeps the first). Deterministic, no LLM.",
|
|
640
579
|
inputSchema: {},
|
|
641
580
|
},
|
|
642
581
|
async () => {
|
|
643
582
|
const result = consolidateEntries()
|
|
644
583
|
if (result.removed === 0) {
|
|
645
|
-
return { content: [{ type: "text" as const, text: `✅
|
|
584
|
+
return { content: [{ type: "text" as const, text: `✅ Memory already consolidated (${result.kept} entries, 0 duplicates)` }] }
|
|
646
585
|
}
|
|
647
586
|
return {
|
|
648
587
|
content: [{
|
|
649
588
|
type: "text" as const,
|
|
650
|
-
text: `🧹
|
|
589
|
+
text: `🧹 Consolidated ${result.removed} duplicate entries.\n${result.kept} active entries remaining.\nDuplicates: ${result.duplicates.join(", ")}`,
|
|
651
590
|
}],
|
|
652
591
|
}
|
|
653
592
|
}
|
|
@@ -659,9 +598,9 @@ server.registerTool(
|
|
|
659
598
|
"memory_sessions",
|
|
660
599
|
{
|
|
661
600
|
title: "Active Sessions & Conflicts",
|
|
662
|
-
description: "
|
|
601
|
+
description: "Show active agent sessions in this project (git branch, files touched, last-seen) and detect soft conflicts (files touched by 2+ sessions). Use at startup to avoid overwriting other parallel sessions' work.",
|
|
663
602
|
inputSchema: {
|
|
664
|
-
conflictsOnly: z.boolean().optional().default(false).describe("
|
|
603
|
+
conflictsOnly: z.boolean().optional().default(false).describe("If true, only show soft conflicts"),
|
|
665
604
|
},
|
|
666
605
|
},
|
|
667
606
|
async ({ conflictsOnly }) => {
|
|
@@ -670,14 +609,14 @@ server.registerTool(
|
|
|
670
609
|
|
|
671
610
|
if (conflictsOnly) {
|
|
672
611
|
if (conflicts.length === 0) {
|
|
673
|
-
return { content: [{ type: "text" as const, text: "✅ No
|
|
612
|
+
return { content: [{ type: "text" as const, text: "✅ No soft conflicts between active sessions." }] }
|
|
674
613
|
}
|
|
675
614
|
const lines = conflicts.map((c) => {
|
|
676
615
|
const who = c.sessions.map((s) => `${s.agent}@${s.branch} (${s.id})`).join(", ")
|
|
677
616
|
return `⚠️ ${c.file}\n ↔ ${who}`
|
|
678
617
|
})
|
|
679
618
|
return {
|
|
680
|
-
content: [{ type: "text" as const, text: `🔥
|
|
619
|
+
content: [{ type: "text" as const, text: `🔥 Soft conflicts (${conflicts.length}):\n\n${lines.join("\n\n")}` }],
|
|
681
620
|
}
|
|
682
621
|
}
|
|
683
622
|
|
|
@@ -685,7 +624,7 @@ server.registerTool(
|
|
|
685
624
|
return {
|
|
686
625
|
content: [{
|
|
687
626
|
type: "text" as const,
|
|
688
|
-
text: "🟢 No
|
|
627
|
+
text: "🟢 No other active sessions in this project.\n(This session: " + selfId + " @ " + currentBranch() + ")",
|
|
689
628
|
}],
|
|
690
629
|
}
|
|
691
630
|
}
|
|
@@ -693,27 +632,27 @@ server.registerTool(
|
|
|
693
632
|
const ttlMin = Math.round(SESSION_TTL_MS / 60000)
|
|
694
633
|
const section = (s: ReturnType<typeof coordinationView>["active"][number]) => {
|
|
695
634
|
const mins = Math.max(0, Math.round(s.ageMs / 60000))
|
|
696
|
-
const tag = s.id === selfId ? " (
|
|
635
|
+
const tag = s.id === selfId ? " (you)" : ""
|
|
697
636
|
const ended = s.ended ? " 🏁" : ""
|
|
698
637
|
const files = Object.keys(s.files).slice(0, 8).map((f) => ` • ${f}`).join("\n")
|
|
699
|
-
const fileBlock = files ? `\n
|
|
700
|
-
return `• ${s.agent} @ ${s.branch}${tag}${ended}\n id: ${s.id}\n
|
|
638
|
+
const fileBlock = files ? `\n Files:\n${files}` : ""
|
|
639
|
+
return `• ${s.agent} @ ${s.branch}${tag}${ended}\n id: ${s.id}\n ${mins} min ago${fileBlock}`
|
|
701
640
|
}
|
|
702
641
|
|
|
703
642
|
const parts = [
|
|
704
|
-
`🧭
|
|
643
|
+
`🧭 Active sessions (${active.length}) — window ${ttlMin} min:`,
|
|
705
644
|
"",
|
|
706
645
|
...active.map(section),
|
|
707
646
|
]
|
|
708
647
|
|
|
709
648
|
if (conflicts.length > 0) {
|
|
710
|
-
parts.push("", `🔥
|
|
649
|
+
parts.push("", `🔥 Soft conflicts (${conflicts.length}):`)
|
|
711
650
|
for (const c of conflicts) {
|
|
712
651
|
const who = c.sessions.map((s) => `${s.agent}@${s.branch}`).join(", ")
|
|
713
652
|
parts.push(` ⚠️ ${c.file} ↔ ${who}`)
|
|
714
653
|
}
|
|
715
654
|
} else {
|
|
716
|
-
parts.push("", "✅
|
|
655
|
+
parts.push("", "✅ No soft conflicts detected.")
|
|
717
656
|
}
|
|
718
657
|
|
|
719
658
|
return { content: [{ type: "text" as const, text: parts.join("\n") }] }
|
|
@@ -726,10 +665,10 @@ server.registerTool(
|
|
|
726
665
|
"context_brief",
|
|
727
666
|
{
|
|
728
667
|
title: "Context Briefing",
|
|
729
|
-
description: "
|
|
668
|
+
description: "Generate a compact context briefing: relevant memory + active sessions + project health. Single call instead of 5-6 separate calls. Zero LLM, pure deterministic logic.",
|
|
730
669
|
inputSchema: {
|
|
731
|
-
task: z.string().optional().default("").describe("
|
|
732
|
-
limit: z.number().optional().default(6).describe("
|
|
670
|
+
task: z.string().optional().default("").describe("Current agent task. If provided, entries are ranked by relevance to this task. If empty, shows top entries by importance."),
|
|
671
|
+
limit: z.number().optional().default(6).describe("Maximum relevant entries to show"),
|
|
733
672
|
},
|
|
734
673
|
},
|
|
735
674
|
async ({ task, limit }) => {
|
|
@@ -745,9 +684,9 @@ server.registerTool(
|
|
|
745
684
|
"context_generate",
|
|
746
685
|
{
|
|
747
686
|
title: "Generate Full Context",
|
|
748
|
-
description: "
|
|
687
|
+
description: "Generate a full system prompt: project (package.json, deps, framework) + git (branch, commits) + memory (relevant entries) + sessions. Single call to prepare the agent. Zero LLM.",
|
|
749
688
|
inputSchema: {
|
|
750
|
-
task: z.string().optional().default("").describe("
|
|
689
|
+
task: z.string().optional().default("").describe("Agent task. If provided, ranks entries by relevance to this task."),
|
|
751
690
|
},
|
|
752
691
|
},
|
|
753
692
|
async ({ task }) => {
|
|
@@ -764,9 +703,9 @@ server.registerTool(
|
|
|
764
703
|
"context_diff",
|
|
765
704
|
{
|
|
766
705
|
title: "Context Diff",
|
|
767
|
-
description: "
|
|
706
|
+
description: "What changed since the last session: git commits + modified files + new/updated memory entries. Zero LLM.",
|
|
768
707
|
inputSchema: {
|
|
769
|
-
since: z.string().optional().default("").describe("
|
|
708
|
+
since: z.string().optional().default("").describe("Start date (YYYY-MM-DD or relative like '7d'). Empty = last visible changes."),
|
|
770
709
|
},
|
|
771
710
|
},
|
|
772
711
|
async ({ since }) => {
|
|
@@ -783,10 +722,10 @@ server.registerTool(
|
|
|
783
722
|
"context_focus",
|
|
784
723
|
{
|
|
785
724
|
title: "Focus Context for Task",
|
|
786
|
-
description: "
|
|
725
|
+
description: "Hyper-focused context for a specific task: relevant entries + related files + code referencing the symbol + existing tests. Zero LLM.",
|
|
787
726
|
inputSchema: {
|
|
788
|
-
task: z.string().describe("
|
|
789
|
-
limit: z.number().optional().default(6).describe("
|
|
727
|
+
task: z.string().describe("Task or symbol to search context for (e.g. 'fix auth bug', 'authenticate')"),
|
|
728
|
+
limit: z.number().optional().default(6).describe("Maximum memory entries to include"),
|
|
790
729
|
},
|
|
791
730
|
},
|
|
792
731
|
async ({ task, limit }) => {
|
|
@@ -803,7 +742,7 @@ server.registerTool(
|
|
|
803
742
|
"context_health",
|
|
804
743
|
{
|
|
805
744
|
title: "Memory Health Audit",
|
|
806
|
-
description: "
|
|
745
|
+
description: "Full memory health audit: orphan links, duplicates, broken refs, expired TTL, quality, stale sessions. Includes score 0-100. Zero LLM.",
|
|
807
746
|
inputSchema: {},
|
|
808
747
|
},
|
|
809
748
|
async () => {
|
|
@@ -820,9 +759,9 @@ server.registerTool(
|
|
|
820
759
|
"context_export",
|
|
821
760
|
{
|
|
822
761
|
title: "Export Memory as Markdown",
|
|
823
|
-
description: "
|
|
762
|
+
description: "Export all memory as markdown injectable for other agents or sessions. Full (detailed) or compact format. Zero LLM.",
|
|
824
763
|
inputSchema: {
|
|
825
|
-
format: z.enum(["full", "compact"]).optional().default("full").describe("full =
|
|
764
|
+
format: z.enum(["full", "compact"]).optional().default("full").describe("full = detailed with graph, compact = summarized without edges"),
|
|
826
765
|
},
|
|
827
766
|
},
|
|
828
767
|
async ({ format }) => {
|
|
@@ -832,4 +771,57 @@ server.registerTool(
|
|
|
832
771
|
}
|
|
833
772
|
)
|
|
834
773
|
|
|
774
|
+
// ── memory_backup ───────────────────────────────────────────────────
|
|
775
|
+
|
|
776
|
+
server.registerTool(
|
|
777
|
+
"memory_backup",
|
|
778
|
+
{
|
|
779
|
+
title: "Backup Memory",
|
|
780
|
+
description: "Create a timestamped backup of the memory file. Use before major operations or to preserve state.",
|
|
781
|
+
inputSchema: {},
|
|
782
|
+
},
|
|
783
|
+
async () => {
|
|
784
|
+
if (!existsSync(MEMORY_FILE)) {
|
|
785
|
+
return { content: [{ type: "text" as const, text: "No memory file to backup" }] }
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
const backupDir = join(MEMORY_DIR, "backups")
|
|
789
|
+
if (!existsSync(backupDir)) mkdirSync(backupDir, { recursive: true })
|
|
790
|
+
|
|
791
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19)
|
|
792
|
+
const backupFile = join(backupDir, `memory-${timestamp}.toon`)
|
|
793
|
+
|
|
794
|
+
copyFileSync(MEMORY_FILE, backupFile)
|
|
795
|
+
|
|
796
|
+
// Also backup archive if it exists
|
|
797
|
+
let archiveBackup = ""
|
|
798
|
+
if (existsSync(ARCHIVE_FILE)) {
|
|
799
|
+
const archiveBackupFile = join(backupDir, `archive-${timestamp}.toon`)
|
|
800
|
+
copyFileSync(ARCHIVE_FILE, archiveBackupFile)
|
|
801
|
+
archiveBackup = `\n📦 Archive backed up`
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
// Prune old backups (keep last 10)
|
|
805
|
+
const backups = readdirSync(backupDir)
|
|
806
|
+
.filter((f) => f.startsWith("memory-") && f.endsWith(".toon"))
|
|
807
|
+
.sort()
|
|
808
|
+
.reverse()
|
|
809
|
+
for (const old of backups.slice(10)) {
|
|
810
|
+
try {
|
|
811
|
+
const { unlinkSync } = require("fs")
|
|
812
|
+
unlinkSync(join(backupDir, old))
|
|
813
|
+
} catch {
|
|
814
|
+
// ignore
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
return {
|
|
819
|
+
content: [{
|
|
820
|
+
type: "text" as const,
|
|
821
|
+
text: `💾 Memory backed up to ${basename(backupFile)}${archiveBackup}\n📦 ${backups.length} backup(s) retained`
|
|
822
|
+
}],
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
)
|
|
826
|
+
|
|
835
827
|
} // end registerTools
|