toon-memory 2.8.0 → 2.9.1
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 +2 -2
- 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/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "toon-memory",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "MCP server giving AI coding agents offline, token-efficient persistent memory —
|
|
3
|
+
"version": "2.9.1",
|
|
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": {
|
|
7
7
|
"toon-memory": "./bin/toon-memory.js"
|
package/src/cli/agents.ts
CHANGED
|
@@ -72,8 +72,9 @@ export function detectAgents(): Agent[] {
|
|
|
72
72
|
local: join(projectRoot, ".cursor", "mcp.json"),
|
|
73
73
|
mcpKey: "mcpServers",
|
|
74
74
|
format: "json",
|
|
75
|
-
needsHooks:
|
|
76
|
-
needsInstructions: false
|
|
75
|
+
needsHooks: true,
|
|
76
|
+
needsInstructions: false,
|
|
77
|
+
captureJson: ["PostToolUse", "Stop"]
|
|
77
78
|
})
|
|
78
79
|
|
|
79
80
|
agents.push({
|
|
@@ -81,8 +82,9 @@ export function detectAgents(): Agent[] {
|
|
|
81
82
|
global: join(HOME, ".codeium", "windsurf", "mcp_config.json"),
|
|
82
83
|
mcpKey: "mcpServers",
|
|
83
84
|
format: "json",
|
|
84
|
-
needsHooks:
|
|
85
|
-
needsInstructions: false
|
|
85
|
+
needsHooks: true,
|
|
86
|
+
needsInstructions: false,
|
|
87
|
+
captureJson: ["PostToolUse", "Stop"]
|
|
86
88
|
})
|
|
87
89
|
|
|
88
90
|
agents.push({
|
|
@@ -121,7 +123,8 @@ export function detectAgents(): Agent[] {
|
|
|
121
123
|
format: "json",
|
|
122
124
|
needsHooks: true,
|
|
123
125
|
needsInstructions: true,
|
|
124
|
-
instructionFile: join(projectRoot, ".gemini", "GEMINI.md")
|
|
126
|
+
instructionFile: join(projectRoot, ".gemini", "GEMINI.md"),
|
|
127
|
+
captureJson: ["PostToolUse", "Stop"]
|
|
125
128
|
})
|
|
126
129
|
|
|
127
130
|
agents.push({
|
|
@@ -20,10 +20,10 @@ export function captureToggle(enable: boolean): void {
|
|
|
20
20
|
writeFileSync(CAPTURE_CONFIG, JSON.stringify(config, null, 2))
|
|
21
21
|
|
|
22
22
|
if (enable) {
|
|
23
|
-
console.log("\n🔴
|
|
24
|
-
console.log("
|
|
25
|
-
console.log("
|
|
23
|
+
console.log("\n🔴 Activity capture ENABLED.")
|
|
24
|
+
console.log("Hooks will write observations to .toon-memory/memory/observations.toon.")
|
|
25
|
+
console.log("Review with `memory_captured` and promote with `memory_remember`.\n")
|
|
26
26
|
} else {
|
|
27
|
-
console.log("\n⚪
|
|
27
|
+
console.log("\n⚪ Activity capture DISABLED.\n")
|
|
28
28
|
}
|
|
29
29
|
}
|
package/src/cli/capture.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { join } from "path"
|
|
|
3
3
|
import { createHash } from "crypto"
|
|
4
4
|
import { withLockSync, atomicWrite } from "../lib/lock"
|
|
5
5
|
import { heartbeat, endSession, resolveSessionId, currentBranch } from "../lib/sessions"
|
|
6
|
+
import { pruneObservations } from "../mcp/observations"
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Hook capture script for toon-memory.
|
|
@@ -181,6 +182,10 @@ function main(): void {
|
|
|
181
182
|
if (!isStop && !toolName) process.exit(0)
|
|
182
183
|
|
|
183
184
|
appendObservation(`${ts}|${sessionId}|${agent}|${branch}|${effectiveTool}|${hash}|${file}|${summary}`)
|
|
185
|
+
|
|
186
|
+
// Auto-prune observations log to prevent unbounded growth
|
|
187
|
+
pruneObservations()
|
|
188
|
+
|
|
184
189
|
process.exit(0)
|
|
185
190
|
}
|
|
186
191
|
|
package/src/cli/commands.ts
CHANGED
|
@@ -17,9 +17,9 @@ const CAPTURE_CONFIG = join(MEMORY_DIR, "config.json")
|
|
|
17
17
|
/** Interactive multi-select for agents using @inquirer/prompts. */
|
|
18
18
|
export async function promptAgentSelection(agents: Agent[]): Promise<Agent[]> {
|
|
19
19
|
return checkbox({
|
|
20
|
-
message: "
|
|
20
|
+
message: "Which agents do you want to configure?",
|
|
21
21
|
choices: agents.map((a) => ({
|
|
22
|
-
name: `${a.name} (${a.format === "none" ? "
|
|
22
|
+
name: `${a.name} (${a.format === "none" ? "instructions" : a.global ? "local/global" : "local only"})`,
|
|
23
23
|
value: a,
|
|
24
24
|
checked: true
|
|
25
25
|
}))
|
|
@@ -75,23 +75,23 @@ export async function init(scope?: string, agentFilter?: string[]): Promise<void
|
|
|
75
75
|
return
|
|
76
76
|
}
|
|
77
77
|
finalScope = await select({
|
|
78
|
-
message: "
|
|
78
|
+
message: "Scope:",
|
|
79
79
|
choices: [
|
|
80
|
-
{ name: "Local (
|
|
80
|
+
{ name: "Local (project)", value: "local" },
|
|
81
81
|
{ name: "Global (~home)", value: "global" }
|
|
82
82
|
],
|
|
83
83
|
default: finalScope as "local" | "global"
|
|
84
84
|
})
|
|
85
|
-
const ok = await confirm({ message: "
|
|
85
|
+
const ok = await confirm({ message: "Proceed?", default: true })
|
|
86
86
|
if (!ok) {
|
|
87
|
-
console.log("\
|
|
87
|
+
console.log("\nInstallation cancelled.\n")
|
|
88
88
|
return
|
|
89
89
|
}
|
|
90
90
|
} else {
|
|
91
91
|
selected = agents
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
console.log(`\
|
|
94
|
+
console.log(`\nInstalling (${finalScope}) — ${selected.map((a) => a.name).join(", ")}...\n`)
|
|
95
95
|
|
|
96
96
|
for (const agent of selected) {
|
|
97
97
|
installForAgent(agent, finalScope)
|
|
@@ -485,33 +485,33 @@ export function dumpMemory(): void {
|
|
|
485
485
|
*/
|
|
486
486
|
export async function interactiveInstall(): Promise<void> {
|
|
487
487
|
if (!process.stdin.isTTY) {
|
|
488
|
-
console.log("\n🧠 toon-memory —
|
|
489
|
-
console.log("
|
|
488
|
+
console.log("\n🧠 toon-memory — interactive installation requires a terminal.")
|
|
489
|
+
console.log("Run 'toon-memory init [--agent <name>]' for non-interactive installation.\n")
|
|
490
490
|
return
|
|
491
491
|
}
|
|
492
492
|
|
|
493
493
|
const agents = detectAgents()
|
|
494
494
|
const selected = await promptAgentSelection(agents)
|
|
495
495
|
if (selected.length === 0) {
|
|
496
|
-
console.log("\nNo
|
|
496
|
+
console.log("\nNo agents selected. Nothing installed.\n")
|
|
497
497
|
return
|
|
498
498
|
}
|
|
499
499
|
|
|
500
500
|
const scope = await select({
|
|
501
|
-
message: "
|
|
501
|
+
message: "Scope:",
|
|
502
502
|
choices: [
|
|
503
|
-
{ name: "Local (
|
|
503
|
+
{ name: "Local (project)", value: "local" },
|
|
504
504
|
{ name: "Global (~home)", value: "global" }
|
|
505
505
|
]
|
|
506
506
|
})
|
|
507
507
|
|
|
508
|
-
const ok = await confirm({ message: "
|
|
508
|
+
const ok = await confirm({ message: "Proceed?", default: true })
|
|
509
509
|
if (!ok) {
|
|
510
|
-
console.log("\
|
|
510
|
+
console.log("\nInstallation cancelled.\n")
|
|
511
511
|
return
|
|
512
512
|
}
|
|
513
513
|
|
|
514
|
-
console.log(`\
|
|
514
|
+
console.log(`\nInstalling (${scope}) — ${selected.map((a) => a.name).join(", ")}...\n`)
|
|
515
515
|
installMemoryDir()
|
|
516
516
|
|
|
517
517
|
const deps = extractProjectDeps(projectRoot)
|
package/src/cli/constants.ts
CHANGED
|
@@ -52,5 +52,5 @@ Persistent memory for this project. Use it to avoid re-investigating things.
|
|
|
52
52
|
*/
|
|
53
53
|
export const OPENCODE_PASSIVE_INSTRUCTION = `# toon-memory
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
Memory auto-injects via .opencode/instructions/memory-autoload.md; use memory_recall only as fallback.
|
|
56
56
|
`
|
package/src/cli/session-start.ts
CHANGED
|
@@ -41,28 +41,28 @@ function main(): void {
|
|
|
41
41
|
const others = view.active.filter((s) => s.id !== sessionId && !s.ended)
|
|
42
42
|
const lines: string[] = []
|
|
43
43
|
lines.push("toon-memory:")
|
|
44
|
-
lines.push(`
|
|
44
|
+
lines.push(` This session: ${agent} @ ${currentBranch()} (window ${Math.round(SESSION_TTL_MS / 60000)} min)`)
|
|
45
45
|
|
|
46
46
|
if (others.length > 0) {
|
|
47
|
-
lines.push(`
|
|
47
|
+
lines.push(` Other active sessions (${others.length}):`)
|
|
48
48
|
for (const s of others) {
|
|
49
49
|
const files = Object.keys(s.files).slice(0, 5).join(", ") || "—"
|
|
50
50
|
lines.push(` • ${s.agent} @ ${s.branch} — ${files}`)
|
|
51
51
|
}
|
|
52
52
|
} else {
|
|
53
|
-
lines.push("
|
|
53
|
+
lines.push(" No other active sessions.")
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
if (view.conflicts.length > 0) {
|
|
57
|
-
lines.push(` ⚠️
|
|
57
|
+
lines.push(` ⚠️ Soft conflicts (${view.conflicts.length}): use memory_sessions for details.`)
|
|
58
58
|
for (const c of view.conflicts) {
|
|
59
59
|
lines.push(` • ${c.file}`)
|
|
60
60
|
}
|
|
61
61
|
} else {
|
|
62
|
-
lines.push("
|
|
62
|
+
lines.push(" No file conflicts detected.")
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
lines.push("
|
|
65
|
+
lines.push(" Use memory_recall before reading files and memory_sessions to coordinate.")
|
|
66
66
|
console.log(lines.join("\n"))
|
|
67
67
|
process.exit(0)
|
|
68
68
|
}
|
package/src/cli/setup.ts
CHANGED
|
@@ -78,17 +78,17 @@ if (args.length === 0) {
|
|
|
78
78
|
process.exit(0)
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
console.log(`
|
|
81
|
+
console.log(`Unknown command: '${args[0]}'\n`)
|
|
82
82
|
printUsage()
|
|
83
83
|
process.exit(1)
|
|
84
84
|
|
|
85
85
|
/** Minimal usage string for unknown commands. */
|
|
86
86
|
function printUsage(): void {
|
|
87
|
-
console.log(`
|
|
88
|
-
|
|
89
|
-
Init: toon-memory init [--agent <
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
87
|
+
console.log(`Usage: toon-memory <command> [options]
|
|
88
|
+
Commands: init, status, stats, export, import, watch, uninstall, capture, upgrade, mcp
|
|
89
|
+
Init: toon-memory init [--agent <name> --agent <name>...] [--scope local|global]
|
|
90
|
+
Without --agent: interactive agent and scope selector.
|
|
91
|
+
With --agent: non-interactive installation (default: scope local).
|
|
92
|
+
Options: -v/--version, -h/--help
|
|
93
|
+
No arguments starts the interactive installer.`)
|
|
94
94
|
}
|
package/src/lib/context.ts
CHANGED
|
@@ -31,7 +31,7 @@ import { findFilesByPattern, searchCode, findCallers } from "./code-search"
|
|
|
31
31
|
*/
|
|
32
32
|
function formatOverview(data: string): string {
|
|
33
33
|
const entries = parseEntries(data)
|
|
34
|
-
if (entries.length === 0) return "
|
|
34
|
+
if (entries.length === 0) return "Memory: empty"
|
|
35
35
|
|
|
36
36
|
const byCategory: Record<string, number> = {}
|
|
37
37
|
let expired = 0
|
|
@@ -43,9 +43,9 @@ function formatOverview(data: string): string {
|
|
|
43
43
|
const cats = Object.entries(byCategory)
|
|
44
44
|
.map(([k, v]) => `${k}:${v}`)
|
|
45
45
|
.join(" ")
|
|
46
|
-
const ttlNote = expired > 0 ? ` | ${expired}
|
|
46
|
+
const ttlNote = expired > 0 ? ` | ${expired} expired` : ""
|
|
47
47
|
|
|
48
|
-
return `##
|
|
48
|
+
return `## Memory (${entries.length} entries)${ttlNote}\n${cats}`
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/**
|
|
@@ -95,7 +95,7 @@ function formatRelevantEntries(data: string, task: string, limit: number = 6): s
|
|
|
95
95
|
{ adjacency: subAdj, seeds, snippetLen: 80 }
|
|
96
96
|
)
|
|
97
97
|
|
|
98
|
-
return `##
|
|
98
|
+
return `## Relevant entries\n${rendered}`
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
/**
|
|
@@ -114,7 +114,7 @@ function formatPatterns(data: string): string {
|
|
|
114
114
|
const lines = patterns.map(
|
|
115
115
|
(e) => `- **${e.key}**: ${e.content.slice(0, 120)}`
|
|
116
116
|
)
|
|
117
|
-
return `##
|
|
117
|
+
return `## Established patterns\n${lines.join("\n")}`
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
/**
|
|
@@ -126,19 +126,19 @@ function formatSessions(): string {
|
|
|
126
126
|
const branch = currentBranch()
|
|
127
127
|
|
|
128
128
|
if (active.length === 0 && conflicts.length === 0) {
|
|
129
|
-
return `##
|
|
129
|
+
return `## Sessions\nBranch: ${branch} | No other active sessions`
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
const lines: string[] = [`##
|
|
132
|
+
const lines: string[] = [`## Sessions (${active.length} active) — branch: ${branch}`]
|
|
133
133
|
|
|
134
134
|
for (const s of active.slice(0, 5)) {
|
|
135
135
|
const mins = Math.max(0, Math.round(s.ageMs / 60000))
|
|
136
136
|
const files = Object.keys(s.files).length
|
|
137
|
-
lines.push(`- ${s.agent}@${s.branch} (${mins}min, ${files}
|
|
137
|
+
lines.push(`- ${s.agent}@${s.branch} (${mins}min, ${files} files)`)
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
if (conflicts.length > 0) {
|
|
141
|
-
lines.push("", `**
|
|
141
|
+
lines.push("", `**Soft conflicts (${conflicts.length}):**`)
|
|
142
142
|
for (const c of conflicts.slice(0, 3)) {
|
|
143
143
|
const who = c.sessions.map((s) => s.agent).join(" + ")
|
|
144
144
|
lines.push(`- ${c.file} ↔ ${who}`)
|
|
@@ -158,21 +158,21 @@ function formatHealth(data: string): string {
|
|
|
158
158
|
|
|
159
159
|
const expired = entries.filter((e) => e.ttl && isExpiredLocal(e.ttl))
|
|
160
160
|
if (expired.length > 0) {
|
|
161
|
-
warnings.push(`${expired.length}
|
|
161
|
+
warnings.push(`${expired.length} entries with expired TTL (run memory_archive)`)
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
if (entries.length > 80) {
|
|
165
|
-
warnings.push(`${entries.length}
|
|
165
|
+
warnings.push(`${entries.length} entries (near limit of 100, auto-archive will trigger)`)
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
const orphanLinks = findOrphanLinks(entries)
|
|
169
169
|
if (orphanLinks.length > 0) {
|
|
170
|
-
warnings.push(`${orphanLinks.length} links
|
|
170
|
+
warnings.push(`${orphanLinks.length} orphan links: ${orphanLinks.slice(0, 3).join(", ")}`)
|
|
171
171
|
}
|
|
172
172
|
|
|
173
173
|
if (warnings.length === 0) return ""
|
|
174
174
|
|
|
175
|
-
return `##
|
|
175
|
+
return `## Health\n⚠️ ${warnings.join("\n⚠️ ")}`
|
|
176
176
|
}
|
|
177
177
|
|
|
178
178
|
/**
|
|
@@ -252,7 +252,7 @@ function formatTopEntries(data: string, limit: number): string {
|
|
|
252
252
|
if (top.length === 0) return ""
|
|
253
253
|
|
|
254
254
|
const rendered = renderCompact(top)
|
|
255
|
-
return `## Top
|
|
255
|
+
return `## Top memories\n${rendered}`
|
|
256
256
|
}
|
|
257
257
|
|
|
258
258
|
// ── context_generate ─────────────────────────────────────────────────
|
|
@@ -272,9 +272,9 @@ export function generateContextGenerate(data: string, root: string, opts: Contex
|
|
|
272
272
|
// 1. Project manifest
|
|
273
273
|
const manifest = readManifest(root)
|
|
274
274
|
if (manifest) {
|
|
275
|
-
const deps = manifest.deps.length > 0 ? manifest.deps.slice(0, 15).join(", ") : "
|
|
275
|
+
const deps = manifest.deps.length > 0 ? manifest.deps.slice(0, 15).join(", ") : "none"
|
|
276
276
|
const version = manifest.version ? ` v${manifest.version}` : ""
|
|
277
|
-
sections.push(`##
|
|
277
|
+
sections.push(`## Project\n${manifest.name}${version} (${manifest.language})\nDeps: ${deps}`)
|
|
278
278
|
}
|
|
279
279
|
|
|
280
280
|
// 2. Structure
|
|
@@ -287,9 +287,9 @@ export function generateContextGenerate(data: string, root: string, opts: Contex
|
|
|
287
287
|
.slice(0, 5)
|
|
288
288
|
.map(([k, v]) => `${k}:${v}`)
|
|
289
289
|
.join(" ")
|
|
290
|
-
const src = `${structure.sourceFileCount}
|
|
290
|
+
const src = `${structure.sourceFileCount} source files`
|
|
291
291
|
const parts = [dirs, files, `${src} ${exts ? `(${exts})` : ""}`].filter(Boolean)
|
|
292
|
-
if (parts.length > 0) sections.push(`##
|
|
292
|
+
if (parts.length > 0) sections.push(`## Structure\n${parts.join("\n")}`)
|
|
293
293
|
}
|
|
294
294
|
|
|
295
295
|
// 3. Environment variables
|
|
@@ -304,8 +304,8 @@ export function generateContextGenerate(data: string, root: string, opts: Contex
|
|
|
304
304
|
if (git.branch !== "unknown") {
|
|
305
305
|
const recent = git.recentCommits.slice(0, 3)
|
|
306
306
|
const commitLines = recent.map((c) => `- ${c.shortHash} ${c.subject}`)
|
|
307
|
-
const parts = [`
|
|
308
|
-
if (commitLines.length > 0) parts.push(
|
|
307
|
+
const parts = [`Branch: ${git.branch}`, `Tracked files: ${git.trackedFiles}`]
|
|
308
|
+
if (commitLines.length > 0) parts.push(`Recent commits:\n${commitLines.join("\n")}`)
|
|
309
309
|
sections.push(`## Git\n${parts.join("\n")}`)
|
|
310
310
|
}
|
|
311
311
|
|
|
@@ -332,7 +332,7 @@ export function generateContextGenerate(data: string, root: string, opts: Contex
|
|
|
332
332
|
// ── context_diff ─────────────────────────────────────────────────────
|
|
333
333
|
|
|
334
334
|
/**
|
|
335
|
-
* What changed since the last session: git commits + memory changes + touched files.
|
|
335
|
+
* What changed since the last session: git commits + memory changes + touched files + session activity.
|
|
336
336
|
* ~200-400 tokens. Zero LLM.
|
|
337
337
|
*/
|
|
338
338
|
export function generateContextDiff(data: string, root: string, since?: string): string {
|
|
@@ -354,7 +354,7 @@ export function generateContextDiff(data: string, root: string, since?: string):
|
|
|
354
354
|
// 2. Modified files from git index
|
|
355
355
|
const tracked = readGitIndex(root)
|
|
356
356
|
if (tracked.length > 0) {
|
|
357
|
-
sections.push(`##
|
|
357
|
+
sections.push(`## Tracked files (${tracked.length})\n${tracked.slice(0, 20).join("\n")}`)
|
|
358
358
|
}
|
|
359
359
|
|
|
360
360
|
// 3. Memory changes (reuse existing diff logic)
|
|
@@ -366,19 +366,33 @@ export function generateContextDiff(data: string, root: string, since?: string):
|
|
|
366
366
|
const created = recentEntries.filter((e) => e.date === today)
|
|
367
367
|
const updated = recentEntries.filter((e) => e.date !== today)
|
|
368
368
|
const parts: string[] = []
|
|
369
|
-
if (created.length > 0) parts.push(`
|
|
370
|
-
if (updated.length > 0) parts.push(`
|
|
371
|
-
if (parts.length > 0) sections.push(`##
|
|
369
|
+
if (created.length > 0) parts.push(`New: ${created.map((e) => e.key).join(", ")}`)
|
|
370
|
+
if (updated.length > 0) parts.push(`Updated: ${updated.map((e) => e.key).join(", ")}`)
|
|
371
|
+
if (parts.length > 0) sections.push(`## Memory (${recentEntries.length} changes)\n${parts.join("\n")}`)
|
|
372
372
|
}
|
|
373
373
|
|
|
374
|
-
// 4.
|
|
375
|
-
const
|
|
376
|
-
if (
|
|
377
|
-
const lines =
|
|
378
|
-
|
|
374
|
+
// 4. Session activity with file details
|
|
375
|
+
const { active, conflicts } = coordinationView()
|
|
376
|
+
if (active.length > 0) {
|
|
377
|
+
const lines = active.slice(0, 5).map((s) => {
|
|
378
|
+
const mins = Math.max(0, Math.round(s.ageMs / 60000))
|
|
379
|
+
const files = Object.keys(s.files)
|
|
380
|
+
const fileList = files.length > 0 ? ` — ${files.slice(0, 3).join(", ")}${files.length > 3 ? ` +${files.length - 3}` : ""}` : ""
|
|
381
|
+
return `- ${s.agent}@${s.branch} (${mins}min ago)${fileList}`
|
|
382
|
+
})
|
|
383
|
+
sections.push(`## Active sessions (${active.length})\n${lines.join("\n")}`)
|
|
379
384
|
}
|
|
380
385
|
|
|
381
|
-
|
|
386
|
+
// 5. Soft conflicts between sessions
|
|
387
|
+
if (conflicts.length > 0) {
|
|
388
|
+
const lines = conflicts.slice(0, 3).map((c) => {
|
|
389
|
+
const who = c.sessions.map((s) => s.agent).join(" + ")
|
|
390
|
+
return `- ${c.file} ↔ ${who}`
|
|
391
|
+
})
|
|
392
|
+
sections.push(`## Session conflicts (${conflicts.length})\n${lines.join("\n")}`)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (sections.length === 0) return "No recent changes."
|
|
382
396
|
return sections.filter(Boolean).join("\n\n")
|
|
383
397
|
}
|
|
384
398
|
|
|
@@ -401,17 +415,23 @@ export function generateContextFocus(data: string, root: string, task: string, o
|
|
|
401
415
|
const relevant = formatRelevantEntries(data, task, limit)
|
|
402
416
|
if (relevant) sections.push(relevant)
|
|
403
417
|
|
|
404
|
-
// 2. Extract keywords from task
|
|
418
|
+
// 2. Extract keywords and filenames from task
|
|
405
419
|
const keywords = task.toLowerCase().split(/\s+/).filter((w) => w.length > 2)
|
|
406
420
|
|
|
407
|
-
//
|
|
421
|
+
// Also extract file-like patterns (e.g. "auth.ts", "src/index.js")
|
|
422
|
+
const filePatterns = task.toLowerCase().match(/[\w/.-]+\.[a-z]{1,4}/g) || []
|
|
423
|
+
// Extract path-like patterns (e.g. "src/auth", "lib/utils")
|
|
424
|
+
const pathPatterns = task.toLowerCase().match(/[\w/.-]+\/[\w/.-]+/g) || []
|
|
425
|
+
|
|
426
|
+
// 3. Related files - search by keywords, file patterns, and path patterns
|
|
408
427
|
const allFiles: string[] = []
|
|
409
|
-
|
|
428
|
+
const searchTerms = [...keywords, ...filePatterns, ...pathPatterns]
|
|
429
|
+
for (const kw of searchTerms) {
|
|
410
430
|
allFiles.push(...findFilesByPattern(root, kw))
|
|
411
431
|
}
|
|
412
432
|
const uniqueFiles = [...new Set(allFiles)].slice(0, 10)
|
|
413
433
|
if (uniqueFiles.length > 0) {
|
|
414
|
-
sections.push(`##
|
|
434
|
+
sections.push(`## Related files\n${uniqueFiles.join("\n")}`)
|
|
415
435
|
}
|
|
416
436
|
|
|
417
437
|
// 4. Code references (callers and matches)
|
|
@@ -419,23 +439,24 @@ export function generateContextFocus(data: string, root: string, task: string, o
|
|
|
419
439
|
const callers = findCallers(root, kw)
|
|
420
440
|
if (callers.length > 0) {
|
|
421
441
|
const lines = callers.slice(0, 5).map((c) => `${c.file}:${c.line} — ${c.content.slice(0, 80)}`)
|
|
422
|
-
sections.push(`##
|
|
442
|
+
sections.push(`## References: ${kw}\n${lines.join("\n")}`)
|
|
423
443
|
}
|
|
424
444
|
}
|
|
425
445
|
|
|
426
|
-
// 5. Related tests
|
|
446
|
+
// 5. Related tests - search by keywords and file patterns
|
|
427
447
|
const testFiles: string[] = []
|
|
428
|
-
|
|
448
|
+
const testSearchTerms = [...keywords.slice(0, 2), ...filePatterns]
|
|
449
|
+
for (const kw of testSearchTerms) {
|
|
429
450
|
const tests = findFilesByPattern(root, ".test.")
|
|
430
451
|
.filter((f) => f.toLowerCase().includes(kw))
|
|
431
452
|
testFiles.push(...tests)
|
|
432
453
|
}
|
|
433
454
|
const uniqueTests = [...new Set(testFiles)].slice(0, 5)
|
|
434
455
|
if (uniqueTests.length > 0) {
|
|
435
|
-
sections.push(`##
|
|
456
|
+
sections.push(`## Existing tests\n${uniqueTests.join("\n")}`)
|
|
436
457
|
}
|
|
437
458
|
|
|
438
|
-
if (sections.length === 0) return `
|
|
459
|
+
if (sections.length === 0) return `No context found for "${task}".`
|
|
439
460
|
return sections.filter(Boolean).join("\n\n")
|
|
440
461
|
}
|
|
441
462
|
|
|
@@ -468,7 +489,7 @@ export function generateContextHealth(data: string, root: string): { report: Hea
|
|
|
468
489
|
}
|
|
469
490
|
}
|
|
470
491
|
if (orphanLinks.length > 0) {
|
|
471
|
-
warnings.push(`${orphanLinks.length} links
|
|
492
|
+
warnings.push(`${orphanLinks.length} orphan links: ${orphanLinks.slice(0, 5).join(", ")}`)
|
|
472
493
|
score -= 5
|
|
473
494
|
}
|
|
474
495
|
|
|
@@ -484,7 +505,7 @@ export function generateContextHealth(data: string, root: string): { report: Hea
|
|
|
484
505
|
if (keys.length > 1) duplicates.push(keys.join(" = "))
|
|
485
506
|
}
|
|
486
507
|
if (duplicates.length > 0) {
|
|
487
|
-
warnings.push(`${duplicates.length}
|
|
508
|
+
warnings.push(`${duplicates.length} duplicate entries: ${duplicates.slice(0, 3).join("; ")}`)
|
|
488
509
|
score -= 5
|
|
489
510
|
}
|
|
490
511
|
|
|
@@ -492,14 +513,14 @@ export function generateContextHealth(data: string, root: string): { report: Hea
|
|
|
492
513
|
const weekAgo = new Date(Date.now() - 7 * 86400000).toISOString().split("T")[0]
|
|
493
514
|
const staleNeverAccessed = entries.filter((e) => e.accessed === 0 && e.date < weekAgo)
|
|
494
515
|
if (staleNeverAccessed.length > 0) {
|
|
495
|
-
info.push(`${staleNeverAccessed.length}
|
|
516
|
+
info.push(`${staleNeverAccessed.length} old entries never recalled (possibly stale)`)
|
|
496
517
|
score -= 2
|
|
497
518
|
}
|
|
498
519
|
|
|
499
520
|
// 4. Expired TTL
|
|
500
521
|
const expired = entries.filter((e) => e.ttl && isExpiredLocal(e.ttl))
|
|
501
522
|
if (expired.length > 0) {
|
|
502
|
-
info.push(`${expired.length}
|
|
523
|
+
info.push(`${expired.length} entries with expired TTL (auto-prune on next startup)`)
|
|
503
524
|
score -= 3
|
|
504
525
|
}
|
|
505
526
|
|
|
@@ -514,7 +535,7 @@ export function generateContextHealth(data: string, root: string): { report: Hea
|
|
|
514
535
|
}
|
|
515
536
|
}
|
|
516
537
|
if (brokenFiles.length > 0) {
|
|
517
|
-
warnings.push(`${brokenFiles.length} refs
|
|
538
|
+
warnings.push(`${brokenFiles.length} refs to non-existent files: ${brokenFiles.slice(0, 3).join("; ")}`)
|
|
518
539
|
score -= 5
|
|
519
540
|
}
|
|
520
541
|
|
|
@@ -522,15 +543,15 @@ export function generateContextHealth(data: string, root: string): { report: Hea
|
|
|
522
543
|
const sessions = listSessions()
|
|
523
544
|
const stale = sessions.filter((s) => !s.active && !s.ended)
|
|
524
545
|
if (stale.length > 0) {
|
|
525
|
-
info.push(`${stale.length}
|
|
546
|
+
info.push(`${stale.length} stale sessions (will be pruned automatically)`)
|
|
526
547
|
}
|
|
527
548
|
|
|
528
549
|
// 7. Entry count
|
|
529
550
|
if (entries.length > 80) {
|
|
530
|
-
warnings.push(`${entries.length}
|
|
551
|
+
warnings.push(`${entries.length} entries (near limit of 100)`)
|
|
531
552
|
score -= 3
|
|
532
553
|
} else if (entries.length === 0) {
|
|
533
|
-
info.push("
|
|
554
|
+
info.push("Empty memory")
|
|
534
555
|
}
|
|
535
556
|
|
|
536
557
|
score = Math.max(0, score)
|
|
@@ -541,7 +562,7 @@ export function generateContextHealth(data: string, root: string): { report: Hea
|
|
|
541
562
|
const parts: string[] = [`## Memory Health: ${score >= 90 ? "✅" : score >= 70 ? "⚠️" : "❌"} ${score}/100`]
|
|
542
563
|
|
|
543
564
|
if (critical.length > 0) {
|
|
544
|
-
parts.push("", "###
|
|
565
|
+
parts.push("", "### Critical", ...critical.map((w) => `- ${w}`))
|
|
545
566
|
}
|
|
546
567
|
if (warnings.length > 0) {
|
|
547
568
|
parts.push("", "### Warnings", ...warnings.map((w) => `- ${w}`))
|
|
@@ -567,10 +588,10 @@ export function generateContextExport(data: string, format: ExportFormat = "full
|
|
|
567
588
|
|
|
568
589
|
// Header
|
|
569
590
|
const today = new Date().toISOString().split("T")[0]
|
|
570
|
-
sections.push(`# toon-memory export\
|
|
591
|
+
sections.push(`# toon-memory export\nDate: ${today} | Entries: ${entries.length}\n`)
|
|
571
592
|
|
|
572
593
|
if (entries.length === 0) {
|
|
573
|
-
sections.push("
|
|
594
|
+
sections.push("Empty memory.")
|
|
574
595
|
return sections.join("\n")
|
|
575
596
|
}
|
|
576
597
|
|
|
@@ -582,10 +603,10 @@ export function generateContextExport(data: string, format: ExportFormat = "full
|
|
|
582
603
|
}
|
|
583
604
|
|
|
584
605
|
const categoryLabels: Record<string, string> = {
|
|
585
|
-
decision: "
|
|
586
|
-
pattern: "
|
|
606
|
+
decision: "Decisions",
|
|
607
|
+
pattern: "Patterns",
|
|
587
608
|
bug: "Bugs",
|
|
588
|
-
knowledge: "
|
|
609
|
+
knowledge: "Knowledge",
|
|
589
610
|
}
|
|
590
611
|
|
|
591
612
|
for (const [cat, items] of Object.entries(byCategory)) {
|
|
@@ -615,8 +636,8 @@ export function generateContextExport(data: string, format: ExportFormat = "full
|
|
|
615
636
|
if (key < n) edges.push(`${key} -> ${n}`)
|
|
616
637
|
}
|
|
617
638
|
}
|
|
618
|
-
|
|
619
|
-
|
|
639
|
+
if (edges.length > 0) {
|
|
640
|
+
sections.push(`## Graph (${edges.length} edges)`)
|
|
620
641
|
sections.push(edges.slice(0, 20).join("\n"))
|
|
621
642
|
}
|
|
622
643
|
}
|
|
@@ -626,7 +647,7 @@ export function generateContextExport(data: string, format: ExportFormat = "full
|
|
|
626
647
|
const sessions = listSessions()
|
|
627
648
|
if (sessions.length > 0) {
|
|
628
649
|
const active = sessions.filter((s) => s.active)
|
|
629
|
-
sections.push(`##
|
|
650
|
+
sections.push(`## Sessions (${active.length} active / ${sessions.length} total)`)
|
|
630
651
|
for (const s of sessions.slice(0, 5)) {
|
|
631
652
|
const status = s.active ? "🟢" : "⚪"
|
|
632
653
|
sections.push(`- ${status} ${s.agent}@${s.branch} (${s.id})`)
|