Skip to content

Commit c1a25bf

Browse files
committed
fix(cleanup): remove the root temp dir when a transitive scan stops
The root graph's tracker only guards the graph run. Once it returns, the root's temp_dir_for_cleanup lives only in the local result, and scan() removes it only when _scan_skill returns. A Ctrl+C during a transitive child scan, or an error in the merge step, propagated out of _scan_transitive and left the root clone or extraction behind. Remove it in _scan_skill when the transitive phase raises; on success the merged result still carries the same path for the caller. Also accept a temp_dir_for_cleanup in TempDirTracker only when it names an existing, non-symlink directory with the skillspector_ prefix, since the value is later removed recursively, and add unit tests for the tracker's contract. Signed-off-by: kevin9327 <[email protected]>
1 parent e99aeb2 commit c1a25bf

4 files changed

Lines changed: 219 additions & 18 deletions

File tree

‎src/skillspector/cleanup.py‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ def remove_temp_tree(path: str | Path) -> None:
4747
shutil.rmtree(path, onexc=_retry_writable)
4848

4949

50+
SCAN_TEMP_DIR_PREFIX = "skillspector_"
51+
52+
53+
def _is_scan_temp_dir(value: object) -> bool:
54+
"""Return whether ``value`` names an existing, non-symlink scan temp directory."""
55+
if not isinstance(value, str) or not value:
56+
return False
57+
path = Path(value)
58+
if not path.name.startswith(SCAN_TEMP_DIR_PREFIX):
59+
return False
60+
try:
61+
return path.is_dir() and not path.is_symlink() and not os.path.isjunction(path)
62+
except OSError:
63+
return False
64+
65+
5066
class TempDirTracker(BaseCallbackHandler):
5167
"""Remember the temp directory a graph run materializes, as soon as it exists.
5268
@@ -63,10 +79,17 @@ def __init__(self) -> None:
6379
self.temp_dir: str | None = None
6480

6581
def on_chain_end(self, outputs: Any, **kwargs: Any) -> None:
66-
"""Record ``temp_dir_for_cleanup`` from a node or graph output."""
82+
"""Record ``temp_dir_for_cleanup`` from a node or graph output.
83+
84+
The recorded path is later removed recursively, so only a value that
85+
looks like a scan temp dir is accepted: an existing directory, not a
86+
symlink, whose name carries the ``skillspector_`` prefix that
87+
``InputHandler`` gives ``mkdtemp``. Anything else is ignored and never
88+
replaces a path already recorded.
89+
"""
6790
if isinstance(outputs, Mapping):
6891
temp_dir = outputs.get("temp_dir_for_cleanup")
69-
if isinstance(temp_dir, str) and temp_dir:
92+
if _is_scan_temp_dir(temp_dir):
7093
self.temp_dir = temp_dir
7194

7295
def remove(self) -> None:

‎src/skillspector/cli.py‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,21 +2406,29 @@ def _scan_skill(
24062406
active_visited.add(transitive.canonicalize_source_identity(input_path))
24072407
except ValueError:
24082408
pass
2409-
return _scan_transitive(
2410-
initial_result=result,
2411-
format=format,
2412-
no_llm=no_llm,
2413-
max_depth=transitive_depth,
2414-
transitive_allow_prefix=transitive_allow_prefix,
2415-
transitive_deny_prefix=transitive_deny_prefix,
2416-
baseline=baseline,
2417-
show_suppressed=show_suppressed,
2418-
visited=active_visited,
2419-
scan_cache=transitive_cache,
2420-
yara_dir=yara_dir,
2421-
traversal=transitive_traversal,
2422-
source_local_only=source_local_only,
2423-
)
2409+
# The root graph has returned, so its tracker no longer guards the root's
2410+
# temp dir. If the transitive phase is interrupted or raises, nothing is
2411+
# returned for the caller's cleanup_result, so remove it here. On success
2412+
# the merged result carries the same temp_dir_for_cleanup for the caller.
2413+
try:
2414+
return _scan_transitive(
2415+
initial_result=result,
2416+
format=format,
2417+
no_llm=no_llm,
2418+
max_depth=transitive_depth,
2419+
transitive_allow_prefix=transitive_allow_prefix,
2420+
transitive_deny_prefix=transitive_deny_prefix,
2421+
baseline=baseline,
2422+
show_suppressed=show_suppressed,
2423+
visited=active_visited,
2424+
scan_cache=transitive_cache,
2425+
yara_dir=yara_dir,
2426+
traversal=transitive_traversal,
2427+
source_local_only=source_local_only,
2428+
)
2429+
except BaseException:
2430+
cleanup_result(result)
2431+
raise
24242432

24252433

24262434
def _multi_skill_public_record_count(result: dict[str, object]) -> int:

‎tests/unit/test_cleanup.py‎

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import pytest
1313

14-
from skillspector.cleanup import _retry_writable, cleanup_result
14+
from skillspector.cleanup import TempDirTracker, _retry_writable, cleanup_result
1515
from skillspector.input_handler import InputHandler
1616

1717

@@ -163,3 +163,111 @@ def test_input_handler_cleanup_removes_read_only_git_objects(
163163

164164
assert not temp_dir.exists()
165165
assert handler.temp_dir_for_cleanup() is None
166+
167+
168+
def _scan_temp_dir(root: Path) -> Path:
169+
"""Create a directory named like the ones ``InputHandler`` materializes."""
170+
path = root / "skillspector_abc123"
171+
path.mkdir()
172+
(path / "SKILL.md").write_text("# Skill\n", encoding="utf-8")
173+
return path
174+
175+
176+
def test_tracker_records_a_scan_temp_dir(tmp_path: Path) -> None:
177+
"""A node output naming an existing skillspector_ directory is recorded."""
178+
temp_dir = _scan_temp_dir(tmp_path)
179+
tracker = TempDirTracker()
180+
181+
tracker.on_chain_end({"temp_dir_for_cleanup": str(temp_dir)})
182+
183+
assert tracker.temp_dir == str(temp_dir)
184+
185+
186+
@pytest.mark.parametrize("later", [None, "", 42, ["x"]])
187+
def test_tracker_keeps_recorded_path_when_a_later_output_has_none(
188+
tmp_path: Path, later: object
189+
) -> None:
190+
"""A later output without a usable path does not erase the recorded one."""
191+
temp_dir = _scan_temp_dir(tmp_path)
192+
tracker = TempDirTracker()
193+
tracker.on_chain_end({"temp_dir_for_cleanup": str(temp_dir)})
194+
195+
tracker.on_chain_end({"temp_dir_for_cleanup": later})
196+
tracker.on_chain_end({"other": "value"})
197+
tracker.on_chain_end("not a mapping")
198+
199+
assert tracker.temp_dir == str(temp_dir)
200+
201+
202+
def test_tracker_ignores_paths_that_are_not_scan_temp_dirs(tmp_path: Path) -> None:
203+
"""Only an existing, non-symlink skillspector_ directory can become a deletion target."""
204+
user_dir = tmp_path / "my-skill"
205+
user_dir.mkdir()
206+
missing = tmp_path / "skillspector_missing"
207+
a_file = tmp_path / "skillspector_file"
208+
a_file.write_text("x", encoding="utf-8")
209+
tracker = TempDirTracker()
210+
211+
for candidate in (user_dir, missing, a_file):
212+
tracker.on_chain_end({"temp_dir_for_cleanup": str(candidate)})
213+
214+
assert tracker.temp_dir is None
215+
assert user_dir.exists()
216+
217+
218+
@pytest.mark.skipif(not hasattr(os, "symlink"), reason="symlinks unavailable")
219+
def test_tracker_ignores_a_symlink_named_like_a_scan_temp_dir(tmp_path: Path) -> None:
220+
"""A symlink is never recorded, even with the scan prefix and a directory target."""
221+
target = tmp_path / "keep"
222+
target.mkdir()
223+
link = tmp_path / "skillspector_link"
224+
try:
225+
link.symlink_to(target, target_is_directory=True)
226+
except OSError:
227+
pytest.skip("cannot create symlinks here")
228+
tracker = TempDirTracker()
229+
230+
tracker.on_chain_end({"temp_dir_for_cleanup": str(link)})
231+
232+
assert tracker.temp_dir is None
233+
234+
235+
def test_tracker_remove_twice_is_a_no_op(tmp_path: Path) -> None:
236+
"""Removing an already-removed directory does not raise."""
237+
temp_dir = _scan_temp_dir(tmp_path)
238+
tracker = TempDirTracker()
239+
tracker.on_chain_end({"temp_dir_for_cleanup": str(temp_dir)})
240+
241+
tracker.remove()
242+
tracker.remove()
243+
244+
assert not temp_dir.exists()
245+
246+
247+
@pytest.mark.parametrize("error", [KeyboardInterrupt, RuntimeError])
248+
def test_removing_on_error_reraises_the_original_exception(
249+
tmp_path: Path, error: type[BaseException]
250+
) -> None:
251+
"""The wrapped run's exception propagates unchanged after the directory is removed."""
252+
temp_dir = _scan_temp_dir(tmp_path)
253+
tracker = TempDirTracker()
254+
original = error("stopped")
255+
256+
with pytest.raises(error) as raised:
257+
with tracker.removing_on_error():
258+
tracker.on_chain_end({"temp_dir_for_cleanup": str(temp_dir)})
259+
raise original
260+
261+
assert raised.value is original
262+
assert not temp_dir.exists()
263+
264+
265+
def test_removing_on_error_leaves_the_directory_on_success(tmp_path: Path) -> None:
266+
"""A run that completes leaves removal to the caller's cleanup_result."""
267+
temp_dir = _scan_temp_dir(tmp_path)
268+
tracker = TempDirTracker()
269+
270+
with tracker.removing_on_error():
271+
tracker.on_chain_end({"temp_dir_for_cleanup": str(temp_dir)})
272+
273+
assert temp_dir.exists()

‎tests/unit/test_cli.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,68 @@ def test_scan_that_stops_early_removes_its_temp_dir(
223223
assert not any(path.exists() for path in created)
224224

225225

226+
@pytest.mark.parametrize("error", [KeyboardInterrupt, RuntimeError])
227+
def test_transitive_scan_stopped_in_a_child_removes_the_root_temp_dir(
228+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, error: type[BaseException]
229+
) -> None:
230+
"""A --transitive scan of a zipped root that stops while a child is scanning removes the root's temp dir."""
231+
archive = tmp_path / "skill.zip"
232+
with zipfile.ZipFile(archive, "w") as bundle:
233+
bundle.writestr(
234+
"SKILL.md",
235+
"---\nname: demo\ndescription: d\n---\n# Demo\n\n"
236+
"Install the helper skill from https://github.com/org/dep.git first.\n",
237+
)
238+
created: list[Path] = []
239+
real_mkdtemp = tempfile.mkdtemp
240+
241+
def recording_mkdtemp(*args: Any, **kwargs: Any) -> str:
242+
"""Create scan temp dirs under tmp_path and remember them."""
243+
if kwargs.get("prefix") != "skillspector_":
244+
return real_mkdtemp(*args, **kwargs)
245+
path = real_mkdtemp(*args, **{**kwargs, "dir": tmp_path})
246+
created.append(Path(path))
247+
return path
248+
249+
real_scan_for_source = cli._run_graph_scan_for_source
250+
child_inputs: list[str] = []
251+
252+
def scan_for_source(**kwargs: Any) -> dict[str, object]:
253+
"""Run the root scan for real and stop the first transitive child."""
254+
if kwargs["input_path"] == str(archive):
255+
return real_scan_for_source(**kwargs)
256+
child_inputs.append(kwargs["input_path"])
257+
raise error("child scan stopped")
258+
259+
monkeypatch.setattr("skillspector.input_handler.tempfile.mkdtemp", recording_mkdtemp)
260+
monkeypatch.setattr(cli, "_run_graph_scan_for_source", scan_for_source)
261+
if error is RuntimeError:
262+
# A child failure is recorded as a warning; make the merge step raise instead.
263+
def stop_merge(*args: Any, **kwargs: Any) -> None:
264+
raise error("merge stopped")
265+
266+
monkeypatch.setattr(cli, "_ensure_required_failure_events", stop_merge)
267+
268+
with pytest.raises(error):
269+
cli._scan_skill(
270+
input_path=str(archive),
271+
format=FormatChoice.json,
272+
no_llm=True,
273+
baseline=None,
274+
yara_rules_dir=None,
275+
verbose=False,
276+
show_suppressed=False,
277+
transitive_enabled=True,
278+
transitive_depth=1,
279+
transitive_allow_prefix=None,
280+
transitive_deny_prefix=None,
281+
)
282+
283+
assert child_inputs, "the root should have reached a transitive child scan"
284+
assert created
285+
assert not any(path.exists() for path in created)
286+
287+
226288
@pytest.mark.parametrize(
227289
("terminal", "verbose", "expected"),
228290
[(True, False, True), (False, False, False), (True, True, False)],

0 commit comments

Comments
 (0)