Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed
Comment thread
antonwolfy marked this conversation as resolved.
Resolved

- **`repo` commands refuse a `--cache-dir` that is not a usable directory with exit 2 and a message naming the flag and the path** — a regular file, or a path below one, used to end in `✗ [Errno 17] File exists` and exit 1 (a failed run in `--json`) after the command had started; the value is now checked before any clone, as `skill shorten` and `skill novelty analyze` do. A bad `--cache-dir` next to a local-directory source is refused too, where it used to be ignored. An existing read-only directory still passes, so a populated cache replays; an empty read-only one fails at the clone with exit 1.
45 changes: 45 additions & 0 deletions dokimi/dokimi/commands/cache_dir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright (C) 2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

"""The up-front check of a ``--cache-dir`` value, shared by the novelty and repo commands (#701, #765)."""

from __future__ import annotations

import os
from pathlib import Path

RETENTION_USE = "the retention verdicts can be stored in"
CLONE_USE = "the repository clones can be cached in"


def usable_cache_dir(raw: str | None, use: str = RETENTION_USE) -> Path | None:
"""``--cache-dir`` as a path a cache can store into, or ``None`` when unset.

A directory that does not exist yet is fine (created on the first store) when an existing,
writable directory is above it; one that exists must be a readable directory, writable or
not (a fully cached run replays from it). Anything else raises ``InputError`` before a paid
call or a clone (#701, #765). ``use`` closes the message: "pass a directory <use>".
"""
from dokimi.utils import InputError

if not raw:
return None
path = Path(raw)
probe = path
try:
while not probe.exists() and not probe.is_symlink() and probe != probe.parent:
probe = probe.parent
is_dir = probe.is_dir()
except OSError as exc:
# An ancestor without search permission: exists()/is_dir() raise PermissionError (Python < 3.14).
raise InputError(f"--cache-dir {path}: cannot inspect {probe} ({exc.strerror or type(exc).__name__}); "
f"pass a directory {use}") from exc
if not is_dir:
where = "is not a directory" if probe == path else f"has {probe} above it, which is not a directory"
raise InputError(f"--cache-dir {path} {where}; pass a directory {use}")
# An existing directory only has to be readable: a fully cached run replays from a read-only
# directory and writes nothing. A missing one is created on the first store, so it needs to be writable.
needed, word = (os.R_OK | os.X_OK, "readable") if probe == path else (os.W_OK | os.X_OK, "writable")
if not os.access(probe, needed):
Comment thread
antonwolfy marked this conversation as resolved.
Resolved
raise InputError(f"--cache-dir {path}: {probe} is not {word}; pass a directory {use}")
return path
37 changes: 1 addition & 36 deletions dokimi/dokimi/commands/novelty_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
import argparse
import functools
import math
import os
import sys
from pathlib import Path
from dokimi.commands.cache_dir import usable_cache_dir # noqa: F401 - re-exported for the novelty commands


def stops_at_cap(command):
Expand All @@ -30,40 +29,6 @@ def run(args: argparse.Namespace) -> None:
return run


def usable_cache_dir(raw: str | None) -> Path | None:
"""``--cache-dir`` as a path the retention cache can store into, or ``None`` when unset.

A directory that does not exist yet is fine (created on the first store) when an existing,
writable directory is above it; one that exists must be a readable directory, writable or
not (the oracle skips a store it cannot make). Anything else raises ``InputError`` before a
paid call (#701).
"""
from dokimi.utils import InputError

if not raw:
return None
path = Path(raw)
probe = path
try:
while not probe.exists() and not probe.is_symlink() and probe != probe.parent:
probe = probe.parent
is_dir = probe.is_dir()
except OSError as exc:
# An ancestor without search permission: exists()/is_dir() raise PermissionError (Python < 3.14).
raise InputError(f"--cache-dir {path}: cannot inspect {probe} ({exc.strerror or type(exc).__name__}); "
"pass a directory the retention verdicts can be stored in") from exc
if not is_dir:
where = "is not a directory" if probe == path else f"has {probe} above it, which is not a directory"
raise InputError(f"--cache-dir {path} {where}; pass a directory the retention verdicts can be stored in")
# An existing directory only has to be readable: a fully cached run replays from a read-only
# directory and writes nothing. A missing one is created on the first store, so it needs to be writable.
needed, word = (os.R_OK | os.X_OK, "readable") if probe == path else (os.W_OK | os.X_OK, "writable")
if not os.access(probe, needed):
raise InputError(f"--cache-dir {path}: {probe} is not {word}; pass a directory the retention "
"verdicts can be stored in")
return path


def exit_if_analyze_partial(result, out) -> None:
"""Save and exit when the cap stopped ``analyze`` (``result.partial``); a finished run returns."""
from dokimi.commands.novelty_budget import save_analyze_partial
Expand Down
73 changes: 31 additions & 42 deletions dokimi/dokimi/commands/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,41 @@ def _write_json(path: str, payload: dict) -> None:


def _parse_source(args: argparse.Namespace) -> RepoSource:
"""The source as typed: a malformed string, an unpinned remote or a local path that is not a directory is a usage error (2); a local path the filesystem refuses to stat (no permission to traverse it) is 1."""
"""The source as typed: a malformed string, an unpinned remote, a local path that is not a directory or a ``--cache-dir`` that is not a usable directory is a usage error (2); a local path the filesystem refuses to stat (no permission to traverse it) is 1."""
from dokimi.repos import RepoSource, SourceError
Comment thread
antonwolfy marked this conversation as resolved.
Resolved

try:
return RepoSource.parse(args.source)
source = RepoSource.parse(args.source)
except SourceError as exc:
raise InputError(str(exc)) from exc
except OSError as exc: # Path.is_dir raises PermissionError on Python 3.11-3.13
print(f"✗ {exc}", file=sys.stderr)
sys.exit(1)
_cache_dir(args) # a bad --cache-dir is a usage error too, and must not reach a handler of the clone's failures
return source


def _source_tree(args: argparse.Namespace) -> tuple:
"""The source (source, tree): a malformed string, unpinned remote or pinned local path that is not the repository root is a usage error (2); a clone, fetch or pin resolution failure is 1."""
def _open_tree(source: RepoSource, args: argparse.Namespace):
"""The source's tree, cloned into ``--cache-dir`` when remote: a bad ``--cache-dir`` or a pinned local path that is not the repository root is a usage error (2); a clone, fetch or pin resolution failure is 1."""
from dokimi.repos import SourceError, open_source
from dokimi.repos.source import SourceArgumentError

source = _parse_source(args)
# 2 = malformed source string, unpinned remote, pinned local path inside a repository, manifest/profile errors; 1 = clone/fetch/resolve of a pin
# (incl. pinned local non-git source, pin absent locally, remote pin missing from origin).
cache_dir = _cache_dir(args) # before the handlers below: InputError is a ValueError
try:
return source, open_source(source, _cache_dir(args))
except SourceArgumentError as exc: # a pinned local path that is not the repository root
return open_source(source, cache_dir)
Comment thread
antonwolfy marked this conversation as resolved.
Resolved
except SourceArgumentError as exc:
raise InputError(str(exc)) from exc
except SourceError as exc:
print(f"✗ {exc}", file=sys.stderr)
sys.exit(1)
except (OSError, ValueError) as exc:
# A cache dir that cannot be made, or git output that is not UTF-8: a failed read, not a usage error.
except (SourceError, OSError, ValueError) as exc: # a cache dir that cannot be made, or git output that is not UTF-8
print(f"✗ {exc}", file=sys.stderr)
sys.exit(1)


def _source_tree(args: argparse.Namespace) -> tuple:
"""The source (source, tree): a malformed string, unpinned remote or pinned local path that is not the repository root is a usage error (2); a clone, fetch or pin resolution failure is 1."""
source = _parse_source(args)
return source, _open_tree(source, args)


def _manifest_or_exit(tree, args: argparse.Namespace, *, guidance_default: bool = False,
also_usage: tuple = ()) -> Any:
"""The manifest of the tree. A bad manifest or a manifest path that leaves the tree is a usage error
Expand Down Expand Up @@ -249,22 +251,16 @@ def cmd_repo_inspect(args: argparse.Namespace) -> None:

def cmd_repo_fetch(args: argparse.Namespace) -> None:
"""Clone (once) and fetch a pinned remote source into the cache; print the SHA."""
from dokimi.repos import RepoSource, SourceError, open_source
from dokimi.repos.source import GitTree, SourceArgumentError
from dokimi.repos import RepoSource, SourceError
from dokimi.repos.source import GitTree

try: # the source as typed: a usage error
source = RepoSource.parse(args.source)
if source.rev is None:
raise SourceError("fetch needs a pinned source: <url-or-path>@<rev>")
except SourceError as exc:
raise InputError(str(exc)) from exc
try: # the clone and fetch: a runtime failure (network, git, unknown revision)
tree = open_source(source, _cache_dir(args))
except SourceArgumentError as exc: # a pinned local path that is not the repository root
raise InputError(str(exc)) from exc
except (SourceError, OSError, ValueError) as exc:
print(f"✗ {exc}", file=sys.stderr)
sys.exit(1)
tree = _open_tree(source, args)
assert isinstance(tree, GitTree)
if args.json:
cli_output.emit_leaf("repo fetch", {"sha": tree.sha, "git_dir": str(tree.git_dir)})
Expand Down Expand Up @@ -320,23 +316,16 @@ def _saved_reach_inputs(args: argparse.Namespace):

def _reach_from_saved(args: argparse.Namespace) -> None:
"""The matrix or the receipt of a saved reach.json, checked against the source; no harness runs."""
from dokimi.repos import ManifestError, SourceError, open_source
from dokimi.repos import ManifestError, SourceError
from dokimi.repos.harness_profile import ProfileError
from dokimi.repos.receipt import check_receipt_provenance, check_saved_guidance, check_saved_reach
from dokimi.repos.reach import ReachError, format_matrix
from dokimi.repos.source import SourceArgumentError

try: # the arguments and the saved file as typed: a usage error
profile, source, reach = _saved_reach_inputs(args)
except (SourceError, ProfileError, ReachError, OSError, ValueError) as exc:
raise InputError(str(exc)) from exc
try: # the clone and fetch: a runtime failure
tree = open_source(source, _cache_dir(args))
except SourceArgumentError as exc: # a pinned local path that is not the repository root
raise InputError(str(exc)) from exc
except (SourceError, OSError, ValueError) as exc:
print(f"✗ {exc}", file=sys.stderr)
sys.exit(1)
tree = _open_tree(source, args)
try: # the manifest and the file's provenance: a usage error; a failed git read is a fetch failure (1)
manifest = _load_manifest(tree, args, guidance_default=True)
check_saved_reach(reach, manifest.id, profile.name, getattr(tree, "sha", None))
Expand Down Expand Up @@ -426,9 +415,8 @@ def _reach_plan(args: argparse.Namespace, tree, profile, kinds: tuple[str, ...],

def cmd_repo_reach(args: argparse.Namespace) -> None:
"""L1 reach probe: which guidance files one harness/version/model loads."""
from dokimi.repos import RepoSource, SourceError, open_source
from dokimi.repos import RepoSource, SourceError
from dokimi.repos.harness_profile import ProfileError, load_profile
from dokimi.repos.source import SourceArgumentError
from dokimi.repos.reach import ReachError, check_flags, check_kinds, check_shadow, run_reach, save_reach

kinds = tuple(k.strip() for k in args.scenarios.split(",") if k.strip())
Expand All @@ -441,13 +429,7 @@ def cmd_repo_reach(args: argparse.Namespace) -> None:
source = RepoSource.parse(args.source)
except (SourceError, ProfileError, ReachError) as exc:
raise InputError(str(exc)) from exc
try: # the clone and fetch: a runtime failure
tree = open_source(source, _cache_dir(args))
except SourceArgumentError as exc: # a pinned local path that is not the repository root
raise InputError(str(exc)) from exc
except (SourceError, OSError, ValueError) as exc:
print(f"✗ {exc}", file=sys.stderr)
sys.exit(1)
tree = _open_tree(source, args)
manifest, files, planned_count = _reach_plan(args, tree, profile, kinds, only)
if not args.dry_run:
print(f"{len(files)} guidance files, {planned_count} scenarios, "
Expand Down Expand Up @@ -621,7 +603,14 @@ def _manifest_id(label: str) -> str:


def _cache_dir(args: argparse.Namespace) -> Path | None:
return Path(args.cache_dir) if args.cache_dir else None
"""``--cache-dir`` as given, or ``None``; a value no clone can be cached in is a usage error (2) (#765).

An existing read-only directory passes on purpose: a populated one replays without writing. An empty one
is not told apart without the clone URL, so its clone fails later with exit 1.
"""
from dokimi.commands.cache_dir import CLONE_USE, usable_cache_dir

return usable_cache_dir(args.cache_dir, CLONE_USE)


def register(sub, positive_int, eval_sub) -> None:
Expand Down
127 changes: 127 additions & 0 deletions dokimi/tests/test_repo_cache_dir_usable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Copyright (C) 2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

"""`--cache-dir` of the `repo` commands is checked before any clone (#765): a bad value exits 2 naming the flag."""

from __future__ import annotations

import argparse
import json
import subprocess

import pytest

from dokimi import cli
from dokimi.commands.repo import _cache_dir
from tests.cli_refusal import refusal

PIN = "HEAD"


def _argv(monkeypatch, *argv):
monkeypatch.setattr("sys.argv", ["dokimi", *argv])


def _source(tmp_path) -> str:
return f"file://{tmp_path}/nothere@{PIN}"


def _commands(tmp_path):
return {
"fetch": ["repo", "fetch", _source(tmp_path)],
"scan": ["repo", "scan", _source(tmp_path)],
"reach": ["repo", "reach", _source(tmp_path), "--model", "m"],
}


@pytest.mark.parametrize("command", ["fetch", "scan", "reach"])
def test_repo_command_refuses_a_cache_dir_that_is_a_regular_file(command, tmp_path, monkeypatch, capsys):
cache = tmp_path / "cachefile"
cache.write_text("x", encoding="utf-8")
_argv(monkeypatch, *_commands(tmp_path)[command], "--cache-dir", str(cache))
with refusal() as exc:
cli.main()
err = capsys.readouterr().err
assert exc.code == 2
assert f"--cache-dir {cache} is not a directory; pass a directory the repository clones can be cached in" in err
assert "File exists" not in err and cache.read_text(encoding="utf-8") == "x"


@pytest.mark.parametrize("kind", ["check", "replay"])
def test_eval_commands_that_clone_refuse_a_cache_dir_that_is_a_regular_file(kind, tmp_path, monkeypatch, capsys):
threads = tmp_path / "threads.json"
threads.write_text("[]", encoding="utf-8")
cache = tmp_path / "cachefile"
cache.write_text("x", encoding="utf-8")
argv = {"check": ["eval", "check", _source(tmp_path)],
"replay": ["eval", "tasks", "generate", "--from", "replay", _source(tmp_path), "--threads", str(threads),
"--out-dir", str(tmp_path / "out")]}[kind]
_argv(monkeypatch, *argv, "--cache-dir", str(cache))
with refusal() as exc:
cli.main()
assert exc.code == 2
assert f"--cache-dir {cache} is not a directory" in capsys.readouterr().err


def test_repo_fetch_refuses_a_child_of_a_regular_file_and_makes_nothing(tmp_path, monkeypatch, capsys):
blocker = tmp_path / "file"
blocker.write_text("x", encoding="utf-8")
_argv(monkeypatch, *_commands(tmp_path)["fetch"], "--cache-dir", str(blocker / "cache"))
with refusal() as exc:
cli.main()
err = capsys.readouterr().err
assert exc.code == 2
assert f"--cache-dir {blocker / 'cache'} has {blocker} above it, which is not a directory" in err
assert sorted(p.name for p in tmp_path.iterdir()) == ["file"]


def test_repo_fetch_json_reports_the_bad_flag_as_input_not_a_failed_run(tmp_path, monkeypatch, capsys):
cache = tmp_path / "cachefile"
cache.write_text("x", encoding="utf-8")
_argv(monkeypatch, "repo", "fetch", _source(tmp_path), "--cache-dir", str(cache), "--json")
with refusal() as exc:
cli.main()
document = json.loads(capsys.readouterr().out)
assert exc.code == 2
assert document["status"] == "failed"
assert f"--cache-dir {cache} is not a directory" in json.dumps(document)
assert "exit 1: see stderr" not in json.dumps(document)


def test_repo_scan_of_a_local_directory_also_refuses_a_bad_cache_dir(tmp_path, monkeypatch, capsys):
repo = tmp_path / "repo"
repo.mkdir()
cache = tmp_path / "cachefile"
cache.write_text("x", encoding="utf-8")
_argv(monkeypatch, "repo", "scan", str(repo), "--cache-dir", str(cache))
with refusal() as exc:
cli.main()
assert exc.code == 2
assert f"--cache-dir {cache} is not a directory" in capsys.readouterr().err


def test_cache_dir_passes_unset_an_existing_directory_and_a_missing_child_unchanged(tmp_path):
def args(value):
return argparse.Namespace(cache_dir=value)

assert _cache_dir(args(None)) is None
assert _cache_dir(args("")) is None
assert _cache_dir(args(str(tmp_path))) == tmp_path
assert _cache_dir(args(str(tmp_path / "new" / "deeper"))) == tmp_path / "new" / "deeper"
assert not (tmp_path / "new").exists()


def test_repo_fetch_prints_the_sha_of_a_pinned_source_in_a_writable_cache_dir(tmp_path, monkeypatch, capsys):
src = tmp_path / "src"
src.mkdir()
(src / "a.txt").write_text("a", encoding="utf-8")
git = ["git", "-C", str(src), "-c", "user.name=t", "-c", "user.email=t@example.com"]
subprocess.run([*git, "init", "-q"], check=True)
subprocess.run([*git, "add", "-A"], check=True)
subprocess.run([*git, "commit", "-qm", "init"], check=True)
sha = subprocess.run([*git, "rev-parse", "HEAD"], check=True, capture_output=True, text=True).stdout.strip()
cache = tmp_path / "cache"
cache.mkdir()
_argv(monkeypatch, "repo", "fetch", f"file://{src}@{sha}", "--cache-dir", str(cache))
cli.main()
assert capsys.readouterr().out.startswith(f"{sha}\t")
Loading