claude-mem-lite 4.0.4 → 5.1.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/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +18 -72
- package/README.zh-CN.md +24 -74
- package/adopt-content.mjs +0 -1
- package/cli/common.mjs +0 -17
- package/cli.mjs +19 -3
- package/commands/mem.md +3 -3
- package/commands/update.md +5 -12
- package/format-utils.mjs +0 -21
- package/haiku-client.mjs +36 -5
- package/hash-utils.mjs +21 -0
- package/hook-episode.mjs +7 -1
- package/hook-optimize.mjs +103 -50
- package/hook-shared.mjs +8 -2
- package/hook-update.mjs +56 -14
- package/hook.mjs +21 -29
- package/hooks/hooks.json +0 -10
- package/install.mjs +100 -678
- package/lib/atomic-write.mjs +16 -0
- package/lib/deferred-work.mjs +7 -3
- package/lib/doctor-drift.mjs +0 -1
- package/lib/frontmatter.mjs +1 -2
- package/lib/get-core.mjs +27 -1
- package/lib/hook-prune.mjs +133 -0
- package/lib/hook-stdin.mjs +1 -1
- package/lib/hook-telemetry.mjs +1 -1
- package/lib/install-shape.mjs +45 -4
- package/lib/maintain-core.mjs +9 -0
- package/lib/metrics.mjs +2 -2
- package/lib/proc-lock.mjs +111 -13
- package/lib/proxy-fetch.mjs +26 -2
- package/lib/scrub-record.mjs +8 -0
- package/lib/shard-gc.mjs +11 -7
- package/lib/tmp-fixture-sweep.mjs +9 -0
- package/mem-cli.mjs +46 -434
- package/nlp.mjs +1 -1
- package/npm-shrinkwrap.json +6 -2
- package/package.json +9 -16
- package/project-utils.mjs +43 -15
- package/schema.mjs +112 -50
- package/scripts/hook-launcher.mjs +9 -3
- package/scripts/prompt-search-utils.mjs +0 -30
- package/scripts/setup.sh +5 -0
- package/scripts/user-prompt-search.js +3 -125
- package/secret-scrub.mjs +32 -3
- package/server.mjs +19 -451
- package/source-files.mjs +12 -29
- package/synonyms.mjs +2 -1
- package/tool-schemas.mjs +23 -91
- package/utils.mjs +5 -13
- package/commands/tools.md +0 -67
- package/install-metadata.mjs +0 -2193
- package/lib/registry-core.mjs +0 -264
- package/registry/preinstalled.json +0 -2419
- package/registry-enricher.mjs +0 -124
- package/registry-github.mjs +0 -86
- package/registry-importer.mjs +0 -569
- package/registry-recommend.mjs +0 -503
- package/registry-retriever.mjs +0 -611
- package/registry-scanner.mjs +0 -261
- package/registry.mjs +0 -665
- package/resource-discovery.mjs +0 -199
- package/scripts/pre-skill-bridge.js +0 -146
package/resource-discovery.mjs
DELETED
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
// claude-mem-lite: Shared resource discovery — filesystem traversal for skills/agents
|
|
2
|
-
// Used by registry-scanner.mjs (runtime) and scripts/index-managed.mjs (offline)
|
|
3
|
-
|
|
4
|
-
import { existsSync, readdirSync, statSync } from 'fs';
|
|
5
|
-
import { join, basename, relative } from 'path';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @typedef {object} DiscoveredResource
|
|
9
|
-
* @property {'skill'|'agent'} type
|
|
10
|
-
* @property {string} name Resource name (flat: basename, plugin: "plugin/name")
|
|
11
|
-
* @property {string} absPath Absolute path to content file or directory
|
|
12
|
-
* @property {string|null} parentPlugin Plugin name if nested, null if standalone
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
// ─── Skill File Resolution ───────────────────────────────────────────────────
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Find the primary SKILL.md file within a skill directory.
|
|
19
|
-
* Checks direct, .claude/skills/*, and skills/* paths.
|
|
20
|
-
* @param {string} dir Skill directory
|
|
21
|
-
* @returns {string|null} Absolute path to SKILL.md or null
|
|
22
|
-
*/
|
|
23
|
-
export function findSkillMd(dir) {
|
|
24
|
-
const direct = join(dir, 'SKILL.md');
|
|
25
|
-
if (existsSync(direct)) return direct;
|
|
26
|
-
|
|
27
|
-
// .claude/skills/*/SKILL.md
|
|
28
|
-
const dotClaude = join(dir, '.claude', 'skills');
|
|
29
|
-
if (existsSync(dotClaude)) {
|
|
30
|
-
try {
|
|
31
|
-
for (const sub of readdirSync(dotClaude)) {
|
|
32
|
-
const nested = join(dotClaude, sub, 'SKILL.md');
|
|
33
|
-
if (existsSync(nested)) return nested;
|
|
34
|
-
}
|
|
35
|
-
} catch {}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// skills/*/SKILL.md (repos cloned as-is)
|
|
39
|
-
const skillsDir = join(dir, 'skills');
|
|
40
|
-
if (existsSync(skillsDir)) {
|
|
41
|
-
try {
|
|
42
|
-
for (const sub of readdirSync(skillsDir)) {
|
|
43
|
-
const nested = join(skillsDir, sub, 'SKILL.md');
|
|
44
|
-
if (existsSync(nested)) return nested;
|
|
45
|
-
}
|
|
46
|
-
} catch {}
|
|
47
|
-
}
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// ─── Flat Discovery ──────────────────────────────────────────────────────────
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Discover resources in a flat directory (one resource per subdirectory).
|
|
55
|
-
* @param {string} dirPath Directory to scan
|
|
56
|
-
* @param {'skill'|'agent'} type Resource type
|
|
57
|
-
* @param {object} [opts]
|
|
58
|
-
* @param {string[]} [opts.skipNames] Directory names to skip
|
|
59
|
-
* @param {boolean} [opts.strict=true] When false, skill dirs without SKILL.md are still discovered
|
|
60
|
-
* @param {boolean} [opts.includeFiles=false] When true, loose .md files are also returned
|
|
61
|
-
* @returns {DiscoveredResource[]}
|
|
62
|
-
*/
|
|
63
|
-
export function discoverFlat(dirPath, type, opts = {}) {
|
|
64
|
-
if (!existsSync(dirPath)) return [];
|
|
65
|
-
const skipNames = new Set(opts.skipNames || []);
|
|
66
|
-
const strict = opts.strict !== false;
|
|
67
|
-
const includeFiles = opts.includeFiles || false;
|
|
68
|
-
|
|
69
|
-
const items = [];
|
|
70
|
-
for (const name of readdirSync(dirPath)) {
|
|
71
|
-
if (name.startsWith('.') || name === 'node_modules' || skipNames.has(name)) continue;
|
|
72
|
-
const fullPath = join(dirPath, name);
|
|
73
|
-
let stat;
|
|
74
|
-
try {
|
|
75
|
-
stat = statSync(fullPath);
|
|
76
|
-
} catch {
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (stat.isDirectory()) {
|
|
81
|
-
if (type === 'skill') {
|
|
82
|
-
const skillMd = findSkillMd(fullPath);
|
|
83
|
-
if (skillMd) {
|
|
84
|
-
items.push({ type: 'skill', name, absPath: skillMd, parentPlugin: null });
|
|
85
|
-
} else if (!strict) {
|
|
86
|
-
items.push({ type: 'skill', name, absPath: fullPath, parentPlugin: null });
|
|
87
|
-
}
|
|
88
|
-
} else {
|
|
89
|
-
items.push({ type: 'agent', name, absPath: fullPath, parentPlugin: null });
|
|
90
|
-
}
|
|
91
|
-
} else if (includeFiles && name.endsWith('.md')) {
|
|
92
|
-
items.push({ type, name: basename(name, '.md'), absPath: fullPath, parentPlugin: null });
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return items;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// ─── Plugin Discovery ────────────────────────────────────────────────────────
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Discover agents and skills within a single plugin directory.
|
|
102
|
-
* @param {string} pluginDir Absolute path to plugin directory
|
|
103
|
-
* @param {string} pluginName Plugin directory name
|
|
104
|
-
* @returns {DiscoveredResource[]}
|
|
105
|
-
*/
|
|
106
|
-
export function discoverPlugin(pluginDir, pluginName) {
|
|
107
|
-
const items = [];
|
|
108
|
-
|
|
109
|
-
// agents/*.md
|
|
110
|
-
const agentSubDir = join(pluginDir, 'agents');
|
|
111
|
-
if (existsSync(agentSubDir)) {
|
|
112
|
-
try {
|
|
113
|
-
for (const file of readdirSync(agentSubDir)) {
|
|
114
|
-
if (!file.endsWith('.md')) continue;
|
|
115
|
-
const absPath = join(agentSubDir, file);
|
|
116
|
-
try {
|
|
117
|
-
if (statSync(absPath).size === 0) continue;
|
|
118
|
-
} catch {
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
items.push({
|
|
122
|
-
type: 'agent',
|
|
123
|
-
name: `${pluginName}/${basename(file, '.md')}`,
|
|
124
|
-
absPath,
|
|
125
|
-
parentPlugin: pluginName,
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
} catch {}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// skills/*/SKILL.md
|
|
132
|
-
const skillSubDir = join(pluginDir, 'skills');
|
|
133
|
-
if (existsSync(skillSubDir)) {
|
|
134
|
-
try {
|
|
135
|
-
for (const skillName of readdirSync(skillSubDir)) {
|
|
136
|
-
const skillPath = join(skillSubDir, skillName, 'SKILL.md');
|
|
137
|
-
if (existsSync(skillPath)) {
|
|
138
|
-
items.push({
|
|
139
|
-
type: 'skill',
|
|
140
|
-
name: `${pluginName}/${skillName}`,
|
|
141
|
-
absPath: skillPath,
|
|
142
|
-
parentPlugin: pluginName,
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
} catch {}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return items;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Discover all resources across all plugins in a plugins directory.
|
|
154
|
-
* @param {string} dirPath Directory containing plugin subdirectories
|
|
155
|
-
* @returns {DiscoveredResource[]}
|
|
156
|
-
*/
|
|
157
|
-
export function discoverPlugins(dirPath) {
|
|
158
|
-
if (!existsSync(dirPath)) return [];
|
|
159
|
-
|
|
160
|
-
const items = [];
|
|
161
|
-
for (const pluginName of readdirSync(dirPath)) {
|
|
162
|
-
if (pluginName.startsWith('.') || pluginName === 'node_modules') continue;
|
|
163
|
-
const pluginDir = join(dirPath, pluginName);
|
|
164
|
-
try {
|
|
165
|
-
if (!statSync(pluginDir).isDirectory()) continue;
|
|
166
|
-
} catch {
|
|
167
|
-
continue;
|
|
168
|
-
}
|
|
169
|
-
items.push(...discoverPlugin(pluginDir, pluginName));
|
|
170
|
-
}
|
|
171
|
-
return items;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// ─── Convenience ─────────────────────────────────────────────────────────────
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Discover all managed resources (skills + agent plugins).
|
|
178
|
-
* @param {string} managedDir Path to managed/ directory
|
|
179
|
-
* @returns {DiscoveredResource[]}
|
|
180
|
-
*/
|
|
181
|
-
export function discoverAllManaged(managedDir) {
|
|
182
|
-
return [
|
|
183
|
-
...discoverFlat(join(managedDir, 'skills'), 'skill', { skipNames: ['learned'] }),
|
|
184
|
-
...discoverPlugins(join(managedDir, 'agents')),
|
|
185
|
-
];
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Add relative file paths to discovered resources (for index-managed.mjs).
|
|
190
|
-
* @param {DiscoveredResource[]} items Discovered resources
|
|
191
|
-
* @param {string} managedDir Base managed directory for relative paths
|
|
192
|
-
* @returns {Array<DiscoveredResource & {filePath: string}>}
|
|
193
|
-
*/
|
|
194
|
-
export function withRelativePaths(items, managedDir) {
|
|
195
|
-
return items.map((item) => ({
|
|
196
|
-
...item,
|
|
197
|
-
filePath: relative(managedDir, item.absPath),
|
|
198
|
-
}));
|
|
199
|
-
}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// claude-mem-lite: PreToolUse Skill bridge — loads managed skills from registry
|
|
3
|
-
// Intercepts Skill("name") calls for skills in ~/.claude-mem-lite/managed/
|
|
4
|
-
// Lightweight standalone (~30ms): only imports better-sqlite3, fs, path, os
|
|
5
|
-
|
|
6
|
-
import { existsSync, readFileSync } from 'fs';
|
|
7
|
-
import { join, resolve, sep } from 'path';
|
|
8
|
-
import { homedir } from 'os';
|
|
9
|
-
import { recordHookError } from '../lib/hook-telemetry.mjs';
|
|
10
|
-
import { resolveDataDir, resolveRuntimeDir } from '../lib/resolve-data-dir.mjs';
|
|
11
|
-
// format-utils.mjs is import-free — pulling three defang helpers keeps this script
|
|
12
|
-
// inside its "lightweight standalone" budget (no heavy transitive deps).
|
|
13
|
-
import {
|
|
14
|
-
neutralizeContextDelimiters,
|
|
15
|
-
neutralizeSkillDelimiters,
|
|
16
|
-
neutralizeSkillBridgeDelimiters,
|
|
17
|
-
} from '../format-utils.mjs';
|
|
18
|
-
// D#154: single envelope writer. Also import-free (no runtime deps), so it stays
|
|
19
|
-
// inside this script's "lightweight standalone" budget.
|
|
20
|
-
import { queueHookContext, flushHookStdout } from '../lib/hook-stdout.mjs';
|
|
21
|
-
// P1-9: bounded stdin. Also import-free, so it stays inside the "lightweight standalone"
|
|
22
|
-
// budget this script's header claims.
|
|
23
|
-
import { readHookStdin } from '../lib/hook-stdin.mjs';
|
|
24
|
-
|
|
25
|
-
// CLAUDE_MEM_DIR mirrors pre-tool-recall.js — one env var sandboxes everything.
|
|
26
|
-
const DATA_DIR = resolveDataDir(process.env.CLAUDE_MEM_DIR);
|
|
27
|
-
const RUNTIME_DIR = resolveRuntimeDir(DATA_DIR);
|
|
28
|
-
// D#29: all data artifacts follow DATA_DIR (CLAUDE_MEM_DIR-aware), not a hardcoded
|
|
29
|
-
// homedir — previously REGISTRY_DB_PATH/MANAGED_BASE/MARKER pinned homedir while line 12
|
|
30
|
-
// honored the env, so relocated installs opened the wrong DB and the marker never matched
|
|
31
|
-
// the relocated local_path. MANAGED_MARKER is only a coarse LIKE prefilter; the exact
|
|
32
|
-
// MANAGED_BASE prefix check below is the real confinement gate (so LIKE-wildcard chars in
|
|
33
|
-
// a relocated path can at worst over-admit to that gate, never bypass it).
|
|
34
|
-
const REGISTRY_DB_PATH = join(DATA_DIR, 'resource-registry.db');
|
|
35
|
-
const MANAGED_BASE = DATA_DIR;
|
|
36
|
-
const MANAGED_MARKER = join(DATA_DIR, 'managed') + sep;
|
|
37
|
-
|
|
38
|
-
try {
|
|
39
|
-
// Skip if recursive hook
|
|
40
|
-
if (process.env.CLAUDE_MEM_HOOK_RUNNING) process.exit(0);
|
|
41
|
-
|
|
42
|
-
// Read stdin, bounded (P1-9). This used to be an unbounded `for await` accumulate: no
|
|
43
|
-
// cap, no timeout, the only limit being the host's own fail-open — which looks exactly
|
|
44
|
-
// like the hook having nothing to say.
|
|
45
|
-
const { text: input } = await readHookStdin();
|
|
46
|
-
|
|
47
|
-
// Parse event
|
|
48
|
-
let skillName;
|
|
49
|
-
try {
|
|
50
|
-
const event = JSON.parse(input);
|
|
51
|
-
skillName = event.tool_input?.skill;
|
|
52
|
-
} catch (e) {
|
|
53
|
-
recordHookError('skill-bridge:json', e, RUNTIME_DIR, { inputLen: input.length });
|
|
54
|
-
process.exit(0);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (!skillName || typeof skillName !== 'string') process.exit(0);
|
|
58
|
-
|
|
59
|
-
// Skip if registry DB doesn't exist
|
|
60
|
-
if (!existsSync(REGISTRY_DB_PATH)) process.exit(0);
|
|
61
|
-
|
|
62
|
-
// Open DB readonly
|
|
63
|
-
const Database = (await import('better-sqlite3')).default;
|
|
64
|
-
let db;
|
|
65
|
-
try {
|
|
66
|
-
db = new Database(REGISTRY_DB_PATH, { readonly: true });
|
|
67
|
-
db.pragma('busy_timeout = 1000');
|
|
68
|
-
} catch (e) {
|
|
69
|
-
recordHookError('skill-bridge:db-open', e, RUNTIME_DIR);
|
|
70
|
-
process.exit(0);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
try {
|
|
74
|
-
// Query: find by name or invocation_name, ONLY if managed path
|
|
75
|
-
const row = db
|
|
76
|
-
.prepare(
|
|
77
|
-
`
|
|
78
|
-
SELECT name, local_path FROM resources
|
|
79
|
-
WHERE status = 'active'
|
|
80
|
-
AND (name = ? OR invocation_name = ?)
|
|
81
|
-
AND local_path LIKE ?
|
|
82
|
-
LIMIT 1
|
|
83
|
-
`,
|
|
84
|
-
)
|
|
85
|
-
.get(skillName, skillName, `%${MANAGED_MARKER}%`);
|
|
86
|
-
|
|
87
|
-
if (!row || !row.local_path) process.exit(0);
|
|
88
|
-
|
|
89
|
-
// Resolve path: directory skills → SKILL.md (agents always have full .md paths)
|
|
90
|
-
let skillPath = row.local_path;
|
|
91
|
-
if (!skillPath.endsWith('.md')) {
|
|
92
|
-
const candidate = join(skillPath, 'SKILL.md');
|
|
93
|
-
if (existsSync(candidate)) skillPath = candidate;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (!existsSync(skillPath)) process.exit(0);
|
|
97
|
-
|
|
98
|
-
// Path confinement check — prevent LIKE bypass via '../' in local_path
|
|
99
|
-
const resolvedPath = resolve(skillPath);
|
|
100
|
-
if (resolvedPath !== MANAGED_BASE && !resolvedPath.startsWith(MANAGED_BASE + sep)) process.exit(0);
|
|
101
|
-
|
|
102
|
-
// Read and output
|
|
103
|
-
const content = readFileSync(skillPath, 'utf8');
|
|
104
|
-
// T4-P1-B: JSON hookSpecificOutput parity with pre-tool-recall.js. Some CC variants
|
|
105
|
-
// (notably sdscc) silently drop plain-text stdout from PreToolUse — the previous
|
|
106
|
-
// console.log() form would render on stock CC but no-op on those variants.
|
|
107
|
-
// Token budget: ~4 chars per token, 4000 token limit = 16000 chars.
|
|
108
|
-
const portablePath = resolvedPath.startsWith(homedir())
|
|
109
|
-
? '~' + resolvedPath.slice(homedir().length)
|
|
110
|
-
: resolvedPath;
|
|
111
|
-
// Defang the untrusted skill body + name before wrapping (audit 2026-08-14 M-4):
|
|
112
|
-
// registry rows come from third-party repos, and this was the one AUTO injection
|
|
113
|
-
// surface of that data with zero neutralization — a body carrying a literal
|
|
114
|
-
// `</skill-bridge>` + forged <system-reminder> (or a `<skill-loaded>` execute
|
|
115
|
-
// block) escaped the wrapper verbatim. Name additionally drops quote/bracket
|
|
116
|
-
// chars: it lands in an ATTRIBUTE position, where `"` breaks out of the wrapper
|
|
117
|
-
// tag itself. Applied to the truncated summary too (a cut can't be trusted to
|
|
118
|
-
// land mid-tag).
|
|
119
|
-
const defang = (s) =>
|
|
120
|
-
neutralizeSkillBridgeDelimiters(neutralizeSkillDelimiters(neutralizeContextDelimiters(s)));
|
|
121
|
-
const safeName = String(row.name).replace(/["'<>]/g, '');
|
|
122
|
-
let additionalContext;
|
|
123
|
-
if (content.length > 16000) {
|
|
124
|
-
const summary = defang(content.slice(0, 800));
|
|
125
|
-
// D#122 ③: the path interpolates OUTSIDE the wrapper — defang it too (a
|
|
126
|
-
// locally-created managed dir can carry delimiter chars in its name).
|
|
127
|
-
additionalContext = `<skill-bridge name="${safeName}" source="managed" truncated="true">\n${summary}\n...\n</skill-bridge>\n\nSkill content truncated. Read("${defang(portablePath)}") to load full content.`;
|
|
128
|
-
} else {
|
|
129
|
-
additionalContext = `<skill-bridge name="${safeName}" source="managed">\n${defang(content)}\n</skill-bridge>\n\nThis skill was loaded from the managed registry. Follow the instructions above.`;
|
|
130
|
-
}
|
|
131
|
-
queueHookContext('PreToolUse', additionalContext);
|
|
132
|
-
flushHookStdout();
|
|
133
|
-
} catch (e) {
|
|
134
|
-
// Silent failure — never block Skill tool, but record for self-observation.
|
|
135
|
-
recordHookError('skill-bridge:query', e, RUNTIME_DIR, { skillName });
|
|
136
|
-
} finally {
|
|
137
|
-
try {
|
|
138
|
-
db.close();
|
|
139
|
-
} catch {}
|
|
140
|
-
}
|
|
141
|
-
} catch (e) {
|
|
142
|
-
// Top-level catch — exit 0 no matter what, but record what slipped past.
|
|
143
|
-
try {
|
|
144
|
-
recordHookError('skill-bridge:top', e, RUNTIME_DIR);
|
|
145
|
-
} catch {}
|
|
146
|
-
}
|