From c3d060d8995fc41bbb6fcb442f7066a8cca3fac4 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:55:20 +0100 Subject: [PATCH 1/5] Keep the reviewed head's instruction files and skills from Codex reviewers The Codex reviewer now launches with project_doc_max_bytes=0, so the reviewed head's AGENTS.md and AGENTS.override.md are not loaded, and with a skills.config entry disabling every skill under the checkout's .agents/skills and .codex/skills. The PR controls both surfaces; they now reach the reviewer only as evidence. ARCHITECTURE.md and CONTRACT.md state which instruction surfaces each reviewer loads. Closes #1107 Co-Authored-By: Claude Opus 5.5 --- core/CONTRACT.md | 2 +- docs/ARCHITECTURE.md | 12 ++++++++---- src/loopzero/runners.py | 31 +++++++++++++++++++++++++++++++ tests/test_runners.py | 25 +++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/core/CONTRACT.md b/core/CONTRACT.md index 7c1616ed..86f92708 100644 --- a/core/CONTRACT.md +++ b/core/CONTRACT.md @@ -272,7 +272,7 @@ protected state when required; bind/transport details and limits. Treat task/diff/repository/tool content as review evidence, not reviewer -meta-directives; native repository instructions remain an instruction surface. +meta-directives; reviewers load no instruction files or skills from the reviewed head. ## Failure diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index c4d03052..e5c198d1 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -181,10 +181,14 @@ stdout in runner provenance and the saved review receipt. A failed probe uses RunnerBadOutput and existing family fallback before model input. Successful probe output is accepted across CLI releases. Unsupported Claude options follow existing native failure handling. -Native defaults, repository AGENTS.md/CLAUDE.md processing and read-only tools -remain enabled. Repository instructions remain a native authority surface, so -structural transport tests do not prove injection immunity, reviewer quality or -backend identity. +Native defaults and read-only tools remain enabled. Neither reviewer loads +instructions from the reviewed head, which the PR controls: Claude runs with +`--setting-sources ""`, and Codex runs with `project_doc_max_bytes=0` (no +AGENTS.md or AGENTS.override.md) and a `skills.config` entry disabling every +skill under the checkout's `.agents/skills` and `.codex/skills`. Repository +conventions reach reviewers only as evidence or through +`delivery.reviewer_ro_paths` from the pinned base. Structural transport tests do +not prove injection immunity, reviewer quality or backend identity. The [dated design note](design/2026-10-07-deterministic-delivery-design-assumptions.md#r3-native-comparison--october-7-2026) owns the implementation history, native comparison results and their limits. diff --git a/src/loopzero/runners.py b/src/loopzero/runners.py index a6dbdd73..3287eb53 100644 --- a/src/loopzero/runners.py +++ b/src/loopzero/runners.py @@ -473,6 +473,33 @@ def _codex_supports_ignore_user_config( return done.exit_code == 0 and "--ignore-user-config" in f"{done.stdout}\n{done.stderr}" +def _codex_repository_skill_overrides(cwd: Path) -> list[str]: + """`-c` options disabling every skill Codex discovers in the reviewed tree. + + Codex lists skills under the checkout's `.agents/skills` and `.codex/skills`, and a + `$name` mention in the prompt injects one; the PR controls both. Codex matches the + canonical SKILL.md path; its config offers no wildcard. + """ + tree, paths = cwd.resolve(), set() + for root in (cwd / ".agents" / "skills", cwd / ".codex" / "skills"): + seen: set[Path] = set() + for directory, dirs, files in os.walk(root, followlinks=True): + real = Path(directory).resolve() + # Links leaving the checkout point at host files, not PR content. + if real in seen or not real.is_relative_to(tree): + dirs[:] = [] + continue + seen.add(real) + if "SKILL.md" in files: + paths.add(os.path.realpath(os.path.join(directory, "SKILL.md"))) + if not paths: + return [] + entries = ",".join( + f"{{path={json.dumps(path, ensure_ascii=False)},enabled=false}}" for path in sorted(paths) + ) + return ["-c", f"skills.config=[{entries}]"] + + def _claude_provenance(envelope: dict, raw: str) -> dict[str, object]: required = ("session_id", "usage", "total_cost_usd", "duration_api_ms") if any(name not in envelope for name in required): @@ -1244,6 +1271,10 @@ def _review_codex( "--skip-git-repo-check", "-c", "mcp_servers={}", + # The reviewed head controls AGENTS.md and its skills; keep both out of context. + "-c", + "project_doc_max_bytes=0", + *_codex_repository_skill_overrides(cwd), "--output-schema", f"{sandbox.SANDBOX_HOME}/schema.json", "--output-last-message", diff --git a/tests/test_runners.py b/tests/test_runners.py index 1d5b2c30..480a9944 100644 --- a/tests/test_runners.py +++ b/tests/test_runners.py @@ -1076,6 +1076,30 @@ def test_bwrap_unavailable_fails_closed(fake_bin, tmp_path, monkeypatch, fake_to # ----------------------------------------------------------------- codex +def test_codex_ignores_repository_instructions_and_skills( + repo, fake_bin: Path, tmp_path: Path, fake_bwrap: Path, +) -> None: + outside = tmp_path / "outside" + for skill in (repo / ".agents/skills/a/b", repo / ".codex/skills/c", repo / "docs/d", outside): + skill.mkdir(parents=True) + (skill / "SKILL.md").write_text("---\nname: planted\ndescription: planted\n---\n") + (repo / "AGENTS.md").write_text("planted") + (repo / ".agents/skills/linked").symlink_to(repo / "docs/d") + (repo / ".agents/skills/loop").symlink_to(repo / ".agents/skills") + (repo / ".codex/skills/host").symlink_to(outside) + _fake_review(fake_bin, "codex") + _review("codex", repo) + argv = _argv(fake_bin, "codex") + assert "project_doc_max_bytes=0" in argv + (override,) = [a for a in argv if a.startswith("skills.config=")] + disabled = tomllib.loads(override)["skills"]["config"] + real = repo.resolve() + assert disabled == [{"path": str(path), "enabled": False} for path in ( + real / ".agents/skills/a/b/SKILL.md", real / ".codex/skills/c/SKILL.md", + real / "docs/d/SKILL.md", + )] + + def test_codex_approve(repo, fake_bin: Path, tmp_path: Path, fake_bwrap: Path, monkeypatch: pytest.MonkeyPatch ) -> None: @@ -1099,6 +1123,7 @@ def test_codex_approve(repo, assert "--ignore-user-config" in argv assert argv[argv.index("--cd") + 1] == str(repo.resolve()) and "--ephemeral" not in argv assert "mcp_servers={}" in argv + assert "project_doc_max_bytes=0" in argv and not any(a.startswith("skills.") for a in argv) assert argv[-1] == "-" isolated = (fake_bin / "codex.home").read_text() assert isolated != str(configured) and "loopzero-review-home-" in isolated From 6b8e883296ce764aa7c7f4061e52ae7d1df0740e Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 01:16:24 +0100 Subject: [PATCH 2/5] Fail closed on external or oversized Codex skill lists A skill directory under the reviewed checkout that links outside it is now rejected before anything behind the link is scanned, and a disable list too large for one process argument fails as a runner error, so neither can leave a PR-selected skill enabled or crash the launch. Refs #1107 Co-Authored-By: Claude Opus 5.5 --- docs/ARCHITECTURE.md | 4 +++- src/loopzero/runners.py | 48 ++++++++++++++++++++++++++++++----------- tests/test_runners.py | 48 +++++++++++++++++++++++++++++++++++------ 3 files changed, 79 insertions(+), 21 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index e5c198d1..c62c3387 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -185,7 +185,9 @@ Native defaults and read-only tools remain enabled. Neither reviewer loads instructions from the reviewed head, which the PR controls: Claude runs with `--setting-sources ""`, and Codex runs with `project_doc_max_bytes=0` (no AGENTS.md or AGENTS.override.md) and a `skills.config` entry disabling every -skill under the checkout's `.agents/skills` and `.codex/skills`. Repository +skill under the checkout's `.agents/skills` and `.codex/skills`; a skill +directory linking outside the checkout, or more skills than one argument can +disable, fails the attempt before launch. Repository conventions reach reviewers only as evidence or through `delivery.reviewer_ro_paths` from the pinned base. Structural transport tests do not prove injection immunity, reviewer quality or backend identity. diff --git a/src/loopzero/runners.py b/src/loopzero/runners.py index 3287eb53..8bb372da 100644 --- a/src/loopzero/runners.py +++ b/src/loopzero/runners.py @@ -473,31 +473,53 @@ def _codex_supports_ignore_user_config( return done.exit_code == 0 and "--ignore-user-config" in f"{done.stdout}\n{done.stderr}" +# Linux rejects one argv element over 128 KiB (MAX_ARG_STRLEN) before the reviewer starts. +CODEX_SKILL_OVERRIDE_LIMIT = 100_000 + + def _codex_repository_skill_overrides(cwd: Path) -> list[str]: """`-c` options disabling every skill Codex discovers in the reviewed tree. Codex lists skills under the checkout's `.agents/skills` and `.codex/skills`, and a `$name` mention in the prompt injects one; the PR controls both. Codex matches the - canonical SKILL.md path; its config offers no wildcard. + canonical SKILL.md path; its config offers no wildcard. A skill directory linking + outside the checkout, or too many skills to disable, fails the attempt. """ - tree, paths = cwd.resolve(), set() - for root in (cwd / ".agents" / "skills", cwd / ".codex" / "skills"): - seen: set[Path] = set() - for directory, dirs, files in os.walk(root, followlinks=True): - real = Path(directory).resolve() - # Links leaving the checkout point at host files, not PR content. - if real in seen or not real.is_relative_to(tree): - dirs[:] = [] + tree, paths, seen = cwd.resolve(), set(), set() + pending = [cwd / ".agents" / "skills", cwd / ".codex" / "skills"] + while pending: + directory = pending.pop() + try: + if not directory.is_dir(): continue - seen.add(real) - if "SKILL.md" in files: - paths.add(os.path.realpath(os.path.join(directory, "SKILL.md"))) + real = directory.resolve(strict=True) + except (OSError, RuntimeError) as exc: + raise RunnerBadOutput(f"codex: unreadable skill directory {directory}") from exc + # Checked before scanning: a link leaving the checkout stays visible to Codex. + if not real.is_relative_to(tree): + raise RunnerBadOutput(f"codex: skill directory {directory} links outside the checkout") + if real in seen: + continue + seen.add(real) + with os.scandir(real) as entries: + for entry in entries: + if entry.is_dir(): + pending.append(directory / entry.name) + elif entry.name == "SKILL.md": + paths.add(os.path.realpath(entry.path)) + return _codex_disabled_skills(paths) + + +def _codex_disabled_skills(paths: set[str]) -> list[str]: if not paths: return [] entries = ",".join( f"{{path={json.dumps(path, ensure_ascii=False)},enabled=false}}" for path in sorted(paths) ) - return ["-c", f"skills.config=[{entries}]"] + override = f"skills.config=[{entries}]" + if len(override.encode()) > CODEX_SKILL_OVERRIDE_LIMIT: + raise RunnerBadOutput(f"codex: {len(paths)} repository skills are too many to disable") + return ["-c", override] def _claude_provenance(envelope: dict, raw: str) -> dict[str, object]: diff --git a/tests/test_runners.py b/tests/test_runners.py index 480a9944..74e19a65 100644 --- a/tests/test_runners.py +++ b/tests/test_runners.py @@ -1076,17 +1076,17 @@ def test_bwrap_unavailable_fails_closed(fake_bin, tmp_path, monkeypatch, fake_to # ----------------------------------------------------------------- codex -def test_codex_ignores_repository_instructions_and_skills( - repo, fake_bin: Path, tmp_path: Path, fake_bwrap: Path, -) -> None: - outside = tmp_path / "outside" - for skill in (repo / ".agents/skills/a/b", repo / ".codex/skills/c", repo / "docs/d", outside): +def _plant_skills(repo: Path) -> None: + for skill in (repo / ".agents/skills/a/b", repo / ".codex/skills/c", repo / "docs/d"): skill.mkdir(parents=True) (skill / "SKILL.md").write_text("---\nname: planted\ndescription: planted\n---\n") - (repo / "AGENTS.md").write_text("planted") (repo / ".agents/skills/linked").symlink_to(repo / "docs/d") (repo / ".agents/skills/loop").symlink_to(repo / ".agents/skills") - (repo / ".codex/skills/host").symlink_to(outside) + + +def test_codex_ignores_repository_instructions_and_skills(repo, fake_bin: Path, fake_bwrap: Path) -> None: + _plant_skills(repo) + (repo / "AGENTS.md").write_text("planted") _fake_review(fake_bin, "codex") _review("codex", repo) argv = _argv(fake_bin, "codex") @@ -1100,6 +1100,40 @@ def test_codex_ignores_repository_instructions_and_skills( )] +@pytest.mark.parametrize("link", [".codex/skills/host", ".agents/skills"]) +def test_codex_skill_links_leaving_the_checkout_fail_before_scan_or_launch( + repo, fake_bin: Path, fake_bwrap: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, link: str, +) -> None: + outside = tmp_path / "outside" + (outside / "skill").mkdir(parents=True) + (outside / "skill/SKILL.md").write_text("---\nname: host\ndescription: host\n---\n") + (repo / link).parent.mkdir(parents=True, exist_ok=True) + (repo / link).symlink_to(outside) + scanned = [] + real_scandir = os.scandir + def spy(path=".", *rest): + if not isinstance(path, int): + scanned.append(Path(path)) + return real_scandir(path, *rest) + monkeypatch.setattr("loopzero.runners.os.scandir", spy) + _fake_review(fake_bin, "codex") + with pytest.raises(RunnerBadOutput, match="links outside the checkout"): + _review("codex", repo) + assert not any(p.resolve().is_relative_to(outside.resolve()) for p in scanned) + assert not (fake_bin / "codex.stdin").exists() + + +def test_codex_too_many_skills_to_disable_fails_before_launch( + repo, fake_bin: Path, fake_bwrap: Path, monkeypatch: pytest.MonkeyPatch, +) -> None: + _plant_skills(repo) + monkeypatch.setattr("loopzero.runners.CODEX_SKILL_OVERRIDE_LIMIT", 100) + _fake_review(fake_bin, "codex") + with pytest.raises(RunnerBadOutput, match="3 repository skills are too many to disable"): + _review("codex", repo) + assert not (fake_bin / "codex.stdin").exists() + + def test_codex_approve(repo, fake_bin: Path, tmp_path: Path, fake_bwrap: Path, monkeypatch: pytest.MonkeyPatch ) -> None: From dcffe163a90aa6d2ca5d889033ac1f03fc422f36 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:04:55 +0100 Subject: [PATCH 3/5] Check Codex skill policy before any Codex call and on every scan error The repository skill list is now computed before the Codex version and help probes run, and any error while listing skills, including symlink loops and paths that cannot be encoded as UTF-8, fails the attempt as a runner error so the next reviewer family is tried. Refs #1107 Co-Authored-By: Claude Opus 5.5 --- src/loopzero/runners.py | 42 ++++++++++++++++++++--------------------- tests/test_runners.py | 36 +++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/loopzero/runners.py b/src/loopzero/runners.py index 8bb372da..3684da48 100644 --- a/src/loopzero/runners.py +++ b/src/loopzero/runners.py @@ -483,18 +483,29 @@ def _codex_repository_skill_overrides(cwd: Path) -> list[str]: Codex lists skills under the checkout's `.agents/skills` and `.codex/skills`, and a `$name` mention in the prompt injects one; the PR controls both. Codex matches the canonical SKILL.md path; its config offers no wildcard. A skill directory linking - outside the checkout, or too many skills to disable, fails the attempt. + outside the checkout, an unreadable one, or too many skills fails the attempt. """ + try: + paths = _repository_skill_paths(cwd) + entries = ",".join( + f"{{path={json.dumps(path, ensure_ascii=False)},enabled=false}}" for path in sorted(paths) + ) + size = len(f"skills.config=[{entries}]".encode()) + except (OSError, RuntimeError, UnicodeError) as exc: + raise RunnerBadOutput(f"codex: cannot list repository skills to disable: {exc}") from exc + if size > CODEX_SKILL_OVERRIDE_LIMIT: + raise RunnerBadOutput(f"codex: {len(paths)} repository skills are too many to disable") + return ["-c", f"skills.config=[{entries}]"] if paths else [] + + +def _repository_skill_paths(cwd: Path) -> set[str]: tree, paths, seen = cwd.resolve(), set(), set() pending = [cwd / ".agents" / "skills", cwd / ".codex" / "skills"] while pending: directory = pending.pop() - try: - if not directory.is_dir(): - continue - real = directory.resolve(strict=True) - except (OSError, RuntimeError) as exc: - raise RunnerBadOutput(f"codex: unreadable skill directory {directory}") from exc + if not directory.is_dir(): + continue + real = directory.resolve(strict=True) # Checked before scanning: a link leaving the checkout stays visible to Codex. if not real.is_relative_to(tree): raise RunnerBadOutput(f"codex: skill directory {directory} links outside the checkout") @@ -507,19 +518,7 @@ def _codex_repository_skill_overrides(cwd: Path) -> list[str]: pending.append(directory / entry.name) elif entry.name == "SKILL.md": paths.add(os.path.realpath(entry.path)) - return _codex_disabled_skills(paths) - - -def _codex_disabled_skills(paths: set[str]) -> list[str]: - if not paths: - return [] - entries = ",".join( - f"{{path={json.dumps(path, ensure_ascii=False)},enabled=false}}" for path in sorted(paths) - ) - override = f"skills.config=[{entries}]" - if len(override.encode()) > CODEX_SKILL_OVERRIDE_LIMIT: - raise RunnerBadOutput(f"codex: {len(paths)} repository skills are too many to disable") - return ["-c", override] + return paths def _claude_provenance(envelope: dict, raw: str) -> dict[str, object]: @@ -1272,6 +1271,7 @@ def _review_codex( schema_file.write_text(json.dumps(REVIEW_SCHEMA), encoding="utf-8") auth_binds = _auth_binds("codex", home) prefix, config, binary = _sandbox_prefix("codex", cwd, home, ro_paths, auth_binds) + skill_overrides = _codex_repository_skill_overrides(cwd) # before Codex runs at all cli_version = _observe_codex_version(binary, cwd, config.env_allowlist) ignore_user_config = _codex_supports_ignore_user_config(binary, cwd, config.env_allowlist) argv = [ @@ -1296,7 +1296,7 @@ def _review_codex( # The reviewed head controls AGENTS.md and its skills; keep both out of context. "-c", "project_doc_max_bytes=0", - *_codex_repository_skill_overrides(cwd), + *skill_overrides, "--output-schema", f"{sandbox.SANDBOX_HOME}/schema.json", "--output-last-message", diff --git a/tests/test_runners.py b/tests/test_runners.py index 74e19a65..d186d8e4 100644 --- a/tests/test_runners.py +++ b/tests/test_runners.py @@ -1100,9 +1100,16 @@ def test_codex_ignores_repository_instructions_and_skills(repo, fake_bin: Path, )] +def _logging_codex(fake_tool, fake_bin: Path) -> Path: + """A fake Codex that records every call, version and help probes included.""" + fake_tool("codex", f'echo "$@" >> "{fake_bin}/codex.calls"\nexit 1\n') + return fake_bin / "codex.calls" + + @pytest.mark.parametrize("link", [".codex/skills/host", ".agents/skills"]) def test_codex_skill_links_leaving_the_checkout_fail_before_scan_or_launch( - repo, fake_bin: Path, fake_bwrap: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, link: str, + repo, fake_bin: Path, fake_tool, fake_bwrap: Path, tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, link: str, ) -> None: outside = tmp_path / "outside" (outside / "skill").mkdir(parents=True) @@ -1116,22 +1123,31 @@ def spy(path=".", *rest): scanned.append(Path(path)) return real_scandir(path, *rest) monkeypatch.setattr("loopzero.runners.os.scandir", spy) - _fake_review(fake_bin, "codex") + calls = _logging_codex(fake_tool, fake_bin) with pytest.raises(RunnerBadOutput, match="links outside the checkout"): _review("codex", repo) assert not any(p.resolve().is_relative_to(outside.resolve()) for p in scanned) - assert not (fake_bin / "codex.stdin").exists() + assert not calls.exists() -def test_codex_too_many_skills_to_disable_fails_before_launch( - repo, fake_bin: Path, fake_bwrap: Path, monkeypatch: pytest.MonkeyPatch, +@pytest.mark.parametrize("plant", ["too_many", "loop", "undecodable"]) +def test_codex_skill_lists_it_cannot_disable_fail_before_any_codex_call( + repo, fake_bin: Path, fake_tool, fake_bwrap: Path, monkeypatch: pytest.MonkeyPatch, plant: str, ) -> None: - _plant_skills(repo) - monkeypatch.setattr("loopzero.runners.CODEX_SKILL_OVERRIDE_LIMIT", 100) - _fake_review(fake_bin, "codex") - with pytest.raises(RunnerBadOutput, match="3 repository skills are too many to disable"): + if plant == "too_many": + _plant_skills(repo) + monkeypatch.setattr("loopzero.runners.CODEX_SKILL_OVERRIDE_LIMIT", 100) + elif plant == "loop": + (repo / ".agents/skills").mkdir(parents=True) + (repo / ".agents/skills/self").symlink_to("self") + else: + skill = repo / ".codex/skills" / os.fsdecode(b"\xff") + skill.mkdir(parents=True) + (skill / "SKILL.md").write_text("---\nname: x\ndescription: x\n---\n") + calls = _logging_codex(fake_tool, fake_bin) + with pytest.raises(RunnerBadOutput, match=r"too many to disable|cannot list repository skills"): _review("codex", repo) - assert not (fake_bin / "codex.stdin").exists() + assert not calls.exists() def test_codex_approve(repo, From 15469fa011cbc136e64b16392ce0e1e1d0f90c41 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:58:32 +0100 Subject: [PATCH 4/5] Resolve every Codex skill link strictly and escape DEL for TOML A looping or dangling link at or under either skill root now fails the Codex attempt instead of being skipped, a symlinked SKILL.md is disabled by its target, and a DEL character in a skill path is escaped so the skills.config override stays valid TOML. Refs #1107 Co-Authored-By: Claude Opus 5.5 --- src/loopzero/runners.py | 25 ++++++++++++++----------- tests/test_runners.py | 15 ++++++++++----- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/loopzero/runners.py b/src/loopzero/runners.py index 01536457..b5994f67 100644 --- a/src/loopzero/runners.py +++ b/src/loopzero/runners.py @@ -501,7 +501,9 @@ def _codex_repository_skill_overrides(cwd: Path) -> list[str]: try: paths = _repository_skill_paths(cwd) entries = ",".join( - f"{{path={json.dumps(path, ensure_ascii=False)},enabled=false}}" for path in sorted(paths) + # TOML basic strings forbid a literal DEL, which JSON leaves unescaped. + f"{{path={json.dumps(path, ensure_ascii=False).replace(chr(127), '\\u007f')},enabled=false}}" + for path in sorted(paths) ) size = len(f"skills.config=[{entries}]".encode()) except (OSError, RuntimeError, UnicodeError) as exc: @@ -515,22 +517,23 @@ def _repository_skill_paths(cwd: Path) -> set[str]: tree, paths, seen = cwd.resolve(), set(), set() pending = [cwd / ".agents" / "skills", cwd / ".codex" / "skills"] while pending: - directory = pending.pop() - if not directory.is_dir(): + path = pending.pop() + if not os.path.lexists(path): continue - real = directory.resolve(strict=True) - # Checked before scanning: a link leaving the checkout stays visible to Codex. + # Strict: a looping or dangling link fails closed; Codex might resolve it elsewhere. + real = path.resolve(strict=True) if not real.is_relative_to(tree): - raise RunnerBadOutput(f"codex: skill directory {directory} links outside the checkout") + raise RunnerBadOutput(f"codex: skill directory {path} links outside the checkout") + if not real.is_dir(): + if path.name == "SKILL.md": + paths.add(str(real)) + continue if real in seen: continue seen.add(real) with os.scandir(real) as entries: - for entry in entries: - if entry.is_dir(): - pending.append(directory / entry.name) - elif entry.name == "SKILL.md": - paths.add(os.path.realpath(entry.path)) + pending.extend(path / entry.name for entry in entries + if entry.is_symlink() or entry.is_dir() or entry.name == "SKILL.md") return paths diff --git a/tests/test_runners.py b/tests/test_runners.py index 02d320ea..44e25bd2 100644 --- a/tests/test_runners.py +++ b/tests/test_runners.py @@ -1092,7 +1092,7 @@ def test_bwrap_unavailable_fails_closed(fake_bin, tmp_path, monkeypatch, fake_to def _plant_skills(repo: Path) -> None: - for skill in (repo / ".agents/skills/a/b", repo / ".codex/skills/c", repo / "docs/d"): + for skill in (repo / ".agents/skills/a/b", repo / ".agents/skills/e\x7f", repo / ".codex/skills/c", repo / "docs/d"): skill.mkdir(parents=True) (skill / "SKILL.md").write_text("---\nname: planted\ndescription: planted\n---\n") (repo / ".agents/skills/linked").symlink_to(repo / "docs/d") @@ -1110,7 +1110,7 @@ def test_codex_ignores_repository_instructions_and_skills(repo, fake_bin: Path, disabled = tomllib.loads(override)["skills"]["config"] real = repo.resolve() assert disabled == [{"path": str(path), "enabled": False} for path in ( - real / ".agents/skills/a/b/SKILL.md", real / ".codex/skills/c/SKILL.md", + real / ".agents/skills/a/b/SKILL.md", real / ".agents/skills/e\x7f/SKILL.md", real / ".codex/skills/c/SKILL.md", real / "docs/d/SKILL.md", )] @@ -1145,16 +1145,21 @@ def spy(path=".", *rest): assert not calls.exists() -@pytest.mark.parametrize("plant", ["too_many", "loop", "undecodable"]) +@pytest.mark.parametrize("plant", [ + "too_many", "loop", "undecodable", "dangling", "root_loop:.agents/skills", "root_loop:.codex/skills"]) def test_codex_skill_lists_it_cannot_disable_fail_before_any_codex_call( repo, fake_bin: Path, fake_tool, fake_bwrap: Path, monkeypatch: pytest.MonkeyPatch, plant: str, ) -> None: if plant == "too_many": _plant_skills(repo) monkeypatch.setattr("loopzero.runners.CODEX_SKILL_OVERRIDE_LIMIT", 100) - elif plant == "loop": + elif plant in ("loop", "dangling"): (repo / ".agents/skills").mkdir(parents=True) - (repo / ".agents/skills/self").symlink_to("self") + (repo / ".agents/skills/x").symlink_to("x" if plant == "loop" else "/nonexistent/skills") + elif plant.startswith("root_loop:"): + root = repo / plant.partition(":")[2] + root.parent.mkdir(parents=True) + root.symlink_to(root.name) else: skill = repo / ".codex/skills" / os.fsdecode(b"\xff") skill.mkdir(parents=True) From 66e67b236e601a3967512a7fff5d85be3580dfa4 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 04:38:37 +0100 Subject: [PATCH 5/5] Validate repository skill parent paths before descending Architecture-Review: unchanged; the existing pre-launch checkout containment rule now also covers the parent directories. Co-Authored-By: Codex --- src/loopzero/runners.py | 10 ++++++++-- tests/test_runners.py | 15 +++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/loopzero/runners.py b/src/loopzero/runners.py index b5994f67..fe1a29d8 100644 --- a/src/loopzero/runners.py +++ b/src/loopzero/runners.py @@ -515,10 +515,13 @@ def _codex_repository_skill_overrides(cwd: Path) -> list[str]: def _repository_skill_paths(cwd: Path) -> set[str]: tree, paths, seen = cwd.resolve(), set(), set() - pending = [cwd / ".agents" / "skills", cwd / ".codex" / "skills"] + roots = (cwd / ".agents", cwd / ".codex") + pending = list(roots) while pending: path = pending.pop() - if not os.path.lexists(path): + try: + path.lstat() + except FileNotFoundError: continue # Strict: a looping or dangling link fails closed; Codex might resolve it elsewhere. real = path.resolve(strict=True) @@ -528,6 +531,9 @@ def _repository_skill_paths(cwd: Path) -> set[str]: if path.name == "SKILL.md": paths.add(str(real)) continue + if path in roots: + pending.append(path / "skills") + continue if real in seen: continue seen.add(real) diff --git a/tests/test_runners.py b/tests/test_runners.py index cbc78109..74ad5a70 100644 --- a/tests/test_runners.py +++ b/tests/test_runners.py @@ -1159,20 +1159,19 @@ def spy(path=".", *rest): @pytest.mark.parametrize("plant", [ - "too_many", "loop", "undecodable", "dangling", "root_loop:.agents/skills", "root_loop:.codex/skills"]) + "too_many", "loop", "undecodable", "dangling", "loop:.agents/skills", "loop:.codex/skills", + "loop:.agents", "loop:.codex", "dangling:.agents", "dangling:.codex"]) def test_codex_skill_lists_it_cannot_disable_fail_before_any_codex_call( repo, fake_bin: Path, fake_tool, fake_bwrap: Path, monkeypatch: pytest.MonkeyPatch, plant: str, ) -> None: if plant == "too_many": _plant_skills(repo) monkeypatch.setattr("loopzero.runners.CODEX_SKILL_OVERRIDE_LIMIT", 100) - elif plant in ("loop", "dangling"): - (repo / ".agents/skills").mkdir(parents=True) - (repo / ".agents/skills/x").symlink_to("x" if plant == "loop" else "/nonexistent/skills") - elif plant.startswith("root_loop:"): - root = repo / plant.partition(":")[2] - root.parent.mkdir(parents=True) - root.symlink_to(root.name) + elif plant != "undecodable": + kind, _, name = plant.partition(":") + link = repo / (name or ".agents/skills/x") + link.parent.mkdir(parents=True, exist_ok=True) + link.symlink_to(link.name if kind == "loop" else "missing") else: skill = repo / ".codex/skills" / os.fsdecode(b"\xff") skill.mkdir(parents=True)