claude-mem-lite 2.2.0 → 2.2.2
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/dispatch-feedback.mjs +1 -2
- package/dispatch.mjs +91 -29
- package/hook-episode.mjs +11 -1
- package/hook-llm.mjs +17 -2
- package/hook-shared.mjs +14 -0
- package/hook.mjs +12 -7
- package/install.mjs +1 -1
- package/package.json +1 -1
- package/registry.mjs +19 -1
- package/server.mjs +55 -1
package/dispatch-feedback.mjs
CHANGED
|
@@ -183,13 +183,12 @@ export async function collectFeedback(db, sessionId, sessionEvents = []) {
|
|
|
183
183
|
const invocations = getSessionInvocations(db, sessionId);
|
|
184
184
|
if (invocations.length === 0) return;
|
|
185
185
|
|
|
186
|
-
const outcome = detectOutcome(sessionEvents);
|
|
187
|
-
|
|
188
186
|
for (const inv of invocations) {
|
|
189
187
|
// Skip if already collected (prevents double-collection from stop + session-start)
|
|
190
188
|
if (inv.outcome) continue;
|
|
191
189
|
|
|
192
190
|
const adopted = detectAdoption(inv, sessionEvents);
|
|
191
|
+
const outcome = adopted ? detectOutcome(sessionEvents) : 'ignored';
|
|
193
192
|
const score = adopted ? (outcome === 'success' ? 1.0 : outcome === 'partial' ? 0.5 : 0.2) : 0;
|
|
194
193
|
|
|
195
194
|
// Update invocation record
|
package/dispatch.mjs
CHANGED
|
@@ -5,17 +5,14 @@
|
|
|
5
5
|
// Tier 3: Haiku semantic dispatch (~500ms, only when needed)
|
|
6
6
|
|
|
7
7
|
import { basename, join } from 'path';
|
|
8
|
-
import { existsSync, readFileSync, writeFileSync
|
|
8
|
+
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
|
9
9
|
import { retrieveResources, buildEnhancedQuery, buildQueryFromText, DISPATCH_SYNONYMS } from './registry-retriever.mjs';
|
|
10
10
|
import { renderInjection } from './dispatch-inject.mjs';
|
|
11
11
|
import { updateResourceStats, recordInvocation } from './registry.mjs';
|
|
12
12
|
import { callHaikuJSON } from './haiku-client.mjs';
|
|
13
13
|
import { debugCatch, truncate } from './utils.mjs';
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
// Duplicated from hook-shared.mjs to avoid circular import (dispatch ← hook ← hook-shared)
|
|
17
|
-
const RUNTIME_DIR = join(DB_DIR, 'runtime');
|
|
18
|
-
try { if (!existsSync(RUNTIME_DIR)) mkdirSync(RUNTIME_DIR, { recursive: true }); } catch {}
|
|
14
|
+
import { peekToolEvents, RUNTIME_DIR } from './hook-shared.mjs';
|
|
15
|
+
import { detectActiveSuite, shouldRecommendForStage, detectExplicitRequest, inferCurrentStage } from './dispatch-workflow.mjs';
|
|
19
16
|
|
|
20
17
|
// ─── Constants ───────────────────────────────────────────────────────────────
|
|
21
18
|
|
|
@@ -28,6 +25,10 @@ const READ_ONLY_TOOLS = new Set([
|
|
|
28
25
|
export const COOLDOWN_MINUTES = 60;
|
|
29
26
|
export const SESSION_RECOMMEND_CAP = 3;
|
|
30
27
|
|
|
28
|
+
// Minimum absolute BM25 composite score to recommend. Typical good matches score 5-50;
|
|
29
|
+
// this filters only near-zero noise matches from incidental text overlap.
|
|
30
|
+
export const BM25_MIN_THRESHOLD = 1.5;
|
|
31
|
+
|
|
31
32
|
// ─── Haiku Circuit Breaker ──────────────────────────────────────────────────
|
|
32
33
|
// Prevents cascading latency when Haiku API is down or slow.
|
|
33
34
|
// After BREAKER_THRESHOLD consecutive failures, disable for BREAKER_RESET_MS.
|
|
@@ -691,7 +692,7 @@ function reRankByKeywords(results, rawKeywords) {
|
|
|
691
692
|
* @param {object[]} results FTS5 results with recommend_count/adopt_count
|
|
692
693
|
* @returns {object[]} Filtered results with decayed scores
|
|
693
694
|
*/
|
|
694
|
-
function applyAdoptionDecay(results) {
|
|
695
|
+
function applyAdoptionDecay(results, db) {
|
|
695
696
|
return results.map(r => {
|
|
696
697
|
const recs = r.recommend_count || 0;
|
|
697
698
|
const adopts = r.adopt_count || 0;
|
|
@@ -699,14 +700,23 @@ function applyAdoptionDecay(results) {
|
|
|
699
700
|
|
|
700
701
|
const rate = (adopts + 1) / (recs + 2); // Laplace smoothing
|
|
701
702
|
let multiplier = 1.0;
|
|
702
|
-
if (recs > 100 && rate < 0.01) multiplier = 0;
|
|
703
|
-
else if (recs > 50 && rate < 0.02) multiplier = 0.
|
|
704
|
-
else if (recs > 20 && rate < 0.05) multiplier = 0.
|
|
703
|
+
if (recs > 100 && rate < 0.01) multiplier = 0.05; // Near-block but not permanent
|
|
704
|
+
else if (recs > 50 && rate < 0.02) multiplier = 0.15;
|
|
705
|
+
else if (recs > 20 && rate < 0.05) multiplier = 0.4;
|
|
706
|
+
|
|
707
|
+
// Recent rejection boost: extra penalty for resources rejected many times recently
|
|
708
|
+
if (db && multiplier < 1) {
|
|
709
|
+
try {
|
|
710
|
+
const recentRejects = db.prepare(
|
|
711
|
+
`SELECT COUNT(*) as cnt FROM invocations WHERE resource_id = ? AND adopted = 0 AND created_at > datetime('now', '-7 days')`
|
|
712
|
+
).get(r.id)?.cnt || 0;
|
|
713
|
+
if (recentRejects >= 10) multiplier *= 0.3;
|
|
714
|
+
else if (recentRejects >= 5) multiplier *= 0.5;
|
|
715
|
+
} catch (e) { debugCatch(e, 'applyAdoptionDecay-recentRejects'); }
|
|
716
|
+
}
|
|
705
717
|
|
|
706
|
-
if (multiplier
|
|
718
|
+
if (multiplier < 0.01) return null;
|
|
707
719
|
if (multiplier < 1) {
|
|
708
|
-
// Composite scores are negative (more negative = more relevant).
|
|
709
|
-
// To penalize: multiply by multiplier (<1) to make less negative (worse rank).
|
|
710
720
|
return { ...r, composite_score: (r.composite_score ?? r.relevance) * multiplier, _decayed: true };
|
|
711
721
|
}
|
|
712
722
|
return r;
|
|
@@ -722,6 +732,15 @@ function applyAdoptionDecay(results) {
|
|
|
722
732
|
* @returns {object[]} Filtered results that pass the gate
|
|
723
733
|
*/
|
|
724
734
|
function passesConfidenceGate(results, signals) {
|
|
735
|
+
// BM25 absolute minimum: filter out weak text matches regardless of intent.
|
|
736
|
+
// Only apply when enough results exist (BM25 IDF is unreliable with < 3 matches).
|
|
737
|
+
if (results.length >= 3) {
|
|
738
|
+
results = results.filter(r => {
|
|
739
|
+
const score = Math.abs(r.composite_score ?? r.relevance);
|
|
740
|
+
return score >= BM25_MIN_THRESHOLD;
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
|
|
725
744
|
// signals.intent is a comma-separated string (e.g. "test,fix"), not an array
|
|
726
745
|
const intentTokens = typeof signals?.intent === 'string'
|
|
727
746
|
? signals.intent.split(',').filter(Boolean)
|
|
@@ -748,13 +767,19 @@ function passesConfidenceGate(results, signals) {
|
|
|
748
767
|
|
|
749
768
|
/**
|
|
750
769
|
* Dispatch on SessionStart: analyze user prompt, return best resource suggestion.
|
|
770
|
+
* Only dispatches when continuing from a previous session (handoff).
|
|
771
|
+
* Cold starts (no previous session) showed 0% adoption — skip dispatch entirely.
|
|
751
772
|
* @param {Database} db Registry database
|
|
752
773
|
* @param {string} userPrompt User's prompt text
|
|
753
774
|
* @param {string} [sessionId] Session identifier for dedup
|
|
775
|
+
* @param {Object} [options]
|
|
776
|
+
* @param {boolean} [options.hasHandoff=false] Whether a previous session handoff exists
|
|
754
777
|
* @returns {Promise<string|null>} Injection text or null
|
|
755
778
|
*/
|
|
756
|
-
export async function dispatchOnSessionStart(db, userPrompt, sessionId) {
|
|
757
|
-
if (!
|
|
779
|
+
export async function dispatchOnSessionStart(db, userPrompt, sessionId, { hasHandoff = false } = {}) {
|
|
780
|
+
if (!db) return null;
|
|
781
|
+
if (!hasHandoff) return null; // Only dispatch when continuing from a previous session
|
|
782
|
+
if (!userPrompt) return null; // Prompt still required for FTS query
|
|
758
783
|
|
|
759
784
|
try {
|
|
760
785
|
const projectDomains = detectProjectDomains();
|
|
@@ -783,7 +808,7 @@ export async function dispatchOnSessionStart(db, userPrompt, sessionId) {
|
|
|
783
808
|
}
|
|
784
809
|
|
|
785
810
|
results = reRankByKeywords(results, signals.rawKeywords);
|
|
786
|
-
results = applyAdoptionDecay(results);
|
|
811
|
+
results = applyAdoptionDecay(results, db);
|
|
787
812
|
results = passesConfidenceGate(results, signals);
|
|
788
813
|
results = results.slice(0, 3);
|
|
789
814
|
|
|
@@ -804,7 +829,7 @@ export async function dispatchOnSessionStart(db, userPrompt, sessionId) {
|
|
|
804
829
|
if (haikuResults.length > 0) {
|
|
805
830
|
// Apply same post-processing as Tier2 to prevent zombie/low-confidence bypass
|
|
806
831
|
haikuResults = reRankByKeywords(haikuResults, signals.rawKeywords);
|
|
807
|
-
haikuResults = applyAdoptionDecay(haikuResults);
|
|
832
|
+
haikuResults = applyAdoptionDecay(haikuResults, db);
|
|
808
833
|
haikuResults = passesConfidenceGate(haikuResults, signals);
|
|
809
834
|
if (haikuResults.length > 0) results = haikuResults;
|
|
810
835
|
}
|
|
@@ -848,14 +873,46 @@ export async function dispatchOnSessionStart(db, userPrompt, sessionId) {
|
|
|
848
873
|
* @param {string} [sessionId] Session identifier for dedup
|
|
849
874
|
* @returns {Promise<string|null>} Injection text or null
|
|
850
875
|
*/
|
|
851
|
-
export async function dispatchOnUserPrompt(db, userPrompt, sessionId) {
|
|
876
|
+
export async function dispatchOnUserPrompt(db, userPrompt, sessionId, { sessionEvents } = {}) {
|
|
852
877
|
if (!userPrompt || !db) return null;
|
|
853
878
|
|
|
854
879
|
try {
|
|
880
|
+
// 1. Explicit request → highest priority, bypass all restrictions
|
|
881
|
+
const explicit = detectExplicitRequest(userPrompt);
|
|
882
|
+
if (explicit.isExplicit) {
|
|
883
|
+
const textQuery = buildQueryFromText(explicit.searchTerm);
|
|
884
|
+
if (textQuery) {
|
|
885
|
+
const explicitResults = retrieveResources(db, textQuery, { limit: 3, projectDomains: detectProjectDomains() });
|
|
886
|
+
if (explicitResults.length > 0) {
|
|
887
|
+
const best = explicitResults[0];
|
|
888
|
+
if (!sessionId || !isRecentlyRecommended(db, best.id, sessionId)) {
|
|
889
|
+
recordInvocation(db, { resource_id: best.id, session_id: sessionId, trigger: 'user_prompt', tier: 1, recommended: 1 });
|
|
890
|
+
updateResourceStats(db, best.id, 'recommend_count');
|
|
891
|
+
return renderInjection(best);
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
// 2. Suite auto-flow protection
|
|
898
|
+
const events = sessionEvents || peekToolEvents();
|
|
899
|
+
const activeSuite = detectActiveSuite(events);
|
|
900
|
+
|
|
855
901
|
const projectDomains = detectProjectDomains();
|
|
856
902
|
|
|
857
903
|
// Intent-aware enhanced query (column-targeted)
|
|
858
904
|
const signals = extractContextSignals({ tool_name: '_user_prompt' }, { userPrompt });
|
|
905
|
+
|
|
906
|
+
// Check if active suite covers the current stage
|
|
907
|
+
if (activeSuite) {
|
|
908
|
+
const currentStage = inferCurrentStage(signals.primaryIntent, activeSuite);
|
|
909
|
+
if (currentStage) {
|
|
910
|
+
const { shouldRecommend } = shouldRecommendForStage(activeSuite, currentStage);
|
|
911
|
+
if (!shouldRecommend) return null;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
// 3. Normal FTS flow
|
|
859
916
|
const enhancedQuery = buildEnhancedQuery(signals);
|
|
860
917
|
|
|
861
918
|
// Fetch extra results when rawKeywords are present — the top-3 by BM25 may be
|
|
@@ -882,21 +939,14 @@ export async function dispatchOnUserPrompt(db, userPrompt, sessionId) {
|
|
|
882
939
|
// match those keywords. "帮我做一下SEO审查" → rawKeywords=["seo"] → SEO audit
|
|
883
940
|
// resources should rank above generic code-review resources.
|
|
884
941
|
results = reRankByKeywords(results, signals.rawKeywords);
|
|
885
|
-
results = applyAdoptionDecay(results);
|
|
942
|
+
results = applyAdoptionDecay(results, db);
|
|
886
943
|
results = passesConfidenceGate(results, signals);
|
|
887
944
|
results = results.slice(0, 3);
|
|
888
945
|
|
|
889
946
|
if (results.length === 0) return null;
|
|
890
947
|
|
|
891
|
-
//
|
|
892
|
-
|
|
893
|
-
// close BM25 scores indicate "multiple equally good options in the right domain"
|
|
894
|
-
// rather than "ambiguous/wrong match". Trust the domain match.
|
|
895
|
-
if (needsHaikuDispatch(results)) {
|
|
896
|
-
const hasKeywordMatch = signals.rawKeywords?.length > 0 && results.some(r =>
|
|
897
|
-
signals.rawKeywords.some(kw => (r.intent_tags || '').toLowerCase().includes(kw)));
|
|
898
|
-
if (!hasKeywordMatch) return null;
|
|
899
|
-
}
|
|
948
|
+
// Low confidence → skip (no Haiku in user_prompt path — stay fast)
|
|
949
|
+
if (needsHaikuDispatch(results)) return null;
|
|
900
950
|
|
|
901
951
|
// Filter by cooldown + session dedup (prevents double-recommend with SessionStart)
|
|
902
952
|
const viable = sessionId
|
|
@@ -939,6 +989,18 @@ export async function dispatchOnPreToolUse(db, event, sessionCtx = {}) {
|
|
|
939
989
|
|
|
940
990
|
// Tier 1: Extract context signals
|
|
941
991
|
const signals = extractContextSignals(event, sessionCtx);
|
|
992
|
+
|
|
993
|
+
// Suite protection: if a suite auto-flow is active, suppress recommendations
|
|
994
|
+
// for stages the suite already covers
|
|
995
|
+
const events = peekToolEvents();
|
|
996
|
+
const activeSuite = detectActiveSuite(events);
|
|
997
|
+
if (activeSuite) {
|
|
998
|
+
const stage = inferCurrentStage(signals.primaryIntent, activeSuite);
|
|
999
|
+
if (stage) {
|
|
1000
|
+
const { shouldRecommend } = shouldRecommendForStage(activeSuite, stage);
|
|
1001
|
+
if (!shouldRecommend) return null;
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
942
1004
|
const query = buildEnhancedQuery(signals);
|
|
943
1005
|
if (!query) return null;
|
|
944
1006
|
|
|
@@ -946,7 +1008,7 @@ export async function dispatchOnPreToolUse(db, event, sessionCtx = {}) {
|
|
|
946
1008
|
|
|
947
1009
|
// Tier 2: FTS5 retrieval
|
|
948
1010
|
let results = retrieveResources(db, query, { limit: 3, projectDomains });
|
|
949
|
-
results = applyAdoptionDecay(results);
|
|
1011
|
+
results = applyAdoptionDecay(results, db);
|
|
950
1012
|
results = passesConfidenceGate(results, signals);
|
|
951
1013
|
if (results.length === 0) return null;
|
|
952
1014
|
|
package/hook-episode.mjs
CHANGED
|
@@ -216,13 +216,23 @@ export function mergePendingEntries(episode) {
|
|
|
216
216
|
* @returns {boolean} true if the episode has significant content
|
|
217
217
|
*/
|
|
218
218
|
export function episodeHasSignificantContent(episode) {
|
|
219
|
+
// 1. File edits or Bash errors → always significant
|
|
219
220
|
const hasEditsOrErrors = episode.entries.some(e =>
|
|
220
221
|
EDIT_TOOLS.has(e.tool) ||
|
|
221
222
|
(e.tool === 'Bash' && e.isError)
|
|
222
223
|
);
|
|
223
224
|
if (hasEditsOrErrors) return true;
|
|
224
225
|
|
|
225
|
-
//
|
|
226
|
+
// 2. Important files touched (config, schema, security, migration)
|
|
227
|
+
// Checks episode.files (all touched files, including reads) — catches important-file investigation
|
|
228
|
+
const allFiles = episode.files || [];
|
|
229
|
+
const hasImportantFile = allFiles.some(f =>
|
|
230
|
+
/\.(env|yml|yaml|toml|lock|sql|prisma|proto)$/.test(f) ||
|
|
231
|
+
/(config|schema|migration|auth|security)/i.test(f)
|
|
232
|
+
);
|
|
233
|
+
if (hasImportantFile) return true;
|
|
234
|
+
|
|
235
|
+
// 3. Research pattern: reading many files indicates investigation
|
|
226
236
|
const readCount = episode.entries.filter(e =>
|
|
227
237
|
e.tool === 'Read' || e.tool === 'Grep'
|
|
228
238
|
).length;
|
package/hook-llm.mjs
CHANGED
|
@@ -267,7 +267,7 @@ Error: ${e.isError ? 'yes' : 'no'}
|
|
|
267
267
|
|
|
268
268
|
JSON: {"type":"decision|bugfix|feature|refactor|discovery|change","title":"concise ≤80 char description","narrative":"what changed, why, and outcome (2-3 sentences)","concepts":["kw1","kw2"],"facts":["fact1","fact2"],"importance":1}
|
|
269
269
|
Facts: each MUST be (1) atomic—one claim, (2) self-contained—no pronouns, include file/function name, (3) specific—"refreshToken() in auth.ts:45 uses 1h TTL" not "handles tokens"
|
|
270
|
-
importance: 1=routine, 2=notable (error fix, arch decision, config change), 3=critical (breaking change, security fix, data migration)`;
|
|
270
|
+
importance: 0=not worth saving (pure browsing, trivial query, no learning value), 1=routine, 2=notable (error fix, arch decision, config change), 3=critical (breaking change, security fix, data migration)`;
|
|
271
271
|
} else {
|
|
272
272
|
const actionList = episode.entries.map((e, i) =>
|
|
273
273
|
`${i + 1}. [${e.tool}] ${e.desc}${e.isError ? ' (ERROR)' : ''}`
|
|
@@ -282,7 +282,7 @@ ${actionList}
|
|
|
282
282
|
|
|
283
283
|
JSON: {"type":"decision|bugfix|feature|refactor|discovery|change","title":"coherent ≤80 char summary","narrative":"what was done, why, and outcome (3-5 sentences)","concepts":["keyword1","keyword2"],"facts":["specific fact 1","specific fact 2"],"importance":1}
|
|
284
284
|
Facts: each MUST be (1) atomic—one claim, (2) self-contained—no pronouns, include file/function name, (3) specific—"refreshToken() in auth.ts:45 uses 1h TTL" not "handles tokens"
|
|
285
|
-
importance: 1=routine, 2=notable (error fix, arch decision, config change), 3=critical (breaking change, security fix, data migration)`;
|
|
285
|
+
importance: 0=not worth saving (pure browsing, trivial query, no learning value), 1=routine, 2=notable (error fix, arch decision, config change), 3=critical (breaking change, security fix, data migration)`;
|
|
286
286
|
}
|
|
287
287
|
|
|
288
288
|
const ruleImportance = computeRuleImportance(episode);
|
|
@@ -301,6 +301,21 @@ importance: 1=routine, 2=notable (error fix, arch decision, config change), 3=cr
|
|
|
301
301
|
}
|
|
302
302
|
|
|
303
303
|
if (parsed && parsed.title) {
|
|
304
|
+
// Discard if LLM judges observation has no learning value
|
|
305
|
+
if (parsed.importance === 0 || parsed.importance === '0') {
|
|
306
|
+
debugLog('DEBUG', 'llm-episode', `Discarded low-value observation: ${parsed.title}`);
|
|
307
|
+
// If pre-saved, delete it too
|
|
308
|
+
if (episode.savedId) {
|
|
309
|
+
const ddb = openDb();
|
|
310
|
+
if (ddb) {
|
|
311
|
+
try { ddb.prepare('DELETE FROM observations WHERE id = ?').run(episode.savedId); }
|
|
312
|
+
finally { ddb.close(); }
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
try { unlinkSync(tmpFile); } catch {}
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
|
|
304
319
|
obs = {
|
|
305
320
|
type: validTypes.has(parsed.type) ? parsed.type : 'change',
|
|
306
321
|
title: truncate(parsed.title, 120),
|
package/hook-shared.mjs
CHANGED
|
@@ -179,6 +179,20 @@ export function readAndClearToolEvents() {
|
|
|
179
179
|
}
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
+
/**
|
|
183
|
+
* Read tracked tool events WITHOUT clearing the file.
|
|
184
|
+
* Used by dispatch to detect active suites mid-session.
|
|
185
|
+
* @returns {object[]} Array of tool event objects
|
|
186
|
+
*/
|
|
187
|
+
export function peekToolEvents() {
|
|
188
|
+
try {
|
|
189
|
+
const raw = readFileSync(toolEventsFile(), 'utf8');
|
|
190
|
+
return raw.trim().split('\n').filter(Boolean).map(line => {
|
|
191
|
+
try { return JSON.parse(line); } catch { return null; }
|
|
192
|
+
}).filter(Boolean);
|
|
193
|
+
} catch { return []; }
|
|
194
|
+
}
|
|
195
|
+
|
|
182
196
|
/**
|
|
183
197
|
* Extract partial response from CLI error output (timeout/error recovery).
|
|
184
198
|
* @param {Error} error The caught error from execFileSync
|
package/hook.mjs
CHANGED
|
@@ -369,9 +369,12 @@ async function handleStop() {
|
|
|
369
369
|
// Always clear event file to prevent stale events accumulating if registry DB is unavailable.
|
|
370
370
|
try {
|
|
371
371
|
const sessionEvents = readAndClearToolEvents();
|
|
372
|
-
|
|
373
|
-
if (
|
|
374
|
-
|
|
372
|
+
// Skip feedback for zero-interaction sessions (no tool events = no meaningful signal)
|
|
373
|
+
if (sessionEvents.length > 0) {
|
|
374
|
+
const rdb = getRegistryDb();
|
|
375
|
+
if (rdb) {
|
|
376
|
+
await collectFeedback(rdb, sessionId, sessionEvents);
|
|
377
|
+
}
|
|
375
378
|
}
|
|
376
379
|
} catch (e) { debugCatch(e, 'handleStop-feedback'); }
|
|
377
380
|
|
|
@@ -428,7 +431,7 @@ async function handleSessionStart() {
|
|
|
428
431
|
|
|
429
432
|
// ── DB mutations in a transaction (crash-safe consistency) ──
|
|
430
433
|
const staleSessionCutoff = Date.now() - STALE_SESSION_MS;
|
|
431
|
-
const autoCompressAge = Date.now() -
|
|
434
|
+
const autoCompressAge = Date.now() - 30 * 86400000; // 30 days (accelerated from 90)
|
|
432
435
|
|
|
433
436
|
db.transaction(() => {
|
|
434
437
|
// Ensure session exists in DB (INSERT OR IGNORE avoids race condition)
|
|
@@ -451,7 +454,7 @@ async function handleSessionStart() {
|
|
|
451
454
|
WHERE status = 'active' AND started_at_epoch < ?
|
|
452
455
|
`).run(staleSessionCutoff);
|
|
453
456
|
|
|
454
|
-
// Auto-compress: mark old low-importance observations as compressed (
|
|
457
|
+
// Auto-compress: mark old low-importance observations as compressed (30+ days, importance=1)
|
|
455
458
|
// Lightweight: only marks rows, doesn't create summaries (full compression via mem_compress)
|
|
456
459
|
const compressed = db.prepare(`
|
|
457
460
|
UPDATE observations SET compressed_into = -1
|
|
@@ -675,8 +678,10 @@ async function handleSessionStart() {
|
|
|
675
678
|
try {
|
|
676
679
|
const rdb = getRegistryDb();
|
|
677
680
|
if (rdb && hasInjectionBudget()) {
|
|
678
|
-
|
|
679
|
-
const
|
|
681
|
+
// Build prompt context with fallbacks: next_steps → request → completed (empty = no dispatch)
|
|
682
|
+
const promptCtx = latestSummary?.next_steps || latestSummary?.request || latestSummary?.completed || '';
|
|
683
|
+
const hasHandoff = !!prevSessionId; // prevSessionId set when /clear or rapid restart detected
|
|
684
|
+
const dispatchResult = await dispatchOnSessionStart(rdb, promptCtx, sessionId, { hasHandoff });
|
|
680
685
|
if (dispatchResult) {
|
|
681
686
|
process.stdout.write(dispatchResult + '\n');
|
|
682
687
|
incrementInjection();
|
package/install.mjs
CHANGED
|
@@ -1217,7 +1217,7 @@ async function install() {
|
|
|
1217
1217
|
'haiku-client.mjs', 'utils.mjs', 'schema.mjs', 'package.json', 'skill.md',
|
|
1218
1218
|
'registry.mjs', 'registry-scanner.mjs', 'registry-indexer.mjs',
|
|
1219
1219
|
'registry-retriever.mjs', 'resource-discovery.mjs',
|
|
1220
|
-
'dispatch.mjs', 'dispatch-inject.mjs', 'dispatch-feedback.mjs',
|
|
1220
|
+
'dispatch.mjs', 'dispatch-inject.mjs', 'dispatch-feedback.mjs', 'dispatch-workflow.mjs',
|
|
1221
1221
|
];
|
|
1222
1222
|
|
|
1223
1223
|
if (IS_DEV) {
|
package/package.json
CHANGED
package/registry.mjs
CHANGED
|
@@ -104,7 +104,7 @@ const INVOCATIONS_SCHEMA = `
|
|
|
104
104
|
tier INTEGER CHECK(tier IN (1,2,3)),
|
|
105
105
|
recommended INTEGER DEFAULT 1,
|
|
106
106
|
adopted INTEGER DEFAULT 0,
|
|
107
|
-
outcome TEXT CHECK(outcome IN ('success','partial','failure','skipped',NULL)),
|
|
107
|
+
outcome TEXT CHECK(outcome IN ('success','partial','failure','skipped','ignored',NULL)),
|
|
108
108
|
score REAL,
|
|
109
109
|
created_at TEXT DEFAULT (datetime('now'))
|
|
110
110
|
);
|
|
@@ -192,6 +192,24 @@ export function ensureRegistryDb(dbPath) {
|
|
|
192
192
|
}
|
|
193
193
|
} catch (e) { debugCatch(e, 'ensureRegistryDb-migration'); }
|
|
194
194
|
|
|
195
|
+
// Migrate invocations CHECK constraint: add 'ignored' outcome value
|
|
196
|
+
try {
|
|
197
|
+
const schema = db.prepare(`SELECT sql FROM sqlite_master WHERE type='table' AND name='invocations'`).get();
|
|
198
|
+
if (schema?.sql && !schema.sql.includes("'ignored'")) {
|
|
199
|
+
db.transaction(() => {
|
|
200
|
+
const hasOld = db.prepare(`SELECT 1 FROM sqlite_master WHERE type='table' AND name='invocations_old'`).get();
|
|
201
|
+
if (hasOld) db.exec(`DROP TABLE invocations_old`);
|
|
202
|
+
db.exec(`ALTER TABLE invocations RENAME TO invocations_old`);
|
|
203
|
+
db.exec(INVOCATIONS_SCHEMA);
|
|
204
|
+
db.exec(`INSERT INTO invocations
|
|
205
|
+
(id, resource_id, session_id, trigger, tier, recommended, adopted, outcome, score, created_at)
|
|
206
|
+
SELECT id, resource_id, session_id, trigger, tier, recommended, adopted, outcome, score, created_at
|
|
207
|
+
FROM invocations_old`);
|
|
208
|
+
db.exec(`DROP TABLE invocations_old`);
|
|
209
|
+
})();
|
|
210
|
+
}
|
|
211
|
+
} catch (e) { debugCatch(e, 'ensureRegistryDb-ignored-migration'); }
|
|
212
|
+
|
|
195
213
|
db.exec(PREINSTALLED_SCHEMA);
|
|
196
214
|
|
|
197
215
|
return db;
|
package/server.mjs
CHANGED
|
@@ -61,7 +61,7 @@ const RECENCY_HALF_LIFE_MS = 1209600000; // 14 days in milliseconds
|
|
|
61
61
|
// ─── MCP Server ─────────────────────────────────────────────────────────────
|
|
62
62
|
|
|
63
63
|
const server = new McpServer(
|
|
64
|
-
{ name: 'claude-mem-lite', version: '2.
|
|
64
|
+
{ name: 'claude-mem-lite', version: '2.2.2' },
|
|
65
65
|
{
|
|
66
66
|
instructions: [
|
|
67
67
|
'Proactively search memory to leverage past experience. This is your long-term memory across sessions.',
|
|
@@ -83,9 +83,15 @@ const server = new McpServer(
|
|
|
83
83
|
},
|
|
84
84
|
);
|
|
85
85
|
|
|
86
|
+
// Track MCP request activity for idle-time cleanup (see idle timer below)
|
|
87
|
+
let lastMcpRequestTime = Date.now();
|
|
88
|
+
let idleCleanupRan = false;
|
|
89
|
+
|
|
86
90
|
function safeHandler(fn) {
|
|
87
91
|
return async (args, extra) => {
|
|
88
92
|
try {
|
|
93
|
+
lastMcpRequestTime = Date.now();
|
|
94
|
+
idleCleanupRan = false;
|
|
89
95
|
return await fn(args, extra);
|
|
90
96
|
} catch (err) {
|
|
91
97
|
return { content: [{ type: 'text', text: `Error: ${err.message}` }], isError: true };
|
|
@@ -980,10 +986,58 @@ const walTimer = setInterval(() => {
|
|
|
980
986
|
}, WAL_CHECKPOINT_INTERVAL);
|
|
981
987
|
walTimer.unref(); // Don't keep process alive just for checkpoints
|
|
982
988
|
|
|
989
|
+
// ─── Idle-Time Memory Optimization ──────────────────────────────────────────
|
|
990
|
+
// When no MCP requests for 5 minutes, run lightweight DB maintenance.
|
|
991
|
+
// lastMcpRequestTime and idleCleanupRan are declared near safeHandler (which updates them).
|
|
992
|
+
|
|
993
|
+
const IDLE_THRESHOLD_MS = 5 * 60 * 1000;
|
|
994
|
+
|
|
995
|
+
const idleTimer = setInterval(() => {
|
|
996
|
+
if (idleCleanupRan) return;
|
|
997
|
+
if (Date.now() - lastMcpRequestTime < IDLE_THRESHOLD_MS) return;
|
|
998
|
+
idleCleanupRan = true;
|
|
999
|
+
|
|
1000
|
+
try {
|
|
1001
|
+
const thirtyDaysAgo = Date.now() - 30 * 86400000;
|
|
1002
|
+
|
|
1003
|
+
db.transaction(() => {
|
|
1004
|
+
// Delete old low-quality observations (importance<=1, never accessed, 30+ days)
|
|
1005
|
+
const deleted = db.prepare(`
|
|
1006
|
+
DELETE FROM observations
|
|
1007
|
+
WHERE importance <= 1 AND COALESCE(access_count, 0) = 0
|
|
1008
|
+
AND created_at_epoch < ? AND COALESCE(compressed_into, 0) = 0
|
|
1009
|
+
`).run(thirtyDaysAgo);
|
|
1010
|
+
if (deleted.changes > 0) {
|
|
1011
|
+
debugLog('INFO', 'idle-cleanup', `Deleted ${deleted.changes} stale low-quality observations`);
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
// Mark old importance=1 as compressed (30+ days)
|
|
1015
|
+
// NOTE: compressed_into = -1 is an established sentinel meaning "auto-compressed without merge target"
|
|
1016
|
+
// (same pattern used in hook.mjs:456 for time-based compression)
|
|
1017
|
+
const compressed = db.prepare(`
|
|
1018
|
+
UPDATE observations SET compressed_into = -1
|
|
1019
|
+
WHERE COALESCE(compressed_into, 0) = 0 AND importance = 1
|
|
1020
|
+
AND created_at_epoch < ?
|
|
1021
|
+
`).run(thirtyDaysAgo);
|
|
1022
|
+
if (compressed.changes > 0) {
|
|
1023
|
+
debugLog('INFO', 'idle-cleanup', `Compressed ${compressed.changes} old observations`);
|
|
1024
|
+
}
|
|
1025
|
+
})();
|
|
1026
|
+
|
|
1027
|
+
// FTS5 index optimization (outside transaction — WAL-friendly)
|
|
1028
|
+
db.exec("INSERT INTO observations_fts(observations_fts) VALUES('optimize')");
|
|
1029
|
+
debugLog('DEBUG', 'idle-cleanup', 'FTS5 optimize complete');
|
|
1030
|
+
} catch (e) {
|
|
1031
|
+
debugCatch(e, 'idle-cleanup');
|
|
1032
|
+
}
|
|
1033
|
+
}, 60000); // Check every minute
|
|
1034
|
+
idleTimer.unref();
|
|
1035
|
+
|
|
983
1036
|
// ─── Shutdown Cleanup ────────────────────────────────────────────────────────
|
|
984
1037
|
|
|
985
1038
|
function shutdown(exitCode = 0) {
|
|
986
1039
|
clearInterval(walTimer);
|
|
1040
|
+
clearInterval(idleTimer);
|
|
987
1041
|
try { db.pragma('wal_checkpoint(TRUNCATE)'); } catch {}
|
|
988
1042
|
try { db.close(); } catch {}
|
|
989
1043
|
process.exit(exitCode);
|