Skip to content

Commit 992b922

Browse files
MaxFreedomPollardteknium1
authored andcommitted
fix(curator): stop restore from matching unrelated skills by name prefix
restore_skill() falls back to p.name.startswith(f"{skill_name}-") when no archive directory matches the requested name exactly. That fallback is meant to catch the timestamped duplicate archive_skill() writes on a name collision (<skill>-YYYYMMDDHHMMSS), but the bare prefix also matches any unrelated archived skill named <name>-something. So restoring "git" can pull an archived "git-helpers" out of .archive/, rename it to "git", and report success: the requested skill is not restored and the sibling is gone from the archive. Constrain the fallback to the exact suffix archive_skill() produces, a 14 digit timestamp. The exact-name match and the recursive nested-archive walk are unchanged, so nested and timestamped restores still work; unrelated siblings no longer match. Fixes #47647
1 parent cbfa018 commit 992b922

2 files changed

Lines changed: 86 additions & 3 deletions

File tree

‎tests/tools/test_skill_usage.py‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,76 @@ def test_archive_collision_gets_suffix(skills_home):
453453
assert any(n.startswith("dup-") and n != "dup" for n in archived)
454454

455455

456+
def test_restore_does_not_pull_unrelated_sibling_out_of_archive(skills_home):
457+
"""Restoring a name with no exact archive entry must NOT grab a different
458+
archived skill that merely shares a ``<name>-`` prefix.
459+
460+
The timestamped-duplicate fallback recognises only the suffix
461+
``archive_skill`` writes on a collision (``-YYYYMMDDHHMMSS``). A bare
462+
``startswith(f"{name}-")`` also matches sibling skills, so restoring
463+
``git`` would rip an archived ``git-helpers`` out of the archive, rename
464+
it to ``git``, and report success — destroying the sibling's only copy."""
465+
from tools.skill_usage import (
466+
archive_skill, restore_skill, list_archived_skill_names, mark_agent_created,
467+
)
468+
skills_dir = skills_home / "skills"
469+
_write_skill(skills_dir, "git-helpers")
470+
mark_agent_created("git-helpers")
471+
ok, msg = archive_skill("git-helpers")
472+
assert ok, msg
473+
474+
# "git" was never archived; only its prefix-sharing sibling was.
475+
ok, msg = restore_skill("git")
476+
assert not ok, f"restore('git') should not match 'git-helpers': {msg}"
477+
assert "not found" in msg.lower()
478+
479+
# The sibling must be untouched: still in the archive, never moved to skills/git.
480+
assert (skills_dir / ".archive" / "git-helpers" / "SKILL.md").exists()
481+
assert "git-helpers" in list_archived_skill_names()
482+
assert not (skills_dir / "git").exists()
483+
484+
485+
def test_restore_still_matches_timestamped_duplicate(skills_home):
486+
"""The fix must not over-narrow: a real collision dupe written by
487+
``archive_skill`` (``<name>-YYYYMMDDHHMMSS``) is still restorable by name
488+
when no bare ``<name>`` entry exists."""
489+
from tools.skill_usage import restore_skill
490+
skills_dir = skills_home / "skills"
491+
dupe = skills_dir / ".archive" / "report-tool-20260101000000"
492+
dupe.mkdir(parents=True)
493+
(dupe / "SKILL.md").write_text(
494+
"---\nname: report-tool\ndescription: x\n---\n", encoding="utf-8",
495+
)
496+
497+
ok, msg = restore_skill("report-tool")
498+
assert ok, msg
499+
assert (skills_dir / "report-tool" / "SKILL.md").exists()
500+
501+
502+
def test_restore_prefers_timestamped_dupe_over_unrelated_sibling(skills_home):
503+
"""With both a real timestamped duplicate and an unrelated sibling present,
504+
restoring the bare name picks the duplicate and leaves the sibling alone."""
505+
from tools.skill_usage import restore_skill
506+
archive = skills_home / "skills" / ".archive"
507+
508+
dupe = archive / "report-20260101000000" # real collision dupe of "report"
509+
sibling = archive / "report-card" # unrelated sibling skill
510+
for d, frontname in ((dupe, "report"), (sibling, "report-card")):
511+
d.mkdir(parents=True)
512+
(d / "SKILL.md").write_text(
513+
f"---\nname: {frontname}\ndescription: x\n---\n", encoding="utf-8",
514+
)
515+
516+
ok, msg = restore_skill("report")
517+
assert ok, msg
518+
# The duplicate (name: report) was restored, not the sibling (name: report-card).
519+
restored = (skills_home / "skills" / "report" / "SKILL.md").read_text()
520+
assert "name: report\n" in restored
521+
assert "name: report-card" not in restored
522+
assert not dupe.exists() # the dupe moved out of the archive
523+
assert sibling.exists() # the unrelated sibling stayed put
524+
525+
456526
# ---------------------------------------------------------------------------
457527
# Reporting
458528
# ---------------------------------------------------------------------------

‎tools/skill_usage.py‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,14 +752,27 @@ def restore_skill(skill_name: str) -> Tuple[bool, str]:
752752
if not archive_root.exists():
753753
return False, "no archive directory"
754754

755-
# Try exact name match first, then any prefix match (for timestamped dupes).
755+
# Try exact name match first, then the timestamped-duplicate fallback.
756756
# Recursive walk handles nested archive layouts (e.g. .archive/<category>/<skill>/)
757757
# left behind by older archive paths or external imports.
758758
candidates = [p for p in archive_root.rglob("*") if p.is_dir() and p.name == skill_name]
759759
if not candidates:
760+
# A name collision makes archive_skill() disambiguate by appending its
761+
# UTC timestamp ("<skill>-YYYYMMDDHHMMSS", a 14-digit suffix), so only
762+
# that exact shape is another copy of THIS skill. A bare
763+
# startswith(f"{skill_name}-") also swallows unrelated sibling skills —
764+
# restoring "git" would otherwise pull an archived "git-helpers" out of
765+
# the archive and rename it to "git", destroying the sibling's only
766+
# copy. Require the suffix to be the timestamp archive_skill writes.
767+
prefix = f"{skill_name}-"
760768
candidates = sorted(
761-
[p for p in archive_root.rglob("*")
762-
if p.is_dir() and p.name.startswith(f"{skill_name}-")],
769+
[
770+
p for p in archive_root.rglob("*")
771+
if p.is_dir()
772+
and p.name.startswith(prefix)
773+
and len(p.name) - len(prefix) == 14
774+
and p.name[len(prefix):].isdigit()
775+
],
763776
reverse=True,
764777
)
765778
if not candidates:

0 commit comments

Comments
 (0)