memorix 1.10.2 → 1.10.4
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/CHANGELOG.md +82 -1
- package/README.md +3 -1
- package/README.zh-CN.md +3 -1
- package/dist/cli/index.js +2281 -1486
- package/dist/cli/memcode.js +0 -0
- package/dist/index.js +1874 -1126
- package/dist/{maintenance-jobs-DVRF-46r.d.ts → maintenance-jobs-DweoCoTT.d.ts} +25 -15
- package/dist/maintenance-runner.d.ts +1 -1
- package/dist/maintenance-runner.js +802 -194
- package/dist/media-audio-runner.js +793 -186
- package/dist/media-video-runner.js +801 -194
- package/dist/memcode-runtime/CHANGELOG.md +64 -1
- package/dist/sdk.js +14036 -13317
- package/dist/types.d.ts +1 -1
- package/dist/vector-backfill-runner.d.ts +1 -1
- package/dist/vector-backfill-runner.js +801 -194
- package/docs/ARCHITECTURE.md +219 -0
- package/docs/CONFIGURATION.md +49 -0
- package/docs/DESIGN_DECISIONS.md +37 -0
- package/docs/DEVELOPMENT.md +1 -1
- package/docs/INTEGRATIONS.md +1 -1
- package/docs/MODULES.md +7 -1
- package/docs/PERFORMANCE.md +14 -0
- package/docs/README.md +2 -1
- package/docs/ROADMAP-1.9.12-2.0.md +18 -2
- package/docs/SETUP.md +19 -4
- package/package.json +1 -1
- package/plugins/antigravity/memorix/plugin.json +1 -1
- package/plugins/claude/memorix/.claude-plugin/plugin.json +1 -1
- package/plugins/codebuddy/memorix/.codebuddy-plugin/plugin.json +1 -1
- package/plugins/codex/memorix/.codex-plugin/plugin.json +1 -1
- package/plugins/copilot/memorix/plugin.json +1 -1
- package/plugins/gemini/memorix/gemini-extension.json +1 -1
- package/plugins/hermes/memorix/plugin.yaml +1 -1
- package/plugins/omp/memorix/package.json +1 -1
- package/plugins/openclaw/memorix/.codex-plugin/plugin.json +1 -1
- package/plugins/pi/memorix/package.json +1 -1
- package/server.json +2 -2
- package/src/cli/commands/agent-integrations.ts +1 -1
- package/src/cli/commands/background.ts +1 -1
- package/src/cli/commands/context.ts +3 -2
- package/src/cli/commands/doctor.ts +9 -0
- package/src/cli/commands/explain.ts +3 -2
- package/src/cli/commands/init.ts +6 -2
- package/src/cli/commands/setup.ts +9 -6
- package/src/cli/commands/uninstall.ts +15 -0
- package/src/cli/tui/App.tsx +9 -16
- package/src/cli/tui/ConfigureView.tsx +2 -0
- package/src/cli/tui/chat-service.ts +6 -9
- package/src/cli/tui/data.ts +2 -2
- package/src/codegraph/auto-context.ts +10 -3
- package/src/codegraph/derived-view.ts +63 -0
- package/src/codegraph/store.ts +37 -0
- package/src/codegraph/types.ts +3 -0
- package/src/compact/engine.ts +10 -33
- package/src/config/dotenv-loader.ts +1 -0
- package/src/config/resolved-config.ts +18 -2
- package/src/config/retrieval-strategy.ts +22 -0
- package/src/config/toml-loader.ts +6 -0
- package/src/config/yaml-loader.ts +11 -2
- package/src/core/derived-view.ts +15 -0
- package/src/core/maintenance-result.ts +33 -0
- package/src/core/retrieval-contracts.ts +57 -0
- package/src/core/retrieval-facade.ts +18 -0
- package/src/core/retrieval-policy.ts +44 -0
- package/src/core/runtime-context.ts +42 -0
- package/src/dashboard/server.ts +5 -6
- package/src/embedding/api-provider.ts +18 -8
- package/src/hooks/handler.ts +0 -1
- package/src/integrations/registry.ts +2 -1
- package/src/knowledge/workset.ts +0 -2
- package/src/llm/provider.ts +2 -1
- package/src/memory/consolidation.ts +3 -2
- package/src/memory/feedback-model.ts +1 -4
- package/src/memory/observations.ts +226 -104
- package/src/multimodal/image-loader.ts +2 -2
- package/src/runtime/control-plane-maintenance.ts +1 -0
- package/src/runtime/isolated-maintenance.ts +1 -0
- package/src/runtime/maintenance-jobs.ts +123 -32
- package/src/runtime/open-browser.ts +25 -0
- package/src/runtime/project-maintenance.ts +69 -1
- package/src/sdk.ts +3 -2
- package/src/search/fts-view.ts +53 -0
- package/src/search/orama-view.ts +40 -0
- package/src/search/retrieval-facade.ts +38 -0
- package/src/search/semantic-index.ts +13 -0
- package/src/search/semantic-view.ts +34 -0
- package/src/server.ts +21 -9
- package/src/store/obs-store.ts +15 -0
- package/src/store/orama-store.ts +143 -45
- package/src/store/sqlite-db.ts +22 -0
- package/src/store/sqlite-store.ts +27 -0
- package/src/types.ts +1 -1
- package/src/workspace/mcp-adapters/pi.ts +58 -0
- package/plugins/pi/memorix/skills/memorix-git-memory/SKILL.md +0 -26
- package/plugins/pi/memorix/skills/memorix-memory/SKILL.md +0 -51
- package/plugins/pi/memorix/skills/memorix-mini-skills/SKILL.md +0 -31
- package/plugins/pi/memorix/skills/memorix-orchestrate/SKILL.md +0 -28
- package/plugins/pi/memorix/skills/memorix-reasoning/SKILL.md +0 -30
- package/plugins/pi/memorix/skills/memorix-sessions/SKILL.md +0 -25
- package/plugins/pi/memorix/skills/memorix-troubleshooting/SKILL.md +0 -27
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
2
|
|
|
3
|
+
import { assertMaintenanceProgress, type MaintenanceProgress, type MaintenanceResult } from '../core/maintenance-result.js';
|
|
3
4
|
import { getDatabase } from '../store/sqlite-db.js';
|
|
4
5
|
import { sanitizeCredentials } from '../memory/secret-filter.js';
|
|
5
6
|
|
|
7
|
+
export type { MaintenanceProgress } from '../core/maintenance-result.js';
|
|
8
|
+
|
|
6
9
|
export const MAINTENANCE_JOB_KINDS = [
|
|
7
10
|
'vector-backfill',
|
|
8
11
|
'media-video-generation',
|
|
@@ -11,6 +14,7 @@ export const MAINTENANCE_JOB_KINDS = [
|
|
|
11
14
|
'consolidation',
|
|
12
15
|
'codegraph-refresh',
|
|
13
16
|
'observation-qualify',
|
|
17
|
+
'observation-project',
|
|
14
18
|
'claim-derive',
|
|
15
19
|
'claim-requalification',
|
|
16
20
|
'knowledge-compile',
|
|
@@ -35,6 +39,7 @@ export interface MaintenanceJob {
|
|
|
35
39
|
leaseOwner?: string;
|
|
36
40
|
leaseExpiresAt?: number;
|
|
37
41
|
lastError?: string;
|
|
42
|
+
progress?: MaintenanceProgress;
|
|
38
43
|
createdAt: number;
|
|
39
44
|
updatedAt: number;
|
|
40
45
|
completedAt?: number;
|
|
@@ -68,20 +73,7 @@ export interface MaintenanceJobSummary {
|
|
|
68
73
|
failed: number;
|
|
69
74
|
}
|
|
70
75
|
|
|
71
|
-
export type MaintenanceJobRunResult =
|
|
72
|
-
| { action: 'complete' }
|
|
73
|
-
| {
|
|
74
|
-
action: 'reschedule';
|
|
75
|
-
delayMs: number;
|
|
76
|
-
resetAttempts?: boolean;
|
|
77
|
-
payload?: Record<string, unknown>;
|
|
78
|
-
/** Keep recoverable work out of the immediate pending queue during a cooldown. */
|
|
79
|
-
status?: 'pending' | 'retry';
|
|
80
|
-
/** Persist a sanitized diagnostic without consuming the job's retry budget. */
|
|
81
|
-
lastError?: string;
|
|
82
|
-
/** Clear a stale transient diagnostic after the job makes progress. */
|
|
83
|
-
clearLastError?: boolean;
|
|
84
|
-
};
|
|
76
|
+
export type MaintenanceJobRunResult = MaintenanceResult;
|
|
85
77
|
|
|
86
78
|
export type MaintenanceJobHandler = (
|
|
87
79
|
job: MaintenanceJob,
|
|
@@ -120,6 +112,14 @@ function rowToJob(row: any): MaintenanceJob {
|
|
|
120
112
|
...(row.lease_owner ? { leaseOwner: row.lease_owner } : {}),
|
|
121
113
|
...(row.lease_expires_at != null ? { leaseExpiresAt: Number(row.lease_expires_at) } : {}),
|
|
122
114
|
...(row.last_error ? { lastError: row.last_error } : {}),
|
|
115
|
+
...(Number(row.progress_recorded ?? 0) === 1
|
|
116
|
+
? { progress: {
|
|
117
|
+
attempted: Number(row.attempted ?? 0),
|
|
118
|
+
succeeded: Number(row.succeeded ?? 0),
|
|
119
|
+
failed: Number(row.failed ?? 0),
|
|
120
|
+
remaining: Number(row.remaining ?? 0),
|
|
121
|
+
} }
|
|
122
|
+
: {}),
|
|
123
123
|
createdAt: Number(row.created_at),
|
|
124
124
|
updatedAt: Number(row.updated_at),
|
|
125
125
|
...(row.completed_at != null ? { completedAt: Number(row.completed_at) } : {}),
|
|
@@ -181,6 +181,16 @@ export class MaintenanceJobStore {
|
|
|
181
181
|
this.commit();
|
|
182
182
|
return rowToJob(existing);
|
|
183
183
|
}
|
|
184
|
+
if (existing.status === 'running') {
|
|
185
|
+
this.db.prepare(`
|
|
186
|
+
UPDATE maintenance_jobs
|
|
187
|
+
SET rerun_requested = 1, payload_json = ?, updated_at = ?
|
|
188
|
+
WHERE id = ?
|
|
189
|
+
`).run(payloadJson, now, existing.id);
|
|
190
|
+
const updated = this.getRow(existing.id)!;
|
|
191
|
+
this.commit();
|
|
192
|
+
return rowToJob(updated);
|
|
193
|
+
}
|
|
184
194
|
const nextRunAfter = Math.min(Number(existing.run_after), runAfter);
|
|
185
195
|
this.db.prepare(`
|
|
186
196
|
UPDATE maintenance_jobs
|
|
@@ -327,8 +337,10 @@ export class MaintenanceJobStore {
|
|
|
327
337
|
this.db.prepare(`
|
|
328
338
|
UPDATE maintenance_jobs
|
|
329
339
|
SET
|
|
330
|
-
status = CASE WHEN attempts >= max_attempts THEN 'failed' ELSE 'retry' END,
|
|
331
|
-
run_after = CASE WHEN attempts >= max_attempts THEN run_after ELSE ? END,
|
|
340
|
+
status = CASE WHEN attempts >= max_attempts AND rerun_requested = 0 THEN 'failed' ELSE 'retry' END,
|
|
341
|
+
run_after = CASE WHEN attempts >= max_attempts AND rerun_requested = 0 THEN run_after ELSE ? END,
|
|
342
|
+
attempts = CASE WHEN rerun_requested = 1 AND attempts >= max_attempts THEN 0 ELSE attempts END,
|
|
343
|
+
rerun_requested = 0,
|
|
332
344
|
lease_owner = NULL,
|
|
333
345
|
lease_expires_at = NULL,
|
|
334
346
|
last_error = COALESCE(last_error, 'worker lease expired'),
|
|
@@ -398,14 +410,55 @@ export class MaintenanceJobStore {
|
|
|
398
410
|
return Number(result.changes) > 0;
|
|
399
411
|
}
|
|
400
412
|
|
|
401
|
-
complete(id: string, workerId: string, now = Date.now()): MaintenanceJob | undefined {
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
413
|
+
complete(id: string, workerId: string, now = Date.now(), progress?: MaintenanceProgress): MaintenanceJob | undefined {
|
|
414
|
+
assertMaintenanceProgress(progress);
|
|
415
|
+
this.begin();
|
|
416
|
+
try {
|
|
417
|
+
const current = this.db.prepare(`
|
|
418
|
+
SELECT rerun_requested FROM maintenance_jobs
|
|
419
|
+
WHERE id = ? AND status = 'running' AND lease_owner = ?
|
|
420
|
+
`).get(id, workerId);
|
|
421
|
+
if (!current) {
|
|
422
|
+
this.commit();
|
|
423
|
+
return undefined;
|
|
424
|
+
}
|
|
425
|
+
if (Number(current.rerun_requested) === 1) {
|
|
426
|
+
this.db.prepare(`
|
|
427
|
+
UPDATE maintenance_jobs
|
|
428
|
+
SET status = 'pending', attempts = 0, run_after = ?,
|
|
429
|
+
rerun_requested = 0, lease_owner = NULL, lease_expires_at = NULL,
|
|
430
|
+
attempted = 0, succeeded = 0, failed = 0, remaining = 0,
|
|
431
|
+
progress_recorded = 0, last_error = NULL, completed_at = NULL,
|
|
432
|
+
updated_at = ?
|
|
433
|
+
WHERE id = ?
|
|
434
|
+
`).run(now, now, id);
|
|
435
|
+
} else {
|
|
436
|
+
this.db.prepare(`
|
|
437
|
+
UPDATE maintenance_jobs
|
|
438
|
+
SET status = 'completed', lease_owner = NULL, lease_expires_at = NULL,
|
|
439
|
+
attempted = COALESCE(?, attempted), succeeded = COALESCE(?, succeeded),
|
|
440
|
+
failed = COALESCE(?, failed), remaining = COALESCE(?, remaining),
|
|
441
|
+
progress_recorded = CASE WHEN ? THEN 1 ELSE progress_recorded END,
|
|
442
|
+
completed_at = ?, updated_at = ?
|
|
443
|
+
WHERE id = ?
|
|
444
|
+
`).run(
|
|
445
|
+
progress?.attempted ?? null,
|
|
446
|
+
progress?.succeeded ?? null,
|
|
447
|
+
progress?.failed ?? null,
|
|
448
|
+
progress?.remaining ?? null,
|
|
449
|
+
progress ? 1 : 0,
|
|
450
|
+
now,
|
|
451
|
+
now,
|
|
452
|
+
id,
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
const updated = this.get(id);
|
|
456
|
+
this.commit();
|
|
457
|
+
return updated;
|
|
458
|
+
} catch (error) {
|
|
459
|
+
this.rollback();
|
|
460
|
+
throw error;
|
|
461
|
+
}
|
|
409
462
|
}
|
|
410
463
|
|
|
411
464
|
reschedule(
|
|
@@ -418,6 +471,7 @@ export class MaintenanceJobStore {
|
|
|
418
471
|
status?: 'pending' | 'retry';
|
|
419
472
|
lastError?: string;
|
|
420
473
|
clearLastError?: boolean;
|
|
474
|
+
progress?: MaintenanceProgress;
|
|
421
475
|
now?: number;
|
|
422
476
|
},
|
|
423
477
|
): MaintenanceJob | undefined {
|
|
@@ -427,22 +481,37 @@ export class MaintenanceJobStore {
|
|
|
427
481
|
const status = options.status === 'retry' ? 'retry' : 'pending';
|
|
428
482
|
const hasLastError = options.lastError !== undefined;
|
|
429
483
|
const lastError = hasLastError ? errorText(options.lastError) : null;
|
|
484
|
+
const progress = options.progress;
|
|
485
|
+
assertMaintenanceProgress(progress);
|
|
430
486
|
this.db.prepare(`
|
|
431
487
|
UPDATE maintenance_jobs
|
|
432
|
-
SET status = ?,
|
|
433
|
-
|
|
488
|
+
SET status = ?,
|
|
489
|
+
run_after = CASE WHEN rerun_requested = 1 AND ? = 0 THEN ? ELSE ? END,
|
|
490
|
+
attempts = CASE WHEN rerun_requested = 1 AND ? = 0 THEN 0 WHEN ? THEN 0 ELSE attempts END,
|
|
491
|
+
payload_json = CASE WHEN rerun_requested = 1 THEN payload_json ELSE COALESCE(?, payload_json) END,
|
|
492
|
+
attempted = COALESCE(?, attempted), succeeded = COALESCE(?, succeeded),
|
|
493
|
+
failed = COALESCE(?, failed), remaining = COALESCE(?, remaining),
|
|
494
|
+
progress_recorded = CASE WHEN ? THEN 1 ELSE progress_recorded END,
|
|
434
495
|
last_error = CASE
|
|
435
496
|
WHEN ? THEN NULL
|
|
436
497
|
WHEN ? THEN ?
|
|
437
498
|
ELSE last_error
|
|
438
499
|
END,
|
|
439
|
-
lease_owner = NULL, lease_expires_at = NULL, updated_at = ?
|
|
500
|
+
rerun_requested = 0, lease_owner = NULL, lease_expires_at = NULL, updated_at = ?
|
|
440
501
|
WHERE id = ? AND status = 'running' AND lease_owner = ?
|
|
441
502
|
`).run(
|
|
442
503
|
status,
|
|
504
|
+
status === 'retry' ? 1 : 0,
|
|
505
|
+
now,
|
|
443
506
|
now + delayMs,
|
|
507
|
+
status === 'retry' ? 1 : 0,
|
|
444
508
|
options.resetAttempts ? 1 : 0,
|
|
445
509
|
payloadJson,
|
|
510
|
+
progress?.attempted ?? null,
|
|
511
|
+
progress?.succeeded ?? null,
|
|
512
|
+
progress?.failed ?? null,
|
|
513
|
+
progress?.remaining ?? null,
|
|
514
|
+
progress ? 1 : 0,
|
|
446
515
|
options.clearLastError ? 1 : 0,
|
|
447
516
|
hasLastError ? 1 : 0,
|
|
448
517
|
lastError,
|
|
@@ -462,6 +531,21 @@ export class MaintenanceJobStore {
|
|
|
462
531
|
return Number(result.changes ?? 0);
|
|
463
532
|
}
|
|
464
533
|
|
|
534
|
+
/** A successful foreground projection can acknowledge its pending outbox row. */
|
|
535
|
+
acknowledgePending(
|
|
536
|
+
projectId: string,
|
|
537
|
+
kind: MaintenanceJobKind,
|
|
538
|
+
dedupeKey: string,
|
|
539
|
+
now = Date.now(),
|
|
540
|
+
): number {
|
|
541
|
+
const result = this.db.prepare(`
|
|
542
|
+
UPDATE maintenance_jobs
|
|
543
|
+
SET status = 'completed', completed_at = ?, updated_at = ?
|
|
544
|
+
WHERE project_id = ? AND kind = ? AND dedupe_key = ? AND status = 'pending'
|
|
545
|
+
`).run(now, now, projectId, kind, dedupeKey);
|
|
546
|
+
return Number(result.changes ?? 0);
|
|
547
|
+
}
|
|
548
|
+
|
|
465
549
|
fail(id: string, workerId: string, error: unknown, now = Date.now()): MaintenanceJob | undefined {
|
|
466
550
|
this.begin();
|
|
467
551
|
try {
|
|
@@ -475,15 +559,18 @@ export class MaintenanceJobStore {
|
|
|
475
559
|
}
|
|
476
560
|
|
|
477
561
|
const attempts = Number(current.attempts);
|
|
478
|
-
const
|
|
562
|
+
const rerunRequested = Number(current.rerun_requested) === 1;
|
|
563
|
+
const exhausted = attempts >= Number(current.max_attempts) && !rerunRequested;
|
|
479
564
|
const status: MaintenanceJobStatus = exhausted ? 'failed' : 'retry';
|
|
480
565
|
const runAfter = exhausted ? Number(current.run_after) : now + getMaintenanceRetryDelayMs(attempts);
|
|
566
|
+
const nextAttempts = rerunRequested && attempts >= Number(current.max_attempts) ? 0 : attempts;
|
|
481
567
|
this.db.prepare(`
|
|
482
568
|
UPDATE maintenance_jobs
|
|
483
|
-
SET status = ?, run_after = ?,
|
|
569
|
+
SET status = ?, run_after = ?, attempts = ?, rerun_requested = 0,
|
|
570
|
+
lease_owner = NULL, lease_expires_at = NULL,
|
|
484
571
|
last_error = ?, updated_at = ?
|
|
485
572
|
WHERE id = ?
|
|
486
|
-
`).run(status, runAfter, errorText(error), now, id);
|
|
573
|
+
`).run(status, runAfter, nextAttempts, errorText(error), now, id);
|
|
487
574
|
const updated = this.getRow(id)!;
|
|
488
575
|
this.commit();
|
|
489
576
|
return rowToJob(updated);
|
|
@@ -575,12 +662,16 @@ export class MaintenanceJobWorker {
|
|
|
575
662
|
status: result.status,
|
|
576
663
|
lastError: result.lastError,
|
|
577
664
|
clearLastError: result.clearLastError,
|
|
665
|
+
progress: result.progress,
|
|
578
666
|
now,
|
|
579
667
|
});
|
|
580
668
|
return { state: 'rescheduled', job: updated };
|
|
581
669
|
}
|
|
582
|
-
|
|
583
|
-
|
|
670
|
+
if (result?.progress && result.progress.remaining > 0) {
|
|
671
|
+
throw new Error('maintenance job reported completion with remaining work');
|
|
672
|
+
}
|
|
673
|
+
const updated = this.store.complete(job.id, this.workerId, now, result?.progress);
|
|
674
|
+
return { state: updated?.status === 'pending' ? 'rescheduled' : 'completed', job: updated };
|
|
584
675
|
} catch (error) {
|
|
585
676
|
const updated = this.store.fail(job.id, this.workerId, error, now);
|
|
586
677
|
return { state: 'failed', job: updated };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
|
|
3
|
+
const LOCAL_HTTP_HOSTS = new Set(['127.0.0.1', 'localhost', '[::1]']);
|
|
4
|
+
|
|
5
|
+
export function resolveLocalBrowserCommand(
|
|
6
|
+
target: string,
|
|
7
|
+
platform: NodeJS.Platform = process.platform,
|
|
8
|
+
): { command: string; args: string[] } {
|
|
9
|
+
const url = new URL(target);
|
|
10
|
+
if (url.protocol !== 'http:' || !LOCAL_HTTP_HOSTS.has(url.hostname) || !url.port || url.username || url.password) {
|
|
11
|
+
throw new Error('Only unauthenticated loopback HTTP URLs can be opened automatically');
|
|
12
|
+
}
|
|
13
|
+
return platform === 'win32'
|
|
14
|
+
? { command: 'explorer.exe', args: [url.toString()] }
|
|
15
|
+
: platform === 'darwin'
|
|
16
|
+
? { command: 'open', args: [url.toString()] }
|
|
17
|
+
: { command: 'xdg-open', args: [url.toString()] };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function openLocalBrowser(target: string): void {
|
|
21
|
+
const { command, args } = resolveLocalBrowserCommand(target);
|
|
22
|
+
const child = spawn(command, args, { detached: true, stdio: 'ignore', windowsHide: true });
|
|
23
|
+
child.once('error', () => undefined);
|
|
24
|
+
child.unref();
|
|
25
|
+
}
|
|
@@ -121,6 +121,17 @@ function observationId(payload: Record<string, unknown>): number | undefined {
|
|
|
121
121
|
: undefined;
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
+
async function automaticCleanupEnabled(projectRoot: string | undefined): Promise<boolean> {
|
|
125
|
+
if (!projectRoot) return true;
|
|
126
|
+
try {
|
|
127
|
+
const { getResolvedConfig } = await import('../config/resolved-config.js');
|
|
128
|
+
return getResolvedConfig({ projectRoot }).memory.autoCleanup !== false;
|
|
129
|
+
} catch {
|
|
130
|
+
// Keep existing behavior when configuration is malformed or unavailable.
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
124
135
|
function workspaceMode(payload: Record<string, unknown>): 'local' | 'versioned' | undefined {
|
|
125
136
|
return payload.workspaceMode === 'local' || payload.workspaceMode === 'versioned'
|
|
126
137
|
? payload.workspaceMode
|
|
@@ -234,6 +245,15 @@ export function createProjectMaintenanceHandler(
|
|
|
234
245
|
return { action: 'reschedule', delayMs: VECTOR_RETRY_DELAY_MS };
|
|
235
246
|
}
|
|
236
247
|
|
|
248
|
+
if (
|
|
249
|
+
(job.kind === 'retention-archive'
|
|
250
|
+
|| job.kind === 'consolidation'
|
|
251
|
+
|| job.kind === 'long-term-maintenance')
|
|
252
|
+
&& !(await automaticCleanupEnabled(projectRoot))
|
|
253
|
+
) {
|
|
254
|
+
return { action: 'complete' };
|
|
255
|
+
}
|
|
256
|
+
|
|
237
257
|
if (job.kind === 'media-video-generation') {
|
|
238
258
|
const { runMiniMaxVideoGenerationJob } = await import('../media/video-jobs.js');
|
|
239
259
|
return runMiniMaxVideoGenerationJob(job, { dataDir: projectDir, projectId });
|
|
@@ -334,6 +354,34 @@ export function createProjectMaintenanceHandler(
|
|
|
334
354
|
return { action: 'complete' };
|
|
335
355
|
}
|
|
336
356
|
|
|
357
|
+
if (job.kind === 'observation-project') {
|
|
358
|
+
if (!projectRoot) {
|
|
359
|
+
throw new Error(`Observation projection requires a project root for ${projectId}`);
|
|
360
|
+
}
|
|
361
|
+
const id = observationId(job.payload);
|
|
362
|
+
if (!id) return { action: 'complete' };
|
|
363
|
+
const [
|
|
364
|
+
{ CodeGraphStore },
|
|
365
|
+
{ bindObservationToCode },
|
|
366
|
+
{ EvidenceCardStore },
|
|
367
|
+
{ getObservationStore },
|
|
368
|
+
] = await Promise.all([
|
|
369
|
+
import('../codegraph/store.js'),
|
|
370
|
+
import('../codegraph/binder.js'),
|
|
371
|
+
import('../store/evidence-store.js'),
|
|
372
|
+
import('../store/obs-store.js'),
|
|
373
|
+
]);
|
|
374
|
+
const observation = await getObservationStore().getById(id);
|
|
375
|
+
if (!observation || observation.projectId !== projectId) return { action: 'complete' };
|
|
376
|
+
const codeStore = new CodeGraphStore();
|
|
377
|
+
await codeStore.init(projectDir);
|
|
378
|
+
await bindObservationToCode(codeStore, observation);
|
|
379
|
+
const evidence = new EvidenceCardStore();
|
|
380
|
+
await evidence.init(projectDir);
|
|
381
|
+
evidence.upsertObservation(observation);
|
|
382
|
+
return { action: 'complete' };
|
|
383
|
+
}
|
|
384
|
+
|
|
337
385
|
if (job.kind === 'observation-qualify') {
|
|
338
386
|
const [
|
|
339
387
|
{ CodeGraphStore },
|
|
@@ -558,7 +606,15 @@ export function createProjectMaintenanceHandler(
|
|
|
558
606
|
const after = getVectorStatus(projectId);
|
|
559
607
|
if (after.missing === 0) {
|
|
560
608
|
resolveSupersededVectorFailures(projectDir, projectId);
|
|
561
|
-
return {
|
|
609
|
+
return {
|
|
610
|
+
action: 'complete',
|
|
611
|
+
progress: {
|
|
612
|
+
attempted: result.attempted,
|
|
613
|
+
succeeded: result.succeeded,
|
|
614
|
+
failed: result.failed,
|
|
615
|
+
remaining: 0,
|
|
616
|
+
},
|
|
617
|
+
};
|
|
562
618
|
}
|
|
563
619
|
|
|
564
620
|
if (result.failed > 0 && result.succeeded === 0) {
|
|
@@ -574,6 +630,12 @@ export function createProjectMaintenanceHandler(
|
|
|
574
630
|
...job.payload,
|
|
575
631
|
vectorBackfillFailureStreak: streak,
|
|
576
632
|
},
|
|
633
|
+
progress: {
|
|
634
|
+
attempted: result.attempted,
|
|
635
|
+
succeeded: result.succeeded,
|
|
636
|
+
failed: result.failed,
|
|
637
|
+
remaining: after.missing,
|
|
638
|
+
},
|
|
577
639
|
lastError: result.lastError ?? `vector backfill made no progress (${result.failed}/${result.attempted} failed)`,
|
|
578
640
|
};
|
|
579
641
|
}
|
|
@@ -584,6 +646,12 @@ export function createProjectMaintenanceHandler(
|
|
|
584
646
|
action: 'reschedule',
|
|
585
647
|
delayMs: result.attempted === 0 ? VECTOR_RETRY_DELAY_MS : 0,
|
|
586
648
|
resetAttempts: result.succeeded > 0,
|
|
649
|
+
progress: {
|
|
650
|
+
attempted: result.attempted,
|
|
651
|
+
succeeded: result.succeeded,
|
|
652
|
+
failed: result.failed,
|
|
653
|
+
remaining: after.missing,
|
|
654
|
+
},
|
|
587
655
|
...(result.succeeded > 0 ? {
|
|
588
656
|
...(priorFailureStreak > 0 ? { payload } : {}),
|
|
589
657
|
...(result.failed > 0 && result.lastError
|
package/src/sdk.ts
CHANGED
|
@@ -34,6 +34,7 @@ import {
|
|
|
34
34
|
canReadObservation,
|
|
35
35
|
filterReadableObservations,
|
|
36
36
|
} from './memory/visibility.js';
|
|
37
|
+
import { retrievalFacade } from './search/retrieval-facade.js';
|
|
37
38
|
|
|
38
39
|
// ── Re-exports for convenience ──────────────────────────────────────
|
|
39
40
|
|
|
@@ -236,12 +237,12 @@ export class MemoryClient {
|
|
|
236
237
|
limit: options.limit ?? 20,
|
|
237
238
|
type: options.type,
|
|
238
239
|
source: options.source,
|
|
239
|
-
status: options.status
|
|
240
|
+
status: options.status ?? 'active',
|
|
240
241
|
quality: options.quality,
|
|
241
242
|
reader: this._reader,
|
|
242
243
|
};
|
|
243
244
|
|
|
244
|
-
return
|
|
245
|
+
return (await retrievalFacade.search(searchOpts)).entries;
|
|
245
246
|
}
|
|
246
247
|
|
|
247
248
|
/**
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { DerivedView } from '../core/derived-view.js';
|
|
2
|
+
import { closeDatabase, getDatabase } from '../store/sqlite-db.js';
|
|
3
|
+
import {
|
|
4
|
+
isObservationLexicalIndexEnabled,
|
|
5
|
+
rebuildObservationLexicalIndex,
|
|
6
|
+
} from '../store/sqlite-fts.js';
|
|
7
|
+
|
|
8
|
+
export interface FtsRefreshInput {
|
|
9
|
+
rebuild?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** SQLite FTS5 derived view over canonical observations. */
|
|
13
|
+
export function createFtsDerivedView(dataDir: string): DerivedView<FtsRefreshInput> {
|
|
14
|
+
const rowCount = (db: any): number => Number(
|
|
15
|
+
db.prepare('SELECT COUNT(*) AS count FROM observations_fts').get()?.count ?? 0,
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
async status() {
|
|
20
|
+
try {
|
|
21
|
+
const db = getDatabase(dataDir);
|
|
22
|
+
if (!isObservationLexicalIndexEnabled(db)) {
|
|
23
|
+
return { state: 'unavailable', reason: 'SQLite FTS5 is unavailable' };
|
|
24
|
+
}
|
|
25
|
+
const observationCount = Number(db.prepare('SELECT COUNT(*) AS count FROM observations').get()?.count ?? 0);
|
|
26
|
+
const indexedCount = rowCount(db);
|
|
27
|
+
return indexedCount === observationCount
|
|
28
|
+
? { state: 'ready', rowCount: indexedCount }
|
|
29
|
+
: { state: 'stale', rowCount: indexedCount, reason: 'FTS row count differs from canonical observations' };
|
|
30
|
+
} catch (error) {
|
|
31
|
+
return { state: 'degraded', reason: error instanceof Error ? error.message : String(error) };
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
async refresh(input) {
|
|
35
|
+
if (!input.rebuild) return;
|
|
36
|
+
const db = getDatabase(dataDir);
|
|
37
|
+
if (!isObservationLexicalIndexEnabled(db) || !rebuildObservationLexicalIndex(db)) {
|
|
38
|
+
throw new Error('SQLite FTS5 rebuild is unavailable');
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
async rebuild() {
|
|
42
|
+
const db = getDatabase(dataDir);
|
|
43
|
+
return isObservationLexicalIndexEnabled(db) && rebuildObservationLexicalIndex(db);
|
|
44
|
+
},
|
|
45
|
+
async compact() {
|
|
46
|
+
// FTS5 external-content tables are compacted by rebuild; there is no
|
|
47
|
+
// separate vacuum-like operation that should run in a request handler.
|
|
48
|
+
},
|
|
49
|
+
async close() {
|
|
50
|
+
closeDatabase(dataDir);
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { DerivedView } from '../core/derived-view.js';
|
|
2
|
+
import type { Observation } from '../types.js';
|
|
3
|
+
import {
|
|
4
|
+
countIndexedDocuments,
|
|
5
|
+
getDb,
|
|
6
|
+
hydrateIndex,
|
|
7
|
+
resetDb,
|
|
8
|
+
} from '../store/orama-store.js';
|
|
9
|
+
import { reindexObservations, getSearchIndexStatus } from '../memory/observations.js';
|
|
10
|
+
|
|
11
|
+
/** In-process Orama view; SQLite remains the canonical source of records. */
|
|
12
|
+
export function createOramaDerivedView(): DerivedView<Observation[]> {
|
|
13
|
+
return {
|
|
14
|
+
async status() {
|
|
15
|
+
try {
|
|
16
|
+
const database = await getDb({ allowNetworkProbe: false });
|
|
17
|
+
const status = getSearchIndexStatus();
|
|
18
|
+
const rowCount = await countIndexedDocuments(database);
|
|
19
|
+
return status.prepared || rowCount > 0
|
|
20
|
+
? { state: 'ready', rowCount }
|
|
21
|
+
: { state: 'missing', rowCount };
|
|
22
|
+
} catch (error) {
|
|
23
|
+
return { state: 'degraded', reason: error instanceof Error ? error.message : String(error) };
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
async refresh(observations) {
|
|
27
|
+
await resetDb();
|
|
28
|
+
await hydrateIndex(observations, { allowNetworkProbe: false, skipCachedVectors: true });
|
|
29
|
+
},
|
|
30
|
+
async rebuild() {
|
|
31
|
+
return (await reindexObservations()) > 0;
|
|
32
|
+
},
|
|
33
|
+
async compact() {
|
|
34
|
+
// Orama has no durable compaction phase; rebuild is the explicit repair.
|
|
35
|
+
},
|
|
36
|
+
async close() {
|
|
37
|
+
await resetDb();
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createRetrievalFacade } from '../core/retrieval-facade.js';
|
|
2
|
+
import { applyRetrievalFeedback } from '../core/retrieval-policy.js';
|
|
3
|
+
import { MemoryFeedbackStore } from '../memory/feedback.js';
|
|
4
|
+
import { getObservationStoreDataDir } from '../store/obs-store.js';
|
|
5
|
+
import { searchObservationsDetailed } from '../store/orama-store.js';
|
|
6
|
+
|
|
7
|
+
export const retrievalFacade = createRetrievalFacade(async (plan) => {
|
|
8
|
+
const result = await searchObservationsDetailed(plan);
|
|
9
|
+
const execution = {
|
|
10
|
+
entries: result.entries,
|
|
11
|
+
receipt: result.diagnostics,
|
|
12
|
+
views: result.derivedViews,
|
|
13
|
+
};
|
|
14
|
+
let selected;
|
|
15
|
+
if (!plan.projectId || execution.entries.length === 0) {
|
|
16
|
+
selected = { ...execution, receipt: { ...execution.receipt, omittedByFeedback: 0 } };
|
|
17
|
+
} else {
|
|
18
|
+
try {
|
|
19
|
+
const feedback = new MemoryFeedbackStore();
|
|
20
|
+
await feedback.init(getObservationStoreDataDir());
|
|
21
|
+
selected = applyRetrievalFeedback(execution, feedback.weights(plan.projectId, 'observation'), plan.status);
|
|
22
|
+
} catch {
|
|
23
|
+
selected = {
|
|
24
|
+
...execution,
|
|
25
|
+
receipt: {
|
|
26
|
+
...execution.receipt,
|
|
27
|
+
omittedByFeedback: 0,
|
|
28
|
+
degradedReasons: [
|
|
29
|
+
...(execution.receipt.degradedReasons ?? []),
|
|
30
|
+
'feedback state unavailable',
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
result.recordAccess?.(selected.entries);
|
|
37
|
+
return selected;
|
|
38
|
+
});
|
|
@@ -474,6 +474,19 @@ export async function closeSemanticIndex(dataDir: string): Promise<void> {
|
|
|
474
474
|
}
|
|
475
475
|
}
|
|
476
476
|
|
|
477
|
+
/** Close one profile without invalidating other semantic tables in this directory. */
|
|
478
|
+
export async function closeSemanticIndexProfile(dataDir: string, profile: SemanticIndexProfile): Promise<void> {
|
|
479
|
+
const key = stateKey(dataDir, profile);
|
|
480
|
+
await flushPendingVectorBatch(key);
|
|
481
|
+
await writeQueues.get(key);
|
|
482
|
+
const state = await statePromises.get(key)?.catch(() => null);
|
|
483
|
+
try { state?.connection?.close?.(); } catch { /* best effort */ }
|
|
484
|
+
statePromises.delete(key);
|
|
485
|
+
writeQueues.delete(key);
|
|
486
|
+
indexBuilds.delete(key);
|
|
487
|
+
mutationCounts.delete(key);
|
|
488
|
+
}
|
|
489
|
+
|
|
477
490
|
/** @internal Reset state for isolated tests. */
|
|
478
491
|
export function resetSemanticIndexRuntime(): void {
|
|
479
492
|
for (const pending of pendingVectorBatches.values()) {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { DerivedView } from '../core/derived-view.js';
|
|
2
|
+
import {
|
|
3
|
+
closeSemanticIndexProfile,
|
|
4
|
+
ensureSemanticIndex,
|
|
5
|
+
getSemanticIndexStats,
|
|
6
|
+
isSemanticIndexAvailable,
|
|
7
|
+
optimizeSemanticIndex,
|
|
8
|
+
upsertSemanticVector,
|
|
9
|
+
upsertSemanticVectors,
|
|
10
|
+
type SemanticIndexProfile,
|
|
11
|
+
type SemanticVectorRecord,
|
|
12
|
+
} from './semantic-index.js';
|
|
13
|
+
|
|
14
|
+
export function createSemanticDerivedView(
|
|
15
|
+
dataDir: string,
|
|
16
|
+
profile: SemanticIndexProfile,
|
|
17
|
+
): DerivedView<SemanticVectorRecord[]> {
|
|
18
|
+
return {
|
|
19
|
+
async status() {
|
|
20
|
+
if (!(await isSemanticIndexAvailable())) return { state: 'unavailable' };
|
|
21
|
+
const stats = await getSemanticIndexStats(dataDir, profile);
|
|
22
|
+
return stats
|
|
23
|
+
? { state: 'ready', generation: stats.version, rowCount: stats.rowCount }
|
|
24
|
+
: { state: 'missing' };
|
|
25
|
+
},
|
|
26
|
+
async refresh(records) {
|
|
27
|
+
if (records.length === 1) await upsertSemanticVector(dataDir, profile, records[0]);
|
|
28
|
+
else await upsertSemanticVectors(dataDir, profile, records);
|
|
29
|
+
},
|
|
30
|
+
rebuild: () => ensureSemanticIndex(dataDir, profile),
|
|
31
|
+
compact: () => optimizeSemanticIndex(dataDir, profile),
|
|
32
|
+
close: () => closeSemanticIndexProfile(dataDir, profile),
|
|
33
|
+
};
|
|
34
|
+
}
|