Personal_AI_Infrastructure icon indicating copy to clipboard operation
Personal_AI_Infrastructure copied to clipboard

SessionHarvester.ts: PROJECTS_DIR hardcoded — cannot harvest from archives or sibling project dirs

Open JElliottMiller opened this issue 6 months ago • 1 comments

SessionHarvester.ts hardcodes PROJECTS_DIR = path.join(CLAUDE_DIR, "projects", CWD_SLUG) at line 36 and offers no override flag. As a result, the harvester only sees session transcripts for sessions launched with cwd = ~/.claude/. Sessions launched from any other working directory (e.g., a side project at ~/code/<project-A>/ or ~/<work>/<project-B>/) live under different cwd-slugs in ~/.claude/projects/-home-<user>-..., and SessionHarvester cannot see them.

Same problem applies to archive directories: a user wanting to harvest from a backup tree (e.g., ~/.claude.<previous-version>-backup/projects/...) has no way to point the tool at that location.

This is a sibling of #680 (fixed): the slug-derivation bug was resolved by dynamic CWD_SLUG, but the harvester is still single-directory — it can't reach siblings, archives, or alternate project trees.

Steps to Reproduce

  1. Have sessions in multiple project directories, e.g.:
    • ~/.claude/projects/-home-<user>--claude/ (cwd = ~/.claude/)
    • ~/.claude/projects/-home-<user>--claude-Projects-<other>/ (cwd = ~/.claude/Projects/<other>/)
  2. Run bun run SessionHarvester.ts --mine --recent 20 — it only sees the first directory's .jsonl files.
  3. There is no flag to point the tool at the second.

Expected Behavior

A --projects-dir <path> flag that overrides PROJECTS_DIR for the run, allowing harvest from any project archive (current sibling cwd, v4 backup, exported archive, etc.). When the override is set, the --all 7-day age filter should be skipped (archives are old by definition; the filter would reject everything).

Suggested Fix (drop-in-patch)

Add the flag to parseArgs, extend getSessionFiles() to accept an override, and pass it through. The patch is ~10 added lines across three locations:

1. parseArgs options (line 489-496):

options: {
  recent: { type: "string" },
  all: { type: "boolean" },
  session: { type: "string" },
  "projects-dir": { type: "string" },   // NEW
  "dry-run": { type: "boolean" },
  mine: { type: "boolean", short: "m" },
  help: { type: "boolean", short: "h" },
},

2. getSessionFiles() signature + body (line 157):

function getSessionFiles(options: {
  recent?: number;
  all?: boolean;
  sessionId?: string;
  projectsDir?: string;   // NEW
}): string[] {
  const dir = options.projectsDir ?? PROJECTS_DIR;
  if (!fs.existsSync(dir)) {
    console.error(`Projects directory not found: ${dir}`);
    return [];
  }

  const files = fs.readdirSync(dir)
    .filter(f => f.endsWith('.jsonl'))
    .map(f => ({
      name: f,
      path: path.join(dir, f),
      mtime: fs.statSync(path.join(dir, f)).mtime.getTime()
    }))
    .sort((a, b) => b.mtime - a.mtime);

  if (options.sessionId) {
    const match = files.find(f => f.name.includes(options.sessionId!));
    return match ? [match.path] : [];
  }

  if (options.all) {
    // Archive mode: when --projects-dir is set, skip the 7-day filter
    // (archives are old by definition; the filter would reject them all).
    if (options.projectsDir) return files.map(f => f.path);
    const sevenDaysAgo = Date.now() - 7 * 24 * 60 * 60 * 1000;
    return files.filter(f => f.mtime > sevenDaysAgo).map(f => f.path);
  }

  const limit = options.recent || 10;
  return files.slice(0, limit).map(f => f.path);
}

3. Call site (line 522-526):

const sessionFiles = getSessionFiles({
  recent: values.recent ? parseInt(values.recent) : undefined,
  all: values.all,
  sessionId: values.session,
  projectsDir: values["projects-dir"]   // NEW
});

4. Help text (one line):

bun run SessionHarvester.ts --projects-dir <path>  Override PROJECTS_DIR (e.g. v4 archive); skips 7-day filter when set

Verification

Applied locally 2026-05-13 (backup at SessionHarvester.ts.pre-projects-dir-<ts>):

  • bun run SessionHarvester.ts --mine --dry-run --recent 2 (no flag) — works identically to pre-patch behavior (regression-tested: same session list, same mining output)
  • bun run SessionHarvester.ts --mine --dry-run --projects-dir ~/.claude.<prev-version>-backup/projects/-home-<user>--claude-Projects-<other> --session <uuid> — successfully scans the archive and reports candidates
  • --help shows the new flag
  • Parse-clean via bun -e "import('...SessionHarvester.ts').then(()=>console.log('parse ok'))"

Impact

  • Severity: Medium — workaround exists (symlinks into PROJECTS_DIR), but it's awkward and the symlink mtime issue defeats the --all filter
  • Scope: Anyone with multiple project directories (which is the norm for power users), anyone migrating from a v4 backup, anyone wanting to mine an exported PAI archive
  • Failure mode: No error — the tool simply doesn't see the data

File:line citation

PAI/TOOLS/SessionHarvester.ts:36 (hardcoded PROJECTS_DIR) and :157 (getSessionFiles() accepting no override).

Related

Same hardcoded-path-prevents-reuse pattern as the sibling issue for MemoryRetriever.ts filed alongside this one. Two-of-a-kind worth fixing together. Also adjacent to #1170 (KnowledgeHarvester per-project visibility) — different tool, similar single-PAI-instance assumption.


Local-fix status: Applied on my install with backup; happy to clarify or attach a full diff.

JElliottMiller avatar May 13 '26 19:05 JElliottMiller

Hey — Kai here.

Thanks for the drop-in patch. The --projects-dir <path> flag is now in the upstream tree. The signature on getSessionFiles() was extended with projectsDir?: string, parseArgs learned the new option, the call site passes values["projects-dir"] through, and --all skips the 7-day filter when the override is set (since archives are old by definition, which was the point you made).

Help text now reads:

bun run SessionHarvester.ts --projects-dir PATH     Override PROJECTS_DIR (e.g. v4 archive or sibling cwd); skips 7-day --all filter when set

Verified via bun ~/.claude/PAI/TOOLS/SessionHarvester.ts --help (the flag shows up correctly) and full bun build (16.48 KB, clean).

The archive-mode case (--projects-dir against a v4 backup tree) is exactly the one I'd hit too if I migrated machines — appreciate the foresight on the --all filter interaction.

Ships with the next release. Closing as fixed.

— Kai

danielmiessler avatar May 14 '26 18:05 danielmiessler