Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 58 additions & 12 deletions scripts/memory_dedup.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,44 @@ def dump_entities() -> list[dict]:
sys.exit(f"psql failed: {out.stderr.strip()}")
return json.loads(out.stdout.strip())

def read_fm(p: Path) -> dict:
def read_fm(p: Path, strict: bool = False) -> dict:
"""Scoped frontmatter field reader — mirrors src/file-sync.ts ::
readFrontmatterField. Matching is confined to the `---...---` block (or
the first 800 chars when no fence is found), so a same-named key in the
BODY (e.g. a memory documenting `tier: HOT` as prose) is never picked up.
Values have surrounding quotes stripped, so `tier: "HOT"` parses the same
as `tier: HOT`.

`strict=True` is for paths that DELETE on the result (main()'s T1/T2), like
readFrontmatterField's `{ strict: true }`: a real `---` block is required
(LF or CRLF), only top-level keys count, and there is no 800-char fallback,
so a body line or another key's nested `pg_id` never selects a file. When
in doubt every field is None and the file lands in T4_REVIEW, untouched.

`pgid` is lower-cased here, once, because Postgres returns uuids
lower-case: every by-id lookup then matches a hand-edited upper-case pg_id.
file-sync.ts instead returns the raw value and lower-cases where it
compares (validPgIds, updateMemoryIndex, findFilesByPgId). Either way,
both sides of every comparison are lower-case.
"""
t = p.read_text(errors="replace")
m = re.match(r"^---\n(.*?)\n---", t, re.S)
blk = m.group(1) if m else t[:800]
def g(k):
mm = re.search(rf"^\s*{k}\s*:\s*(.+)$", blk, re.M)
return mm.group(1).strip().strip('"').strip("'") if mm else None
if strict:
# `??` tries an EMPTY block first, so `---\n---` closes on line 2; the
# closing fence must be a whole line, so `---foo` does not close it.
m = re.match(r"?\s*---\r?\n((?:.*?\r?\n)??)---[ \t]*(?:\r?\n|$)", t, re.S)
blk = m.group(1) if m else ""
key_re = r"^{k}:[ \t]*(.+?)\r?$" # colon right after the key, as in TS
else:
m = re.match(r"^---\n(.*?)\n---", t, re.S)
blk = m.group(1) if m else t[:800]
key_re = r"^\s*{k}\s*:\s*(.+)$"

def g(k: str) -> str | None:
mm = re.search(key_re.format(k=k), blk, re.M)
return (mm.group(1).strip().strip('"').strip("'") or None) if mm else None
pgid = g("pg_id")
return {
"pgid": g("pg_id"), "name": g("name"), "type": g("type"),
"pgid": pgid.lower() if pgid else pgid, "name": g("name"), "type": g("type"),
"tier": g("tier"), "temperature": g("temperature"),
}

Expand Down Expand Up @@ -161,6 +183,28 @@ def home_has_file(e: dict) -> str | None:
_FETCH = object() # sentinel: "resolve the map yourself"


# A pg_id must be a real 8-4-4-4-12 uuid before it joins the batch: one id that
# passes a looser [0-9a-fA-F-]{36} filter but fails Postgres's ::uuid cast (36 hex
# chars, no dashes) fails the WHOLE ::uuid[] batch, and every memory silently
# falls back to frontmatter ranking. Ids come back lower-cased because Postgres
# returns uuids lower-case and the lookup below uses the same form, so a
# hand-edited upper-case id still resolves. Must match UUID_RE / validPgIds in
# src/file-sync.ts.
UUID_RE = re.compile(
r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
)


def valid_pg_ids(pg_ids: list[str]) -> list[str]:
"""Keep only well-formed uuids, lower-cased; report the rest on stderr
(they need repair)."""
ok = [i.lower() for i in pg_ids if UUID_RE.fullmatch(i)]
bad = len(pg_ids) - len(ok)
if bad:
print(f"[memory_dedup] skipped {bad} malformed pg_id(s)", file=sys.stderr)
return ok


def fetch_thermal_by_pgid(
pg_ids: list[str], timeout: int = 30, url: str | None = None
) -> dict[str, tuple[str, float]] | None:
Expand All @@ -187,7 +231,7 @@ def fetch_thermal_by_pgid(

Must match src/file-sync.ts :: fetchThermalByPgId.
"""
ids = ",".join(i for i in pg_ids if re.fullmatch(r"[0-9a-fA-F-]{36}", i))
ids = ",".join(valid_pg_ids(pg_ids))
if not ids:
return None
try:
Expand All @@ -197,7 +241,7 @@ def fetch_thermal_by_pgid(
return None
# Nothing is interpolated into the SQL: the id list travels as a psql
# variable, so psql owns the quoting and the ::uuid[] cast makes Postgres
# reject anything that is not a uuid. The regex above is a second gate.
# reject anything that is not a uuid. valid_pg_ids() is the gate before it.
#
# The query goes in on STDIN, not via -c: psql only performs :'var'
# interpolation on lexed input, and -c is sent verbatim (verified 2026-08-16
Expand Down Expand Up @@ -287,7 +331,8 @@ def regen_index(thermal: object = _FETCH) -> int:
if resolved is not None:
orphans = 0
for e in entries:
row = resolved.get(e["pgid"]) if e["pgid"] else None
# Defensive: read_fm is the normalisation point and already lower-cases.
row = resolved.get(str(e["pgid"]).lower()) if e["pgid"] else None
if row:
e["rank"] = TIER_RANK.get(row[0].upper(), UNKNOWN_TIER_RANK)
e["temp"] = row[1]
Expand Down Expand Up @@ -334,8 +379,9 @@ def main():
present = {p.name for p in files}
tiers = defaultdict(list)
for f in files:
fi = read_fm(f)
ent = by_id.get(fi["pgid"]) if fi["pgid"] else None
# strict: T1/T2 below are deleted under --apply.
fi = read_fm(f, strict=True)
ent = by_id.get(fi["pgid"]) if fi["pgid"] else None # read_fm lower-cases
if fi["pgid"] and ent is None:
tiers["T4_REVIEW"].append((f.name, f"dangling pg_id {fi['pgid'][:8]}")); continue
if ent is None:
Expand Down
57 changes: 49 additions & 8 deletions src/file-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,23 +436,28 @@ export async function syncToFile(
* is the only stable cross-rename anchor, so we glob the dir and read
* frontmatter to surface every file pointing at this entity.
*
* Frontmatter-only match: the regex is anchored to a line so a `pg_id`
* mention in the body cannot trigger a false delete.
* Reads pg_id with readFrontmatterField in strict mode: only a TOP-LEVEL key
* inside a real `---` block (LF or CRLF) counts, so a body line, a file with
* no frontmatter, or another key's nested `pg_id` can never trigger a delete.
* The value is compared case-insensitively: Postgres returns uuids
* lower-case, so a hand-edited upper-case pg_id is the same entity.
*/
function findFilesByPgId(dir: string, pgId: string): string[] {
if (!existsSync(dir)) {
const want = pgId.trim().toLowerCase();
// An empty id would equal the '' readFrontmatterField returns for every
// file WITHOUT a pg_id, and the caller deletes what this returns.
if (!want || !existsSync(dir)) {
return [];
}
const matches: string[] = [];
const pattern = new RegExp(`^pg_id:[ \\t]*${pgId}\\s*$`, 'm');
for (const file of readdirSync(dir)) {
if (!file.endsWith('.md') || file === 'MEMORY.md') {
continue;
}
const fullPath = join(dir, file);
try {
const content = readFileSync(fullPath, 'utf-8');
if (pattern.test(content)) {
if (readFrontmatterField(content, 'pg_id', { strict: true }).toLowerCase() === want) {
matches.push(fullPath);
}
} catch {
Expand Down Expand Up @@ -543,7 +548,24 @@ export async function syncMerge(
*
* @internal — exported for tests; not part of the MCP surface.
*/
export function readFrontmatterField(content: string, key: string): string {
export function readFrontmatterField(
content: string,
key: string,
{ strict = false }: { strict?: boolean } = {},
): string {
// strict (for paths that DELETE on a match): a real `---` block is required
// (LF or CRLF), and only a top-level key counts. No first-800-chars
// fallback and no nested fallback, so a body line or another key's nested
// field can never select a file. When in doubt the file is kept.
if (strict) {
// `??` tries an EMPTY block first, so `---\n---` closes on line 2 instead
// of running on to a later `---` rule in the body; the closing fence must
// be a whole line, so `---foo` does not close the block.
const block = content.match(/^\uFEFF?\s*---\r?\n((?:[\s\S]*?\r?\n)??)---[ \t]*(?:\r?\n|$)/)?.[1];
if (block === undefined) return '';
const flat = block.match(new RegExp(`^${key}:[ \\t]*(.*)$`, 'm'));
return (flat?.[1] ?? '').trim().replace(/^["']|["']$/g, '');
}
const block = content.match(/^\uFEFF?\s*---\n([\s\S]*?)\n---/)?.[1] ?? content.slice(0, 800);
const flat = block.match(new RegExp(`^${key}:[ \\t]*(.*)$`, 'm'));
const nested = flat ? null : block.match(new RegExp(`^[ \\t]+${key}:[ \\t]*(.*)$`, 'm'));
Expand All @@ -554,6 +576,25 @@ export function readFrontmatterField(content: string, key: string): string {
export type ThermalRow = { tier: string; temperature: number };
export type ThermalMap = Map<string, ThermalRow>;

/**
* A pg_id must be a real 8-4-4-4-12 uuid before it joins the batch: one id that
* passes a looser `[0-9a-fA-F-]{36}` filter but fails Postgres's `::uuid` cast
* (36 hex chars, no dashes) fails the WHOLE `::uuid[]` batch, and every memory
* silently falls back to frontmatter ranking. Ids come back lower-cased because
* Postgres returns uuids lower-case and the index lookup uses the same form, so
* a hand-edited upper-case id still resolves. Must match `UUID_RE` /
* `valid_pg_ids` in scripts/memory_dedup.py.
*/
const UUID_RE = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;

/** Keep only well-formed uuids, lower-cased; report the rest on stderr (they need repair). */
export function validPgIds(pgIds: string[]): string[] {
const ok = pgIds.filter(id => UUID_RE.test(id)).map(id => id.toLowerCase());
const bad = pgIds.length - ok.length;
if (bad > 0) process.stderr.write(`[file-sync] skipped ${bad} malformed pg_id(s)\n`);
return ok;
}

/**
* Batch-resolve live thermal state for the entities an index rebuild is about
* to rank. ONE query per rebuild, never one per file.
Expand All @@ -574,7 +615,7 @@ export type ThermalMap = Map<string, ThermalRow>;
* then never exit.
*/
export async function fetchThermalByPgId(pgIds: string[]): Promise<ThermalMap | null> {
const ids = pgIds.filter(id => /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(id));
const ids = validPgIds(pgIds);
if (ids.length === 0) return null;
try {
// db.js MUST be imported before the DATABASE_URL check: importing it is what
Expand Down Expand Up @@ -686,7 +727,7 @@ export async function updateMemoryIndex(
if (resolved) {
let orphans = 0;
for (const e of entries) {
const row = e.pgId ? resolved.get(e.pgId) : undefined;
const row = e.pgId ? resolved.get(e.pgId.toLowerCase()) : undefined;
if (row) {
const t = row.tier.toUpperCase();
e.rank = t in TIER_RANK ? TIER_RANK[t] : UNKNOWN_TIER_RANK;
Expand Down
Loading
Loading