From 14663a3291b8df393341f8f56f41d2024100692c Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 28 Nov 2026 18:40:31 +0800 Subject: [PATCH] fix(tier3): include staged file metadata in baseline identity Bind regular-file and symlink kind and permissions to retained baseline compatibility, while keeping timestamps and generated context metadata outside the identity. Exercise actual staging and script execution. Signed-off-by: Afloat --- CHANGELOG.md | 4 ++ src/skillevaluator/tier3/baseline_store.py | 12 ++-- tests/tier3/test_baseline_file_metadata.py | 66 ++++++++++++++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 tests/tier3/test_baseline_file_metadata.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 8445af60a..4fc6c1bcb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,10 @@ All notable changes to SkillEvaluator are documented in this file. ### Fixed +- Retained Tier 3 baselines are invalidated when a staged file's permissions or + kind changes, including a script losing its executable bit with unchanged + contents. The updated task-tree identity requires a fresh baseline for runs + that recorded the older digest format. - `make lint` checks the whole repository, as CI does, instead of only `src` and `tests`, so a lint error in `scripts/` or `ci/` no longer passes the documented local gate and then fails CI. - Tier 2 deduplication verdict requests carry the documented output ceiling, diff --git a/src/skillevaluator/tier3/baseline_store.py b/src/skillevaluator/tier3/baseline_store.py index 51ade89c6..833275d09 100644 --- a/src/skillevaluator/tier3/baseline_store.py +++ b/src/skillevaluator/tier3/baseline_store.py @@ -10,6 +10,7 @@ import json import math import re +import stat from collections.abc import Sequence from pathlib import Path from typing import Any @@ -109,8 +110,8 @@ def identity_dataset_digest(task_dirs: Sequence[Path]) -> str: def baseline_task_tree_digest(task_dirs_by_agent: dict[str, Sequence[Path]]) -> str: - """Hash the staged without-skill task trees, normalizing arm-specific task names.""" - digest = hashlib.sha256() + """Hash staged file kinds, permissions and contents, normalizing arm-specific task names.""" + digest = hashlib.sha256(b"skillevaluator-baseline-task-tree-v2\0") for agent, task_dirs in sorted(task_dirs_by_agent.items()): for task_dir in sorted(task_dirs, key=lambda path: path.name): task_name = _ARM_TASK_DIR_SUFFIX.sub("", task_dir.name) @@ -121,9 +122,10 @@ def baseline_task_tree_digest(task_dirs_by_agent: dict[str, Sequence[Path]]) -> relative = path.relative_to(task_dir).as_posix() if relative == "environment/repo-context.json": continue - if path.is_symlink(): + metadata = path.lstat() + if stat.S_ISLNK(metadata.st_mode): content = b"symlink\0" + path.readlink().as_posix().encode("utf-8") - elif path.is_file(): + elif stat.S_ISREG(metadata.st_mode): content = path.read_bytes() if relative == "task.toml": content = _ARM_TASK_NAME_SUFFIX.sub(r"\1\2", content.decode("utf-8"), count=1).encode("utf-8") @@ -132,6 +134,8 @@ def baseline_task_tree_digest(task_dirs_by_agent: dict[str, Sequence[Path]]) -> file_header = relative.encode("utf-8") digest.update(len(file_header).to_bytes(8, "big")) digest.update(file_header) + digest.update(stat.S_IFMT(metadata.st_mode).to_bytes(4, "big")) + digest.update(stat.S_IMODE(metadata.st_mode).to_bytes(2, "big")) digest.update(len(content).to_bytes(8, "big")) digest.update(content) return "sha256:" + digest.hexdigest() diff --git a/tests/tier3/test_baseline_file_metadata.py b/tests/tier3/test_baseline_file_metadata.py new file mode 100644 index 000000000..83bae7c79 --- /dev/null +++ b/tests/tier3/test_baseline_file_metadata.py @@ -0,0 +1,66 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""Staged file metadata that changes execution must invalidate baseline reuse.""" + +import os +import subprocess +from pathlib import Path + +import pytest + +from skillevaluator.tier3.baseline_store import baseline_task_tree_digest, comparison_mismatches +from skillevaluator.tier3.harbor.secure_copy import copy_file_secure + + +@pytest.mark.skipif(os.name != "posix", reason="POSIX executable permission bits") +def test_staged_executable_permission_change_rejects_the_old_baseline(tmp_path: Path) -> None: + source = tmp_path / "authored" + source.mkdir() + script = source / "check.sh" + script.write_text("#!/bin/sh\nprintf ready", encoding="utf-8") + script.chmod(0o755) + task = tmp_path / "case-001" + staged = task / "environment" / "repo" / script.name + staged.parent.mkdir(parents=True) + copy_file_secure(script, staged, allowed_root=source) + before = baseline_task_tree_digest({"codex": [task]}) + assert subprocess.run([str(staged)], capture_output=True, text=True, check=True, timeout=5).stdout == "ready" + + script.chmod(0o644) + copy_file_secure(script, staged, allowed_root=source) + assert staged.read_bytes() == script.read_bytes() + with pytest.raises(PermissionError): + subprocess.run([str(staged)], capture_output=True, text=True, check=True, timeout=5) + after = baseline_task_tree_digest({"codex": [task]}) + assert comparison_mismatches( + {"config": {}, "baseline_task_tree_digest": after}, + {"config": {}, "baseline_task_tree_digest": before}, + ) == ["staged baseline task tree changed"] + + +@pytest.mark.skipif(os.name != "posix", reason="POSIX file modes and symlinks") +def test_regular_file_cannot_impersonate_a_symlinks_digest_payload(tmp_path: Path) -> None: + task = tmp_path / "case-001" + task.mkdir() + entry = task / "entry" + entry.write_bytes(b"symlink\0target") + entry.chmod(0o777) + regular = baseline_task_tree_digest({"codex": [task]}) + entry.unlink() + entry.symlink_to("target") + assert baseline_task_tree_digest({"codex": [task]}) != regular + + +@pytest.mark.skipif(os.name != "posix", reason="POSIX permission bits") +def test_file_permissions_matter_but_timestamps_do_not(tmp_path: Path) -> None: + task = tmp_path / "case-001" + task.mkdir() + entry = task / "settings.json" + entry.write_text("{}", encoding="utf-8") + entry.chmod(0o644) + original = baseline_task_tree_digest({"codex": [task]}) + os.utime(entry, (1_000_000_000, 1_000_000_000)) + assert baseline_task_tree_digest({"codex": [task]}) == original + entry.chmod(0o444) + assert baseline_task_tree_digest({"codex": [task]}) != original