#294·SkillOpt

sleep: --scope invoked harvests every $HOME-rooted session into every project (unbounded ancestor match)

Author: fschmutzCreated Sep 27, 2026Updated Sep 27, 2026

Summary

With --scope invoked, _project_matches in skillopt_sleep/harvest.py accepts any ancestor of the invoked project:

python
return a == b or a.startswith(b + os.sep) or b.startswith(a + os.sep)

So every session whose cwd is $HOME (or /) is harvested into every project below it. The same helper is used by the codex, copilot, copilot_cli and opencode harvesters.

Repro

  1. Have Claude Code sessions started from ~ and from ~/work/repo.
  2. cd ~/work/repo && skillopt-sleep dry-run --project "$(pwd)" --scope invoked --source claude --backend mock --progress
  3. The evidence log's harvest rows include the ~ sessions.

Observed on a real run: 13 of 31 harvested sessions came from $HOME, not from the invoked repository. Those unrelated tasks are then mined, replayed and gated as if they belonged to the project, which dilutes the held-out set and costs replay tokens.

Expected

An ancestor match is legitimate when invoking from a subdirectory of a repository (repo/sub should include sessions started at repo, which test_harvest_filters_scope_time_and_limit relies on). It should stop at the repository root, and never reach $HOME or /.

Proposed fix

Bound the ancestor clause: accept an ancestor only when it is inside the invoked project's git root; with no git root, accept any ancestor except $HOME and /. Patch plus two unit tests, full suite green (1499 passed, 11 skipped):

python
    if a == b or a.startswith(b + os.sep):
        return True
    return b.startswith(a + os.sep) and _within_ancestor_bound(a, b)


def _within_ancestor_bound(ancestor: str, invoked: str) -> bool:
    root = _repo_root(invoked)
    if root:
        return ancestor == root or ancestor.startswith(root + os.sep)
    home = os.path.abspath(os.path.expanduser("~"))
    return ancestor not in (home, os.path.abspath(os.sep))


def _repo_root(path: str) -> str:
    cur = path
    while True:
        if os.path.exists(os.path.join(cur, ".git")):
            return cur
        parent = os.path.dirname(cur)
        if parent == cur:
            return ""
        cur = parent

Happy to open a PR if that helps.