memorix 1.7.2 → 1.8.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.
Files changed (90) hide show
  1. package/CHANGELOG.md +59 -1
  2. package/README.md +1 -1
  3. package/README.zh-CN.md +1 -1
  4. package/assets/star-history-dark.svg +4 -4
  5. package/assets/star-history-light.svg +4 -4
  6. package/assets/star-history.json +47 -3
  7. package/dist/cli/index.js +1801 -254
  8. package/dist/dashboard/static/app.js +88 -4
  9. package/dist/dashboard/static/index.html +10 -0
  10. package/dist/dashboard/static/style.css +86 -0
  11. package/dist/index.js +1241 -224
  12. package/dist/maintenance-runner.js +972 -126
  13. package/dist/media-audio-runner.js +967 -121
  14. package/dist/media-video-runner.js +972 -126
  15. package/dist/memcode-runtime/CHANGELOG.md +59 -1
  16. package/dist/sdk.js +1240 -223
  17. package/dist/types.d.ts +8 -0
  18. package/dist/vector-backfill-runner.js +972 -126
  19. package/docs/1.2.4-PERSISTENT-MEMORY-DELIVERY.md +1 -1
  20. package/docs/1.8.0-RELEASE-CANDIDATE-SPEC.md +75 -0
  21. package/docs/API_REFERENCE.md +37 -1
  22. package/docs/ARCHITECTURE.md +1 -1
  23. package/docs/CONFIGURATION.md +57 -1
  24. package/docs/DASHBOARD.md +10 -0
  25. package/docs/DEVELOPMENT.md +3 -2
  26. package/docs/INTEGRATIONS.md +1 -1
  27. package/docs/PERFORMANCE.md +3 -1
  28. package/docs/README.md +11 -1
  29. package/docs/SETUP.md +2 -1
  30. package/package.json +1 -1
  31. package/plugins/antigravity/memorix/rules/memorix.md +1 -1
  32. package/plugins/claude/memorix/.claude-plugin/plugin.json +1 -1
  33. package/plugins/claude/memorix/SKILL.md +1 -1
  34. package/plugins/claude/memorix/skills/memorix-memory/SKILL.md +2 -2
  35. package/plugins/codebuddy/memorix/.codebuddy-plugin/plugin.json +1 -1
  36. package/plugins/codebuddy/memorix/skills/memorix-memory/SKILL.md +2 -2
  37. package/plugins/codex/memorix/.codex-plugin/plugin.json +1 -1
  38. package/plugins/codex/memorix/skills/memorix-memory/SKILL.md +2 -2
  39. package/plugins/copilot/memorix/plugin.json +1 -1
  40. package/plugins/copilot/memorix/skills/memorix-memory/SKILL.md +2 -2
  41. package/plugins/cursor/memorix/skills/memorix-memory/SKILL.md +2 -2
  42. package/plugins/gemini/memorix/GEMINI.md +1 -1
  43. package/plugins/gemini/memorix/gemini-extension.json +1 -1
  44. package/plugins/omp/memorix/package.json +1 -1
  45. package/plugins/openclaw/memorix/.codex-plugin/plugin.json +1 -1
  46. package/plugins/pi/memorix/package.json +1 -1
  47. package/plugins/pi/memorix/skills/memorix-memory/SKILL.md +2 -2
  48. package/server.json +2 -2
  49. package/src/cli/capability-map.ts +2 -0
  50. package/src/cli/commands/config-migrate.ts +7 -0
  51. package/src/cli/commands/context.ts +11 -0
  52. package/src/cli/commands/doctor.ts +5 -1
  53. package/src/cli/commands/evidence.ts +56 -0
  54. package/src/cli/commands/feedback.ts +58 -0
  55. package/src/cli/commands/init.ts +17 -0
  56. package/src/cli/commands/resume.ts +2 -0
  57. package/src/cli/commands/serve-http.ts +201 -12
  58. package/src/cli/commands/status.ts +2 -0
  59. package/src/cli/fallback-budget.ts +59 -0
  60. package/src/cli/index.ts +5 -1
  61. package/src/compact/engine.ts +26 -1
  62. package/src/config/dotenv-loader.ts +17 -0
  63. package/src/config/resolved-config.ts +110 -12
  64. package/src/config/toml-loader.ts +8 -0
  65. package/src/config/yaml-loader.ts +9 -0
  66. package/src/dashboard/config-provenance.ts +17 -0
  67. package/src/dashboard/evidence.ts +63 -0
  68. package/src/dashboard/server.ts +93 -68
  69. package/src/dashboard/static/index.html +10 -0
  70. package/src/dashboard/static/style.css +86 -0
  71. package/src/hooks/installers/index.ts +1 -1
  72. package/src/hooks/official-skills.ts +3 -3
  73. package/src/hooks/rules/memorix-agent-rules.md +1 -1
  74. package/src/llm/provider.ts +33 -7
  75. package/src/memory/feedback-model.ts +4 -0
  76. package/src/memory/feedback.ts +220 -0
  77. package/src/memory/observations.ts +19 -0
  78. package/src/memory/retention.ts +38 -5
  79. package/src/multimodal/image-loader.ts +6 -3
  80. package/src/rerank/http-provider.ts +125 -0
  81. package/src/rerank/index.ts +81 -0
  82. package/src/rerank/policy.ts +88 -0
  83. package/src/server/mcp-binding-store.ts +79 -0
  84. package/src/server/mcp-diagnostics.ts +40 -0
  85. package/src/server/tool-profile.ts +4 -0
  86. package/src/server.ts +121 -1
  87. package/src/store/evidence-store.ts +261 -0
  88. package/src/store/orama-store.ts +79 -53
  89. package/src/store/sqlite-db.ts +104 -0
  90. package/src/types.ts +8 -0
@@ -0,0 +1,261 @@
1
+ import { createHash, randomUUID } from 'node:crypto';
2
+ import type { Observation } from '../types.js';
3
+ import { getDatabase } from './sqlite-db.js';
4
+
5
+ export type EvidenceCandidateKind = 'observation' | 'code' | 'git' | 'test' | 'document' | 'workflow' | 'graph';
6
+ export type EvidenceVerification = 'unverified' | 'verified' | 'conflicted';
7
+ export type EvidenceFreshness = 'current' | 'stale' | 'unknown';
8
+
9
+ export interface EvidenceCardRecord {
10
+ id: string;
11
+ projectId: string;
12
+ candidateKind: EvidenceCandidateKind;
13
+ candidateId: string;
14
+ title: string;
15
+ summary: string;
16
+ entityName?: string;
17
+ sourceKind: string;
18
+ sourceRef: string;
19
+ locator?: string;
20
+ capturedHash?: string;
21
+ sessionId?: string;
22
+ status: string;
23
+ verification: EvidenceVerification;
24
+ freshness: EvidenceFreshness;
25
+ staleReason?: string;
26
+ files: string[];
27
+ relatedEntities: string[];
28
+ createdAt: string;
29
+ updatedAt: string;
30
+ }
31
+
32
+ export interface EvidenceCardEvent {
33
+ id: string;
34
+ projectId: string;
35
+ cardId: string;
36
+ kind: string;
37
+ detail?: string;
38
+ createdAt: string;
39
+ }
40
+
41
+ function safeJson<T>(value: unknown, fallback: T): T {
42
+ if (typeof value !== 'string' || value.length === 0) return fallback;
43
+ try {
44
+ return JSON.parse(value) as T;
45
+ } catch {
46
+ return fallback;
47
+ }
48
+ }
49
+
50
+ function rowToCard(row: any): EvidenceCardRecord {
51
+ return {
52
+ id: String(row.id),
53
+ projectId: String(row.project_id),
54
+ candidateKind: row.candidate_kind,
55
+ candidateId: String(row.candidate_id),
56
+ title: String(row.title),
57
+ summary: String(row.summary ?? ''),
58
+ ...(row.entity_name ? { entityName: String(row.entity_name) } : {}),
59
+ sourceKind: String(row.source_kind),
60
+ sourceRef: String(row.source_ref),
61
+ ...(row.locator ? { locator: String(row.locator) } : {}),
62
+ ...(row.captured_hash ? { capturedHash: String(row.captured_hash) } : {}),
63
+ ...(row.session_id ? { sessionId: String(row.session_id) } : {}),
64
+ status: String(row.status ?? 'active'),
65
+ verification: row.verification,
66
+ freshness: row.freshness,
67
+ ...(row.stale_reason ? { staleReason: String(row.stale_reason) } : {}),
68
+ files: safeJson(row.files_json, []),
69
+ relatedEntities: safeJson(row.related_entities_json, []),
70
+ createdAt: String(row.created_at),
71
+ updatedAt: String(row.updated_at),
72
+ };
73
+ }
74
+
75
+ function observationHash(observation: Observation): string {
76
+ return createHash('sha256')
77
+ .update(JSON.stringify({
78
+ projectId: observation.projectId,
79
+ id: observation.id,
80
+ title: observation.title,
81
+ narrative: observation.narrative,
82
+ facts: observation.facts,
83
+ files: observation.filesModified,
84
+ relatedEntities: observation.relatedEntities,
85
+ }))
86
+ .digest('hex');
87
+ }
88
+
89
+ function sourceKind(observation: Observation): string {
90
+ if (observation.source === 'git' || observation.sourceDetail === 'git-ingest') return 'git';
91
+ if (observation.sourceDetail === 'hook') return 'hook';
92
+ return observation.source ?? 'agent';
93
+ }
94
+
95
+ /** SQLite-backed provenance cards used by MCP, CLI, and Dashboard surfaces. */
96
+ export class EvidenceCardStore {
97
+ private db: any = null;
98
+
99
+ async init(dataDir: string): Promise<void> {
100
+ this.db = getDatabase(dataDir);
101
+ }
102
+
103
+ private requireDb(): any {
104
+ if (!this.db) throw new Error('EvidenceCardStore is not initialized.');
105
+ return this.db;
106
+ }
107
+
108
+ upsertObservation(observation: Observation): EvidenceCardRecord {
109
+ const db = this.requireDb();
110
+ const now = new Date().toISOString();
111
+ const card: EvidenceCardRecord = {
112
+ id: `evidence:observation:${observation.projectId}:${observation.id}`,
113
+ projectId: observation.projectId,
114
+ candidateKind: 'observation',
115
+ candidateId: String(observation.id),
116
+ title: observation.title,
117
+ summary: observation.narrative.slice(0, 500),
118
+ entityName: observation.entityName,
119
+ sourceKind: sourceKind(observation),
120
+ sourceRef: `observation:${observation.id}`,
121
+ locator: observation.filesModified?.join(', ') || `observation:${observation.id}`,
122
+ capturedHash: observationHash(observation),
123
+ ...(observation.sessionId ? { sessionId: observation.sessionId } : {}),
124
+ status: observation.status ?? 'active',
125
+ verification: observation.status === 'resolved' ? 'verified' : 'unverified',
126
+ freshness: 'current',
127
+ files: observation.filesModified ?? [],
128
+ relatedEntities: observation.relatedEntities ?? [],
129
+ createdAt: observation.createdAt,
130
+ updatedAt: now,
131
+ };
132
+ db.prepare(`
133
+ INSERT INTO evidence_cards (
134
+ id, project_id, candidate_kind, candidate_id, title, summary, entity_name,
135
+ source_kind, source_ref, locator, captured_hash, session_id, status,
136
+ verification, freshness, stale_reason, files_json, related_entities_json,
137
+ created_at, updated_at
138
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?)
139
+ ON CONFLICT(project_id, candidate_kind, candidate_id) DO UPDATE SET
140
+ title = excluded.title,
141
+ summary = excluded.summary,
142
+ entity_name = excluded.entity_name,
143
+ source_kind = excluded.source_kind,
144
+ source_ref = excluded.source_ref,
145
+ locator = excluded.locator,
146
+ captured_hash = excluded.captured_hash,
147
+ session_id = excluded.session_id,
148
+ status = excluded.status,
149
+ freshness = 'current',
150
+ stale_reason = NULL,
151
+ files_json = excluded.files_json,
152
+ related_entities_json = excluded.related_entities_json,
153
+ updated_at = excluded.updated_at
154
+ `).run(
155
+ card.id,
156
+ card.projectId,
157
+ card.candidateKind,
158
+ card.candidateId,
159
+ card.title,
160
+ card.summary,
161
+ card.entityName ?? null,
162
+ card.sourceKind,
163
+ card.sourceRef,
164
+ card.locator ?? null,
165
+ card.capturedHash ?? null,
166
+ card.sessionId ?? null,
167
+ card.status,
168
+ card.verification,
169
+ card.freshness,
170
+ JSON.stringify(card.files),
171
+ JSON.stringify(card.relatedEntities),
172
+ card.createdAt,
173
+ card.updatedAt,
174
+ );
175
+ return this.get(card.projectId, card.candidateKind, card.candidateId) ?? card;
176
+ }
177
+
178
+ get(projectId: string, candidateKind: EvidenceCandidateKind, candidateId: string): EvidenceCardRecord | undefined {
179
+ const row = this.requireDb().prepare(`
180
+ SELECT * FROM evidence_cards WHERE project_id = ? AND candidate_kind = ? AND candidate_id = ?
181
+ `).get(projectId, candidateKind, candidateId);
182
+ return row ? rowToCard(row) : undefined;
183
+ }
184
+
185
+ list(projectId: string, options: { limit?: number; freshness?: EvidenceFreshness; status?: string } = {}): EvidenceCardRecord[] {
186
+ const limit = Math.min(100, Math.max(1, Math.floor(options.limit ?? 20)));
187
+ const clauses = ['project_id = ?'];
188
+ const args: unknown[] = [projectId];
189
+ if (options.freshness) { clauses.push('freshness = ?'); args.push(options.freshness); }
190
+ if (options.status) { clauses.push('status = ?'); args.push(options.status); }
191
+ args.push(limit);
192
+ const rows = this.requireDb().prepare(`
193
+ SELECT * FROM evidence_cards WHERE ${clauses.join(' AND ')} ORDER BY updated_at DESC, id DESC LIMIT ?
194
+ `).all(...args);
195
+ return rows.map(rowToCard);
196
+ }
197
+
198
+ syncObservations(observations: Observation[]): EvidenceCardRecord[] {
199
+ return observations.map((observation) => this.upsertObservation(observation));
200
+ }
201
+
202
+ markStaleForPaths(projectId: string, paths: string[], reason = 'source file changed'): number {
203
+ const normalized = new Set(paths.map((value) => value.replace(/\\/g, '/').toLowerCase()));
204
+ if (normalized.size === 0) return 0;
205
+ const db = this.requireDb();
206
+ const rows = db.prepare(`SELECT * FROM evidence_cards WHERE project_id = ? AND freshness != 'stale'`).all(projectId);
207
+ const update = db.prepare(`
208
+ UPDATE evidence_cards SET freshness = 'stale', stale_reason = ?, updated_at = ?
209
+ WHERE id = ?
210
+ `);
211
+ let changed = 0;
212
+ const now = new Date().toISOString();
213
+ const run = db.transaction(() => {
214
+ for (const row of rows) {
215
+ const files = safeJson<string[]>(row.files_json, []);
216
+ if (!files.some((file) => normalized.has(file.replace(/\\/g, '/').toLowerCase()))) continue;
217
+ update.run(reason, now, row.id);
218
+ changed++;
219
+ }
220
+ });
221
+ run();
222
+ return changed;
223
+ }
224
+
225
+ setVerification(projectId: string, cardId: string, verification: EvidenceVerification): void {
226
+ this.requireDb().prepare(`
227
+ UPDATE evidence_cards SET verification = ?, updated_at = ? WHERE project_id = ? AND id = ?
228
+ `).run(verification, new Date().toISOString(), projectId, cardId);
229
+ }
230
+
231
+ recordEvent(projectId: string, cardId: string, kind: string, detail?: string): EvidenceCardEvent {
232
+ const event: EvidenceCardEvent = {
233
+ id: randomUUID(),
234
+ projectId,
235
+ cardId,
236
+ kind,
237
+ ...(detail ? { detail: detail.slice(0, 2_000) } : {}),
238
+ createdAt: new Date().toISOString(),
239
+ };
240
+ this.requireDb().prepare(`
241
+ INSERT INTO evidence_card_events (id, project_id, card_id, kind, detail, created_at)
242
+ VALUES (?, ?, ?, ?, ?, ?)
243
+ `).run(event.id, event.projectId, event.cardId, event.kind, event.detail ?? null, event.createdAt);
244
+ return event;
245
+ }
246
+
247
+ listEvents(cardId: string, limit = 50): EvidenceCardEvent[] {
248
+ const rows = this.requireDb().prepare(`
249
+ SELECT * FROM evidence_card_events WHERE card_id = ? ORDER BY created_at DESC LIMIT ?
250
+ `).all(cardId, Math.min(200, Math.max(1, Math.floor(limit))));
251
+ return rows.map((row: any) => ({
252
+ id: String(row.id), projectId: String(row.project_id), cardId: String(row.card_id),
253
+ kind: String(row.kind), ...(row.detail ? { detail: String(row.detail) } : {}),
254
+ createdAt: String(row.created_at),
255
+ }));
256
+ }
257
+ }
258
+
259
+ export function buildEvidenceCardId(projectId: string, candidateKind: EvidenceCandidateKind, candidateId: string): string {
260
+ return `evidence:${candidateKind}:${projectId}:${candidateId}`;
261
+ }
@@ -23,6 +23,7 @@ import {
23
23
  import { calculateProjectAffinity, extractProjectKeywords, type AffinityContext, type MemoryContent } from './project-affinity.js';
24
24
  import { detectQueryIntent, applyIntentBoost } from '../search/intent-detector.js';
25
25
  import { maybeExpandSearchQuery } from '../search/query-expansion.js';
26
+ import { parseRerankTimeoutMs, shouldAttemptHttpRerank, shouldAttemptLlmRerank } from '../rerank/policy.js';
26
27
  import { withTimeout, withTimeoutSignal } from '../timeout.js';
27
28
 
28
29
  let db: AnyOrama | null = null;
@@ -1054,62 +1055,87 @@ export async function searchObservations(options: SearchOptions): Promise<IndexE
1054
1055
  }
1055
1056
  }
1056
1057
 
1057
- // ── LLM Reranking (heavy-tier only) ────────────────────────────
1058
- // Only triggered for heavy-tier queries with ambiguous top results.
1059
- // Non-thorough profiles, fast, and standard tiers skip entirely.
1060
- // Ambiguity check: top-2 scores within 30% → results are uncertain.
1061
- const shouldRerank = quality === 'thorough'
1062
- && tier === 'heavy'
1063
- && hasQuery
1064
- && intermediate.length > 2
1065
- && (() => {
1066
- const top = intermediate[0]?.score ?? 0;
1067
- const second = intermediate[1]?.score ?? 0;
1068
- return top > 0 && second / top > 0.7; // top-2 within 30% = ambiguous
1069
- })();
1070
-
1071
- if (shouldRerank) {
1072
- try {
1073
- const { rerankResults } = await import('../llm/quality.js');
1074
- const narrativeMap = new Map<string, string>();
1075
- for (const hit of results.hits) {
1076
- const doc = hit.document as unknown as MemorixDocument;
1077
- narrativeMap.set(makeEntryKey(doc.projectId, doc.observationId), doc.narrative);
1058
+ // HTTP rerank: thorough search with enough candidates.
1059
+ // LLM rerank: original thorough + heavy + close top-2 gate, and only when
1060
+ // HTTP rerank is not configured. A configured HTTP miss keeps original order.
1061
+ const httpGate = shouldAttemptHttpRerank({
1062
+ quality,
1063
+ hasQuery: Boolean(hasQuery),
1064
+ candidateCount: intermediate.length,
1065
+ });
1066
+ const llmGateWithoutHttp = shouldAttemptLlmRerank({
1067
+ quality,
1068
+ tier,
1069
+ hasQuery: Boolean(hasQuery),
1070
+ candidateCount: intermediate.length,
1071
+ topScore: intermediate[0]?.score ?? 0,
1072
+ secondScore: intermediate[1]?.score ?? 0,
1073
+ httpRerankConfigured: false,
1074
+ });
1075
+
1076
+ if (httpGate || llmGateWithoutHttp) {
1077
+ const narrativeMap = new Map<string, string>();
1078
+ for (const hit of results.hits) {
1079
+ const doc = hit.document as unknown as MemorixDocument;
1080
+ narrativeMap.set(makeEntryKey(doc.projectId, doc.observationId), doc.narrative);
1081
+ }
1082
+ const RERANK_TOP_K = 5;
1083
+ const toRerank = intermediate.slice(0, RERANK_TOP_K);
1084
+ const candidates = toRerank.map((e, index) => ({
1085
+ id: `r${index + 1}`,
1086
+ title: e.title,
1087
+ type: e.type,
1088
+ score: e.score,
1089
+ narrative: narrativeMap.get(makeEntryKey(e.projectId, e.id)),
1090
+ }));
1091
+ const RERANK_TIMEOUT_MS = parseRerankTimeoutMs(process.env.MEMORIX_RERANK_TIMEOUT_MS);
1092
+ const candidateMap = new Map(candidates.map((candidate, index) => [candidate.id, toRerank[index]]));
1093
+ const { isNeuralRerankEnabled, neuralRerankCandidates } = await import('../rerank/index.js');
1094
+ const httpConfigured = isNeuralRerankEnabled();
1095
+
1096
+ if (httpConfigured && httpGate) {
1097
+ try {
1098
+ const neural = await withTimeout(
1099
+ neuralRerankCandidates(originalQuery!, candidates),
1100
+ RERANK_TIMEOUT_MS,
1101
+ 'neural rerank',
1102
+ );
1103
+ if (neural && neural.length > 0) {
1104
+ const rerankedTop = neural
1105
+ .map((row) => candidateMap.get(row.id))
1106
+ .filter((entry): entry is NonNullable<typeof entry> => entry != null);
1107
+ if (rerankedTop.length > 0) {
1108
+ intermediate = [...rerankedTop, ...intermediate.slice(RERANK_TOP_K)];
1109
+ lastSearchModeByProject.set(modeKey, (lastSearchModeByProject.get(modeKey) ?? 'fulltext') + ' + neural rerank');
1110
+ mark('rerank(usedNeural=true)');
1111
+ }
1112
+ }
1113
+ } catch {
1114
+ console.error('[memorix] neural rerank failed or timed out; keeping original order');
1078
1115
  }
1079
- // Rerank only top-5 (was 10) to save LLM tokens and latency
1080
- const RERANK_TOP_K = 5;
1081
- const toRerank = intermediate.slice(0, RERANK_TOP_K);
1082
- const candidates = toRerank.map((e, index) => ({
1083
- id: `r${index + 1}`,
1084
- title: e.title,
1085
- type: e.type,
1086
- score: e.score,
1087
- narrative: narrativeMap.get(makeEntryKey(e.projectId, e.id)),
1088
- }));
1089
-
1090
- // LLM rerank timeout: configurable via MEMORIX_RERANK_TIMEOUT_MS, default 5s
1091
- const _parsedRerank = parseInt(process.env.MEMORIX_RERANK_TIMEOUT_MS || '', 10);
1092
- const RERANK_TIMEOUT_MS = Number.isFinite(_parsedRerank) && _parsedRerank > 0 ? _parsedRerank : 5000;
1093
- const { reranked, usedLLM } = await withTimeout(
1094
- rerankResults(originalQuery!, candidates),
1095
- RERANK_TIMEOUT_MS,
1096
- 'LLM rerank',
1097
- );
1098
- mark(`rerank(usedLLM=${usedLLM})`);
1099
-
1100
- if (usedLLM) {
1101
- lastSearchModeByProject.set(modeKey, (lastSearchModeByProject.get(modeKey) ?? 'fulltext') + ' + LLM rerank');
1102
- const candidateMap = new Map(candidates.map((candidate, index) => [candidate.id, toRerank[index]]));
1103
- const rerankedTop = reranked
1104
- .map(r => candidateMap.get(r.id))
1105
- .filter((e): e is NonNullable<typeof e> => e != null);
1106
- if (rerankedTop.length > 0) {
1107
- intermediate = [...rerankedTop, ...intermediate.slice(RERANK_TOP_K)];
1116
+ } else if (!httpConfigured && llmGateWithoutHttp) {
1117
+ try {
1118
+ const { rerankResults } = await import('../llm/quality.js');
1119
+ const { reranked, usedLLM } = await withTimeout(
1120
+ rerankResults(originalQuery!, candidates),
1121
+ RERANK_TIMEOUT_MS,
1122
+ 'LLM rerank',
1123
+ );
1124
+ mark(`rerank(usedLLM=${usedLLM})`);
1125
+ if (usedLLM) {
1126
+ lastSearchModeByProject.set(modeKey, (lastSearchModeByProject.get(modeKey) ?? 'fulltext') + ' + LLM rerank');
1127
+ const rerankedTop = reranked
1128
+ .map((row) => candidateMap.get(row.id))
1129
+ .filter((entry): entry is NonNullable<typeof entry> => entry != null);
1130
+ if (rerankedTop.length > 0) {
1131
+ intermediate = [...rerankedTop, ...intermediate.slice(RERANK_TOP_K)];
1132
+ }
1108
1133
  }
1134
+ } catch {
1135
+ console.error('[memorix] LLM rerank failed or timed out, using original order');
1109
1136
  }
1110
- } catch (error) {
1111
- // Reranking is best-effort: fall back to original order on timeout or error
1112
- console.error('[memorix] LLM rerank failed or timed out, using original order');
1137
+ } else {
1138
+ mark(`rerank(skipped,tier=${tier})`);
1113
1139
  }
1114
1140
  } else {
1115
1141
  mark(`rerank(skipped,tier=${tier})`);
@@ -742,6 +742,88 @@ CREATE TABLE IF NOT EXISTS compaction_checkpoints (
742
742
  );
743
743
  `;
744
744
 
745
+ // 1.8 context-control plane: persisted provenance cards, feedback state, and
746
+ // explicit stateless MCP project handles. These are append/update records in
747
+ // the same SQLite database as observations so a process restart cannot lose
748
+ // the context-control boundary.
749
+ const CREATE_EVIDENCE_CARDS_TABLE = `
750
+ CREATE TABLE IF NOT EXISTS evidence_cards (
751
+ id TEXT PRIMARY KEY,
752
+ project_id TEXT NOT NULL,
753
+ candidate_kind TEXT NOT NULL,
754
+ candidate_id TEXT NOT NULL,
755
+ title TEXT NOT NULL,
756
+ summary TEXT NOT NULL DEFAULT '',
757
+ entity_name TEXT,
758
+ source_kind TEXT NOT NULL,
759
+ source_ref TEXT NOT NULL,
760
+ locator TEXT,
761
+ captured_hash TEXT,
762
+ session_id TEXT,
763
+ status TEXT NOT NULL DEFAULT 'active',
764
+ verification TEXT NOT NULL DEFAULT 'unverified',
765
+ freshness TEXT NOT NULL DEFAULT 'current',
766
+ stale_reason TEXT,
767
+ files_json TEXT NOT NULL DEFAULT '[]',
768
+ related_entities_json TEXT NOT NULL DEFAULT '[]',
769
+ created_at TEXT NOT NULL,
770
+ updated_at TEXT NOT NULL,
771
+ UNIQUE(project_id, candidate_kind, candidate_id)
772
+ );
773
+ `;
774
+
775
+ const CREATE_EVIDENCE_CARD_EVENTS_TABLE = `
776
+ CREATE TABLE IF NOT EXISTS evidence_card_events (
777
+ id TEXT PRIMARY KEY,
778
+ project_id TEXT NOT NULL,
779
+ card_id TEXT NOT NULL,
780
+ kind TEXT NOT NULL,
781
+ detail TEXT,
782
+ created_at TEXT NOT NULL,
783
+ FOREIGN KEY (card_id) REFERENCES evidence_cards(id) ON DELETE CASCADE
784
+ );
785
+ `;
786
+
787
+ const CREATE_MEMORY_FEEDBACK_STATES_TABLE = `
788
+ CREATE TABLE IF NOT EXISTS memory_feedback_states (
789
+ project_id TEXT NOT NULL,
790
+ candidate_kind TEXT NOT NULL,
791
+ candidate_id TEXT NOT NULL,
792
+ weight REAL NOT NULL DEFAULT 1.0,
793
+ lifecycle_status TEXT NOT NULL DEFAULT 'active',
794
+ updated_at TEXT NOT NULL,
795
+ PRIMARY KEY (project_id, candidate_kind, candidate_id)
796
+ );
797
+ `;
798
+
799
+ const CREATE_MEMORY_FEEDBACK_EVENTS_TABLE = `
800
+ CREATE TABLE IF NOT EXISTS memory_feedback_events (
801
+ id TEXT PRIMARY KEY,
802
+ project_id TEXT NOT NULL,
803
+ candidate_kind TEXT NOT NULL,
804
+ candidate_id TEXT NOT NULL,
805
+ signal TEXT NOT NULL,
806
+ actor TEXT,
807
+ note TEXT,
808
+ source_ref TEXT NOT NULL,
809
+ target_event_id TEXT,
810
+ delta REAL NOT NULL DEFAULT 0,
811
+ observed_at TEXT NOT NULL
812
+ );
813
+ `;
814
+
815
+ const CREATE_MCP_BINDINGS_TABLE = `
816
+ CREATE TABLE IF NOT EXISTS mcp_bindings (
817
+ handle_id TEXT PRIMARY KEY,
818
+ project_id TEXT NOT NULL,
819
+ project_root TEXT NOT NULL,
820
+ data_dir TEXT NOT NULL,
821
+ created_at TEXT NOT NULL,
822
+ last_used_at TEXT NOT NULL,
823
+ expires_at TEXT
824
+ );
825
+ `;
826
+
745
827
  const CREATE_INDEXES = `
746
828
  CREATE INDEX IF NOT EXISTS idx_observations_projectId ON observations(projectId);
747
829
  CREATE INDEX IF NOT EXISTS idx_observations_topicKey ON observations(projectId, topicKey);
@@ -803,6 +885,12 @@ CREATE INDEX IF NOT EXISTS idx_maintenance_targets_updated ON maintenance_target
803
885
  CREATE INDEX IF NOT EXISTS idx_compaction_checkpoints_project_recent ON compaction_checkpoints(project_id, status, completed_at DESC, pre_captured_at DESC);
804
886
  CREATE INDEX IF NOT EXISTS idx_compaction_checkpoints_session_pending ON compaction_checkpoints(project_id, session_id, agent, phase, status, pre_captured_at DESC);
805
887
  CREATE INDEX IF NOT EXISTS idx_compaction_checkpoints_source ON compaction_checkpoints(project_id, source_key);
888
+ CREATE INDEX IF NOT EXISTS idx_evidence_cards_project ON evidence_cards(project_id, status, updated_at DESC);
889
+ CREATE INDEX IF NOT EXISTS idx_evidence_cards_candidate ON evidence_cards(project_id, candidate_kind, candidate_id);
890
+ CREATE INDEX IF NOT EXISTS idx_evidence_card_events_card ON evidence_card_events(card_id, created_at);
891
+ CREATE INDEX IF NOT EXISTS idx_memory_feedback_states_project ON memory_feedback_states(project_id, candidate_kind, updated_at DESC);
892
+ CREATE INDEX IF NOT EXISTS idx_memory_feedback_events_candidate ON memory_feedback_events(project_id, candidate_kind, candidate_id, observed_at DESC);
893
+ CREATE INDEX IF NOT EXISTS idx_mcp_bindings_project ON mcp_bindings(project_id, last_used_at DESC);
806
894
  CREATE UNIQUE INDEX IF NOT EXISTS idx_maintenance_jobs_active_dedupe
807
895
  ON maintenance_jobs(project_id, kind, dedupe_key)
808
896
  WHERE status IN ('pending', 'running', 'retry');
@@ -967,6 +1055,22 @@ const SCHEMA_MIGRATIONS: SchemaMigration[] = [
967
1055
  db.exec('CREATE INDEX IF NOT EXISTS idx_media_jobs_source_asset ON media_jobs(project_id, source_asset_id, status)');
968
1056
  },
969
1057
  },
1058
+ {
1059
+ id: '1.8-context-control-plane',
1060
+ apply: (db) => {
1061
+ db.exec(CREATE_EVIDENCE_CARDS_TABLE);
1062
+ db.exec(CREATE_EVIDENCE_CARD_EVENTS_TABLE);
1063
+ db.exec(CREATE_MEMORY_FEEDBACK_STATES_TABLE);
1064
+ db.exec(CREATE_MEMORY_FEEDBACK_EVENTS_TABLE);
1065
+ db.exec(CREATE_MCP_BINDINGS_TABLE);
1066
+ db.exec('CREATE INDEX IF NOT EXISTS idx_evidence_cards_project ON evidence_cards(project_id, status, updated_at DESC)');
1067
+ db.exec('CREATE INDEX IF NOT EXISTS idx_evidence_cards_candidate ON evidence_cards(project_id, candidate_kind, candidate_id)');
1068
+ db.exec('CREATE INDEX IF NOT EXISTS idx_evidence_card_events_card ON evidence_card_events(card_id, created_at)');
1069
+ db.exec('CREATE INDEX IF NOT EXISTS idx_memory_feedback_states_project ON memory_feedback_states(project_id, candidate_kind, updated_at DESC)');
1070
+ db.exec('CREATE INDEX IF NOT EXISTS idx_memory_feedback_events_candidate ON memory_feedback_events(project_id, candidate_kind, candidate_id, observed_at DESC)');
1071
+ db.exec('CREATE INDEX IF NOT EXISTS idx_mcp_bindings_project ON mcp_bindings(project_id, last_used_at DESC)');
1072
+ },
1073
+ },
970
1074
  ];
971
1075
 
972
1076
  function applySchemaMigrations(db: any): void {
package/src/types.ts CHANGED
@@ -280,6 +280,10 @@ export interface IndexEntry {
280
280
  documentType?: DocumentType;
281
281
  /** Knowledge layer for layer-aware ranking (Phase 3a) */
282
282
  knowledgeLayer?: KnowledgeLayer;
283
+ /** Feedback materialized weight used by the context-control plane. */
284
+ feedbackWeight?: number;
285
+ /** Feedback lifecycle projection used to avoid recalling conflicted records. */
286
+ feedbackStatus?: ObservationStatus;
283
287
  }
284
288
 
285
289
  /** Explicit reference to an observation, optionally scoped to a project. */
@@ -390,6 +394,10 @@ export interface MemorixDocument {
390
394
  documentType?: DocumentType;
391
395
  /** Knowledge layer for layer-aware ranking (Phase 3a) */
392
396
  knowledgeLayer?: KnowledgeLayer;
397
+ /** Feedback materialized weight used by context-control retrieval. */
398
+ feedbackWeight?: number;
399
+ /** Feedback lifecycle projection used by retention and retrieval. */
400
+ feedbackStatus?: ObservationStatus;
393
401
  }
394
402
 
395
403
  // ============================================================