@thincoder/core 0.9.1
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/abort-provenance.mjs +116 -0
- package/advisor/citations.mjs +139 -0
- package/advisor/compaction.mjs +174 -0
- package/advisor/convergence.mjs +80 -0
- package/advisor/history.mjs +77 -0
- package/advisor/loop.mjs +288 -0
- package/advisor/messages.mjs +299 -0
- package/advisor/notice.mjs +141 -0
- package/advisor/project-context.mjs +197 -0
- package/advisor/repos.mjs +150 -0
- package/advisor/run.mjs +190 -0
- package/advisor/truncate.mjs +57 -0
- package/advisor.mjs +281 -0
- package/agent/child-marks.mjs +24 -0
- package/agent/completion.mjs +145 -0
- package/agent/dispatch.mjs +493 -0
- package/agent/family-tools.mjs +174 -0
- package/agent/helpers.mjs +412 -0
- package/agent/post-turn.mjs +70 -0
- package/agent/record-results.mjs +174 -0
- package/agent/relay-prefix.mjs +39 -0
- package/agent/run-stages.mjs +244 -0
- package/agent/setup-reminders.mjs +199 -0
- package/agent/setup.mjs +234 -0
- package/agent/spawn-child.mjs +258 -0
- package/agent/suspension.mjs +240 -0
- package/agent/write-gate.mjs +87 -0
- package/agent-tools/advisor-async.mjs +481 -0
- package/agent-tools/advisor-settle.mjs +240 -0
- package/agent-tools/advisor.mjs +280 -0
- package/agent-tools/async-discard.mjs +143 -0
- package/agent-tools/async-settle.mjs +299 -0
- package/agent-tools/batch-segment.mjs +265 -0
- package/agent-tools/child-permission.mjs +45 -0
- package/agent-tools/consult.mjs +471 -0
- package/agent-tools/design-token.mjs +117 -0
- package/agent-tools/digest-budget.mjs +76 -0
- package/agent-tools/eng.mjs +102 -0
- package/agent-tools/escalate-async.mjs +302 -0
- package/agent-tools/goal.mjs +119 -0
- package/agent-tools/panel-blocks.mjs +24 -0
- package/agent-tools/parent-channel.mjs +231 -0
- package/agent-tools/plan.mjs +86 -0
- package/agent-tools/read-history.mjs +309 -0
- package/agent-tools/recent-changes.mjs +24 -0
- package/agent-tools/review-facts.mjs +31 -0
- package/agent-tools/settings.mjs +268 -0
- package/agent-tools/skill.mjs +63 -0
- package/agent-tools/spawn-gates.mjs +109 -0
- package/agent-tools/subagent-actions.mjs +496 -0
- package/agent-tools/subagent-async.mjs +456 -0
- package/agent-tools/subagent-panel.mjs +160 -0
- package/agent-tools/subagent-run.mjs +208 -0
- package/agent-tools/subagent-scheduler.mjs +446 -0
- package/agent-tools/subagent-spawn.mjs +478 -0
- package/agent-tools/subagent.mjs +419 -0
- package/agent-tools/task.mjs +87 -0
- package/agent-tools/timer.mjs +46 -0
- package/agent-tools/verify.mjs +295 -0
- package/agent-tools.mjs +23 -0
- package/agent.mjs +430 -0
- package/auto-think.mjs +115 -0
- package/compress-form.mjs +24 -0
- package/config-io.mjs +277 -0
- package/config-migrate.mjs +178 -0
- package/config-presets.mjs +49 -0
- package/config.mjs +419 -0
- package/context.mjs +495 -0
- package/conventions.mjs +223 -0
- package/embedding.mjs +120 -0
- package/escape.mjs +152 -0
- package/expand-home.mjs +16 -0
- package/explore-distill.mjs +152 -0
- package/generate-title.mjs +123 -0
- package/git/checkpoint.mjs +448 -0
- package/git/gitmem.mjs +100 -0
- package/history-window.mjs +179 -0
- package/hooks.mjs +108 -0
- package/i18n.mjs +106 -0
- package/index-bin.mjs +48 -0
- package/index-discover.mjs +176 -0
- package/ledger-cmd.mjs +209 -0
- package/ledger-db.mjs +86 -0
- package/ledger-surface.mjs +76 -0
- package/ledger.mjs +202 -0
- package/log.mjs +195 -0
- package/manifest.mjs +338 -0
- package/markdown.mjs +106 -0
- package/mcp/helpers.mjs +51 -0
- package/mcp/transport-http.mjs +248 -0
- package/mcp/transport-stdio.mjs +140 -0
- package/mcp/transport-ws.mjs +122 -0
- package/mcp.mjs +295 -0
- package/memory/code-index.mjs +219 -0
- package/memory/code-sync.mjs +427 -0
- package/memory/core.mjs +318 -0
- package/memory/delete.mjs +242 -0
- package/memory/docs.mjs +431 -0
- package/memory/file-walk.mjs +109 -0
- package/memory/origin.mjs +24 -0
- package/memory/scan.mjs +177 -0
- package/memory/schema.mjs +460 -0
- package/memory.mjs +21 -0
- package/model-ref.mjs +66 -0
- package/model-specs.mjs +277 -0
- package/package.json +32 -0
- package/peer-domains.mjs +265 -0
- package/peer-instances.mjs +178 -0
- package/permission.mjs +79 -0
- package/process-probe.mjs +315 -0
- package/prompt-files.mjs +113 -0
- package/prompt-overlays.mjs +78 -0
- package/prompts/advisor-design.md +43 -0
- package/prompts/advisor-round1.md +41 -0
- package/prompts/advisor-round2.md +46 -0
- package/prompts/advisor-round3.md +42 -0
- package/prompts/common.md +158 -0
- package/prompts/consult-base.md +19 -0
- package/prompts/discipline-engineering.md +123 -0
- package/prompts/discipline-normal.md +206 -0
- package/prompts/persona-coder.md +21 -0
- package/prompts/persona-eng-coder.md +41 -0
- package/prompts/persona-eng-designer.md +80 -0
- package/prompts/persona-engineering.md +160 -0
- package/prompts/persona-explore.md +15 -0
- package/prompts/persona-normal.md +35 -0
- package/prompts/persona-plan.md +27 -0
- package/provider/anthropic.mjs +225 -0
- package/provider/core.mjs +491 -0
- package/provider/errors.mjs +101 -0
- package/provider/google.mjs +257 -0
- package/provider/index.mjs +7 -0
- package/provider/list-models.mjs +163 -0
- package/provider/normalize.mjs +81 -0
- package/provider/rate.mjs +168 -0
- package/provider/responses.mjs +495 -0
- package/provider/retry.mjs +88 -0
- package/provider/sse.mjs +264 -0
- package/provider/wait-status.mjs +59 -0
- package/proxy.mjs +274 -0
- package/rules.mjs +53 -0
- package/session-gc.mjs +248 -0
- package/session-guard.mjs +59 -0
- package/session-lifecycle.mjs +305 -0
- package/session-migrate.mjs +48 -0
- package/session-rename.mjs +38 -0
- package/session-segments.mjs +99 -0
- package/session-slot-write.mjs +168 -0
- package/session-slots-manifest.mjs +264 -0
- package/session-slots.mjs +298 -0
- package/session-store.mjs +441 -0
- package/session.mjs +244 -0
- package/skills.mjs +234 -0
- package/text-budget.mjs +79 -0
- package/token-ttl.mjs +285 -0
- package/tool-docs/apply_patch.md +15 -0
- package/tool-docs/bash.md +38 -0
- package/tool-docs/delete.md +13 -0
- package/tool-docs/edit.md +30 -0
- package/tool-docs/execute.md +21 -0
- package/tool-docs/fetch.md +12 -0
- package/tool-docs/file_ops.md +17 -0
- package/tool-docs/get_current_time.md +8 -0
- package/tool-docs/git.md +54 -0
- package/tool-docs/glob.md +11 -0
- package/tool-docs/grep.md +19 -0
- package/tool-docs/hashline_edit.md +14 -0
- package/tool-docs/insert_after.md +15 -0
- package/tool-docs/lint.md +10 -0
- package/tool-docs/ls.md +12 -0
- package/tool-docs/lsp.md +10 -0
- package/tool-docs/process.md +10 -0
- package/tool-docs/question.md +16 -0
- package/tool-docs/read.md +20 -0
- package/tool-docs/read_image.md +8 -0
- package/tool-docs/tree.md +14 -0
- package/tool-docs/wait_for.md +22 -0
- package/tool-docs/websearch.md +16 -0
- package/tool-docs/write.md +11 -0
- package/tools/bash.mjs +276 -0
- package/tools/edit-batch.mjs +204 -0
- package/tools/edit-diff.mjs +388 -0
- package/tools/exec-run.mjs +43 -0
- package/tools/execute.mjs +243 -0
- package/tools/file.mjs +464 -0
- package/tools/git-checkpoint.mjs +143 -0
- package/tools/git-ext.mjs +173 -0
- package/tools/git.mjs +415 -0
- package/tools/glob-dialect.mjs +130 -0
- package/tools/index.mjs +76 -0
- package/tools/linter.mjs +120 -0
- package/tools/lsp.mjs +335 -0
- package/tools/ops.mjs +293 -0
- package/tools/patch.mjs +290 -0
- package/tools/question.mjs +26 -0
- package/tools/repomap.mjs +314 -0
- package/tools/search.mjs +248 -0
- package/tools/shared.mjs +467 -0
- package/tools/tree.mjs +81 -0
- package/tools/web.mjs +224 -0
- package/tools/write-path.mjs +191 -0
- package/traces/trace-store.mjs +303 -0
- package/undo-stack.mjs +47 -0
package/memory/core.mjs
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* memory/core.mjs — memory CRUD, hybrid retrieval, embedding management.
|
|
3
|
+
* The disk-truth row-match + deletion family (matchMemoryRows/deleteWhere/deleteByUid/remove/
|
|
4
|
+
* assertPathInside) lives in delete.mjs — this module imports nothing from it (delete.mjs
|
|
5
|
+
* imports fetchEntry/syncDir from here).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { parseEntry, serializeEntry, entryFilename } from "../markdown.mjs"
|
|
9
|
+
import { embed, cosine, toBlob, fromBlob } from "../embedding.mjs"
|
|
10
|
+
import { scanVectors, createTopK } from "./scan.mjs"
|
|
11
|
+
import { normalizeOrigin } from "./origin.mjs"
|
|
12
|
+
import { readFile, stat, readdir, writeFile, mkdir } from "node:fs/promises"
|
|
13
|
+
import { join } from "node:path"
|
|
14
|
+
import { segmentCJK, VALID_TYPES, SCHEMA_VERSION } from "./schema.mjs"
|
|
15
|
+
import { safeSliceUTF16 } from "../text-budget.mjs"
|
|
16
|
+
|
|
17
|
+
const EMBED_BATCH_SIZE = 256
|
|
18
|
+
export const EMBED_TEXT_MAX_LEN = 2000
|
|
19
|
+
const FTS_TOKEN_MAX = 16
|
|
20
|
+
const DEFAULT_LIST_LIMIT = 50
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Write a memory entry. entry: { type, title, content, tags? }
|
|
24
|
+
* Returns the new entry id.
|
|
25
|
+
*/
|
|
26
|
+
export async function put(memory, { type, title, content, tags = "" }) {
|
|
27
|
+
if (!VALID_TYPES.has(type)) {
|
|
28
|
+
throw new Error(`Invalid memory type "${type}"; expected one of: ${[...VALID_TYPES].join(", ")}`)
|
|
29
|
+
}
|
|
30
|
+
if (!title || !content) throw new Error("memory entry requires title and content")
|
|
31
|
+
const now = Date.now()
|
|
32
|
+
const stmt = memory.db.prepare(
|
|
33
|
+
`INSERT INTO entries (type, title, content, tags, seg_title, seg_content, seg_tags, created_at, updated_at)
|
|
34
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
35
|
+
)
|
|
36
|
+
const info = stmt.run(type, title, content, tags, segmentCJK(title), segmentCJK(content), segmentCJK(tags), now, now)
|
|
37
|
+
return Number(info.lastInsertRowid)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Hybrid retrieval: FTS5(BM25) + vector cosine, RRF(k=60) merged ranking.
|
|
42
|
+
* Falls back to pure FTS when no embedder. Results include layer label.
|
|
43
|
+
* Returns [{ id, layer, type, title, content, tags, rank }]
|
|
44
|
+
*/
|
|
45
|
+
export async function search(memory, query, { limit = 5 } = {}) {
|
|
46
|
+
const ftsQuery = buildFtsQuery(query)
|
|
47
|
+
const ftsList = ftsQuery ? ftsSearch(memory, ftsQuery, Math.max(limit * 4, 20)) : []
|
|
48
|
+
|
|
49
|
+
if (!memory.embedder) return ftsList.slice(0, limit)
|
|
50
|
+
|
|
51
|
+
// ---- vector channel ----
|
|
52
|
+
try { await ensureEmbeddings(memory) } catch (e) {
|
|
53
|
+
console.error(`[memory] embedding ensure failed, falling back to FTS-only: ${e.message}`)
|
|
54
|
+
return ftsList.slice(0, limit)
|
|
55
|
+
}
|
|
56
|
+
let qvec
|
|
57
|
+
try { [qvec] = await embed(memory.embedder, [query]) } catch (e) {
|
|
58
|
+
console.error(`[memory] query embedding failed, falling back to FTS-only: ${e.message}`)
|
|
59
|
+
return ftsList.slice(0, limit)
|
|
60
|
+
}
|
|
61
|
+
// §6.11 读缝归一(单点取名——函数体内一律用归一值)
|
|
62
|
+
const projectOrigin = normalizeOrigin(memory.projectOrigin)
|
|
63
|
+
const vecFilter = projectOrigin ? `AND (layer = 'team' OR origin = ?)` : ""
|
|
64
|
+
const vecParams = projectOrigin ? [projectOrigin] : []
|
|
65
|
+
// TUI-OOM-ROOTCAUSE(MEMORY.md §6.8):分块扫描 + 有界 top-K(原全表 .all() 物化 +
|
|
66
|
+
// 全量排序——峰值 = 块 + K;召回语义不变)。两表各自游标扫描、共享同一 top-K。
|
|
67
|
+
// TUI 假死批(§6.10):scanVectors = async(让出)——游标键缺省 rowid(= 本表 PK 别名,零改)。
|
|
68
|
+
const top = createTopK(Math.max(limit * 4, 20))
|
|
69
|
+
const onRow = (r) => top.push({ id: r.uid, score: cosine(qvec, fromBlob(r.embedding)) })
|
|
70
|
+
await scanVectors(memory.db, `SELECT rowid, 'personal:' || id AS uid, embedding FROM entries WHERE embedding IS NOT NULL`, [], { onRow })
|
|
71
|
+
await scanVectors(memory.db, `SELECT rowid, layer || ':' || COALESCE(origin, '') || ':' || path AS uid, embedding FROM files WHERE embedding IS NOT NULL ${vecFilter}`, vecParams, { onRow })
|
|
72
|
+
const vecList = top.list()
|
|
73
|
+
|
|
74
|
+
// ---- RRF merge ----
|
|
75
|
+
const K = 60
|
|
76
|
+
const scores = new Map()
|
|
77
|
+
ftsList.forEach((r, i) => scores.set(r.id, (scores.get(r.id) ?? 0) + 1 / (K + i + 1)))
|
|
78
|
+
vecList.forEach((r, i) => scores.set(r.id, (scores.get(r.id) ?? 0) + 1 / (K + i + 1)))
|
|
79
|
+
|
|
80
|
+
return [...scores.entries()]
|
|
81
|
+
.sort((a, b) => b[1] - a[1])
|
|
82
|
+
.slice(0, limit)
|
|
83
|
+
.map(([id, score]) => {
|
|
84
|
+
const entry = fetchEntry(memory, id)
|
|
85
|
+
return entry ? { ...entry, rrf: score } : null
|
|
86
|
+
})
|
|
87
|
+
.filter(Boolean)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Pure FTS search (two-table merge, sorted by bm25), used as the positional input for RRF */
|
|
91
|
+
export function ftsSearch(memory, ftsQuery, limit) {
|
|
92
|
+
const personal = memory.db.prepare(`
|
|
93
|
+
SELECT e.id, e.type, e.title, e.content, e.tags, bm25(entries_fts) AS rank
|
|
94
|
+
FROM entries_fts JOIN entries e ON e.id = entries_fts.rowid
|
|
95
|
+
WHERE entries_fts MATCH ?
|
|
96
|
+
ORDER BY rank LIMIT ?
|
|
97
|
+
`).all(ftsQuery, limit).map((r) => ({ ...r, layer: "personal", id: `personal:${r.id}` }))
|
|
98
|
+
|
|
99
|
+
const projectOrigin = normalizeOrigin(memory.projectOrigin) // §6.11 读缝归一
|
|
100
|
+
const originFilter = projectOrigin ? `AND (f.layer = 'team' OR f.origin = ?)` : ""
|
|
101
|
+
const originParams = projectOrigin ? [ftsQuery, projectOrigin, limit] : [ftsQuery, limit]
|
|
102
|
+
const files = memory.db.prepare(`
|
|
103
|
+
SELECT f.layer, f.origin, f.path, f.type, f.title, f.content, f.tags, f.author, bm25(files_fts) AS rank
|
|
104
|
+
FROM files_fts JOIN files f ON f.rowid = files_fts.rowid
|
|
105
|
+
WHERE files_fts MATCH ? ${originFilter}
|
|
106
|
+
ORDER BY rank LIMIT ?
|
|
107
|
+
`).all(...originParams).map((r) => ({ ...r, id: `${r.layer}:${r.origin}:${r.path}` }))
|
|
108
|
+
|
|
109
|
+
return [...personal, ...files].sort((a, b) => a.rank - b.rank).slice(0, limit)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Fetch a full entry by unified id (personal:<n> / project:<origin>:<path> / team:<origin>:<path>)
|
|
113
|
+
* Note: since v9 the files table PK is (layer, origin, path); the same layer+path may span multiple origins.
|
|
114
|
+
* For project layer, prefers the row matching projectOrigin; for team layer, returns any row (first match when multiple team repos share a path). */
|
|
115
|
+
export function fetchEntry(memory, uid) {
|
|
116
|
+
const [layer, ...rest] = uid.split(":")
|
|
117
|
+
if (layer === "personal") {
|
|
118
|
+
const r = memory.db.prepare(`SELECT id, type, title, content, tags FROM entries WHERE id = ?`).get(Number(rest[0]))
|
|
119
|
+
return r ? { ...r, layer, id: uid } : null
|
|
120
|
+
}
|
|
121
|
+
// Files branch: origins may contain colons (Windows drive letters, e.g. project:C:\dir:file.md),
|
|
122
|
+
// so the LAST colon is always the origin/path separator — same parsing as deleteByUid.
|
|
123
|
+
// origin may be empty (compat with the old `project::file.md` format).
|
|
124
|
+
const lastColon = uid.lastIndexOf(":")
|
|
125
|
+
const origin = lastColon > layer.length ? uid.slice(layer.length + 1, lastColon) : ""
|
|
126
|
+
const path = lastColon > layer.length ? uid.slice(lastColon + 1) : uid.slice(layer.length + 1)
|
|
127
|
+
const projectOrigin = normalizeOrigin(memory.projectOrigin) // §6.11 读缝归一(uid 解析面零改——`:131` 余量兜底仍在)
|
|
128
|
+
if (layer === "project" && projectOrigin) {
|
|
129
|
+
const r = memory.db.prepare(`SELECT type, title, content, tags, author FROM files WHERE layer = ? AND origin = ? AND path = ?`).get(layer, origin || projectOrigin, path)
|
|
130
|
+
if (r) return { ...r, layer, id: uid }
|
|
131
|
+
}
|
|
132
|
+
// team layer or project fallback: query by origin+path; when origin is empty, degrade to path-only (compat with old UID)
|
|
133
|
+
if (origin) {
|
|
134
|
+
const r = memory.db.prepare(`SELECT type, title, content, tags, author FROM files WHERE layer = ? AND origin = ? AND path = ?`).get(layer, origin, path)
|
|
135
|
+
if (r) return { ...r, layer, id: uid }
|
|
136
|
+
}
|
|
137
|
+
const r = memory.db.prepare(`SELECT type, title, content, tags, author FROM files WHERE layer = ? AND path = ?`).get(layer, path || rest.join(":"))
|
|
138
|
+
return r ? { ...r, layer, id: uid } : null
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Lazy embedding: batch-compute vectors for entries that don't have them yet (slow first time, zero cost thereafter).
|
|
143
|
+
* When the embedding model changes, clear all vectors and rebuild.
|
|
144
|
+
* Guarded by a module-level lock — concurrent fire-and-forget callers share the same promise,
|
|
145
|
+
* so embedding API calls are never duplicated.
|
|
146
|
+
*/
|
|
147
|
+
let _embedLock = null
|
|
148
|
+
export function ensureEmbeddings(memory) {
|
|
149
|
+
if (_embedLock) return _embedLock
|
|
150
|
+
_embedLock = _runEnsureEmbeddings(memory).finally(() => { _embedLock = null })
|
|
151
|
+
return _embedLock
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function _runEnsureEmbeddings(memory) {
|
|
155
|
+
const modelKey = memory.embedder.model
|
|
156
|
+
const stored = memory.db.prepare(`SELECT value FROM meta WHERE key = 'embedding_model'`).get()?.value
|
|
157
|
+
if (stored !== modelKey) {
|
|
158
|
+
// Invalidate all three tables + three meta keys in one go, to prevent stale vectors from dimension mismatch
|
|
159
|
+
memory.db.prepare(`UPDATE entries SET embedding = NULL`).run()
|
|
160
|
+
memory.db.prepare(`UPDATE files SET embedding = NULL`).run()
|
|
161
|
+
memory.db.prepare(`UPDATE code_chunks SET embedding = NULL`).run()
|
|
162
|
+
memory.db.prepare(`UPDATE doc_chunks SET embedding = NULL`).run()
|
|
163
|
+
const upsert = memory.db.prepare(`INSERT INTO meta (key, value) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value`)
|
|
164
|
+
upsert.run("embedding_model", modelKey)
|
|
165
|
+
upsert.run("code_embedding_model", modelKey)
|
|
166
|
+
upsert.run("doc_embedding_model", modelKey)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const pendingEntries = memory.db.prepare(`SELECT id, title, content FROM entries WHERE embedding IS NULL LIMIT ${EMBED_BATCH_SIZE}`).all()
|
|
170
|
+
const pendingFiles = memory.db.prepare(`SELECT rowid, title, content FROM files WHERE embedding IS NULL LIMIT ${EMBED_BATCH_SIZE}`).all()
|
|
171
|
+
if (pendingEntries.length + pendingFiles.length === 0) {
|
|
172
|
+
// No pending memory entries — also backfill code and doc chunk vectors
|
|
173
|
+
await (await import("./code-sync.mjs")).ensureCodeEmbeddings(memory)
|
|
174
|
+
await (await import("./docs.mjs")).ensureDocEmbeddings(memory)
|
|
175
|
+
return
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const items = [...pendingEntries, ...pendingFiles]
|
|
179
|
+
const texts = items.map((r) => `${r.title}\n${safeSliceUTF16(r.content, EMBED_TEXT_MAX_LEN)}`)
|
|
180
|
+
const vecs = await embed(memory.embedder, texts)
|
|
181
|
+
|
|
182
|
+
const updateEntry = memory.db.prepare(`UPDATE entries SET embedding = ? WHERE id = ?`)
|
|
183
|
+
pendingEntries.forEach((r, i) => updateEntry.run(toBlob(vecs[i]), r.id))
|
|
184
|
+
const updateFile = memory.db.prepare(`UPDATE files SET embedding = ? WHERE rowid = ?`)
|
|
185
|
+
pendingFiles.forEach((r, i) => updateFile.run(toBlob(vecs[pendingEntries.length + i]), r.rowid))
|
|
186
|
+
|
|
187
|
+
// After each batch of embeddings, also backfill code and doc chunks
|
|
188
|
+
await (await import("./code-sync.mjs")).ensureCodeEmbeddings(memory)
|
|
189
|
+
await (await import("./docs.mjs")).ensureDocEmbeddings(memory)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Write a markdown memory entry to the specified layer directory (project/team) and index it immediately.
|
|
194
|
+
* Writes the file only — the project layer never performs git operations on the user's project repo;
|
|
195
|
+
* team layer commit+push is handled by gitmem.mjs.
|
|
196
|
+
* Returns the filename.
|
|
197
|
+
*/
|
|
198
|
+
export async function putMarkdown(memory, { layer, dir, type, title, content, tags = [], author = "unknown" }) {
|
|
199
|
+
if (layer !== "project" && layer !== "team") throw new Error(`invalid markdown layer: ${layer}`)
|
|
200
|
+
const filename = entryFilename(title)
|
|
201
|
+
const markdown = serializeEntry({ type, title, tags, author }, content)
|
|
202
|
+
await mkdir(dir, { recursive: true })
|
|
203
|
+
await writeFile(join(dir, filename), markdown, "utf8")
|
|
204
|
+
await indexMarkdownFile(memory, { layer, dir, filename })
|
|
205
|
+
return filename
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Sync a markdown directory to the index: new/changed (by mtime) entries are re-indexed,
|
|
210
|
+
* vanished entries are removed from the index.
|
|
211
|
+
*/
|
|
212
|
+
export async function syncDir(memory, { layer, dir }) {
|
|
213
|
+
const origin = normalizeOrigin(dir) // §6.11 写缝归一(目录 I/O 用原样 dir;与库面比较一律用归一值)
|
|
214
|
+
let names
|
|
215
|
+
try {
|
|
216
|
+
names = (await readdir(dir)).filter((n) => n.endsWith(".md"))
|
|
217
|
+
} catch {
|
|
218
|
+
names = []
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const indexed = new Map(
|
|
222
|
+
memory.db.prepare(`SELECT path, mtime_ms FROM files WHERE layer = ? AND origin = ?`).all(layer, origin).map((r) => [r.path, r.mtime_ms]),
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
let added = 0, updated = 0, skipped = 0
|
|
226
|
+
for (const filename of names) {
|
|
227
|
+
let mtimeMs
|
|
228
|
+
// 档在 readdir 与 stat 之间消失 / 不可读 ⇒ 跳过本行(**不**移出 stale 候选:该文件已无
|
|
229
|
+
// 盘面依据,交由下方清理移除其索引行——避免 stat 抛出打断整趟 sync,调用面
|
|
230
|
+
// `deleteByUid` 先 unlink 后 syncDir,抛出即「删除已成功却报失败」)
|
|
231
|
+
try { mtimeMs = Math.floor((await stat(join(dir, filename))).mtimeMs) } catch { continue }
|
|
232
|
+
const old = indexed.get(filename)
|
|
233
|
+
const isNew = old === undefined
|
|
234
|
+
if (!isNew && old === mtimeMs) {
|
|
235
|
+
// 已见且未变:行完好——**必须**移出 stale 候选(既有缺陷修复:原 `continue` 不带
|
|
236
|
+
// delete ⇒ 未变文件被下方清理误删,索引在两次 sync 间 1→0→1 翻覆;T-O1 暴露)
|
|
237
|
+
skipped++
|
|
238
|
+
indexed.delete(filename)
|
|
239
|
+
continue
|
|
240
|
+
}
|
|
241
|
+
try {
|
|
242
|
+
await indexMarkdownFile(memory, { layer, dir, filename, mtimeMs })
|
|
243
|
+
} catch (e) {
|
|
244
|
+
console.error(`[memory] skip ${layer}/${filename}: ${e.message}`)
|
|
245
|
+
skipped++
|
|
246
|
+
indexed.delete(filename)
|
|
247
|
+
continue
|
|
248
|
+
}
|
|
249
|
+
if (isNew) added++
|
|
250
|
+
else updated++
|
|
251
|
+
indexed.delete(filename)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
let removed = 0
|
|
255
|
+
for (const stale of indexed.keys()) {
|
|
256
|
+
memory.db.prepare(`DELETE FROM files WHERE layer = ? AND origin = ? AND path = ?`).run(layer, origin, stale)
|
|
257
|
+
removed++
|
|
258
|
+
}
|
|
259
|
+
return { added, updated, removed, skipped }
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Parse a single .md and upsert into the files table */
|
|
263
|
+
export async function indexMarkdownFile(memory, { layer, dir, filename, mtimeMs }) {
|
|
264
|
+
const origin = normalizeOrigin(dir) // §6.11 写缝归一(文件 I/O 用原样 dir;origin 列用归一值)
|
|
265
|
+
const abs = join(dir, filename)
|
|
266
|
+
const mtime = mtimeMs ?? Math.floor((await stat(abs)).mtimeMs)
|
|
267
|
+
const { meta, content } = parseEntry(await readFile(abs, "utf8"))
|
|
268
|
+
const tags = meta.tags.join(" ")
|
|
269
|
+
memory.db.prepare(`
|
|
270
|
+
INSERT INTO files (layer, origin, path, type, title, content, tags, author, mtime_ms, seg_title, seg_content, seg_tags, updated_at)
|
|
271
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
272
|
+
ON CONFLICT(layer, origin, path) DO UPDATE SET
|
|
273
|
+
type=excluded.type, title=excluded.title, content=excluded.content, tags=excluded.tags,
|
|
274
|
+
author=excluded.author, mtime_ms=excluded.mtime_ms, origin=excluded.origin,
|
|
275
|
+
seg_title=excluded.seg_title, seg_content=excluded.seg_content, seg_tags=excluded.seg_tags,
|
|
276
|
+
updated_at=excluded.updated_at
|
|
277
|
+
`).run(
|
|
278
|
+
layer, origin, filename, meta.type, meta.title, content, tags, meta.author, mtime,
|
|
279
|
+
segmentCJK(meta.title), segmentCJK(content), segmentCJK(tags), Date.now(),
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** List entries, optionally filtered by type */
|
|
284
|
+
export async function list(memory, { type, limit = DEFAULT_LIST_LIMIT } = {}) {
|
|
285
|
+
if (type) {
|
|
286
|
+
if (!VALID_TYPES.has(type)) throw new Error(`Invalid memory type "${type}"`)
|
|
287
|
+
return memory.db
|
|
288
|
+
.prepare(`SELECT id, type, title, content, tags, updated_at FROM entries WHERE type = ? ORDER BY updated_at DESC LIMIT ?`)
|
|
289
|
+
.all(type, limit)
|
|
290
|
+
}
|
|
291
|
+
return memory.db
|
|
292
|
+
.prepare(`SELECT id, type, title, content, tags, updated_at FROM entries ORDER BY updated_at DESC LIMIT ?`)
|
|
293
|
+
.all(limit)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** §6 clear action: wipe ALL personal entries (pure DB rows — files are project/team only).
|
|
297
|
+
* FTS + embedding go with the row triggers. Returns the number of deleted rows. */
|
|
298
|
+
export function clearPersonal(memory) {
|
|
299
|
+
const { changes } = memory.db.prepare(`DELETE FROM entries`).run()
|
|
300
|
+
return changes
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Build an FTS5 query: first split by whitespace/punctuation into tokens,
|
|
305
|
+
* then apply CJK character segmentation to each token.
|
|
306
|
+
* This keeps multi-character CJK words as FTS5 phrases ("分号" → "分 号" → phrase query, exact adjacency match),
|
|
307
|
+
* while different tokens are joined with OR ("命名 规范" → "命 名" OR "规 范", each phrase requires its own adjacency).
|
|
308
|
+
*/
|
|
309
|
+
export function buildFtsQuery(query) {
|
|
310
|
+
const terms = query
|
|
311
|
+
.split(/[\s,,。、;;!!??()()"`]+/)
|
|
312
|
+
.map((t) => t.trim())
|
|
313
|
+
.filter(Boolean)
|
|
314
|
+
.slice(0, FTS_TOKEN_MAX)
|
|
315
|
+
.map((t) => segmentCJK(t))
|
|
316
|
+
if (terms.length === 0) return ""
|
|
317
|
+
return terms.map((t) => `"${t.replaceAll('"', '""')}"`).join(" OR ")
|
|
318
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* memory/delete.mjs — file-layer disk-truth row match + deletion family.
|
|
3
|
+
*
|
|
4
|
+
* Split out of core.mjs (2026-09-08 — core.mjs crossed the 500-line hard limit).
|
|
5
|
+
* Owns: the shared §6 list/batch-delete row match (matchMemoryRows/diskFileRows/
|
|
6
|
+
* likePattern — one match surface, MEMORY.md §6), single/batch deletion
|
|
7
|
+
* (deleteByUid/deleteWhere), the legacy personal-only compat shell (remove),
|
|
8
|
+
* file-row fetching and path containment (fetchFileEntry/assertPathInside).
|
|
9
|
+
*
|
|
10
|
+
* Imports CRUD/retrieval/sync primitives from core.mjs (fetchEntry/syncDir) —
|
|
11
|
+
* one direction, no cycle.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { parseEntry } from "../markdown.mjs"
|
|
15
|
+
import { readFile, stat, readdir, unlink } from "node:fs/promises"
|
|
16
|
+
import { join, resolve } from "node:path"
|
|
17
|
+
import { fetchEntry, syncDir } from "./core.mjs"
|
|
18
|
+
import { normalizeOrigin } from "./origin.mjs"
|
|
19
|
+
|
|
20
|
+
/** LIKE pattern from a keyword (wildcards escaped — literal substring match, MEMORY.md §6 keyword filter). */
|
|
21
|
+
function likePattern(keyword) {
|
|
22
|
+
return `%${keyword.replace(/[\\%_]/g, (c) => `\\${c}`)}%`
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Shared row query for the §6 list action and the §6 batch delete (one match surface —
|
|
27
|
+
* rows carry { layer, id, type, title, ts }). Filters:
|
|
28
|
+
* layer: "personal" | "project" | "team" | null (null = all layers)
|
|
29
|
+
* type / keyword: optional (keyword matches title OR content, substring)
|
|
30
|
+
* Personal rows come from the entries table; project/team rows come from a DISK scan of
|
|
31
|
+
* the managed dir (2026-09-05 fix — disk is the truth): files present on disk but
|
|
32
|
+
* missing from the files index (orphans: external copies / gitmem pull / an earlier
|
|
33
|
+
* index failure) were invisible to list AND immune to batch delete — the old table-only
|
|
34
|
+
* match surface made a layer wipe need repeated delete rounds (deleteWhere→syncDir
|
|
35
|
+
* re-indexed the orphans one round later). Scanning disk keeps list and batch delete
|
|
36
|
+
* consistent with what the user can see and delete. Rows from other projects'/team
|
|
37
|
+
* repos' dirs stay out (the scan only covers the dirs this memory context manages).
|
|
38
|
+
* Malformed files are skipped (parseEntry failure — same semantics as syncDir). Sorted
|
|
39
|
+
* by ts (created/updated, ms) DESC.
|
|
40
|
+
*/
|
|
41
|
+
export async function matchMemoryRows(memory, { layer = null, type = null, keyword = null, projectDir = null, teamDir = null } = {}) {
|
|
42
|
+
const rows = []
|
|
43
|
+
const wantLayer = (l) => !layer || layer === l
|
|
44
|
+
if (wantLayer("personal")) {
|
|
45
|
+
let sql = `SELECT id, type, title, created_at AS ts FROM entries`
|
|
46
|
+
const cond = []
|
|
47
|
+
const params = []
|
|
48
|
+
if (type) { cond.push("type = ?"); params.push(type) }
|
|
49
|
+
if (keyword) { cond.push("(title LIKE ? ESCAPE '\\' OR content LIKE ? ESCAPE '\\')"); const p = likePattern(keyword); params.push(p, p) }
|
|
50
|
+
if (cond.length) sql += " WHERE " + cond.join(" AND ")
|
|
51
|
+
sql += " ORDER BY created_at DESC"
|
|
52
|
+
for (const r of memory.db.prepare(sql).all(...params)) {
|
|
53
|
+
rows.push({ layer: "personal", id: `personal:${r.id}`, uid: `personal:${r.id}`, type: r.type, title: r.title, ts: r.ts })
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (wantLayer("project") && projectDir) {
|
|
57
|
+
// §6.11 删/读缝归一:uid 的 origin 段与索引行的 origin 必须同一形态
|
|
58
|
+
const org = normalizeOrigin(projectDir)
|
|
59
|
+
for (const r of await diskFileRows(projectDir, type, keyword)) rows.push({ ...r, layer: "project", id: `project:${org}:${r.path}` })
|
|
60
|
+
}
|
|
61
|
+
if (wantLayer("team") && teamDir) {
|
|
62
|
+
const org = normalizeOrigin(teamDir)
|
|
63
|
+
for (const r of await diskFileRows(teamDir, type, keyword)) rows.push({ ...r, layer: "team", id: `team:${org}:${r.path}` })
|
|
64
|
+
}
|
|
65
|
+
rows.sort((a, b) => (b.ts ?? 0) - (a.ts ?? 0))
|
|
66
|
+
return rows
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Disk-truth file scan for the project/team layer (2026-09-05 fix — see matchMemoryRows):
|
|
71
|
+
* readdir + parse every .md entry in dir, filter by type equality and keyword substring
|
|
72
|
+
* on title OR content (case-insensitive — SQLite LIKE parity). ts = file mtime (ms).
|
|
73
|
+
* Rows come back WITHOUT the layer field — the caller stamps layer and builds the uid.
|
|
74
|
+
*/
|
|
75
|
+
async function diskFileRows(dir, type, keyword) {
|
|
76
|
+
let names
|
|
77
|
+
try {
|
|
78
|
+
names = (await readdir(dir)).filter((n) => n.endsWith(".md"))
|
|
79
|
+
} catch {
|
|
80
|
+
return []
|
|
81
|
+
}
|
|
82
|
+
const kw = keyword ? keyword.toLowerCase() : null
|
|
83
|
+
const out = []
|
|
84
|
+
for (const name of names) {
|
|
85
|
+
try {
|
|
86
|
+
const abs = join(dir, name)
|
|
87
|
+
const { meta, content } = parseEntry(await readFile(abs, "utf8"))
|
|
88
|
+
if (type && meta.type !== type) continue
|
|
89
|
+
if (kw && !(meta.title.toLowerCase().includes(kw) || content.toLowerCase().includes(kw))) continue
|
|
90
|
+
const mtime = Math.floor((await stat(abs)).mtimeMs)
|
|
91
|
+
out.push({ path: name, type: meta.type, title: meta.title, ts: mtime })
|
|
92
|
+
} catch (e) {
|
|
93
|
+
console.error(`[memory] skip ${name}: ${e.message}`)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
out.sort((a, b) => b.ts - a.ts)
|
|
97
|
+
return out
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* §6 batch delete (action delete + type/keyword filter, confirm handled by the tool layer):
|
|
102
|
+
* deletes every row matchMemoryRows returns for the layer. Personal rows go straight to the
|
|
103
|
+
* DB (FTS + embedding cleanup via row triggers); project/team rows delete the markdown file
|
|
104
|
+
* (path containment enforced, ENOENT tolerated) then re-sync the layer dir once (index
|
|
105
|
+
* cleanup single source). Match surface = disk scan (2026-09-05 fix — orphans on disk
|
|
106
|
+
* with no index row are matched and deleted in the same pass; the trailing syncDir
|
|
107
|
+
* re-indexes the survivors). Team deletion never touches git — a later gitmem pull may
|
|
108
|
+
* resurrect the file while the remote still has it (same semantics as deleteByUid).
|
|
109
|
+
* Returns the number of deleted rows.
|
|
110
|
+
*/
|
|
111
|
+
export async function deleteWhere(memory, { layer, type = null, keyword = null } = {}, { dirs = {} } = {}) {
|
|
112
|
+
const rows = await matchMemoryRows(memory, { layer, type, keyword, projectDir: dirs.project ?? null, teamDir: dirs.team ?? null })
|
|
113
|
+
if (rows.length === 0) return 0
|
|
114
|
+
const personalIds = []
|
|
115
|
+
const byDir = new Map() // "layer\x00dir" → { layer, dir, paths: [] }
|
|
116
|
+
for (const r of rows) {
|
|
117
|
+
if (r.layer === "personal") {
|
|
118
|
+
const id = Number(String(r.uid).split(":")[1])
|
|
119
|
+
if (Number.isInteger(id)) personalIds.push(id)
|
|
120
|
+
continue
|
|
121
|
+
}
|
|
122
|
+
const dir = r.layer === "project" ? dirs.project : dirs.team
|
|
123
|
+
if (!dir) continue
|
|
124
|
+
const key = `${r.layer}\x00${dir}`
|
|
125
|
+
let group = byDir.get(key)
|
|
126
|
+
if (!group) { group = { layer: r.layer, dir, paths: [] }; byDir.set(key, group) }
|
|
127
|
+
group.paths.push(r.path)
|
|
128
|
+
}
|
|
129
|
+
const del = memory.db.prepare(`DELETE FROM entries WHERE id = ?`)
|
|
130
|
+
for (const id of personalIds) del.run(id)
|
|
131
|
+
for (const group of byDir.values()) {
|
|
132
|
+
for (const path of group.paths) {
|
|
133
|
+
assertPathInside(group.dir, path)
|
|
134
|
+
const abs = join(group.dir, path)
|
|
135
|
+
await unlink(abs).catch((e) => { if (e.code !== "ENOENT") throw e })
|
|
136
|
+
}
|
|
137
|
+
await syncDir(memory, { layer: group.layer, dir: group.dir })
|
|
138
|
+
}
|
|
139
|
+
return rows.length
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Delete a memory entry by unified id. Returns the deleted entry (F3: { id, layer, type, title, content, tags }).
|
|
143
|
+
* - personal:<n> (or bare <n>) → DELETE the entries row; FTS syncs via the entries_ad trigger and the
|
|
144
|
+
* embedding BLOB column goes with the row.
|
|
145
|
+
* - project:<origin>:<path> / team:<origin>:<path> → delete the markdown file at the uid's ORIGIN dir
|
|
146
|
+
* (2026-09-08, MEMORY.md §6.2: search rows surfaced from a non-current origin — another project's
|
|
147
|
+
* memory dir / another team clone — must be deletable where the file actually lives; the caller's
|
|
148
|
+
* dirs[layer] is only the fallback base for origin-less legacy uids `project::file.md`/`project:file.md`).
|
|
149
|
+
* Path containment is enforced against that base (assertPathInside — `..`/absolute variants incl.
|
|
150
|
+
* `..\` are rejected), then syncDir clears the files row (single source of index cleanup). ENOENT on
|
|
151
|
+
* the file is treated as already-deleted and continues; when the origin dir is missing locally,
|
|
152
|
+
* syncDir still drops the stale (layer, origin) index rows (readdir of a missing dir yields nothing —
|
|
153
|
+
* every row of a vanished dir is stale). Team deletion never touches git (git propagation is gitmem's
|
|
154
|
+
* job; a later gitmem pull may resurrect the file while the remote still has it).
|
|
155
|
+
* Throws on invalid id / missing entry (NF2) / path escaping the base dir.
|
|
156
|
+
* 2026-09-08 (code review): the orphan-disk fallback below is confined to managed memory dirs —
|
|
157
|
+
* an unmanaged origin must not make deleteByUid a crafted-id deletion primitive. */
|
|
158
|
+
export async function deleteByUid(memory, uid, { dirs = {} } = {}) {
|
|
159
|
+
const norm = /^\d+$/.test(uid) ? `personal:${uid}` : String(uid)
|
|
160
|
+
const [layer, ...rest] = norm.split(":")
|
|
161
|
+
if (layer === "personal") {
|
|
162
|
+
// 畸形尾缀拒绝(2026-09-08 代码正确性批 2.2):personal:5:extra 不得静默删 id=5
|
|
163
|
+
if (norm.split(":").length > 2) throw new Error(`invalid memory id: ${norm}`)
|
|
164
|
+
const id = rest[0] ?? ""
|
|
165
|
+
if (!/^\d+$/.test(id)) throw new Error(`invalid memory id: ${norm}`)
|
|
166
|
+
const entry = fetchEntry(memory, norm)
|
|
167
|
+
if (!entry) throw new Error(`memory ${norm} not found in layer personal`)
|
|
168
|
+
memory.db.prepare(`DELETE FROM entries WHERE id = ?`).run(Number(id))
|
|
169
|
+
return entry
|
|
170
|
+
}
|
|
171
|
+
if (layer !== "project" && layer !== "team") throw new Error(`invalid memory id: ${norm}`)
|
|
172
|
+
// origin = between the layer prefix and the LAST colon (origins may contain colons — Windows drive
|
|
173
|
+
// letters); path = segment after the last colon. Empty origin = legacy uid (`project::file.md` /
|
|
174
|
+
// `project:file.md`) → dirs[layer] fallback.
|
|
175
|
+
const lastColon = norm.lastIndexOf(":")
|
|
176
|
+
// §6.11 删面归一(uid 解析面):uid 内 origin 与库内键同一形态——旧形态 uid(`C:\…`)仍可直接删;
|
|
177
|
+
// `path` = 末冒号后段(原样);`origin` 空串 = 旧式无 origin uid ⇒ 落到 `dirs[layer]` 基准。
|
|
178
|
+
const origin = normalizeOrigin(lastColon > layer.length ? norm.slice(layer.length + 1, lastColon) : "")
|
|
179
|
+
const path = lastColon > layer.length ? norm.slice(lastColon + 1) : norm.slice(layer.length + 1)
|
|
180
|
+
const dir = origin || normalizeOrigin(dirs[layer])
|
|
181
|
+
if (!dir) throw new Error(`${layer} layer unavailable: no ${layer} directory configured for an origin-less id`)
|
|
182
|
+
assertPathInside(dir, path)
|
|
183
|
+
// Entry lookup: exact (layer, origin, path) row for origin-ful uids — the delete target is the uid's
|
|
184
|
+
// own dir, so a path-only fallback row from another origin must not be reported as deleted.
|
|
185
|
+
// Origin-less legacy uids keep the compat fallbacks (fetchFileEntry: current-origin then path-only).
|
|
186
|
+
let entry = null
|
|
187
|
+
if (origin) {
|
|
188
|
+
const r = memory.db.prepare(`SELECT type, title, content, tags, author FROM files WHERE layer = ? AND origin = ? AND path = ?`).get(layer, origin, path)
|
|
189
|
+
if (r) entry = { ...r, layer, id: norm }
|
|
190
|
+
} else {
|
|
191
|
+
entry = fetchFileEntry(memory, layer, norm, path)
|
|
192
|
+
}
|
|
193
|
+
const abs = join(dir, path)
|
|
194
|
+
let fileExists = false
|
|
195
|
+
try { await stat(abs); fileExists = true } catch { /* ENOENT — treat as already deleted */ }
|
|
196
|
+
if (!entry && fileExists) {
|
|
197
|
+
// Origin confinement (2026-09-08 code review): the orphan fallback deletes a file that has NO
|
|
198
|
+
// index row — only allow it when the origin is a managed memory dir (it already has files rows,
|
|
199
|
+
// or it is the caller's current layer dir). A uid naming an unmanaged directory must not delete
|
|
200
|
+
// a memory-shaped .md there.
|
|
201
|
+
if (origin && origin !== normalizeOrigin(dirs[layer])) {
|
|
202
|
+
const known = memory.db.prepare(`SELECT 1 FROM files WHERE layer = ? AND origin = ? LIMIT 1`).get(layer, origin)
|
|
203
|
+
if (!known) throw new Error(`memory ${norm} not found in layer ${layer}: origin is not a managed ${layer} memory dir`)
|
|
204
|
+
}
|
|
205
|
+
try {
|
|
206
|
+
const { meta, content } = parseEntry(await readFile(abs, "utf8"))
|
|
207
|
+
entry = { layer, id: norm, type: meta.type, title: meta.title, content, tags: meta.tags.join(" ") }
|
|
208
|
+
} catch { /* malformed file — keep the DB row (or null → not found below) */ }
|
|
209
|
+
}
|
|
210
|
+
if (!entry) throw new Error(`memory ${norm} not found in layer ${layer}`)
|
|
211
|
+
if (fileExists) await unlink(abs).catch((e) => { if (e.code !== "ENOENT") throw e })
|
|
212
|
+
await syncDir(memory, { layer, dir })
|
|
213
|
+
return entry
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Legacy personal-only delete (bare numeric id) — kept as the compat surface over deleteByUid. */
|
|
217
|
+
export async function remove(memory, id) {
|
|
218
|
+
const uid = /^\d+$/.test(String(id)) ? `personal:${id}` : String(id)
|
|
219
|
+
if (!fetchEntry(memory, uid)) return false
|
|
220
|
+
await deleteByUid(memory, uid, {})
|
|
221
|
+
return true
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Fetch a project/team file row for deletion: fetchEntry first, then a path-only fallback
|
|
225
|
+
* (origins with Windows drive letters, e.g. project:C:\dir:file.md, break naive ":" splitting). */
|
|
226
|
+
function fetchFileEntry(memory, layer, uid, path) {
|
|
227
|
+
const entry = fetchEntry(memory, uid)
|
|
228
|
+
if (entry) return entry
|
|
229
|
+
const r = memory.db.prepare(`SELECT type, title, content, tags, author FROM files WHERE layer = ? AND path = ?`).get(layer, path)
|
|
230
|
+
return r ? { ...r, layer, id: uid } : null
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Separator-agnostic containment check: the resolved path must stay inside dir.
|
|
234
|
+
* Both / and \ count as separators, so Windows-style traversal (..\..\\x) is caught on every platform. */
|
|
235
|
+
function assertPathInside(dir, path) {
|
|
236
|
+
if (!path) throw new Error(`invalid memory id: empty path`)
|
|
237
|
+
const base = resolve(dir).replaceAll("\\", "/")
|
|
238
|
+
const abs = resolve(dir, path.replaceAll("\\", "/")).replaceAll("\\", "/")
|
|
239
|
+
if (abs !== base && !abs.startsWith(base + "/")) {
|
|
240
|
+
throw new Error(`invalid memory path "${path}": must stay within ${dir}`)
|
|
241
|
+
}
|
|
242
|
+
}
|