Repository navigation
Expand file tree
/
Copy pathtest_log_redaction_copies.py
More file actions
73 lines (59 loc) · 3.12 KB
/
Copy pathtest_log_redaction_copies.py
File metadata and controls
73 lines (59 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""The log redaction module's two copies agree, and the shared one stays portable.
agentic/log_redaction.py (imported by the agent's logging_config) and
recon_orchestrator/log_archive/redaction.py (the orchestrator redacts scan detail
after the run) must be byte-identical: one pattern list, so an agent log and a
scan log archived under the same `redaction` version were redacted alike. The
orchestrator image has none of the agent's modules, so the shared file may import
only the standard library.
Both copies are loaded by path under unique module names, as
tests/test_sandbox_contract.py does: this section puts agentic/ on sys.path, so a
plain import could compare one copy with itself.
"""
from __future__ import annotations
import ast
import importlib.util
import os
import sys
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
AGENT_REL = "agentic/log_redaction.py"
ORCH_REL = "recon_orchestrator/log_archive/redaction.py"
AGENT_PATH = os.path.join(ROOT, *AGENT_REL.split("/"))
ORCH_PATH = os.path.join(ROOT, *ORCH_REL.split("/"))
def _load(path: str, name: str):
spec = importlib.util.spec_from_file_location(name, path)
mod = importlib.util.module_from_spec(spec)
sys.modules[name] = mod
spec.loader.exec_module(mod)
return mod
def _read(path: str) -> bytes:
with open(path, "rb") as f:
return f.read()
def test_the_two_copies_are_byte_identical():
assert os.path.realpath(AGENT_PATH) != os.path.realpath(ORCH_PATH), "not two real copies"
agent, orch = _read(AGENT_PATH), _read(ORCH_PATH)
assert agent, f"{AGENT_REL} is empty"
assert agent == orch, (
f"{ORCH_REL} differs from {AGENT_REL}. The agent copy is the one to edit: "
f"copy {AGENT_REL} over {ORCH_REL} (cp {AGENT_REL} {ORCH_REL}), and bump "
"REDACTION_VERSION if the patterns changed")
def test_both_copies_load_on_their_own_and_carry_the_same_version():
agent_copy = _load(AGENT_PATH, "_agent_log_redaction")
orch_copy = _load(ORCH_PATH, "_orch_log_archive_redaction")
assert agent_copy.__file__ != orch_copy.__file__
assert agent_copy.REDACTION_VERSION == orch_copy.REDACTION_VERSION
sample = "curl -H 'Cookie: sid=FAKE-SESSION-0001' https://app.example.com/"
assert orch_copy.redact_text(sample) == agent_copy.redact_text(sample) != sample
def test_the_shared_module_imports_only_the_standard_library():
tree = ast.parse(_read(AGENT_PATH).decode("utf-8"), filename=AGENT_REL)
imported = set()
for node in ast.walk(tree):
if isinstance(node, ast.Import):
imported.update(alias.name.split(".")[0] for alias in node.names)
elif isinstance(node, ast.ImportFrom):
assert node.level == 0, f"{AGENT_REL} uses a relative import; the copy has no package"
imported.add(node.module.split(".")[0])
assert imported, "no import found: the walk is not reading the module"
foreign = sorted(imported - set(sys.stdlib_module_names) - {"__future__"})
assert not foreign, (
f"{AGENT_REL} imports {foreign}; its copy runs in the orchestrator image, "
"which has only the standard library and none of the agent's modules")