[verified] Enforce delegation adoption at runtime consumers

This commit is contained in:
2026-10-11 07:11:40 +00:00
parent 9367d5a3a1
commit 6cb2317667
19 changed files with 1112 additions and 47 deletions
@@ -0,0 +1,208 @@
import copy
import hashlib
import json
import subprocess
import tempfile
import unittest
from pathlib import Path
from unittest import mock
from harness.lib import delegation_adoption as adoption
class DelegationAdoptionTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory(dir="/opt/data/cache/scratch")
self.base = Path(self.tmp.name)
self.bundle = self.base / "bundle"
(self.bundle / "src").mkdir(parents=True)
(self.bundle / "evidence").mkdir()
(self.bundle / "src/value.py").write_bytes(b"VALUE = 1\n")
(self.bundle / "evidence/run.txt").write_bytes(b"run\n")
self.files = {"src/value.py": hashlib.sha256((self.bundle / "src/value.py").read_bytes()).hexdigest()}
self.bundle_sha = self.digest(self.files)
self.context = {
"schema": "gitea-agent.delegation-evidence-context/v1",
"source": {"kind": "captured_worktree", "base_revision": "a" * 40},
"stage": "implementation",
"bundle_sha256": self.bundle_sha,
"files": self.files,
"allowed_actions": ["static"],
"obligations": [{"id": "static", "kind": "static"}],
"executions": [],
}
self.result = {
"schema": "gitea-agent.delegation-evidence-result/v1",
"source": copy.deepcopy(self.context["source"]),
"stage": self.context["stage"],
"bundle_sha256": self.bundle_sha,
"claims": [{"id": "static", "status": "supported", "anchors": [{
"kind": "lines", "path": "src/value.py", "start_line": 1,
"end_line": 1, "text_sha256": hashlib.sha256(b"VALUE = 1\n").hexdigest(),
}], "execution_ids": []}],
}
self.context_path = self.base / "context.json"
self.result_path = self.base / "result.json"
self.context_path.write_text(json.dumps(self.context))
self.result_path.write_text(json.dumps(self.result))
def tearDown(self):
self.tmp.cleanup()
@staticmethod
def digest(value):
return hashlib.sha256(json.dumps(value, sort_keys=True, separators=(",", ":")).encode()).hexdigest()
def declaration(self):
return {
"mode": "delegated",
"bundle_root": str(self.bundle),
"context_path": str(self.context_path),
"result_path": str(self.result_path),
"git_dir": None,
}
def test_valid_delegated_declaration_returns_fresh_pass_result(self):
expected_source = copy.deepcopy(self.context["source"])
out = adoption.require_adoption(
self.declaration(), expected_source=expected_source, expected_stage="implementation"
)
self.assertEqual(out["status"], "pass")
self.assertEqual(out["counts"]["obligations"], 1)
def test_valid_git_tree_declaration_uses_expected_revision(self):
repo = self.base / "repo"
repo.mkdir()
(repo / "src").mkdir()
(repo / "src/value.py").write_bytes(b"VALUE = 1\n")
subprocess.run(["/usr/bin/git", "init", "-q", str(repo)], check=True)
subprocess.run(["/usr/bin/git", "-C", str(repo), "config", "user.email", "test@example.invalid"], check=True)
subprocess.run(["/usr/bin/git", "-C", str(repo), "config", "user.name", "Test"], check=True)
subprocess.run(["/usr/bin/git", "-C", str(repo), "add", "."], check=True)
subprocess.run(["/usr/bin/git", "-C", str(repo), "commit", "-q", "-m", "fixture"], check=True)
revision = subprocess.check_output(["/usr/bin/git", "-C", str(repo), "rev-parse", "HEAD"], text=True).strip()
context = copy.deepcopy(self.context)
context["source"] = {"kind": "git_tree", "revision": revision}
result = copy.deepcopy(self.result)
result["source"] = copy.deepcopy(context["source"])
self.context_path.write_text(json.dumps(context))
self.result_path.write_text(json.dumps(result))
declaration = self.declaration()
declaration["git_dir"] = str(repo / ".git")
out = adoption.require_adoption(declaration, expected_source=context["source"], expected_stage="implementation")
self.assertEqual(out["status"], "pass")
def test_explicit_none_is_the_only_non_delegated_success(self):
self.assertIsNone(adoption.require_adoption(
{"mode": "none"}, expected_source=self.context["source"], expected_stage="implementation"
))
def test_missing_declaration_is_rejected_with_sanitized_error(self):
with self.assertRaisesRegex(adoption.AdoptionError, "^delegation adoption rejected$"):
adoption.require_adoption(None, expected_source=self.context["source"], expected_stage="implementation")
def test_unsupported_or_extra_declaration_fields_are_rejected(self):
for declaration in (
{"mode": "worker"},
{"mode": "none", "bundle_root": str(self.bundle)},
{**self.declaration(), "extra": 1},
):
with self.subTest(declaration=declaration):
with self.assertRaises(adoption.AdoptionError):
adoption.require_adoption(declaration, expected_source=self.context["source"], expected_stage="implementation")
def test_expected_source_and_stage_are_independent_and_exact(self):
for source, stage in (
({"kind": "captured_worktree", "base_revision": "b" * 40}, "implementation"),
(self.context["source"], "review"),
):
with self.subTest(source=source, stage=stage), self.assertRaises(adoption.AdoptionError):
adoption.require_adoption(self.declaration(), expected_source=source, expected_stage=stage)
def test_stale_context_result_or_bundle_is_rejected(self):
for field, value in (("stage", "review"), ("bundle_sha256", "0" * 64)):
context = copy.deepcopy(self.context)
context[field] = value
self.context_path.write_text(json.dumps(context))
with self.subTest(field=field), self.assertRaises(adoption.AdoptionError):
adoption.require_adoption(self.declaration(), expected_source=self.context["source"], expected_stage="implementation")
self.context_path.write_text(json.dumps(self.context))
self.result_path.write_text(json.dumps({**self.result, "stage": "review"}))
with self.assertRaises(adoption.AdoptionError):
adoption.require_adoption(self.declaration(), expected_source=self.context["source"], expected_stage="implementation")
self.result_path.write_text(json.dumps(self.result))
(self.bundle / "src/value.py").write_bytes(b"VALUE = 2\n")
with self.assertRaises(adoption.AdoptionError):
adoption.require_adoption(self.declaration(), expected_source=self.context["source"], expected_stage="implementation")
def test_empty_obligations_or_claims_cannot_be_adopted(self):
for context, result in (
({**self.context, "obligations": []}, self.result),
(self.context, {**self.result, "claims": []}),
):
self.context_path.write_text(json.dumps(context))
self.result_path.write_text(json.dumps(result))
with self.subTest(context=context, result=result), self.assertRaises(adoption.AdoptionError):
adoption.require_adoption(self.declaration(), expected_source=self.context["source"], expected_stage="implementation")
self.context_path.write_text(json.dumps(self.context))
self.result_path.write_text(json.dumps(self.result))
def test_block_and_verifier_exception_fail_closed(self):
with mock.patch.object(adoption.delegation_evidence, "verify_evidence", return_value={"status": "block"}):
with self.assertRaises(adoption.AdoptionError):
adoption.require_adoption(self.declaration(), expected_source=self.context["source"], expected_stage="implementation")
with mock.patch.object(adoption.delegation_evidence, "verify_evidence", side_effect=RuntimeError("secret")):
with self.assertRaisesRegex(adoption.AdoptionError, "^delegation adoption rejected$"):
adoption.require_adoption(self.declaration(), expected_source=self.context["source"], expected_stage="implementation")
def test_verifier_is_called_again_after_a_prior_pass(self):
with mock.patch.object(adoption.delegation_evidence, "verify_evidence", wraps=adoption.delegation_evidence.verify_evidence) as verifier:
adoption.require_adoption(self.declaration(), expected_source=self.context["source"], expected_stage="implementation")
adoption.require_adoption(self.declaration(), expected_source=self.context["source"], expected_stage="implementation")
self.assertEqual(verifier.call_count, 2)
def test_malformed_expected_inputs_and_paths_fail_closed(self):
cases = (
({"kind": "captured_worktree", "base_revision": "A" * 40}, "implementation"),
(self.context["source"], ""),
(self.context["source"], None),
)
for source, stage in cases:
with self.subTest(source=source, stage=stage), self.assertRaises(adoption.AdoptionError):
adoption.require_adoption({"mode": "none"}, expected_source=source, expected_stage=stage)
for key, value in (("bundle_root", str(self.base / ".." / self.base.name / "bundle")),
("context_path", "relative.json"), ("result_path", str(self.result_path) + "\x00"),
("git_dir", 3)):
declaration = self.declaration()
declaration[key] = value
with self.subTest(key=key), self.assertRaises(adoption.AdoptionError):
adoption.require_adoption(declaration, expected_source=self.context["source"], expected_stage="implementation")
def test_all_git_declaration_paths_reject_symlinks_at_runtime(self):
from harness.tests.common.test_delegation_consumers_integration import RealEvidence
evidence = RealEvidence(self.base / 'safe-path-fixture')
declaration = evidence.declaration()
for field in ('bundle_root', 'context_path', 'result_path', 'git_dir'):
alias = self.base / ('alias-' + field)
alias.symlink_to(declaration[field], target_is_directory=field in ('bundle_root', 'git_dir'))
bad = {**declaration, field: str(alias)}
with self.subTest(field=field), self.assertRaisesRegex(adoption.AdoptionError, '^delegation adoption rejected$'):
adoption.require_adoption(bad, expected_source=evidence.context['source'], expected_stage='spec-review')
alias_parent = self.base / 'alias-parent'
alias_parent.symlink_to(evidence.root, target_is_directory=True)
with self.assertRaises(adoption.AdoptionError):
adoption.require_adoption({**declaration, 'context_path': str(alias_parent / 'context.json')},
expected_source=evidence.context['source'], expected_stage='spec-review')
def test_duplicate_json_and_missing_files_fail_closed(self):
self.context_path.write_text('{"schema":"x","schema":"y"}')
with self.assertRaises(adoption.AdoptionError):
adoption.require_adoption(self.declaration(), expected_source=self.context["source"], expected_stage="implementation")
self.context_path.write_text(json.dumps(self.context))
self.result_path.unlink()
with self.assertRaises(adoption.AdoptionError):
adoption.require_adoption(self.declaration(), expected_source=self.context["source"], expected_stage="implementation")
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,34 @@
"""Executable consumer declaration contract wiring."""
from pathlib import Path
import unittest
ROOT = Path(__file__).resolve().parents[3]
class DelegationAdoptionContractTests(unittest.TestCase):
def test_common_runtime_contract_is_explicit(self):
text = (ROOT / 'harness/operations/repository-cache.md').read_text()
for token in ('require_adoption', 'expected_source', 'expected_stage',
'--no-delegated-evidence', '--delegation-adoption',
'spec-review', 'code-review', 'implementation-delivery',
'PASS receipt', 'sandbox'):
with self.subTest(token=token):
self.assertIn(token, text)
def test_each_consumer_skill_requires_runtime_declaration(self):
paths = (
'spec/skills/gitea-implementation-spec-review',
'impl/skills/gitea-issue-implementation',
'impl/skills/gitea-issue-rework',
'review/skills/gitea-code-review',
)
for path in paths:
text = (ROOT / 'harness/agents' / path / 'SKILL.md').read_text()
with self.subTest(path=path):
self.assertIn('require_adoption', text)
self.assertIn('mode', text)
self.assertIn('none', text)
self.assertIn('delegated', text)
self.assertIn('delegation-adoption-runtime-consumption', text)
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,46 @@
import importlib.util
import unittest
from pathlib import Path
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[3]
PATH = ROOT / "harness/agents/impl/skills/gitea-issue-implementation/scripts/delivery_adoption.py"
spec = importlib.util.spec_from_file_location("delivery_adoption", PATH)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
class DeliveryConsumerTests(unittest.TestCase):
def test_delivery_requires_adoption_then_calls_adapter_once(self):
calls = []
with patch.object(mod, "require_adoption", return_value={"status": "pass"}) as adopted:
result = mod.deliver(lambda: {"adapter": "actual"}, head="a" * 40,
declaration={"mode": "delegated"})
adopted.assert_called_once_with({"mode": "delegated"}, expected_source={"kind": "git_tree", "revision": "a" * 40}, expected_stage="implementation-delivery")
self.assertEqual(result, {"adapter": "actual"})
def test_rejected_adoption_blocks_adapter(self):
calls = []
with patch.object(mod, "require_adoption", side_effect=mod.AdoptionError("no")):
with self.assertRaises(mod.AdoptionError):
mod.deliver(lambda: calls.append("called"), head="a" * 40,
declaration={"mode": "delegated"})
self.assertEqual(calls, [])
def test_missing_declaration_blocks_adapter(self):
calls = []
with self.assertRaises(mod.AdoptionError):
mod.deliver(lambda: calls.append("called"), head="a" * 40, declaration=None)
self.assertEqual(calls, [])
def test_explicit_none_still_validates_head_and_calls_adapter(self):
calls = []
with patch.object(mod, "require_adoption", return_value={"status": "none"}):
with self.assertRaises(mod.AdoptionError):
mod.deliver(lambda: calls.append("called"), head="bad", declaration={"mode": "none"})
mod.deliver(lambda: calls.append("called"), head="a" * 40, declaration={"mode": "none"})
self.assertEqual(calls, ["called"])
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,298 @@
import copy
import hashlib
import importlib.util
import json
import os
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[3]
SCRATCH = Path('/opt/data/cache/scratch/delegation-adoption-integration')
SPEC = ROOT / 'harness/agents/spec/skills/gitea-implementation-spec-review/scripts/review_support.py'
REWORK = ROOT / 'harness/agents/impl/skills/gitea-issue-rework/scripts/rework_comment.py'
DELIVERY = ROOT / 'harness/agents/impl/skills/gitea-issue-implementation/scripts/delivery_adoption.py'
NATIVE = ROOT / 'harness/agents/review/skills/gitea-code-review/scripts/native_review.py'
def load(path, name):
spec = importlib.util.spec_from_file_location(name, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
support = load(SPEC, 'integration_spec_support')
delivery = load(DELIVERY, 'integration_delivery')
native = load(NATIVE, 'integration_native')
rework = load(REWORK, 'integration_rework')
class RealEvidence:
def __init__(self, root):
self.root = root
self.bundle = root / 'bundle'
(self.bundle / 'src').mkdir(parents=True)
(self.bundle / 'tests').mkdir()
(self.bundle / 'evidence').mkdir()
(self.bundle / 'src/a.py').write_text('VALUE = 1\n')
(self.bundle / 'tests/test_a.py').write_text('def test_value():\n assert 1 == 1\n')
(self.bundle / 'evidence/red.txt').write_text('red\n')
(self.bundle / 'evidence/green.txt').write_text('green\n')
subprocess.run(['/usr/bin/git', 'init', '-q', str(self.root / 'repo')], check=True)
repo = self.root / 'repo'
(repo / 'src').mkdir(); (repo / 'tests').mkdir()
(repo / 'src/a.py').write_text('VALUE = 1\n')
(repo / 'tests/test_a.py').write_text('def test_value():\n assert 1 == 1\n')
subprocess.run(['/usr/bin/git', '-C', str(repo), 'config', 'user.email', 'test@example.invalid'], check=True)
subprocess.run(['/usr/bin/git', '-C', str(repo), 'config', 'user.name', 'Integration'], check=True)
subprocess.run(['/usr/bin/git', '-C', str(repo), 'add', '.'], check=True)
subprocess.run(['/usr/bin/git', '-C', str(repo), 'commit', '-q', '-m', 'fixture'], check=True)
self.revision = subprocess.check_output(['/usr/bin/git', '-C', str(repo), 'rev-parse', 'HEAD'], text=True).strip()
self.git_dir = repo / '.git'
self.files = {p: hashlib.sha256((self.bundle / p).read_bytes()).hexdigest() for p in ('src/a.py', 'tests/test_a.py')}
self.bundle_sha = self.digest(self.files)
self.context = {'schema': 'gitea-agent.delegation-evidence-context/v1',
'source': {'kind': 'git_tree', 'revision': self.revision},
'stage': 'spec-review', 'bundle_sha256': self.bundle_sha,
'files': self.files, 'allowed_actions': ['static', 'execute'],
'obligations': [{'id': 'static', 'kind': 'static'}, {'id': 'rg', 'kind': 'red_green'}],
'executions': [self.execution('red', 1, 'assertion', 'evidence/red.txt'),
self.execution('green', 0, 'none', 'evidence/green.txt')]}
line_hash = hashlib.sha256(b'VALUE = 1\n').hexdigest()
self.result = {'schema': 'gitea-agent.delegation-evidence-result/v1',
'source': copy.deepcopy(self.context['source']), 'stage': 'spec-review',
'bundle_sha256': self.bundle_sha,
'claims': [{'id': 'static', 'status': 'supported', 'anchors': [{'kind': 'lines', 'path': 'src/a.py', 'start_line': 1, 'end_line': 1, 'text_sha256': line_hash}], 'execution_ids': []},
{'id': 'rg', 'status': 'supported', 'anchors': [{'kind': 'test', 'path': 'tests/test_a.py', 'symbol': 'test_value'}], 'execution_ids': ['red', 'green']}]}
self.context_path = root / 'context.json'; self.result_path = root / 'result.json'
self.write()
@staticmethod
def digest(value):
return hashlib.sha256(json.dumps(value, sort_keys=True, separators=(',', ':')).encode()).hexdigest()
def execution(self, eid, code, kind, path):
return {'id': eid, 'test_id': 'tests/test_a.py::test_value', 'bundle_sha256': self.bundle_sha,
'test_file_sha256': self.files['tests/test_a.py'], 'argv': ['python', '-m', 'pytest'],
'exit_code': code, 'failure_kind': kind, 'evidence_path': path,
'evidence_sha256': hashlib.sha256((self.bundle / path).read_bytes()).hexdigest(), 'origin': 'direct'}
def write(self):
self.context_path.write_text(json.dumps(self.context))
self.result_path.write_text(json.dumps(self.result))
def declaration(self):
return {'mode': 'delegated', 'bundle_root': str(self.bundle), 'context_path': str(self.context_path),
'result_path': str(self.result_path), 'git_dir': str(self.git_dir)}
def set_stage(self, stage):
self.context['stage'] = stage; self.result['stage'] = stage; self.write()
class IntegrationTests(unittest.TestCase):
def setUp(self):
SCRATCH.mkdir(parents=True, exist_ok=True)
self.tmp = tempfile.TemporaryDirectory(dir=SCRATCH)
self.ev = RealEvidence(Path(self.tmp.name))
def tearDown(self): self.tmp.cleanup()
def test_spec_real_git_pass_roundtrip_and_blocks_drift(self):
self.ev.set_stage('spec-review')
result = {'schema': support.SCHEMA, 'target': {'owner': 'acme', 'repository': 'repo', 'issue': 3, 'pull': 7, 'head': self.ev.revision},
'contract_sha256': hashlib.sha256(b'contract').hexdigest(), 'verdict': 'approve',
'criteria': [{'criterion': 'REQ-1', 'contract': 'contract', 'implementation_paths': ['src/a.py'], 'test_paths': ['tests/test_a.py'], 'evidence': 'verified', 'result': 'satisfied', 'reason': 'ok'}],
'scope': {'missing': [], 'excess': [], 'spec_conflicts': []}, 'conclusion': 'ok'}
body = support.render_review(result, {'src/a.py', 'tests/test_a.py'}, declaration=self.ev.declaration())
self.assertEqual(support.parse_review(body, {'src/a.py', 'tests/test_a.py'}), result)
self.ev.set_stage('other'); self.ev.write()
with self.assertRaises(support.ValidationError): support.render_review(result, {'src/a.py', 'tests/test_a.py'}, declaration=self.ev.declaration())
def test_spec_cli_delegated_import_without_pythonpath_and_block_is_quiet(self):
self.ev.set_stage('spec-review')
result = {'schema': support.SCHEMA, 'target': {'owner': 'acme', 'repository': 'repo', 'issue': 3, 'pull': 7, 'head': self.ev.revision},
'contract_sha256': hashlib.sha256(b'contract').hexdigest(), 'verdict': 'approve',
'criteria': [{'criterion': 'REQ-1', 'contract': 'contract', 'implementation_paths': ['src/a.py'], 'test_paths': [], 'evidence': 'verified', 'result': 'satisfied', 'reason': 'ok'}],
'scope': {'missing': [], 'excess': [], 'spec_conflicts': []}, 'conclusion': 'ok'}
result_path = Path(self.tmp.name) / 'spec-result.json'; paths_path = Path(self.tmp.name) / 'paths.json'; decl_path = Path(self.tmp.name) / 'decl.json'; result_path.write_text(json.dumps(result)); paths_path.write_text(json.dumps(['src/a.py'])); decl_path.write_text(json.dumps(self.ev.declaration()))
env = os.environ.copy(); env.pop('PYTHONPATH', None)
cmd = [sys.executable, str(SPEC), 'render', '--result', str(result_path), '--allowed-paths', str(paths_path), '--delegation-adoption', str(decl_path)]
run = subprocess.run(cmd, cwd=self.tmp.name, env=env, text=True, capture_output=True)
self.assertEqual(run.returncode, 0, run.stderr); self.assertIn('implementation-spec-review/v1', run.stdout)
self.ev.context['stage'] = 'drift'; self.ev.write()
blocked = subprocess.run(cmd, cwd=self.tmp.name, env=env, text=True, capture_output=True)
self.assertNotEqual(blocked.returncode, 0); self.assertEqual(blocked.stdout, '')
def test_delivery_real_pass_returns_adapter_and_blocks_without_write(self):
self.ev.set_stage('implementation-delivery')
calls = []
out = delivery.deliver(lambda: {'receipt': 'actual'}, head=self.ev.revision, declaration=self.ev.declaration())
self.assertEqual(out, {'receipt': 'actual'})
self.ev.bundle.joinpath('src/a.py').write_text('VALUE = 2\n')
with self.assertRaises(delivery.AdoptionError): delivery.deliver(lambda: calls.append(1), head=self.ev.revision, declaration=self.ev.declaration())
self.assertEqual(calls, [])
def bound_publication_fixture(self):
from harness.tests.review import test_native_review as fixture
plan, before, after, exact = fixture.publication_fixture()
old_head = plan['target']['head']
def replace(value):
if isinstance(value, str): return self.ev.revision if value == old_head else value
if isinstance(value, list): return [replace(x) for x in value]
if isinstance(value, dict): return {k: replace(v) for k, v in value.items()}
return value
plan, before, after, exact = map(replace, (plan, before, after, exact))
plan['body'] = native.marker(plan) + '\n- Verdict: `approve`\n검토 완료.\n'
exact['body'] = plan['body']
for review in before['reviews'] + after['reviews']:
if review['id'] == plan['spec_review_id']:
original = fixture.publication_fixture()[1]['reviews'][1]['body']
sr = support.parse_review(original, {'src/a.py'})
sr['target']['head'] = self.ev.revision
review['body'] = support.render_review(sr, {'src/a.py'}, declaration={'mode': 'none'})
review['commit_id'] = self.ev.revision
elif review['id'] == 10:
review['body'] = plan['body']
exact['commit_id'] = self.ev.revision
native.validate_snapshot(plan, before)
return fixture, plan, before, after, exact
def test_review_real_git_adoption_posts_once_then_noops(self):
self.ev.set_stage('code-review')
fixture, plan, before, after, exact = self.bound_publication_fixture()
api = fixture.FakeAPI(plan, before, after, exact)
with tempfile.TemporaryDirectory(dir=self.tmp.name) as journal:
first = native.publish(plan, before, api, fixture.FakeLease(), journal, declaration=self.ev.declaration())
second = native.publish(plan, before, api, fixture.FakeLease(), journal, declaration=self.ev.declaration())
self.assertEqual(first['mode'], 'created')
self.assertEqual(second['mode'], 'no_op')
self.assertEqual(api.posts, 1)
def test_review_fresh_adoption_blocks_post_if_bundle_changes_before_create(self):
self.ev.set_stage('code-review')
fixture, plan, before, after, exact = self.bound_publication_fixture()
api = fixture.FakeAPI(plan, before, after, exact)
original_snapshot = api.snapshot
calls = {'n': 0}
def mutating_snapshot(target):
calls['n'] += 1
out = original_snapshot(target)
if calls['n'] == 2:
self.ev.bundle.joinpath('src/a.py').write_text('VALUE = 9\n')
return out
api.snapshot = mutating_snapshot
with tempfile.TemporaryDirectory(dir=self.tmp.name) as journal:
with self.assertRaisesRegex(native.ValidationError, 'delegation adoption'):
native.publish(plan, before, api, fixture.FakeLease(), journal, declaration=self.ev.declaration())
self.assertEqual(calls['n'], 2, 'must reach both provider reads and final adoption check')
self.assertTrue((Path(journal) / 'intent.json').exists())
self.assertEqual(api.posts, 0)
# Conservatively preserve durable intent: restoring evidence cannot trigger POST.
self.ev.bundle.joinpath('src/a.py').write_text('VALUE = 1\n')
with self.assertRaises(native.UncertainPublication):
native.publish(plan, before, api, fixture.FakeLease(), journal, declaration=self.ev.declaration())
self.assertEqual(api.posts, 0)
def spec_result(self):
return {'schema': support.SCHEMA,
'target': {'owner': 'acme', 'repository': 'repo', 'issue': 3, 'pull': 7, 'head': self.ev.revision},
'contract_sha256': hashlib.sha256(b'contract').hexdigest(), 'verdict': 'approve',
'criteria': [{'criterion': 'REQ-1', 'contract': 'contract', 'implementation_paths': ['src/a.py'],
'test_paths': [], 'evidence': 'verified', 'result': 'satisfied', 'reason': 'ok'}],
'scope': {'missing': [], 'excess': [], 'spec_conflicts': []}, 'conclusion': 'ok'}
def rework_args(self, declaration):
from types import SimpleNamespace
root = Path(self.tmp.name)
data = {'owner': 'acme', 'repository': 'repo', 'issue': 3, 'pr': 7, 'review': 9,
'reviewed_head': self.ev.revision, 'final_head': self.ev.revision,
'contract_sha256': hashlib.sha256(b'contract').hexdigest(), 'result': 'rework_delivered',
'findings': [{'id': 'F-1', 'status': 'resolved', 'classification': 'valid_fix',
'changes': 'fixed', 'verification': 'passed', 'reason': 'ok'}],
'local_checks': 'passed', 'runtime_browser': 'passed', 'commit_status': 'success', 'actions_runs': '없음'}
(root / 'input.json').write_text(json.dumps(data))
path = root / 'declaration.json'
if declaration is not None:
path.write_text(json.dumps(declaration))
return SimpleNamespace(input=str(root / 'input.json'), comment=str(root / 'denied.md'),
expected=str(root / 'denied.json'), no_delegated_evidence=False,
delegation_adoption=str(path) if declaration is not None else None)
def test_all_consumers_reject_missing_malformed_and_indeterminate_without_effects(self):
for mode in ('missing', 'malformed', 'indeterminate'):
self.ev.result['claims'][0]['status'] = 'indeterminate' if mode == 'indeterminate' else 'supported'
self.ev.write()
declaration = None if mode == 'missing' else {'mode': 'bad'} if mode == 'malformed' else self.ev.declaration()
with self.subTest(mode=mode, consumer='spec'):
self.ev.set_stage('spec-review')
with self.assertRaises(support.ValidationError):
support.render_review(self.spec_result(), {'src/a.py'}, declaration=declaration)
self.ev.set_stage('implementation-delivery')
calls = []
with self.subTest(mode=mode, consumer='delivery'):
with self.assertRaises(delivery.AdoptionError):
delivery.deliver(lambda: calls.append(1), head=self.ev.revision, declaration=declaration)
self.assertEqual(calls, [])
with self.subTest(mode=mode, consumer='rework'):
args = self.rework_args(declaration)
with self.assertRaises(Exception):
rework.do_render(args)
self.assertFalse(Path(args.comment).exists())
self.assertFalse(Path(args.expected).exists())
self.ev.set_stage('code-review')
fixture, plan, before, after, exact = self.bound_publication_fixture()
api = fixture.FakeAPI(plan, before, after, exact)
journal = Path(self.tmp.name) / ('denied-journal-' + mode)
with self.subTest(mode=mode, consumer='review'):
with self.assertRaises(native.ValidationError):
native.publish(plan, before, api, fixture.FakeLease(), journal, declaration=declaration)
self.assertEqual(api.posts, 0)
self.assertEqual(api.events, [])
self.assertFalse(journal.exists())
def test_rework_explicit_none_does_not_bypass_checker_failure(self):
from unittest.mock import patch
from harness.lib.delegation_adoption import AdoptionError
args = self.rework_args(None)
args.no_delegated_evidence = True
with patch('harness.lib.delegation_adoption.require_adoption', side_effect=AdoptionError('rejected')):
try:
rework.do_render(args)
except rework.Bad:
pass
else:
self.fail('explicit none must still invoke common checker')
self.assertFalse(Path(args.comment).exists())
self.assertFalse(Path(args.expected).exists())
def test_spec_cli_rejects_symlink_declaration(self):
self.ev.set_stage('spec-review')
root = Path(self.tmp.name)
(root / 'spec.json').write_text(json.dumps(self.spec_result()))
(root / 'paths.json').write_text(json.dumps(['src/a.py']))
(root / 'real-decl.json').write_text(json.dumps(self.ev.declaration()))
(root / 'alias.json').symlink_to(root / 'real-decl.json')
env = os.environ.copy(); env.pop('PYTHONPATH', None)
cmd = [sys.executable, str(SPEC), 'render', '--result', str(root / 'spec.json'),
'--allowed-paths', str(root / 'paths.json'), '--delegation-adoption', str(root / 'alias.json')]
proc = subprocess.run(cmd, cwd=root, env=env, text=True, capture_output=True, timeout=10)
self.assertNotEqual(proc.returncode, 0)
self.assertEqual(proc.stdout, '')
def test_cli_import_and_safe_declaration_reader_without_pythonpath(self):
self.ev.set_stage('implementation-delivery')
inp = Path(self.tmp.name) / 'rework.json'; comment = Path(self.tmp.name) / 'comment.md'; expected = Path(self.tmp.name) / 'expected.json'
data = {'owner': 'acme', 'repository': 'repo', 'issue': 3, 'pr': 7, 'review': 9, 'reviewed_head': self.ev.revision, 'final_head': self.ev.revision,
'contract_sha256': hashlib.sha256(b'contract').hexdigest(), 'result': 'rework_delivered',
'findings': [{'id': 'F-1', 'status': 'resolved', 'classification': 'valid_fix', 'changes': 'fixed', 'verification': 'passed', 'reason': 'ok'}],
'local_checks': 'passed', 'runtime_browser': 'passed', 'commit_status': 'success', 'actions_runs': '없음'}
inp.write_text(json.dumps(data)); decl = Path(self.tmp.name) / 'decl.json'; decl.write_text(json.dumps(self.ev.declaration()))
env = os.environ.copy(); env.pop('PYTHONPATH', None)
cmd = [sys.executable, str(REWORK), 'render', '--input', str(inp), '--comment', str(comment), '--expected', str(expected), '--delegation-adoption', str(decl)]
run = subprocess.run(cmd, cwd=self.tmp.name, env=env, text=True, capture_output=True)
self.assertEqual(run.returncode, 0, run.stderr)
self.assertTrue(comment.exists() and expected.exists())
os.symlink(self.ev.result_path, Path(self.tmp.name) / 'evil.json')
bad = subprocess.run([sys.executable, str(REWORK), 'render', '--input', str(inp), '--comment', str(comment), '--expected', str(expected), '--delegation-adoption', str(self.tmp.name) + '/evil.json'], cwd=self.tmp.name, env=env, text=True, capture_output=True)
self.assertNotEqual(bad.returncode, 0); self.assertEqual(bad.stdout, '')
if __name__ == '__main__': unittest.main()