minicode-ai 0.13.1 → 0.14.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/CHANGELOG.md +17 -0
- package/LICENSE +21 -21
- package/README.md +4 -0
- package/cli/args.ts +50 -2
- package/cli/commands/acp.ts +18 -3
- package/cli/commands/daemon.ts +114 -19
- package/cli/commands/exec.ts +95 -7
- package/cli/commands/mcp.ts +66 -5
- package/cli/commands/memory.ts +46 -14
- package/cli/commands.ts +5 -0
- package/cli/index.ts +30 -5
- package/cli/setup.ts +365 -24
- package/cli/tui.ts +72 -27
- package/docs/TERMINAL_CONTRACT.md +91 -24
- package/docs/agents.md +2 -2
- package/docs/cli.md +1 -0
- package/docs/environment.md +1 -0
- package/docs/memory-sessions.md +5 -0
- package/package.json +2 -1
- package/src/app/session.ts +153 -35
- package/src/app/tool-layer.ts +67 -61
- package/src/daemon/approvals.ts +25 -1
- package/src/daemon/client.ts +51 -1
- package/src/daemon/control.ts +8 -0
- package/src/daemon/protocol.ts +47 -1
- package/src/daemon/server.ts +45 -9
- package/src/daemon/subscription.ts +25 -1
- package/src/daemon/telemetry.ts +4 -0
- package/src/hooks/run.ts +5 -1
- package/src/mcp/client.ts +349 -90
- package/src/mcp/http-transport.ts +7 -0
- package/src/mcp/transport.ts +5 -0
- package/src/memory/files.ts +69 -3
- package/src/memory/vector.ts +556 -160
- package/src/policy/approval-binding.ts +199 -0
- package/src/policy/context.ts +20 -1
- package/src/policy/executor.ts +44 -5
- package/src/policy/permission.ts +243 -26
- package/src/policy/tool-contract.ts +77 -0
- package/src/policy/usage.ts +3 -1
- package/src/policy/work-mode.ts +49 -1
- package/src/presentation/adapter.ts +157 -7
- package/src/presentation/events.ts +173 -1
- package/src/presentation/model.ts +37 -0
- package/src/presentation/projection.ts +107 -18
- package/src/presentation/reducer.ts +134 -10
- package/src/sandbox/os.ts +18 -1
- package/src/session/persistence.ts +153 -11
- package/src/task/store.ts +43 -3
- package/src/tools/memory.ts +70 -10
- package/src/tools/task.ts +7 -1
- package/src/tools/todo.ts +14 -4
- package/src/ui/approval/prompt.ts +52 -7
- package/src/ui/assistant/simple.ts +160 -57
- package/src/ui/contract.ts +46 -0
- package/src/ui/footer.ts +14 -5
- package/src/ui/i18n/en.ts +9 -0
- package/src/ui/i18n/id.ts +9 -0
- package/src/ui/render/collapse.ts +24 -52
- package/src/ui/render/detail.ts +14 -9
- package/src/ui/render/errors.ts +8 -0
- package/src/ui/render/reasoning.ts +13 -8
- package/src/ui/session-state.ts +243 -0
- package/src/ui/tui/app.ts +7 -3
- package/src/ui/tui/transcript.ts +271 -30
- package/vendor/minicore/src/core/budget.ts +25 -25
- package/vendor/minicore/src/core/compact.ts +75 -75
- package/vendor/minicore/src/core/errors.ts +59 -59
- package/vendor/minicore/src/core/events.ts +73 -73
- package/vendor/minicore/src/core/executor.ts +139 -139
- package/vendor/minicore/src/core/history.ts +31 -31
- package/vendor/minicore/src/core/index.ts +41 -41
- package/vendor/minicore/src/core/loop.ts +355 -355
- package/vendor/minicore/src/core/permission.ts +22 -22
- package/vendor/minicore/src/core/provider.ts +39 -39
- package/vendor/minicore/src/core/recovery.ts +35 -35
- package/vendor/minicore/src/core/session.ts +327 -327
- package/vendor/minicore/src/core/snapshot.ts +146 -146
- package/vendor/minicore/src/core/tokens.ts +107 -107
- package/vendor/minicore/src/core/tool.ts +115 -115
- package/vendor/minicore/src/core/types.ts +50 -50
- package/vendor/minicore/src/providers/openai-compat.ts +417 -417
package/src/memory/vector.ts
CHANGED
|
@@ -421,7 +421,22 @@ export async function embedTexts(
|
|
|
421
421
|
budget,
|
|
422
422
|
timeouts?.attemptMs,
|
|
423
423
|
)
|
|
424
|
-
|
|
424
|
+
// [Area E / D12] Bentuk salah = fallback keyword (bukan vektor sampah).
|
|
425
|
+
// Provider/gateway bisa mengembalikan `{data: []}`/`embedding: null`;
|
|
426
|
+
// dulu itu lolos jadi baris `dim=0`/BLOB rusak yang ikut dibandingkan.
|
|
427
|
+
if (json?.data && Array.isArray(json.data) && json.data.length === texts.length) {
|
|
428
|
+
const vecs = json.data.map((d) => d?.embedding)
|
|
429
|
+
const valid =
|
|
430
|
+
vecs.length > 0 &&
|
|
431
|
+
vecs.every(
|
|
432
|
+
(v) =>
|
|
433
|
+
Array.isArray(v) &&
|
|
434
|
+
v.length > 0 &&
|
|
435
|
+
v.every((n) => typeof n === "number" && Number.isFinite(n)),
|
|
436
|
+
) &&
|
|
437
|
+
vecs.every((v) => v.length === vecs[0]!.length)
|
|
438
|
+
if (valid) return vecs
|
|
439
|
+
}
|
|
425
440
|
}
|
|
426
441
|
}
|
|
427
442
|
// Semua attempt gagal (timeout/404/405/501/koneksi ditolak) → tandai endpoint
|
|
@@ -473,19 +488,32 @@ export async function deleteMemoryByQuery(query: string, cwd?: string): Promise<
|
|
|
473
488
|
for (const p of paths) {
|
|
474
489
|
const db = openAt(p)
|
|
475
490
|
try {
|
|
476
|
-
//
|
|
477
|
-
//
|
|
491
|
+
// [Area E / D8] Hapus SELURUH entri (semua chunk berbagi `parent`), bukan
|
|
492
|
+
// hanya baris yang teksnya cocok. Menghapus chunk 0 saja meninggalkan
|
|
493
|
+
// chunk 1..N hidup dengan `parent` menggantung; menghapus chunk tengah
|
|
494
|
+
// meninggalkan fakta separuh. Query bisa mengenai chunk mana pun.
|
|
495
|
+
const matched = db
|
|
496
|
+
.prepare(
|
|
497
|
+
"SELECT DISTINCT COALESCE(parent, id) AS head FROM memory WHERE lower(text) LIKE '%' || ? || '%' ESCAPE '\\'",
|
|
498
|
+
)
|
|
499
|
+
.all(pattern) as { head: string }[]
|
|
500
|
+
if (matched.length === 0) continue
|
|
501
|
+
const placeholders = matched.map(() => "?").join(",")
|
|
502
|
+
const heads = matched.map((m) => m.head)
|
|
503
|
+
// Hitung dulu (trigger FTS ikut terhitung di changes DELETE).
|
|
478
504
|
const before = db
|
|
479
505
|
.prepare(
|
|
480
|
-
|
|
506
|
+
`SELECT count(*) as c FROM memory WHERE id IN (${placeholders}) OR parent IN (${placeholders})`,
|
|
481
507
|
)
|
|
482
|
-
.get(
|
|
508
|
+
.get(...heads, ...heads) as { c: number } | null
|
|
483
509
|
const n = before?.c ?? 0
|
|
484
510
|
if (n === 0) continue
|
|
485
511
|
await withBusyRetry(() =>
|
|
486
512
|
db
|
|
487
|
-
.prepare(
|
|
488
|
-
|
|
513
|
+
.prepare(
|
|
514
|
+
`DELETE FROM memory WHERE id IN (${placeholders}) OR parent IN (${placeholders})`,
|
|
515
|
+
)
|
|
516
|
+
.run(...heads, ...heads),
|
|
489
517
|
)
|
|
490
518
|
total += n
|
|
491
519
|
} finally {
|
|
@@ -580,48 +608,62 @@ export async function addMemory(
|
|
|
580
608
|
}
|
|
581
609
|
} catch {}
|
|
582
610
|
try {
|
|
583
|
-
// F8.1
|
|
584
|
-
//
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
.
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
embedding
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
611
|
+
// F8.1 + Live Hardening §7.5: validasi supersedesId + insert semua chunk +
|
|
612
|
+
// tombstone baris lama dalam SATU transaksi SQLite. Sebelumnya tiap INSERT dan
|
|
613
|
+
// tombstone berjalan sebagai statement terpisah lewat withBusyRetry, sehingga
|
|
614
|
+
// (a) crash di antaranya bisa menyisakan baris baru hidup tanpa baris lama
|
|
615
|
+
// tertombstone, dan (b) supersedesId tak dikenal hanya no-op diam-diam.
|
|
616
|
+
await withBusyRetry(() =>
|
|
617
|
+
db
|
|
618
|
+
.transaction(() => {
|
|
619
|
+
// §7.5 + P1.3: target harus ada, AKTIF, dan dinormalisasi ke kepala
|
|
620
|
+
// entri. Id chunk non-kepala dulu hanya menombstone chunk itu sendiri
|
|
621
|
+
// (kepala + sibling tetap hidup → split-brain dengan MEMORY.md), dan
|
|
622
|
+
// target yang sudah di-supersede dulu ditimpa diam-diam (dua penerus
|
|
623
|
+
// hidup). Keduanya kini throw → rollback penuh, nol tulisan parsial.
|
|
624
|
+
const oldHeadId = opts.supersedesId
|
|
625
|
+
? requireActiveSupersessionTarget(db, opts.supersedesId as string).headId
|
|
626
|
+
: null
|
|
627
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
628
|
+
const v = vecs?.[i]
|
|
629
|
+
const embedding = v ? toBlob(v) : null
|
|
630
|
+
const id = i === 0 ? parentId : randomUUID()
|
|
631
|
+
const category = (opts.category as string) ?? "fact"
|
|
632
|
+
const tagsJson = opts.tags ? JSON.stringify(opts.tags) : null
|
|
633
|
+
// MEM-01: insert provenance columns (nullable; legacy DBs get NULL default)
|
|
634
|
+
// P1.3: `supersedes` selalu id KEPALA kanonik, bukan id chunk mentah.
|
|
635
|
+
const supersedes = i === 0 ? oldHeadId : null
|
|
636
|
+
db.prepare(
|
|
637
|
+
"INSERT INTO memory (id, text, embedding, created_at, model, dim, parent, category, tags, access_count, session_id, turn_id, source_kind, source_ref, validation_status, supersedes, superseded_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?, ?, ?, NULL)",
|
|
638
|
+
).run(
|
|
639
|
+
id,
|
|
640
|
+
chunks[i]!,
|
|
641
|
+
embedding,
|
|
642
|
+
now,
|
|
643
|
+
embedding ? embedModel : null,
|
|
644
|
+
v?.length ?? null,
|
|
645
|
+
parentId,
|
|
646
|
+
category,
|
|
647
|
+
tagsJson,
|
|
648
|
+
opts.sessionId ?? null,
|
|
649
|
+
opts.turnId ?? null,
|
|
650
|
+
opts.sourceKind ?? null,
|
|
651
|
+
opts.sourceRef ?? null,
|
|
652
|
+
opts.validationStatus ?? "validated",
|
|
653
|
+
supersedes,
|
|
654
|
+
)
|
|
655
|
+
// Tombstone SEMUA chunk entri lama, bukan hanya chunk 0. Entri lama yang
|
|
656
|
+
// di-chunk membagi `parent = oldId`; `supersedesId` menunjuk chunk 0-nya.
|
|
657
|
+
// `WHERE id = ? OR parent = ?` menutup sibling chunk 1..N yang sebelumnya
|
|
658
|
+
// bocor sebagai memori stale (Risk A). `id` di sini = id baru (parentId),
|
|
659
|
+
// sehingga chunk entri BARU tidak ikut tertombstone.
|
|
660
|
+
if (i === 0 && oldHeadId) {
|
|
661
|
+
tombstoneEntry(db, oldHeadId, id)
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
})
|
|
665
|
+
.immediate(),
|
|
666
|
+
)
|
|
625
667
|
// [Fase 9 / 9.2] Fault-injection boundary ":after_vector_before_file":
|
|
626
668
|
// vector.db sudah ter-commit (loop insert di atas selesai), but "file write"
|
|
627
669
|
// (MEMORY.md) yang dalam model atomic two-store belum selesai. Injector yang
|
|
@@ -655,13 +697,7 @@ export async function addMemory(
|
|
|
655
697
|
const cnt =
|
|
656
698
|
(db.prepare("SELECT count(*) as c FROM memory").get() as { c: number } | null)?.c ?? 0
|
|
657
699
|
if (cnt > LIMITS.MEMORY_MAX_ROWS) {
|
|
658
|
-
await withBusyRetry(() =>
|
|
659
|
-
db
|
|
660
|
-
.prepare(
|
|
661
|
-
`DELETE FROM memory WHERE id IN (SELECT id FROM memory ORDER BY created_at DESC LIMIT -1 OFFSET ${LIMITS.MEMORY_MAX_ROWS})`,
|
|
662
|
-
)
|
|
663
|
-
.run(),
|
|
664
|
-
)
|
|
700
|
+
await withBusyRetry(() => pruneMemoryCap(db))
|
|
665
701
|
}
|
|
666
702
|
if (cnt % 1000 === 0) {
|
|
667
703
|
try {
|
|
@@ -694,46 +730,233 @@ export function topicKey(name: string): string {
|
|
|
694
730
|
return `key:${name}`
|
|
695
731
|
}
|
|
696
732
|
|
|
733
|
+
/**
|
|
734
|
+
* [Area E / D8] Id chunk kepala untuk satu baris memori. Semua chunk satu
|
|
735
|
+
* entri berbagi `parent = <chunk-0 id>`; baris legacy/reconcile bisa `NULL`
|
|
736
|
+
* sehingga id-nya sendiri yang menjadi kepala. Supersession/delete per-entri
|
|
737
|
+
* WAJIB memakai kepala ini, bukan id chunk yang kebetulan disentuh query.
|
|
738
|
+
*/
|
|
739
|
+
function memoryEntryHeadId(
|
|
740
|
+
db: Database,
|
|
741
|
+
id: string,
|
|
742
|
+
): { headId: string; headText: string | null } | null {
|
|
743
|
+
const row = db
|
|
744
|
+
.prepare(`SELECT COALESCE(parent, id) AS head FROM memory WHERE id = ?`)
|
|
745
|
+
.get(id) as {
|
|
746
|
+
head: string | null
|
|
747
|
+
} | null
|
|
748
|
+
if (!row?.head) return null
|
|
749
|
+
const head = db.prepare(`SELECT text FROM memory WHERE id = ?`).get(row.head) as {
|
|
750
|
+
text: string | null
|
|
751
|
+
} | null
|
|
752
|
+
if (head) return { headId: row.head, headText: head.text }
|
|
753
|
+
// Chunk kepala sudah hilang (delete lama yang menyisakan anak): ambil chunk
|
|
754
|
+
// tertua yang masih berbagi parent agar teks entry tidak hilang.
|
|
755
|
+
const sibling = db
|
|
756
|
+
.prepare(`SELECT text FROM memory WHERE parent = ? ORDER BY rowid ASC LIMIT 1`)
|
|
757
|
+
.get(row.head) as { text: string | null } | null
|
|
758
|
+
return { headId: row.head, headText: sibling?.text ?? null }
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
/**
|
|
762
|
+
* P1.3: kegagalan supersession yang SEMANTIK (target tak ada / sudah
|
|
763
|
+
* digantikan / diri sendiri / konflik konkuren) — dibedakan dari kegagalan
|
|
764
|
+
* infrastruktur (embedding/jaringan) supaya pemanggil (write_memory) tidak
|
|
765
|
+
* menelannya sebagai "vector index unavailable" lalu tetap menulis berkas.
|
|
766
|
+
*/
|
|
767
|
+
export class MemorySupersessionError extends Error {
|
|
768
|
+
constructor(
|
|
769
|
+
message: string,
|
|
770
|
+
readonly code: "NOT_FOUND" | "ALREADY_SUPERSEDED" | "SELF" | "CONFLICT",
|
|
771
|
+
) {
|
|
772
|
+
super(message)
|
|
773
|
+
this.name = "MemorySupersessionError"
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* P1.3: hasil validasi target supersession — `headId` kanonik bila aktif,
|
|
779
|
+
* atau alasan penolakan yang bisa ditindaklanjuti. Murni baca (tanpa tulis).
|
|
780
|
+
*/
|
|
781
|
+
export type SupersessionTargetCheck =
|
|
782
|
+
| { ok: true; headId: string; headText: string | null }
|
|
783
|
+
| { ok: false; code: MemorySupersessionError["code"]; supersededBy?: string; reason: string }
|
|
784
|
+
|
|
785
|
+
function checkSupersessionTarget(db: Database, id: string): SupersessionTargetCheck {
|
|
786
|
+
const head = memoryEntryHeadId(db, id)
|
|
787
|
+
if (!head) {
|
|
788
|
+
return { ok: false, code: "NOT_FOUND", reason: `memory id not found in this store: ${id}` }
|
|
789
|
+
}
|
|
790
|
+
// Seluruh baris entri (kepala + chunk) harus masih hidup. Satu chunk yang
|
|
791
|
+
// sudah bertombstone = entri sudah digantikan (atau korup sebagian) →
|
|
792
|
+
// fail-closed, jangan timpa `superseded_by` dan menciptakan dua penerus.
|
|
793
|
+
const dead = db
|
|
794
|
+
.prepare(
|
|
795
|
+
`SELECT superseded_by FROM memory WHERE (id = ? OR parent = ?) AND superseded_by IS NOT NULL AND superseded_by != '' LIMIT 1`,
|
|
796
|
+
)
|
|
797
|
+
.get(head.headId, head.headId) as { superseded_by: string } | null
|
|
798
|
+
if (dead) {
|
|
799
|
+
return {
|
|
800
|
+
ok: false,
|
|
801
|
+
code: "ALREADY_SUPERSEDED",
|
|
802
|
+
supersededBy: dead.superseded_by,
|
|
803
|
+
reason: `memory ${head.headId} is already superseded by ${dead.superseded_by}`,
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
return { ok: true, headId: head.headId, headText: head.headText }
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
function requireActiveSupersessionTarget(
|
|
810
|
+
db: Database,
|
|
811
|
+
id: string,
|
|
812
|
+
): { headId: string; headText: string | null } {
|
|
813
|
+
const r = checkSupersessionTarget(db, id)
|
|
814
|
+
if (!r.ok) {
|
|
815
|
+
// Pesan NOT_FOUND mempertahankan awalan lama (`supersedesId not found`)
|
|
816
|
+
// yang dikunci test regresi §7.5.
|
|
817
|
+
const msg = r.code === "NOT_FOUND" ? `supersedesId not found in memory store: ${id}` : r.reason
|
|
818
|
+
throw new MemorySupersessionError(msg, r.code)
|
|
819
|
+
}
|
|
820
|
+
return r
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
/**
|
|
824
|
+
* P1.3: tombstone SELURUH entri lama secara kondisional. `WHERE superseded_by
|
|
825
|
+
* IS NULL` + cek `changes` menutup lost-update konkuren: penulis kedua yang
|
|
826
|
+
* lolos validasi sebelum penulis pertama commit tetap ditolak di sini.
|
|
827
|
+
*/
|
|
828
|
+
function tombstoneEntry(db: Database, oldHeadId: string, newHeadId: string): void {
|
|
829
|
+
if (oldHeadId === newHeadId) {
|
|
830
|
+
throw new MemorySupersessionError(`memory ${oldHeadId} cannot supersede itself`, "SELF")
|
|
831
|
+
}
|
|
832
|
+
const res = db
|
|
833
|
+
.prepare(
|
|
834
|
+
`UPDATE memory SET superseded_by = ? WHERE (id = ? OR parent = ?) AND (superseded_by IS NULL OR superseded_by = '')`,
|
|
835
|
+
)
|
|
836
|
+
.run(newHeadId, oldHeadId, oldHeadId)
|
|
837
|
+
if (Number(res.changes) === 0) {
|
|
838
|
+
throw new MemorySupersessionError(
|
|
839
|
+
`memory ${oldHeadId} was superseded concurrently; re-read memory and retry`,
|
|
840
|
+
"CONFLICT",
|
|
841
|
+
)
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/** P1.3: validasi target supersession tanpa menulis (dipakai write_memory sebelum IO berkas). */
|
|
846
|
+
export async function checkMemorySupersessionTarget(
|
|
847
|
+
id: string,
|
|
848
|
+
cwd?: string,
|
|
849
|
+
): Promise<SupersessionTargetCheck> {
|
|
850
|
+
// Sama dengan memoryRowExists/addMemory: resolusi harus mendarat di DB lokal
|
|
851
|
+
// yang nanti benar-benar ditulis, bukan fallback global.
|
|
852
|
+
ensureLocalMemoryDir(cwd)
|
|
853
|
+
const db = open(cwd)
|
|
854
|
+
try {
|
|
855
|
+
return checkSupersessionTarget(db, id)
|
|
856
|
+
} finally {
|
|
857
|
+
db.close()
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
function ensureLocalMemoryDir(cwd?: string): void {
|
|
862
|
+
try {
|
|
863
|
+
const { mkdirSync } = require("node:fs") as typeof import("node:fs")
|
|
864
|
+
const { resolve: resolvePath, join: joinPath } =
|
|
865
|
+
require("node:path") as typeof import("node:path")
|
|
866
|
+
mkdirSync(joinPath(resolvePath(cwd ?? process.cwd()), ".minicode"), { recursive: true })
|
|
867
|
+
} catch {}
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* [Area E / D6] Teks entri kepala untuk id baris memori mana pun — dipakai
|
|
872
|
+
* `write_memory` agar baris lama di MEMORY.md ikut dihapus saat supersession.
|
|
873
|
+
* `null` = id tidak ada (fail-closed; pemanggil tak boleh menghapus apa pun).
|
|
874
|
+
*/
|
|
875
|
+
export async function memoryEntryText(id: string, cwd?: string): Promise<string | null> {
|
|
876
|
+
const db = open(cwd)
|
|
877
|
+
try {
|
|
878
|
+
return memoryEntryHeadId(db, id)?.headText ?? null
|
|
879
|
+
} finally {
|
|
880
|
+
db.close()
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
|
|
697
884
|
export async function supersedeMemory(oldId: string, newId: string, cwd?: string): Promise<void> {
|
|
698
885
|
const db = open(cwd)
|
|
886
|
+
let oldText: string | null = null
|
|
699
887
|
try {
|
|
700
888
|
await withBusyRetry(() => {
|
|
701
|
-
//
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
889
|
+
// P1.3: BEGIN IMMEDIATE mencegah race condition antara read dan write —
|
|
890
|
+
// tanpa ini dua supersession konkuren bisa lolos validasi bersamaan.
|
|
891
|
+
db.transaction(() => {
|
|
892
|
+
const oldRow = requireActiveSupersessionTarget(db, oldId)
|
|
893
|
+
oldText = oldRow.headText
|
|
894
|
+
const newRow = db.prepare(`SELECT id FROM memory WHERE id = ?`).get(newId) as {
|
|
895
|
+
id: string
|
|
896
|
+
} | null
|
|
897
|
+
if (!newRow) {
|
|
898
|
+
throw new Error(`supersedeMemory: new memory id not found: ${newId}`)
|
|
899
|
+
}
|
|
900
|
+
// Tombstone SELURUH entri lama (semua chunk berbagi kepala) —
|
|
901
|
+
// conditional update memastikan row belum di-supersede oleh proses lain.
|
|
902
|
+
tombstoneEntry(db, oldRow.headId, newId)
|
|
903
|
+
// Tautkan baris baru → supersedes = headId kanonik (bukan raw oldId
|
|
904
|
+
// yang bisa berupa chunk non-head).
|
|
905
|
+
db.prepare(`UPDATE memory SET supersedes = ? WHERE id = ?`).run(oldRow.headId, newId)
|
|
906
|
+
}).immediate()
|
|
718
907
|
})
|
|
719
908
|
// Perbarui MEMORY.md (best-effort — jangan gagalkan supersession)
|
|
720
909
|
try {
|
|
721
|
-
await syncMemoryFileToSupersession(cwd, oldId, newId)
|
|
910
|
+
await syncMemoryFileToSupersession(cwd, oldId, newId, oldText)
|
|
722
911
|
} catch {}
|
|
723
912
|
} finally {
|
|
724
913
|
db.close()
|
|
725
914
|
}
|
|
726
915
|
}
|
|
727
916
|
|
|
917
|
+
/**
|
|
918
|
+
* [Live Hardening 0.13.x §7.5] Cek apakah id row memory ada di store TULIS lokal
|
|
919
|
+
* (target yang sama dengan `addMemory`). Dipakai `write_memory` untuk memvalidasi
|
|
920
|
+
* `supersedes_id` SEBELUM file/vendor tersentuh, sehingga id tak dikenal memberi
|
|
921
|
+
* error actionable (bukan tombstone no-op diam-diam).
|
|
922
|
+
*
|
|
923
|
+
* Punya efek meratakan `.minicode` lokal (sama seperti `addMemory`) agar resolusi
|
|
924
|
+
* `open(cwd)` mendarat di DB lokal yang sama yang akan di-tombstone.
|
|
925
|
+
*/
|
|
926
|
+
export async function memoryRowExists(id: string, cwd?: string): Promise<boolean> {
|
|
927
|
+
// Mirror addMemory: pastikan .minicode ada agar open() resolve ke DB lokal.
|
|
928
|
+
try {
|
|
929
|
+
const { mkdirSync } = require("node:fs") as typeof import("node:fs")
|
|
930
|
+
const { resolve: resolvePath, join: joinPath } =
|
|
931
|
+
require("node:path") as typeof import("node:path")
|
|
932
|
+
mkdirSync(joinPath(resolvePath(cwd ?? process.cwd()), ".minicode"), { recursive: true })
|
|
933
|
+
} catch {}
|
|
934
|
+
const db = open(cwd)
|
|
935
|
+
try {
|
|
936
|
+
return !!db.prepare(`SELECT id FROM memory WHERE id = ?`).get(id)
|
|
937
|
+
} finally {
|
|
938
|
+
db.close()
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
|
|
728
942
|
async function syncMemoryFileToSupersession(
|
|
729
943
|
cwd: string | undefined,
|
|
730
944
|
oldId: string,
|
|
731
945
|
newId: string,
|
|
946
|
+
oldText: string | null,
|
|
732
947
|
): Promise<void> {
|
|
948
|
+
const files = await import("../memory/files.ts")
|
|
949
|
+
// [Area E / D6] Buang fakta lama dari MEMORY.md. Sebelumnya hanya catatan
|
|
950
|
+
// supersession yang di-append, sehingga loadMemoryFiles (jalur retrieval
|
|
951
|
+
// file) tetap menyajikan fakta yang sudah digantikan.
|
|
952
|
+
if (oldText) {
|
|
953
|
+
try {
|
|
954
|
+
await files.deleteMemoryEntry(oldText, cwd)
|
|
955
|
+
} catch {}
|
|
956
|
+
}
|
|
733
957
|
// Append catatan supersession ke MEMORY.md (best-effort; appendMemory hanya
|
|
734
|
-
// menerima (text, cwd?)
|
|
735
|
-
|
|
736
|
-
await appendMemory(`[supersession] fact ${oldId} → ${newId} (superseded)`, cwd)
|
|
958
|
+
// menerima (text, cwd?) - tanpa object opts).
|
|
959
|
+
await files.appendMemory(`[supersession] fact ${oldId} → ${newId} (superseded)`, cwd)
|
|
737
960
|
}
|
|
738
961
|
|
|
739
962
|
export async function supersedeMemoryByIds(
|
|
@@ -746,11 +969,16 @@ export async function supersedeMemoryByIds(
|
|
|
746
969
|
try {
|
|
747
970
|
await withBusyRetry(() => {
|
|
748
971
|
for (const oldId of oldIds) {
|
|
749
|
-
const oldRow = db
|
|
750
|
-
id: string
|
|
751
|
-
} | null
|
|
972
|
+
const oldRow = memoryEntryHeadId(db, oldId)
|
|
752
973
|
if (oldRow) {
|
|
753
|
-
|
|
974
|
+
// Tombstone SELURUH entri lama (semua chunk berbagi kepala), bukan
|
|
975
|
+
// hanya id chunk yang diberikan (D8: entri bisa diberikan lewat
|
|
976
|
+
// chunk mana pun).
|
|
977
|
+
db.prepare(`UPDATE memory SET superseded_by = ? WHERE id = ? OR parent = ?`).run(
|
|
978
|
+
newId,
|
|
979
|
+
oldRow.headId,
|
|
980
|
+
oldRow.headId,
|
|
981
|
+
)
|
|
754
982
|
n++
|
|
755
983
|
}
|
|
756
984
|
}
|
|
@@ -831,6 +1059,7 @@ export async function promoteOrRejectLessonsBySession(
|
|
|
831
1059
|
}
|
|
832
1060
|
|
|
833
1061
|
type MemoryRow = {
|
|
1062
|
+
id: string
|
|
834
1063
|
text: string
|
|
835
1064
|
embedding: Buffer | null
|
|
836
1065
|
dim: number | null
|
|
@@ -851,6 +1080,8 @@ type MemoryRow = {
|
|
|
851
1080
|
}
|
|
852
1081
|
|
|
853
1082
|
export interface MemoryHit {
|
|
1083
|
+
/** Id baris memory — diekspos agar `write_memory.supersedes_id` bisa menyasar id nyata (§7.5). */
|
|
1084
|
+
id?: string
|
|
854
1085
|
text: string
|
|
855
1086
|
score: number
|
|
856
1087
|
createdAt: number
|
|
@@ -968,7 +1199,7 @@ export async function searchHybrid(
|
|
|
968
1199
|
// yang menampilkannya kembali (untuk forensik/audit).
|
|
969
1200
|
const notSuperseded =
|
|
970
1201
|
opts.includeSuperseded === true ? `1=1` : `(superseded_by IS NULL OR superseded_by = '')`
|
|
971
|
-
const cols = `text, embedding, dim, model, created_at, category, tags, session_id, turn_id, source_kind, source_ref, validation_status, supersedes, superseded_by`
|
|
1202
|
+
const cols = `id, text, embedding, dim, model, created_at, category, tags, session_id, turn_id, source_kind, source_ref, validation_status, supersedes, superseded_by`
|
|
972
1203
|
if (keywords.length > 0) {
|
|
973
1204
|
const recentRows = db
|
|
974
1205
|
.prepare(
|
|
@@ -981,7 +1212,7 @@ export async function searchHybrid(
|
|
|
981
1212
|
try {
|
|
982
1213
|
keywordRows = db
|
|
983
1214
|
.prepare(
|
|
984
|
-
`SELECT m.text as text, m.embedding as embedding, m.dim as dim, m.model as model, m.created_at as created_at, m.category as category, m.tags as tags, m.session_id as session_id, m.turn_id as turn_id, m.source_kind as source_kind, m.source_ref as source_ref, m.validation_status as validation_status, m.supersedes as supersedes, m.superseded_by as superseded_by FROM memory_fts f JOIN memory m ON m.rowid = f.rowid WHERE ${notSuperseded} AND memory_fts MATCH ? ORDER BY rank LIMIT ${LIMITS.VECTOR_KEYWORD_LIMIT}`,
|
|
1215
|
+
`SELECT m.id as id, m.text as text, m.embedding as embedding, m.dim as dim, m.model as model, m.created_at as created_at, m.category as category, m.tags as tags, m.session_id as session_id, m.turn_id as turn_id, m.source_kind as source_kind, m.source_ref as source_ref, m.validation_status as validation_status, m.supersedes as supersedes, m.superseded_by as superseded_by FROM memory_fts f JOIN memory m ON m.rowid = f.rowid WHERE ${notSuperseded} AND memory_fts MATCH ? ORDER BY rank LIMIT ${LIMITS.VECTOR_KEYWORD_LIMIT}`,
|
|
985
1216
|
)
|
|
986
1217
|
.all(ftsQ) as MemoryRow[]
|
|
987
1218
|
} catch {
|
|
@@ -1108,6 +1339,7 @@ export async function searchHybrid(
|
|
|
1108
1339
|
const row = rowsByText.get(h.text)
|
|
1109
1340
|
return {
|
|
1110
1341
|
...h,
|
|
1342
|
+
id: row?.id,
|
|
1111
1343
|
createdAt: createdByText.get(h.text) ?? Date.now(),
|
|
1112
1344
|
provenance: row
|
|
1113
1345
|
? {
|
|
@@ -1142,6 +1374,36 @@ export async function searchHybrid(
|
|
|
1142
1374
|
return hits
|
|
1143
1375
|
}
|
|
1144
1376
|
|
|
1377
|
+
// [Area E / D9] Prune cap per ENTRI, bukan per baris: memotong offset baris
|
|
1378
|
+
// bisa membuang chunk 0 sambil menyisakan chunk 1..N (`parent` menggantung).
|
|
1379
|
+
// Kelompok = `COALESCE(parent, id)`; kelompok terbaru selalu dipertahankan
|
|
1380
|
+
// walau satu entri sendiri melebihi cap.
|
|
1381
|
+
function pruneMemoryCap(db: Database): number {
|
|
1382
|
+
const groups = db
|
|
1383
|
+
.prepare(
|
|
1384
|
+
"SELECT COALESCE(parent, id) AS head, count(*) AS c, max(created_at) AS newest FROM memory GROUP BY COALESCE(parent, id) ORDER BY newest DESC, head DESC",
|
|
1385
|
+
)
|
|
1386
|
+
.all() as { head: string; c: number; newest: number }[]
|
|
1387
|
+
let keptRows = 0
|
|
1388
|
+
const remove: string[] = []
|
|
1389
|
+
for (const group of groups) {
|
|
1390
|
+
if (keptRows === 0 || keptRows + group.c <= LIMITS.MEMORY_MAX_ROWS) {
|
|
1391
|
+
keptRows += group.c
|
|
1392
|
+
continue
|
|
1393
|
+
}
|
|
1394
|
+
remove.push(group.head)
|
|
1395
|
+
}
|
|
1396
|
+
let deleted = 0
|
|
1397
|
+
for (const head of remove) {
|
|
1398
|
+
const before = db
|
|
1399
|
+
.prepare("SELECT count(*) as c FROM memory WHERE id = ? OR parent = ?")
|
|
1400
|
+
.get(head, head) as { c: number } | null
|
|
1401
|
+
db.prepare("DELETE FROM memory WHERE id = ? OR parent = ?").run(head, head)
|
|
1402
|
+
deleted += before?.c ?? 0
|
|
1403
|
+
}
|
|
1404
|
+
return deleted
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1145
1407
|
// MEM-03 / Fase 5A: Prune TTL dengan virtual/injectable clock untuk deterministik testing.
|
|
1146
1408
|
// Returns jumlah baris yang terhapus. `nowMs` default Date.now() bila tidak diinjeksi.
|
|
1147
1409
|
export async function pruneTTL(cwd?: string, nowMs: number = Date.now()): Promise<number> {
|
|
@@ -1166,18 +1428,11 @@ export async function pruneTTL(cwd?: string, nowMs: number = Date.now()): Promis
|
|
|
1166
1428
|
)
|
|
1167
1429
|
deleted += result.changes ?? 0
|
|
1168
1430
|
}
|
|
1169
|
-
// CAP: potong ke MEMORY_MAX_rows bila melebihi batas
|
|
1431
|
+
// CAP: potong ke MEMORY_MAX_rows bila melebihi batas (per entri utuh)
|
|
1170
1432
|
const cnt =
|
|
1171
1433
|
(db.prepare("SELECT count(*) as c FROM memory").get() as { c: number } | null)?.c ?? 0
|
|
1172
1434
|
if (cnt > LIMITS.MEMORY_MAX_ROWS) {
|
|
1173
|
-
|
|
1174
|
-
db
|
|
1175
|
-
.prepare(
|
|
1176
|
-
`DELETE FROM memory WHERE id IN (SELECT id FROM memory ORDER BY created_at DESC LIMIT -1 OFFSET ${LIMITS.MEMORY_MAX_ROWS})`,
|
|
1177
|
-
)
|
|
1178
|
-
.run(),
|
|
1179
|
-
)
|
|
1180
|
-
deleted += result.changes ?? 0
|
|
1435
|
+
deleted += await withBusyRetry(() => pruneMemoryCap(db))
|
|
1181
1436
|
}
|
|
1182
1437
|
return deleted
|
|
1183
1438
|
} finally {
|
|
@@ -1218,12 +1473,15 @@ export async function revokeByProvenance(
|
|
|
1218
1473
|
.get(...(values as [string | null])) as { c: number } | null
|
|
1219
1474
|
const n = before?.c ?? 0
|
|
1220
1475
|
if (n > 0) {
|
|
1221
|
-
|
|
1476
|
+
await withBusyRetry(() =>
|
|
1222
1477
|
db
|
|
1223
1478
|
.prepare(`DELETE FROM memory WHERE ${conditions.join(" AND ")}`)
|
|
1224
1479
|
.run(...(values as [string | null])),
|
|
1225
1480
|
)
|
|
1226
|
-
|
|
1481
|
+
// `changes` DELETE ikut terhitung tulis trigger FTS (bisa jauh lebih
|
|
1482
|
+
// besar dari baris memory yang benar-benar cocok) — jumlah baris yang
|
|
1483
|
+
// dihapus berasal dari penghitungannya SEBELUM delete.
|
|
1484
|
+
vectorDeleted = n
|
|
1227
1485
|
}
|
|
1228
1486
|
} finally {
|
|
1229
1487
|
db.close()
|
|
@@ -1252,39 +1510,38 @@ export async function revokeByProvenance(
|
|
|
1252
1510
|
let del = 0
|
|
1253
1511
|
|
|
1254
1512
|
for (const line of lines) {
|
|
1255
|
-
//
|
|
1256
|
-
//
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1513
|
+
// [Area E / D2] Kriteria provenance AND (sama dengan query vektor). Versi
|
|
1514
|
+
// lama memakai OR gabungan `!criterion`: revoke dengan satu kriteria
|
|
1515
|
+
// menghapus SEMUA baris tanpa tag, termasuk fakta sesi lain. Baris tanpa
|
|
1516
|
+
// tag tak membuktikan provenance → dipertahankan (fail-closed). Regex
|
|
1517
|
+
// greedy sampai `]` akhir baris karena tag berbentuk array JSON.
|
|
1518
|
+
const tagMatch = line.match(/\[tags:\s*(.+)\]\s*$/)
|
|
1260
1519
|
if (!tagMatch) {
|
|
1261
|
-
|
|
1520
|
+
kept.push(line)
|
|
1521
|
+
continue
|
|
1522
|
+
}
|
|
1523
|
+
if (!sessionId && !turnId && !sourceKind) {
|
|
1524
|
+
kept.push(line)
|
|
1525
|
+
continue
|
|
1526
|
+
}
|
|
1527
|
+
let matched = false
|
|
1528
|
+
try {
|
|
1529
|
+
const tags = JSON.parse(tagMatch![1] as string) as string[]
|
|
1530
|
+
const hasSessionId = sessionId
|
|
1531
|
+
? tags.some((t: string) => t.includes(`session:${sessionId as string}`))
|
|
1532
|
+
: false
|
|
1533
|
+
const hasTurnId = turnId
|
|
1534
|
+
? tags.some((t: string) => t.includes(`turn:${turnId as string}`))
|
|
1535
|
+
: false
|
|
1536
|
+
const hasSource = sourceKind
|
|
1537
|
+
? tags.some((t: string) => t.includes(`source:${sourceKind as string}`))
|
|
1538
|
+
: false
|
|
1262
1539
|
matched =
|
|
1263
|
-
!sessionId ||
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
line.toLowerCase().includes((sourceKind ?? "").toLowerCase())
|
|
1269
|
-
} else {
|
|
1270
|
-
// Parse JSON tags dan cek match
|
|
1271
|
-
try {
|
|
1272
|
-
const tags = JSON.parse(tagMatch![1] as string) as string[]
|
|
1273
|
-
const hasSessionId = sessionId
|
|
1274
|
-
? tags.some((t: string) => t.includes(`session:${sessionId as string}`))
|
|
1275
|
-
: false
|
|
1276
|
-
const hasTurnId = turnId
|
|
1277
|
-
? tags.some((t: string) => t.includes(`turn:${turnId as string}`))
|
|
1278
|
-
: false
|
|
1279
|
-
const hasSource = sourceKind
|
|
1280
|
-
? tags.some((t: string) => t.includes(`source:${sourceKind as string}`))
|
|
1281
|
-
: false
|
|
1282
|
-
matched = hasSessionId || hasTurnId || hasSource
|
|
1283
|
-
} catch {
|
|
1284
|
-
// Tag corrupted / bukan JSON → skip
|
|
1285
|
-
kept.push(line)
|
|
1286
|
-
continue
|
|
1287
|
-
}
|
|
1540
|
+
(!sessionId || hasSessionId) && (!turnId || hasTurnId) && (!sourceKind || hasSource)
|
|
1541
|
+
} catch {
|
|
1542
|
+
// Tag corrupted / bukan JSON → skip
|
|
1543
|
+
kept.push(line)
|
|
1544
|
+
continue
|
|
1288
1545
|
}
|
|
1289
1546
|
|
|
1290
1547
|
if (matched) {
|
|
@@ -1329,21 +1586,102 @@ export function computeRecallAtK(retrievedIds: string[], relevantIds: string[],
|
|
|
1329
1586
|
return relevantIds.length > 0 ? correct / relevantIds.length : 0
|
|
1330
1587
|
}
|
|
1331
1588
|
|
|
1589
|
+
/**
|
|
1590
|
+
* [Area E / D3] Gabungkan chunk ber-overlap jadi teks entri utuh.
|
|
1591
|
+
*
|
|
1592
|
+
* `splitMemoryChunks` memotong [0,2000), [1800,3800), … sehingga chunk
|
|
1593
|
+
* berikut berbagi prefix dengan sufiks sebelumnya. Urutan baris DB tak
|
|
1594
|
+
* dijamin → sambung berulang sampai tak ada pasangan cocok; chunk yang tak
|
|
1595
|
+
* bisa disambung tetap disertakan (dipisah newline) agar data tak hilang.
|
|
1596
|
+
*/
|
|
1597
|
+
function mergeChunkTexts(parts: readonly string[]): string {
|
|
1598
|
+
const pool = parts.filter((p) => p.length > 0)
|
|
1599
|
+
if (pool.length === 0) return ""
|
|
1600
|
+
let merged = pool.shift() as string
|
|
1601
|
+
let progress = true
|
|
1602
|
+
while (pool.length > 0 && progress) {
|
|
1603
|
+
progress = false
|
|
1604
|
+
for (let i = 0; i < pool.length; i++) {
|
|
1605
|
+
const next = pool[i]!
|
|
1606
|
+
const max = Math.min(merged.length, next.length)
|
|
1607
|
+
let overlap = 0
|
|
1608
|
+
for (let k = max; k > 0; k--) {
|
|
1609
|
+
if (merged.slice(-k) === next.slice(0, k)) {
|
|
1610
|
+
overlap = k
|
|
1611
|
+
break
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
if (overlap > 0) {
|
|
1615
|
+
merged += next.slice(overlap)
|
|
1616
|
+
pool.splice(i, 1)
|
|
1617
|
+
progress = true
|
|
1618
|
+
break
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
if (pool.length > 0) merged = [merged, ...pool].join("\n")
|
|
1623
|
+
return merged
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1332
1626
|
// MEM-04 / Fase 5A: Rekonsiliasi split-brain antara vektor DB & file store.
|
|
1333
|
-
// Menyelaraskan ketidakcocokan setelah crash:
|
|
1334
|
-
// - Vector-only
|
|
1335
|
-
// - File-only
|
|
1336
|
-
// Returns { reconciled
|
|
1627
|
+
// Menyelaraskan ketidakcocokan setelah crash TANPA menghapus data:
|
|
1628
|
+
// - Vector-only entry → ditulis ke MEMORY.md (adopt), bukan dihapus
|
|
1629
|
+
// - File-only entry → ditulis ke vektor (adopt)
|
|
1630
|
+
// Returns { reconciled, vectorOnlyAdopted, fileOnlyAdded }
|
|
1337
1631
|
export async function reconcileMemoryStores(cwd?: string): Promise<{
|
|
1338
1632
|
reconciled: number
|
|
1339
|
-
|
|
1633
|
+
vectorOnlyAdopted: number
|
|
1340
1634
|
fileOnlyAdded: number
|
|
1635
|
+
/** P1.3: baris file lama yang dihapus karena entrinya sudah superseded. */
|
|
1636
|
+
supersededFileRemoved: number
|
|
1637
|
+
/** P1.3: baris vektor superseded yang sengaja TIDAK diadopsi kembali. */
|
|
1638
|
+
supersededSkipped: number
|
|
1639
|
+
/** P2.3: baris file legacy yang hanya PARTIAL-match superseded — di-quarantine. */
|
|
1640
|
+
legacySuspectLines: number
|
|
1641
|
+
legacySuspectSamples: string[]
|
|
1341
1642
|
}> {
|
|
1342
1643
|
const db = open(cwd)
|
|
1343
1644
|
try {
|
|
1344
|
-
//
|
|
1345
|
-
|
|
1645
|
+
// P1.3: reconcile hanya boleh menghidupkan fakta AKTIF. Tanpa filter ini,
|
|
1646
|
+
// baris superseded (yang sudah dihapus dari MEMORY.md) dianggap
|
|
1647
|
+
// "vector-only" lalu ditulis ulang ke file — resurrect ke retrieval +
|
|
1648
|
+
// system prompt. Kelompokkan per ENTRI (chunk berbagi `parent`) hanya dari
|
|
1649
|
+
// baris live; teks superseded dipakai untuk membersihkan file, bukan adopsi.
|
|
1650
|
+
const allRows = db.prepare("SELECT id, text, parent, superseded_by FROM memory").all() as {
|
|
1651
|
+
id: string
|
|
1652
|
+
text: string
|
|
1653
|
+
parent: string | null
|
|
1654
|
+
superseded_by: string | null
|
|
1655
|
+
}[]
|
|
1656
|
+
const isLive = (r: { superseded_by: string | null }): boolean =>
|
|
1657
|
+
r.superseded_by === null || r.superseded_by === ""
|
|
1658
|
+
const vectorRows = allRows.filter(isLive)
|
|
1659
|
+
const supersededRows = allRows.filter((r) => !isLive(r))
|
|
1660
|
+
const groups = new Map<string, string[]>()
|
|
1661
|
+
for (const row of vectorRows) {
|
|
1662
|
+
const key = row.parent ?? row.id
|
|
1663
|
+
const list = groups.get(key) ?? []
|
|
1664
|
+
list.push(row.text)
|
|
1665
|
+
groups.set(key, list)
|
|
1666
|
+
}
|
|
1346
1667
|
const vectorTexts = new Set(vectorRows.map((r) => r.text))
|
|
1668
|
+
// Entri superseded dikelompokkan per parent lalu digabung: file menyimpan
|
|
1669
|
+
// ENTRI utuh, sedangkan DB menyimpan chunk. Pencocokan harus exact agar
|
|
1670
|
+
// baris fakta BARU yang kebetulan memuat teks lama sebagai substring tidak
|
|
1671
|
+
// ikut terhapus.
|
|
1672
|
+
const supersededGroups = new Map<string, string[]>()
|
|
1673
|
+
for (const row of supersededRows) {
|
|
1674
|
+
const key = row.parent ?? row.id
|
|
1675
|
+
const list = supersededGroups.get(key) ?? []
|
|
1676
|
+
list.push(row.text)
|
|
1677
|
+
supersededGroups.set(key, list)
|
|
1678
|
+
}
|
|
1679
|
+
const supersededEntries = new Set<string>()
|
|
1680
|
+
for (const parts of supersededGroups.values()) {
|
|
1681
|
+
const merged = mergeChunkTexts(parts)
|
|
1682
|
+
if (merged) supersededEntries.add(merged)
|
|
1683
|
+
for (const p of parts) if (p) supersededEntries.add(p)
|
|
1684
|
+
}
|
|
1347
1685
|
|
|
1348
1686
|
// Baca MEMORY.md dan extract unique texts
|
|
1349
1687
|
const { readFile } = await import("node:fs/promises")
|
|
@@ -1358,7 +1696,7 @@ export async function reconcileMemoryStores(cwd?: string): Promise<{
|
|
|
1358
1696
|
// [Fase 9 / 9.2] File MEMORY.md ABSENT bukan berarti "clean slate" —
|
|
1359
1697
|
// vector.db yang sudah punya baris (crash "after_vector_before_file")
|
|
1360
1698
|
// berarti split-brain vector-only. Perlakukan file kosong (bukan early
|
|
1361
|
-
// return) agar
|
|
1699
|
+
// return) agar entri vector-only tetap terdeteksi & diadopsi.
|
|
1362
1700
|
txt = ""
|
|
1363
1701
|
}
|
|
1364
1702
|
|
|
@@ -1388,42 +1726,87 @@ export async function reconcileMemoryStores(cwd?: string): Promise<{
|
|
|
1388
1726
|
}
|
|
1389
1727
|
}
|
|
1390
1728
|
|
|
1391
|
-
let
|
|
1729
|
+
let vectorOnlyAdopted = 0
|
|
1392
1730
|
let fileOnlyAdded = 0
|
|
1393
1731
|
let reconciled = 0
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
const
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1732
|
+
let supersededFileRemoved = 0
|
|
1733
|
+
let legacySuspectLines = 0
|
|
1734
|
+
const legacySuspectSamples: string[] = []
|
|
1735
|
+
const fileHas = (text: string): boolean =>
|
|
1736
|
+
Array.from(fileTexts).some((ft) => ft.toLowerCase().includes(text.toLowerCase()))
|
|
1737
|
+
|
|
1738
|
+
// 1. Adopsi entri vector-only ke MEMORY.md — JANGAN hapus. Menghapus baris
|
|
1739
|
+
// vektor karena file tidak memuatnya membuang fakta sah saat crash
|
|
1740
|
+
// "after_vector_before_file" (justru arah yang harus dipulihkan).
|
|
1741
|
+
const { appendMemory, deleteMemoryEntry } = await import("../memory/files.ts")
|
|
1742
|
+
for (const parts of groups.values()) {
|
|
1743
|
+
const present = parts.filter((text) => fileHas(text))
|
|
1744
|
+
if (present.length === parts.length) {
|
|
1745
|
+
reconciled += parts.length
|
|
1746
|
+
continue
|
|
1747
|
+
}
|
|
1748
|
+
// Baris file yang hanya memuat sebagian entri (legacy truncation 1000
|
|
1749
|
+
// karakter) dibuang dulu agar tidak ada dua representasi entri yang sama.
|
|
1750
|
+
for (const text of present) {
|
|
1751
|
+
try {
|
|
1752
|
+
await deleteMemoryEntry(text, cwd)
|
|
1753
|
+
} catch {}
|
|
1408
1754
|
}
|
|
1755
|
+
const full = mergeChunkTexts(parts)
|
|
1756
|
+
if (!full) continue
|
|
1757
|
+
await appendMemory(full, cwd)
|
|
1758
|
+
vectorOnlyAdopted++
|
|
1759
|
+
reconciled += parts.length
|
|
1409
1760
|
}
|
|
1410
1761
|
|
|
1411
|
-
// 2. Tambah file-only rows yang belum ada di vector
|
|
1762
|
+
// 2. Tambah file-only rows yang belum ada di vector — KECUALI yang cocok
|
|
1763
|
+
// dengan entri superseded: itu sisa file lama yang harus dibersihkan,
|
|
1764
|
+
// bukan diindeks ulang (P1.3 no-resurrection).
|
|
1412
1765
|
for (const text of fileTexts) {
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1766
|
+
const representedByLive = Array.from(vectorTexts).some(
|
|
1767
|
+
(vt) => vt.length > 0 && (vt === text || text.includes(vt) || vt.includes(text)),
|
|
1768
|
+
)
|
|
1769
|
+
if (representedByLive) continue
|
|
1770
|
+
const representedBySuperseded = supersededEntries.has(text)
|
|
1771
|
+
if (representedBySuperseded) {
|
|
1772
|
+
// Exact match: aman dibersihkan (provenance supersession terdukung).
|
|
1773
|
+
try {
|
|
1774
|
+
await deleteMemoryEntry(text, cwd)
|
|
1775
|
+
supersededFileRemoved++
|
|
1776
|
+
} catch {}
|
|
1777
|
+
continue
|
|
1778
|
+
}
|
|
1779
|
+
// P2.3: PARTIAL match (mis. baris legacy ter-truncate) TIDAK dihapus dan
|
|
1780
|
+
// TIDAK diindeks ulang — keduanya berisiko (hapus fakta sah / resurrect
|
|
1781
|
+
// fakta superseded). Quarantine: biarkan di file, laporkan ke operator.
|
|
1782
|
+
const partialSuperseded = Array.from(supersededEntries).some(
|
|
1783
|
+
(e) => e.length > 0 && (e.includes(text) || text.includes(e)),
|
|
1784
|
+
)
|
|
1785
|
+
if (partialSuperseded) {
|
|
1786
|
+
legacySuspectLines++
|
|
1787
|
+
if (legacySuspectSamples.length < 5) legacySuspectSamples.push(text.slice(0, 160))
|
|
1788
|
+
continue
|
|
1423
1789
|
}
|
|
1790
|
+
// Insert into vector (without embedding, keyword-only fallback)
|
|
1791
|
+
await withBusyRetry(() =>
|
|
1792
|
+
db
|
|
1793
|
+
.prepare(
|
|
1794
|
+
"INSERT OR IGNORE INTO memory (id, text, embedding, created_at, category, session_id, turn_id, source_kind, source_ref) VALUES (?, ?, NULL, ?, 'manual', NULL, NULL, NULL, NULL)",
|
|
1795
|
+
)
|
|
1796
|
+
.run(randomUUID(), text, Date.now()),
|
|
1797
|
+
)
|
|
1798
|
+
fileOnlyAdded++
|
|
1424
1799
|
}
|
|
1425
1800
|
|
|
1426
|
-
return {
|
|
1801
|
+
return {
|
|
1802
|
+
reconciled,
|
|
1803
|
+
vectorOnlyAdopted,
|
|
1804
|
+
fileOnlyAdded,
|
|
1805
|
+
supersededFileRemoved,
|
|
1806
|
+
supersededSkipped: supersededRows.length,
|
|
1807
|
+
legacySuspectLines,
|
|
1808
|
+
legacySuspectSamples,
|
|
1809
|
+
}
|
|
1427
1810
|
} finally {
|
|
1428
1811
|
db.close()
|
|
1429
1812
|
}
|
|
@@ -1431,6 +1814,9 @@ export async function reconcileMemoryStores(cwd?: string): Promise<{
|
|
|
1431
1814
|
|
|
1432
1815
|
export interface MemoryStats {
|
|
1433
1816
|
rows: number
|
|
1817
|
+
/** P1.3: baris aktif vs superseded — operator bisa memeriksa provenance. */
|
|
1818
|
+
liveRows: number
|
|
1819
|
+
supersededRows: number
|
|
1434
1820
|
dbBytes: number
|
|
1435
1821
|
walBytes: number
|
|
1436
1822
|
shmBytes: number
|
|
@@ -1447,6 +1833,14 @@ export function getMemoryStats(cwd?: string): MemoryStats {
|
|
|
1447
1833
|
try {
|
|
1448
1834
|
const rows =
|
|
1449
1835
|
(db.prepare("SELECT count(*) as c FROM memory").get() as { c: number } | null)?.c ?? 0
|
|
1836
|
+
const liveRows =
|
|
1837
|
+
(
|
|
1838
|
+
db
|
|
1839
|
+
.prepare(
|
|
1840
|
+
"SELECT count(*) as c FROM memory WHERE superseded_by IS NULL OR superseded_by = ''",
|
|
1841
|
+
)
|
|
1842
|
+
.get() as { c: number } | null
|
|
1843
|
+
)?.c ?? 0
|
|
1450
1844
|
const bounds = db
|
|
1451
1845
|
.prepare("SELECT min(created_at) as oldest, max(created_at) as newest FROM memory")
|
|
1452
1846
|
.get() as { oldest: number | null; newest: number | null } | null
|
|
@@ -1475,6 +1869,8 @@ export function getMemoryStats(cwd?: string): MemoryStats {
|
|
|
1475
1869
|
}
|
|
1476
1870
|
return {
|
|
1477
1871
|
rows,
|
|
1872
|
+
liveRows,
|
|
1873
|
+
supersededRows: Math.max(0, rows - liveRows),
|
|
1478
1874
|
dbBytes: sizeOf(p),
|
|
1479
1875
|
walBytes: sizeOf(`${p}-wal`),
|
|
1480
1876
|
shmBytes: sizeOf(`${p}-shm`),
|