toon-memory 2.11.0 → 3.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.
- package/README.de.md +1209 -0
- package/README.es.md +1214 -0
- package/README.fr.md +1209 -0
- package/README.ja.md +1209 -0
- package/README.ko.md +1209 -0
- package/README.md +16 -3
- package/README.pt-br.md +1211 -0
- package/README.zh.md +1209 -0
- package/dist/cli/session-start.js +32 -1
- package/mcp/server.js +2857 -2230
- package/package.json +1 -1
- package/src/cli/session-start.ts +47 -1
- package/src/lib/graph.ts +4 -0
- package/src/lib/quality.ts +43 -9
- package/src/mcp/config.ts +2 -0
- package/src/mcp/consolidation.ts +52 -7
- package/src/mcp/scoring.ts +7 -2
- package/src/mcp/tools.ts +570 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "toon-memory",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "MCP server giving AI coding agents offline, token-efficient persistent memory — 21 tools, 80% fewer tool calls, recall decisions, patterns, and bugs across sessions. No LLM, no server; works with 15+ agents.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
package/src/cli/session-start.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { readFileSync } from "fs"
|
|
1
|
+
import { readFileSync, existsSync } from "fs"
|
|
2
|
+
import { join } from "path"
|
|
2
3
|
import { heartbeat, coordinationView, resolveSessionId, currentBranch, SESSION_TTL_MS } from "../lib/sessions"
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -8,11 +9,50 @@ import { heartbeat, coordinationView, resolveSessionId, currentBranch, SESSION_T
|
|
|
8
9
|
* 1. Registers this session's heartbeat (so parallel sessions can see it).
|
|
9
10
|
* 2. Prints a short reminder listing OTHER active sessions, their branches,
|
|
10
11
|
* and any soft file conflicts — so the agent doesn't clobber sibling work.
|
|
12
|
+
* 3. Injects the system primer (top memories by importance) so the agent
|
|
13
|
+
* starts with context without having to call memory_recall.
|
|
11
14
|
*
|
|
12
15
|
* No network, no LLM. Exits 0 always (a failing hook must never block the
|
|
13
16
|
* agent from starting).
|
|
14
17
|
*/
|
|
15
18
|
|
|
19
|
+
const MEMORY_DIR = join(process.cwd(), ".toon-memory", "memory")
|
|
20
|
+
const MEMORY_FILE = join(MEMORY_DIR, "data.toon")
|
|
21
|
+
|
|
22
|
+
/** Minimal system primer: top 5 memories by importance. ~50-100 tokens. */
|
|
23
|
+
function systemPrimer(): string {
|
|
24
|
+
if (!existsSync(MEMORY_FILE)) return ""
|
|
25
|
+
try {
|
|
26
|
+
const data = readFileSync(MEMORY_FILE, "utf-8")
|
|
27
|
+
const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|") && !l.startsWith(" summaries:"))
|
|
28
|
+
if (lines.length === 0) return ""
|
|
29
|
+
|
|
30
|
+
// Parse entries and sort by importance (accessed + recency)
|
|
31
|
+
const entries = lines
|
|
32
|
+
.map((l) => {
|
|
33
|
+
const p = l.trim().split("|")
|
|
34
|
+
if (p.length < 7) return null
|
|
35
|
+
const accessed = p.length > 8 ? parseInt(p[8]) || 0 : 0
|
|
36
|
+
const daysSince = (Date.now() - new Date(`${p[6]}T00:00:00`).getTime()) / 86400000
|
|
37
|
+
const recency = daysSince < 7 ? 3 : daysSince < 30 ? 2 : daysSince < 90 ? 1 : 0
|
|
38
|
+
return { key: p[2], content: p[3], category: p[1], tags: p[5] || "", importance: accessed + recency }
|
|
39
|
+
})
|
|
40
|
+
.filter(Boolean)
|
|
41
|
+
.sort((a, b) => b!.importance - a!.importance)
|
|
42
|
+
.slice(0, 5)
|
|
43
|
+
|
|
44
|
+
if (entries.length === 0) return ""
|
|
45
|
+
|
|
46
|
+
const lines2 = [" Top memories:"]
|
|
47
|
+
for (const e of entries) {
|
|
48
|
+
lines2.push(` [${e!.category}] ${e!.key}: ${e!.content.slice(0, 100)}`)
|
|
49
|
+
}
|
|
50
|
+
return lines2.join("\n")
|
|
51
|
+
} catch {
|
|
52
|
+
return ""
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
16
56
|
function main(): void {
|
|
17
57
|
const agent = process.argv[2] || "agent"
|
|
18
58
|
|
|
@@ -62,6 +102,12 @@ function main(): void {
|
|
|
62
102
|
lines.push(" No file conflicts detected.")
|
|
63
103
|
}
|
|
64
104
|
|
|
105
|
+
// Inject system primer (top memories) — automatic context at session start
|
|
106
|
+
const primer = systemPrimer()
|
|
107
|
+
if (primer) {
|
|
108
|
+
lines.push(primer)
|
|
109
|
+
}
|
|
110
|
+
|
|
65
111
|
lines.push(" Use memory_recall before reading files and memory_sessions to coordinate.")
|
|
66
112
|
console.log(lines.join("\n"))
|
|
67
113
|
process.exit(0)
|
package/src/lib/graph.ts
CHANGED
|
@@ -30,6 +30,8 @@ export interface GraphEntry {
|
|
|
30
30
|
accessed: number
|
|
31
31
|
/** Explicit outgoing links declared on the entry. */
|
|
32
32
|
links: string[]
|
|
33
|
+
/** ISO timestamp of last recall/access. Empty if never accessed. */
|
|
34
|
+
lastAccessed: string
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
export interface MemoryGraph {
|
|
@@ -49,6 +51,7 @@ const entryLines = (data: string): string[] =>
|
|
|
49
51
|
/**
|
|
50
52
|
* Parse TOON memory content into structured entries.
|
|
51
53
|
* Tolerant of the trailing `links` field (absent on older entries).
|
|
54
|
+
* Also handles the `lastAccessed` field (field 12) for access tracking.
|
|
52
55
|
*/
|
|
53
56
|
export function parseEntries(data: string): GraphEntry[] {
|
|
54
57
|
const out: GraphEntry[] = []
|
|
@@ -74,6 +77,7 @@ export function parseEntries(data: string): GraphEntry[] {
|
|
|
74
77
|
.split(/[\s;]+/)
|
|
75
78
|
.map((t) => t.trim())
|
|
76
79
|
.filter(Boolean),
|
|
80
|
+
lastAccessed: parts.length > 12 ? parts[12] || "" : "",
|
|
77
81
|
})
|
|
78
82
|
}
|
|
79
83
|
return out
|
package/src/lib/quality.ts
CHANGED
|
@@ -15,8 +15,24 @@ import { fuzzyMatch } from "./fuzzy"
|
|
|
15
15
|
/**
|
|
16
16
|
* Quality score for an entry (0..1). Pure heuristics, no LLM.
|
|
17
17
|
* Measures how well-structured and useful a memory entry is.
|
|
18
|
+
*
|
|
19
|
+
* Factors:
|
|
20
|
+
* - Tags (0.3 max): structured categorization
|
|
21
|
+
* - Links (0.2 max): graph connectivity
|
|
22
|
+
* - Content length (0.3 max): detail level
|
|
23
|
+
* - Recency (0.1 max): how recently created
|
|
24
|
+
* - Specificity (0.1 max): unique word ratio
|
|
25
|
+
* - Access frequency (0.15 max): how often recalled
|
|
26
|
+
* - Access recency (0.1 max): how recently accessed
|
|
18
27
|
*/
|
|
19
|
-
export function qualityScore(
|
|
28
|
+
export function qualityScore(
|
|
29
|
+
tags: string,
|
|
30
|
+
links: string,
|
|
31
|
+
content: string,
|
|
32
|
+
date: string,
|
|
33
|
+
accessed: number = 0,
|
|
34
|
+
lastAccessed: string = ""
|
|
35
|
+
): number {
|
|
20
36
|
let score = 0
|
|
21
37
|
if (tags) {
|
|
22
38
|
const count = tags.split(";").filter(Boolean).length
|
|
@@ -30,7 +46,7 @@ export function qualityScore(tags: string, links: string, content: string, date:
|
|
|
30
46
|
if (len > 20) score += 0.1
|
|
31
47
|
if (len > 60) score += 0.1
|
|
32
48
|
if (len > 150) score += 0.1
|
|
33
|
-
// Recency bonus
|
|
49
|
+
// Recency bonus (creation date)
|
|
34
50
|
if (date) {
|
|
35
51
|
const days = (Date.now() - new Date(`${date}T00:00:00`).getTime()) / 86400000
|
|
36
52
|
if (days < 7) score += 0.1
|
|
@@ -41,6 +57,17 @@ export function qualityScore(tags: string, links: string, content: string, date:
|
|
|
41
57
|
const unique = new Set(words)
|
|
42
58
|
const specificity = words.length > 0 ? unique.size / words.length : 0
|
|
43
59
|
score += specificity * 0.1
|
|
60
|
+
// Access frequency: entries recalled more often are more valuable
|
|
61
|
+
if (accessed > 0) {
|
|
62
|
+
score += Math.min(0.15, accessed * 0.03)
|
|
63
|
+
}
|
|
64
|
+
// Access recency: recently accessed entries are more relevant
|
|
65
|
+
if (lastAccessed) {
|
|
66
|
+
const daysSinceAccess = (Date.now() - new Date(lastAccessed).getTime()) / 86400000
|
|
67
|
+
if (daysSinceAccess < 1) score += 0.1
|
|
68
|
+
else if (daysSinceAccess < 7) score += 0.07
|
|
69
|
+
else if (daysSinceAccess < 30) score += 0.03
|
|
70
|
+
}
|
|
44
71
|
return Math.min(1, score)
|
|
45
72
|
}
|
|
46
73
|
|
|
@@ -80,19 +107,24 @@ export function mergeEntries(existingLine: string, newLine: string): string {
|
|
|
80
107
|
const newAccessed = np.length > 8 ? parseInt(np[8]) || 0 : 0
|
|
81
108
|
const accessed = Math.max(existingAccessed, newAccessed)
|
|
82
109
|
|
|
110
|
+
// Merge links (union)
|
|
111
|
+
const existingLinks = (ep[9] || "").split(/[\s;]+/).filter(Boolean)
|
|
112
|
+
const newLinks = (np[9] || "").split(/[\s;]+/).filter(Boolean)
|
|
113
|
+
const mergedLinks = [...new Set([...existingLinks, ...newLinks])].join(" ")
|
|
114
|
+
|
|
83
115
|
// Take max confidence — handle entries with <12 fields gracefully
|
|
84
116
|
const existingConf = ep.length > 11 ? parseFloat(ep[11]) || 0 : 0
|
|
85
117
|
const newConf = np.length > 11 ? parseFloat(np[11]) || 0 : 0
|
|
86
118
|
const confidence = Math.max(existingConf, newConf)
|
|
87
119
|
|
|
88
|
-
//
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
const
|
|
120
|
+
// Take most recent lastAccessed (field 12)
|
|
121
|
+
const existingLastAccessed = ep.length > 12 ? ep[12] || "" : ""
|
|
122
|
+
const newLastAccessed = np.length > 12 ? np[12] || "" : ""
|
|
123
|
+
const lastAccessed = newLastAccessed > existingLastAccessed ? newLastAccessed : existingLastAccessed
|
|
92
124
|
|
|
93
|
-
const quality = qualityScore(mergedTags, mergedLinks, content, date)
|
|
125
|
+
const quality = qualityScore(mergedTags, mergedLinks, content, date, accessed, lastAccessed)
|
|
94
126
|
|
|
95
|
-
return `${id}|${category}|${key}|${content}|${file}|${mergedTags}|${date}|${ttl}|${accessed}|${mergedLinks}|${quality.toFixed(2)}|${confidence}`
|
|
127
|
+
return `${id}|${category}|${key}|${content}|${file}|${mergedTags}|${date}|${ttl}|${accessed}|${mergedLinks}|${quality.toFixed(2)}|${confidence}|${lastAccessed}`
|
|
96
128
|
}
|
|
97
129
|
|
|
98
130
|
/**
|
|
@@ -194,7 +226,9 @@ export function generateSystemPrimer(data: string): string {
|
|
|
194
226
|
e.tags.join(";"),
|
|
195
227
|
e.links.join(" "),
|
|
196
228
|
e.content,
|
|
197
|
-
e.date
|
|
229
|
+
e.date,
|
|
230
|
+
e.accessed,
|
|
231
|
+
e.lastAccessed
|
|
198
232
|
)
|
|
199
233
|
const conf = quality >= 0.5 ? "high" : quality >= 0.3 ? "medium" : "low"
|
|
200
234
|
lines.push(
|
package/src/mcp/config.ts
CHANGED
|
@@ -39,6 +39,8 @@ export interface MemoryConfig {
|
|
|
39
39
|
vocab?: Record<string, string[]>
|
|
40
40
|
/** Maximum active entries before auto-archive (default 100). */
|
|
41
41
|
maxEntries?: number
|
|
42
|
+
/** Verbatim mode: skip tag inference and quality scoring at write time. */
|
|
43
|
+
verbatim?: boolean
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
/**
|
package/src/mcp/consolidation.ts
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
1
|
import { readMemory, writeMemory } from "./memory-io"
|
|
2
2
|
import { mergeEntries } from "../lib/quality"
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Jaccard similarity between two content strings (0..1).
|
|
6
|
+
* Normalizes whitespace and case before comparing word sets.
|
|
7
|
+
*/
|
|
8
|
+
function contentSimilarity(a: string, b: string): number {
|
|
9
|
+
const wordsA = new Set(a.toLowerCase().replace(/\s+/g, " ").trim().split(" "))
|
|
10
|
+
const wordsB = new Set(b.toLowerCase().replace(/\s+/g, " ").trim().split(" "))
|
|
11
|
+
if (wordsA.size === 0 && wordsB.size === 0) return 1
|
|
12
|
+
if (wordsA.size === 0 || wordsB.size === 0) return 0
|
|
13
|
+
let intersection = 0
|
|
14
|
+
for (const w of wordsA) {
|
|
15
|
+
if (wordsB.has(w)) intersection++
|
|
16
|
+
}
|
|
17
|
+
const union = wordsA.size + wordsB.size - intersection
|
|
18
|
+
return union > 0 ? intersection / union : 0
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Threshold for near-duplicate detection (0..1). */
|
|
22
|
+
const NEAR_DUP_THRESHOLD = 0.7
|
|
23
|
+
|
|
4
24
|
/**
|
|
5
25
|
* Deterministic consolidation: merge entries with the same key (combining
|
|
6
26
|
* tags, links, max confidence, latest date), then remove exact-content
|
|
7
|
-
* duplicates. No LLM involved.
|
|
27
|
+
* duplicates and merge near-duplicates (similarity > 0.7). No LLM involved.
|
|
8
28
|
*/
|
|
9
29
|
export function consolidateEntries(): { removed: number; kept: number; duplicates: string[] } {
|
|
10
30
|
const data = readMemory()
|
|
@@ -38,8 +58,8 @@ export function consolidateEntries(): { removed: number; kept: number; duplicate
|
|
|
38
58
|
}
|
|
39
59
|
}
|
|
40
60
|
|
|
41
|
-
// Phase 2: Remove exact-content duplicates
|
|
42
|
-
const contentSeen = new Map<string, string>()
|
|
61
|
+
// Phase 2: Remove exact-content duplicates AND merge near-duplicates
|
|
62
|
+
const contentSeen = new Map<string, { line: string; idx: number }>()
|
|
43
63
|
const order: string[] = []
|
|
44
64
|
const duplicates: string[] = []
|
|
45
65
|
|
|
@@ -50,13 +70,38 @@ export function consolidateEntries(): { removed: number; kept: number; duplicate
|
|
|
50
70
|
order.push(line)
|
|
51
71
|
continue
|
|
52
72
|
}
|
|
53
|
-
const content =
|
|
54
|
-
|
|
73
|
+
const content = parts[3] || ""
|
|
74
|
+
const normalized = content.toLowerCase().replace(/\s+/g, " ").trim()
|
|
75
|
+
|
|
76
|
+
// Check for exact duplicate first
|
|
77
|
+
if (contentSeen.has(normalized)) {
|
|
55
78
|
duplicates.push(parts[2])
|
|
56
79
|
continue
|
|
57
80
|
}
|
|
58
|
-
|
|
59
|
-
|
|
81
|
+
|
|
82
|
+
// Check for near-duplicate (same category, similar content)
|
|
83
|
+
let merged = false
|
|
84
|
+
for (const [seenNorm, existing] of contentSeen) {
|
|
85
|
+
const existingParts = existing.line.split("|")
|
|
86
|
+
if (existingParts[1] !== parts[1]) continue // different category
|
|
87
|
+
const sim = contentSimilarity(content, existingParts[3] || "")
|
|
88
|
+
if (sim >= NEAR_DUP_THRESHOLD) {
|
|
89
|
+
// Merge into the existing entry (keep the one with higher quality)
|
|
90
|
+
const mergedLine = mergeEntries(existing.line, line)
|
|
91
|
+
contentSeen.set(seenNorm, { line: mergedLine, idx: existing.idx })
|
|
92
|
+
// Replace in order array
|
|
93
|
+
const orderIdx = order.findIndex((l) => l === existing.line)
|
|
94
|
+
if (orderIdx !== -1) order[orderIdx] = mergedLine
|
|
95
|
+
duplicates.push(parts[2])
|
|
96
|
+
merged = true
|
|
97
|
+
break
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!merged) {
|
|
102
|
+
contentSeen.set(normalized, { line, idx: order.length })
|
|
103
|
+
order.push(line)
|
|
104
|
+
}
|
|
60
105
|
}
|
|
61
106
|
|
|
62
107
|
const totalRemoved = mergedAway.length + duplicates.length
|
package/src/mcp/scoring.ts
CHANGED
|
@@ -48,8 +48,9 @@ export function findRelatedEntries(text: string, excludeKey: string = "", limit:
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
/**
|
|
51
|
-
* Increment the `accessed` counter
|
|
52
|
-
* Used by recall/suggest so frequently-used
|
|
51
|
+
* Increment the `accessed` counter and update `lastAccessed` timestamp
|
|
52
|
+
* for the given entry ids. Used by recall/suggest so frequently-used
|
|
53
|
+
* memories rank higher.
|
|
53
54
|
*/
|
|
54
55
|
export function bumpAccessed(ids: string[]): void {
|
|
55
56
|
if (ids.length === 0) return
|
|
@@ -59,6 +60,7 @@ export function bumpAccessed(ids: string[]): void {
|
|
|
59
60
|
const headerIdx = lines.findIndex((l) => l.startsWith("entries[") || /^\[\d+\|]/.test(l))
|
|
60
61
|
if (headerIdx === -1) return
|
|
61
62
|
|
|
63
|
+
const now = new Date().toISOString()
|
|
62
64
|
for (let i = headerIdx + 1; i < lines.length; i++) {
|
|
63
65
|
const line = lines[i]
|
|
64
66
|
if (!line.startsWith(" ") || !line.includes("|")) continue
|
|
@@ -67,6 +69,9 @@ export function bumpAccessed(ids: string[]): void {
|
|
|
67
69
|
if (idSet.has(parts[0])) {
|
|
68
70
|
const accessed = parts.length > 8 ? (parseInt(parts[8]) || 0) + 1 : 1
|
|
69
71
|
parts[8] = String(accessed)
|
|
72
|
+
// Ensure we have enough fields for lastAccessed (field 12)
|
|
73
|
+
while (parts.length < 13) parts.push("")
|
|
74
|
+
parts[12] = now
|
|
70
75
|
lines[i] = ` ${parts.join("|")}`
|
|
71
76
|
}
|
|
72
77
|
}
|