mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
fix(neuron-lab): verify hook callback identities
Include local and global forward/pre-forward hook callbacks, ordering and behavior flags in reset verification. Retain callback references so identity checks cannot be defeated by equality overrides or object ID reuse. Add 22 regressions including exported mismatch receipts and unchanged-hook controls. Refs #390.
This commit is contained in:
3 files changed
+190
-3
No files matched your search
@@ -1127,6 +1127,7 @@
|
||||
"required_tests": [
|
||||
"tests/test_neuron_interventions.py",
|
||||
"tests/test_neuron_lab.py",
|
||||
"tests/test_neuron_lab_reset.py",
|
||||
"tests/test_ui_neuron_lab_wiring.py",
|
||||
"tests/conditional/test_operator_ui.py",
|
||||
"tests/test_neuron_lab_visuals.py",
|
||||
@@ -3122,6 +3123,7 @@
|
||||
"risk": "Browser-local model ownership, bounded uncached comparisons, chat-template formatting, cancellation and redacted immutable exports",
|
||||
"required_tests": [
|
||||
"tests/test_neuron_lab.py",
|
||||
"tests/test_neuron_lab_reset.py",
|
||||
"tests/test_ui_neuron_lab_wiring.py",
|
||||
"tests/conditional/test_operator_ui.py"
|
||||
],
|
||||
|
||||
@@ -471,9 +471,24 @@ class NeuronLabSession:
|
||||
}
|
||||
|
||||
def _hook_signature(self):
|
||||
"""Compare hook registrations, including pre-existing user hooks, by identity."""
|
||||
return tuple((name, tuple(module._forward_pre_hooks), tuple(module._forward_hooks))
|
||||
for name, module in self.handle.model.named_modules())
|
||||
"""Snapshot local/global forward hooks by callback identity and behavior."""
|
||||
# Retain callbacks to prevent ID reuse. Compare IDs before references so
|
||||
# callable __eq__ overrides cannot substitute equality for identity.
|
||||
def snapshot(owner, prefix=""):
|
||||
return tuple(
|
||||
tuple(
|
||||
(key, id(hook), hook,
|
||||
key in getattr(owner, f"{prefix}{name}_with_kwargs", {}),
|
||||
key in getattr(owner, f"{prefix}{name}_always_called", {}))
|
||||
for key, hook in getattr(owner, f"{prefix}{name}").items()
|
||||
)
|
||||
for name in ("_forward_pre_hooks", "_forward_hooks")
|
||||
)
|
||||
|
||||
return (
|
||||
tuple((name, snapshot(module)) for name, module in self.handle.model.named_modules()),
|
||||
snapshot(torch.nn.modules.module, "_global"),
|
||||
)
|
||||
|
||||
@admission_warning_scope()
|
||||
def verify_reset(self):
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
"""Verify reset receipts against local and global forward-hook registrations."""
|
||||
|
||||
from copy import deepcopy
|
||||
import gc
|
||||
import json
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
import weakref
|
||||
|
||||
import pytest
|
||||
from torch.nn.modules import module as torch_module
|
||||
|
||||
from obliteratus.neuron_lab_visuals import render_comparison
|
||||
from tests import test_neuron_lab as lab_fixtures
|
||||
|
||||
|
||||
pytestmark = [pytest.mark.cpu, pytest.mark.integration]
|
||||
bounded_threads = lab_fixtures.bounded_threads
|
||||
local_model = lab_fixtures.local_model
|
||||
session = lab_fixtures.session
|
||||
|
||||
|
||||
class EqualNoopHook:
|
||||
"""Distinct callbacks may compare equal without sharing registration identity."""
|
||||
|
||||
def __call__(self, *args):
|
||||
return None
|
||||
|
||||
def __eq__(self, other):
|
||||
return True
|
||||
|
||||
|
||||
def _hook_site(session, location, kind):
|
||||
owner = session.handle.model if location == "local" else torch_module
|
||||
prefix = "_" if location == "local" else "_global_"
|
||||
method = "register_" if location == "local" else "register_module_"
|
||||
name = f"{prefix}{kind}_hooks"
|
||||
return SimpleNamespace(
|
||||
owner=owner, name=name, registry=getattr(owner, name),
|
||||
register=getattr(owner, f"{method}{kind}_hook"),
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(params=[
|
||||
("local", "forward_pre"), ("local", "forward"),
|
||||
("global", "forward_pre"), ("global", "forward"),
|
||||
], ids=["local-pre", "local-forward", "global-pre", "global-forward"])
|
||||
def hook_site(request, session):
|
||||
return _hook_site(session, *request.param)
|
||||
|
||||
|
||||
def _assert_hook_mismatch(session):
|
||||
original = deepcopy(session.comparison)
|
||||
session.verify_reset()
|
||||
receipt = session.comparison
|
||||
check = receipt["verification"]
|
||||
assert check["status"] == "mismatch"
|
||||
assert check["hook_registry_restored"] is False
|
||||
assert check["token_ids_exact"] is True and check["first_logits_exact"] is True
|
||||
assert check["active_edits"] == 0 and not session.recipe.edits
|
||||
assert receipt["run_id"] == original["run_id"]
|
||||
assert receipt["trials"]["baseline"] == original["trials"]["baseline"]
|
||||
assert receipt["trials"]["recipe"] == original["trials"]["recipe"]
|
||||
assert "Reset verified" not in render_comparison(receipt)
|
||||
session.export_experiment()
|
||||
exported = json.loads(Path(session.experiment_file).read_text())
|
||||
assert exported["schema"] == "obliteratus.neuron-experiment.v1"
|
||||
assert exported["verification"] == check
|
||||
assert all("text" not in trial and "token_ids" not in trial
|
||||
for trial in exported["trials"].values())
|
||||
|
||||
|
||||
def test_reset_rejects_replaced_callback_even_when_callables_compare_equal(session, hook_site):
|
||||
original, replacement = EqualNoopHook(), EqualNoopHook()
|
||||
assert original == replacement and original is not replacement
|
||||
handle = hook_site.register(original)
|
||||
try:
|
||||
session.compare("hello world", 3)
|
||||
hook_site.registry[handle.id] = replacement
|
||||
_assert_hook_mismatch(session)
|
||||
assert hook_site.registry[handle.id] is replacement
|
||||
finally:
|
||||
handle.remove()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("kind", ["forward_pre", "forward"])
|
||||
@pytest.mark.parametrize("change", ["add", "remove"])
|
||||
def test_reset_rejects_changed_global_hook_membership(session, kind, change):
|
||||
site = _hook_site(session, "global", kind)
|
||||
handle = site.register(EqualNoopHook()) if change == "remove" else None
|
||||
try:
|
||||
session.edit("add", 1, 0, 2, "zero")
|
||||
session.compare("hello world", 3)
|
||||
if change == "add":
|
||||
handle = site.register(EqualNoopHook())
|
||||
else:
|
||||
handle.remove()
|
||||
_assert_hook_mismatch(session)
|
||||
assert (handle.id in site.registry) == (change == "add")
|
||||
finally:
|
||||
if handle is not None:
|
||||
handle.remove()
|
||||
|
||||
|
||||
def test_reset_preserves_unchanged_external_hooks_across_repeated_checks(session, hook_site):
|
||||
callback = EqualNoopHook()
|
||||
handle = hook_site.register(callback)
|
||||
try:
|
||||
session.compare("hello world", 3)
|
||||
run_id = session.comparison["run_id"]
|
||||
for _ in range(2):
|
||||
session.verify_reset()
|
||||
assert session.comparison["verification"]["status"] == "passed"
|
||||
assert session.comparison["verification"]["hook_registry_restored"] is True
|
||||
assert session.comparison["run_id"] == run_id
|
||||
assert hook_site.registry[handle.id] is callback
|
||||
finally:
|
||||
handle.remove()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("location,kind,flag", [
|
||||
("local", "forward_pre", "with_kwargs"),
|
||||
("local", "forward", "with_kwargs"),
|
||||
("local", "forward", "always_called"),
|
||||
("global", "forward", "with_kwargs"),
|
||||
("global", "forward", "always_called"),
|
||||
])
|
||||
def test_reset_rejects_changed_hook_behavior_flags(session, monkeypatch, location, kind, flag):
|
||||
site = _hook_site(session, location, kind)
|
||||
handle = site.register(EqualNoopHook())
|
||||
try:
|
||||
session.compare("hello world", 3)
|
||||
flags = getattr(site.owner, f"{site.name}_{flag}")
|
||||
monkeypatch.setitem(flags, handle.id, True)
|
||||
_assert_hook_mismatch(session)
|
||||
assert handle.id in site.registry and handle.id in flags
|
||||
finally:
|
||||
handle.remove()
|
||||
|
||||
|
||||
def test_reset_rejects_changed_hook_order(session, hook_site):
|
||||
first = hook_site.register(EqualNoopHook())
|
||||
second = hook_site.register(EqualNoopHook())
|
||||
try:
|
||||
session.compare("hello world", 3)
|
||||
hook_site.registry.move_to_end(first.id)
|
||||
_assert_hook_mismatch(session)
|
||||
assert list(hook_site.registry)[-2:] == [second.id, first.id]
|
||||
finally:
|
||||
second.remove()
|
||||
first.remove()
|
||||
|
||||
|
||||
def test_reset_reference_retains_callback_until_invalidated(session):
|
||||
site = _hook_site(session, "local", "forward")
|
||||
callback = EqualNoopHook()
|
||||
reference = weakref.ref(callback)
|
||||
handle = site.register(callback)
|
||||
try:
|
||||
session.compare("hello world", 3)
|
||||
site.registry[handle.id] = EqualNoopHook()
|
||||
del callback
|
||||
gc.collect()
|
||||
assert reference() is not None # An unretained object ID could be reused.
|
||||
_assert_hook_mismatch(session)
|
||||
session.reset()
|
||||
gc.collect()
|
||||
assert reference() is None
|
||||
finally:
|
||||
handle.remove()
|
||||
Reference in new issue
Block a user