sleep: --scope invoked harvests every $HOME-rooted session into every project (unbounded ancestor match)
Summary
With --scope invoked, _project_matches in skillopt_sleep/harvest.py accepts any ancestor of the invoked project:
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
- Have Claude Code sessions started from
~and from~/work/repo. cd ~/work/repo && skillopt-sleep dry-run --project "$(pwd)" --scope invoked --source claude --backend mock --progress- The evidence log's
harvestrows 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):
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 = parentHappy to open a PR if that helps.
Source: microsoft/SkillOpt