feat(tracker): Atlas joining DLL snapshots with emulator registrations

Matches original script APIs against MethodSpec registrations in
Rex-EMoolator and keeps review evidence separate from registration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Patryk Gensch
2026-10-24 18:24:15 +02:00
co-authored by Claude Opus 5.5
parent b0013c5114
commit 10c81c4390
13 changed files with 846 additions and 1 deletions
+174
View File
@@ -0,0 +1,174 @@
from copy import deepcopy
import json
from pathlib import Path
import pytest
from ams.snapshot import Snapshot
from ams.tracker import build_tracker, load_reviews, scan_emulator
SHA = "a" * 64
def emulator(tmp_path):
directory = tmp_path / "core/src/pl/genschu/bloomooemulator/interpreter/variable"
directory.mkdir(parents=True)
(directory / "Variable.java").write_text('''interface Variable {
Map.entry("CLONE", MethodSpec.of((s,a,c) -> clone(s)));
}''')
(directory / "BoolVariable.java").write_text('''record BoolVariable() {
VariableType type() { return VariableType.BOOLEAN; }
// Map.entry("FAKE", MethodSpec.of((s,a,c) -> null));
String misleading = "Map.entry(\\"FAKE2\\", MethodSpec.of(";
Map.entry("SET", MethodSpec.of((s,a,c) -> {
String x = ") /*"; // )
return new Value(a.get(0));
}));
Map.entry("TODO", MethodSpec.of((s,a,c) -> {
throw new UnsupportedOperationException("todo");
}));
Map.entry("NOOP", MethodSpec.of((s,a,c) -> MethodResult.noReturn()));
}''')
result = scan_emulator(tmp_path)
result["commit"] = "f" * 40
return result
def native(sha=SHA):
return Snapshot({"binary": {"name": "test.dll", "sha256": sha},
"types": [{"script_name": "BOOL", "cpp_class": "CMC_Bool"},
{"script_name": "UNKNOWN", "cpp_class": None}],
"methods": [{"owner": "CMC_Bool", "name": "set", "id": 1},
{"owner": "CMC", "name": "CLONE", "id": 1},
{"owner": "CMC", "name": "SET", "id": 2}],
"method_inheritance": [{"runner": "CMC_Bool_Runner", "base_runner": "CMC_Runner"}],
"method_dispatch": [{"owner": "CMC_Bool", "id": 1, "impl_addr": "0x10001000"}]})
def row(result, typ="BOOLEAN", method="SET"):
return next(m for t in result["types"] if t["name"] == typ for m in t["methods"] if m["name"] == method)
def review(emu, **changes):
return {"type": "BOOLEAN", "method": "SET", "binary_sha256": SHA, "rva": "0x1000",
"emulator_commit": emu["commit"], "body_sha256": emu["types"]["BOOLEAN"]["methods"]["SET"]["source"]["body_sha256"],
"source_tree_sha256": emu["source_tree_sha256"], "reviewer": "test", "date": "2026-10-10",
"notes": "Checked integer input", "evidence": "evidence/example.json#method", "kind": "asm",
"verdict": "matches", **changes}
def test_java_adapter_ignores_comments_literals_and_balances(tmp_path):
emu = emulator(tmp_path)
methods = emu["types"]["BOOLEAN"]["methods"]
assert set(methods) == {"SET", "TODO", "NOOP"}
assert methods["TODO"]["status"] == "stub_candidate"
assert methods["NOOP"]["status"] == "registered"
assert methods["SET"]["source"]["line"] == 5
def test_inheritance_aliases_and_globals_are_not_implementation_proof(tmp_path):
result = build_tracker([native()], emulator(tmp_path))
assert row(result)["status"] == "registered"
assert row(result)["verification"] == "unverified"
assert row(result)["original"][0]["owner"] == "CMC_Bool"
assert len(row(result)["original"]) == 1 # child wins over SET on base
assert row(result, method="CLONE")["source"]["owner"] == "*"
assert row(result, method="CLONE")["original"][0]["inherited"]
assert next(t for t in result["types"] if t["name"] == "UNKNOWN")["methods"] == []
def test_unknown_hash_not_fabricated_and_method_ids_are_binary_local(tmp_path):
a, b = native(), native(None)
b.raw["method_dispatch"][0]["impl_addr"] = "0x10009999"
result = build_tracker([a,b], emulator(tmp_path))
assert {o["impl_addr"] for o in row(result)["original"]} == {"0x10001000", "0x10009999"}
assert any(b["sha256"] is None and b["id"].startswith("snapshot:") for b in result["binaries"])
def test_review_stales_on_source_or_commit_change_and_covers_only_one_binary(tmp_path):
emu = emulator(tmp_path)
r = review(emu)
assert row(build_tracker([native()], emu, [r]))["verification"] == "asm_reviewed"
changed = deepcopy(emu)
changed["source_tree_sha256"] = "b" * 64 # includes helper changes, not just lambda
assert row(build_tracker([native()], changed, [r]))["verification"] == "stale"
changed = deepcopy(emu)
changed["commit"] = "d" * 40
assert row(build_tracker([native()], changed, [r]))["verification"] == "stale"
multiple = build_tracker([native(),native("c"*64)], emu, [r])
assert row(multiple)["verification"] == "unverified"
assert row(multiple)["verified_variants"] == 1
def test_review_without_required_anchors_rejected(tmp_path):
path = tmp_path / "reviews.json"
path.write_text(json.dumps({"schema":"tracker-reviews/1", "reviews":[{"type":"BOOLEAN"}]}))
with pytest.raises(ValueError): load_reviews(path)
emu = emulator(tmp_path / "repo")
path.write_text(json.dumps({"schema":"tracker-reviews/1", "reviews":[review(emu),review(emu)]}))
with pytest.raises(ValueError, match="Duplicate"): load_reviews(path)
def test_kolorowanka_registration_is_not_behavior_verification(tmp_path):
snap = Snapshot.load(str(Path(__file__).parents[1] / "snapshots/kolorowanka.snapshot.json"))
result = build_tracker([snap], emulator(tmp_path))
t = next(t for t in result["types"] if t["name"] == "KOLOROWANKA")
assert len(t["methods"]) == 23
assert all(m["verification"] == "unverified" for m in t["methods"])
assert row(result,"KOLOROWANKA","GETENDCOLOR")["original"][0]["evidence_ids"]
def test_duplicate_native_name_and_id_preserved(tmp_path):
snap = native()
snap.raw["methods"].extend([
{"owner": "CMC_Bool", "name": "SET", "id": 3},
{"owner": "CMC_Bool", "name": "OTHER", "id": 3},
])
result = build_tracker([snap], emulator(tmp_path))
assert {o["method_id"] for o in row(result)["original"]} == {1, 3}
assert len(row(result)["notes"]) == 2
assert len(row(result, method="OTHER")["notes"]) == 1
def test_world_snapshot_joins_and_keeps_registration_ambiguity(tmp_path):
snap = Snapshot.load(str(Path(__file__).parents[1] / "snapshots/world.snapshot.json"))
result = build_tracker([snap], emulator(tmp_path))
world = next(t for t in result["types"] if t["name"] == "WORLD")
assert len(world["methods"]) == 66
position = row(result, "WORLD", "SETPOSITION")
assert {o["method_id"] for o in position["original"]} == {24, 43}
assert len(position["notes"]) == 2
assert result["binaries"][0]["limitations"]
def test_known_class_aliases_do_not_create_false_missing_types(tmp_path):
snap = Snapshot({"binary": {"sha256": SHA}, "types": [
{"script_name": "CANVAS_OBSERVER", "cpp_class": None},
{"script_name": "GROUP", "cpp_class": None}], "methods": [
{"owner": "CMC_CanvasObserver", "name": "REFRESH", "id": None},
{"owner": "CMC_Groupped", "name": "ADD", "id": None}]})
result = build_tracker([snap], emulator(tmp_path))
assert row(result, "CANVAS_OBSERVER", "REFRESH")["original"][0]["mapping"] == "inferred"
assert row(result, "GROUP", "ADD")["original"]
assert not {"CANVASOBSERVER", "GROUPPED"} & {t["name"] for t in result["types"]}
def test_api_configuration_and_download(tmp_path, monkeypatch):
from fastapi.testclient import TestClient
from ams.api.app import create_app
monkeypatch.delenv("AMS_EMULATOR_ROOT", raising=False)
monkeypatch.delenv("AMS_TRACKER_REVIEWS", raising=False)
monkeypatch.delenv("AMS_TRACKER_SNAPSHOTS", raising=False)
client = TestClient(create_app("sqlite:///" + str(tmp_path / "test.db")))
assert client.get("/tracker").status_code == 503
emulator(tmp_path / "repo")
monkeypatch.setenv("AMS_EMULATOR_ROOT", str(tmp_path / "repo"))
snaps = tmp_path / "snapshots"
snaps.mkdir()
(snaps / "test.snapshot.json").write_text(json.dumps(native().raw))
monkeypatch.setenv("AMS_TRACKER_SNAPSHOTS", str(snaps))
response = client.get("/tracker?download=1")
assert response.status_code == 200
assert "attachment" in response.headers["content-disposition"]
assert response.json()["schema"] == "tracker/1"
assert client.get("/ui/tracker.html").status_code == 200
+72
View File
@@ -0,0 +1,72 @@
import json
import pytest
pytest.importorskip('fastapi')
from fastapi.testclient import TestClient
from ams.api.app import create_app
from ams.api.routes.tracker import inventory_status, order_snapshots
from ams.snapshot import Snapshot
def test_fresh_snapshot_preferred_and_conflict_reported():
old = Snapshot({'binary': {'sha256': 'a', 'name': 'golden'}})
fresh = Snapshot({'binary': {'sha256': 'a', 'name': 'batch', 'extraction': {'fingerprint': 'v2'}}})
ordered, warnings = order_snapshots([old, fresh])
assert ordered == [fresh, old]
assert len(warnings) == 1
assert 'nie scalono' in warnings[0]
assert order_snapshots([old, old]) == ([old], [])
def test_batch_library_and_game_aliases_reach_compat(tmp_path):
from ams.compat import _library_name
payload = {'binary': {'sha256': 'a'*64, 'name': 'Game__hash__PIKLIB8.DLL',
'library': 'PIKLIB8.DLL', 'games': ['Game A','Game B']}, 'types': []}
assert _library_name(Snapshot(payload)) == 'PIKLIB8.DLL'
with TestClient(create_app(database_url=f'sqlite:///{tmp_path}/compat.db')) as client:
assert client.post('/snapshots',json=payload).status_code == 201
data = client.get('/compat?format=json').json()
assert data['games'] == ['Game A', 'Game B']
assert len(data['libraries']) == 1
assert data['libraries'][0]['name'] == 'PIKLIB8.DLL'
def test_inventory_counts_by_hash_and_scope(tmp_path):
path = tmp_path / 'inventory.json'
path.write_text(json.dumps({'builds': [
{'sha256': 'AAA', 'scope': 'engine', 'analysis': 'pending'},
{'sha256': 'bbb', 'scope': 'engine'},
{'sha256': 'ccc', 'scope': 'plugin'},
{'sha256': 'ddd', 'scope': 'native_dependency'},
]}))
result = inventory_status(path, [{'sha256': 'aaa'}, {'sha256': 'eee'}])
assert result['scopes']['engine'] == {'target': 2, 'loaded': 1, 'pending': 1}
assert result['scopes']['plugin']['pending'] == 1
assert result['scopes']['native_dependency']['pending'] == 1
assert result['builds'][0]['snapshot_loaded'] is True
assert result['unlisted_snapshots'] == 1
def test_recursive_endpoint_prefers_batch(tmp_path, monkeypatch):
from ams.api.routes import tracker
snapshots = tmp_path / 'snapshots'
nested = snapshots / 'batch' / 'by-hash'
nested.mkdir(parents=True)
sha = 'a' * 64
(snapshots / 'golden.snapshot.json').write_text(json.dumps({'binary': {'sha256': sha, 'name': 'old'}}))
(nested / 'fresh.snapshot.json').write_text(json.dumps({'binary': {'sha256': sha, 'name': 'fresh', 'extraction': {'fingerprint': 'v2'}}}))
inventory = tmp_path / 'inventory.json'
inventory.write_text(json.dumps({'builds': [{'sha256': sha, 'scope': 'engine'}]}))
monkeypatch.setenv('AMS_EMULATOR_ROOT', str(tmp_path))
monkeypatch.setenv('AMS_TRACKER_SNAPSHOTS', str(snapshots))
monkeypatch.setenv('AMS_TRACKER_INVENTORY', str(inventory))
monkeypatch.delenv('AMS_TRACKER_REVIEWS', raising=False)
monkeypatch.setattr(tracker, 'scan_emulator', lambda root: {'types': {}, 'warnings': []})
with TestClient(create_app(database_url=f'sqlite:///{tmp_path}/test.db')) as client:
response = client.get('/tracker')
assert response.status_code == 200, response.text
result = response.json()
assert [b['name'] for b in result['binaries']] == ['fresh']
assert result['inventory']['scopes']['engine']['loaded'] == 1
assert any('nie scalono' in w for w in result['warnings'])