pi-memory icon indicating copy to clipboard operation
pi-memory copied to clipboard

projectSlug() does not normalize path separators: project-scoped lessons depend on how the cwd is spelled

Open VladimirMonin opened this issue 2 months ago • 0 comments

Environment

  • @samfp/pi-memory 1.5.0 (installed via pi packages; pi update not applied on top)
  • Windows 11, Node 25.8.1
  • Global store: one shared ~/.pi/memory/memory.db (270 facts / 262 lessons when this was measured; it has 415 lesson rows whose project is a full Windows path)

Summary

projectSlug() splits the cwd on "/" only, so a native Windows path is never split into segments and the whole path becomes the slug:

// dist/index.js:656-663
function projectSlug(cwd) {
  const parts = cwd.split("/").filter(Boolean);
  const skip = new Set(["workplace", "local", "home", "src", "scratch", os.userInfo().username]);
  for (const p of parts.reverse()) {
    if (!skip.has(p.toLowerCase()) && p.length > 1) return p.toLowerCase();
  }
  return "";
}

listLessons() then selects with an exact match, project = <slug> OR project IS NULL (dist/index.js:252, 259), so the spelling of the cwd decides which lessons a session receives:

slash-form     "C:/PY/WVM_DEV/worktrees/media-vision-settings"  -> "media-vision-settings"
backslash-form "C:\\PY\\WVM_DEV\\worktrees\\media-vision-settings" -> "c:\\py\\wvm_dev\\worktrees\\media-vision-settings"

expected for both: "media-vision-settings"

repro-slug.mjs (attached below) prints exactly this against the published build.

Observed effect

Same folder, same prompt, real buildContextBlock(), one shared DB — the two spellings select different lesson sets. Verified now with the same query listLessons() uses:

cwd spelling lessons selected (project = ? OR project IS NULL, LIMIT 50)
C:/PY/WVM_DEV/worktrees/media-vision-settings 15 lessons / 4 886 chars
C:\PY\WVM_DEV\worktrees\media-vision-settings 41 lessons / 13 578 chars

At the block level the divergence showed up as 8 vs 19 negative lessons in the same 8 000-char budget (measured with the real buildContextBlock() before an unrelated cleanup of the store; the absolute counts changed, the divergence did not).

Two consequences:

  1. The same project behaves as two different scopes depending on how the caller spelled the cwd. Since the injector is fed the session cwd, a user whose sessions start with C:\… and one whose sessions start with C:/… get different memory.
  2. It also re-enables an accidental match for legacy rows whose project is a full path (415 in this DB, source: session:unknown/consolidation): they are unreachable from the slash form and reachable from the backslash form. With 1.5.0 the write paths pass a slug (consolidation, dist/index.js:823-824 via index.ts:461-462) or NULL (memory_remember passes no project, dist/index.js:1200), so those rows predate the slug-based write — but the asymmetry means they are still effectively selectable, which makes the store's scoping behaviour hard to reason about.

Suggested fix

Normalize before splitting, e.g.

const parts = cwd.replace(/\\/g, "/").split("/").filter(Boolean);

or use path.parse / path.sep. It is also worth deciding explicitly what should happen to rows whose project is a full path: migrate them to the slug, or leave them as permanently unmatched.

Related (closed) issue: #19 "pi-memory-context injects unrelated project context and corrections from other projects" — same area, different mechanism; this one is purely about separator normalization and an exact-match comparison.

What I did not verify

I did not trace every caller of buildContextBlock/projectSlug, so I cannot say how often a backslash cwd reaches the injector in the wild — only that it does on this machine (Windows, sessions started from a native path) and that the two forms diverge as shown.

Repro script

import { projectSlug } from "file:///…/@samfp/pi-memory/dist/index.js";
const B = String.fromCharCode(92);
const parts = ["C:", "PY", "WVM_DEV", "worktrees", "media-vision-settings"];
console.log(JSON.stringify(projectSlug(parts.join("/"))));  // "media-vision-settings"
console.log(JSON.stringify(projectSlug(parts.join(B))));    // "c:\py\wvm_dev\worktrees\media-vision-settings"

VladimirMonin avatar Sep 26 '26 06:09 VladimirMonin