Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

10.9 KB
Raw Blame
#!/usr/bin/env python3
"""Focused contract tests for post-restart Pixel access-mode reproof."""
import copy
import errno
import contextlib
import importlib.util
import io
import json
import pathlib
import sys
import socket
import tempfile
import unittest
from unittest.mock import patch


HERE = pathlib.Path(__file__).resolve().parent
MODULE = HERE.parent / "bin/pixel_access_reconcile.py"
spec = importlib.util.spec_from_file_location("pixel_access_reconcile", MODULE)
reconcile = importlib.util.module_from_spec(spec)
with patch.object(pathlib.Path, "lstat", autospec=True) as lstat:
    lstat.return_value.st_mode = 0o100644
    lstat.return_value.st_uid = 0
    sys.modules[spec.name] = reconcile
    spec.loader.exec_module(reconcile)


def projection(**changes):
    value = {
        "available": True,
        "scope": "owner-host",
        "configured_mode": "sandboxed",
        "effective_mode": "sandboxed",
        "runtime_verified": True,
        "revision": "a" * 64,
        "busy": False,
        "pending": False,
        "reason": None,
    }
    value.update(changes)
    return value


class ReconcileTests(unittest.TestCase):
    def test_real_unix_socket_missing_or_not_listening_is_preflight_only(self):
        import pixel_access_client
        with tempfile.TemporaryDirectory(prefix='ods-access-') as root:
            endpoint = str(pathlib.Path(root) / 'control.sock')
            with patch.object(pixel_access_client, 'ACCESS_SOCKET_PATH', endpoint):
                with self.assertRaises(reconcile.ReconcileError) as missing:
                    reconcile.reconcile(pixel_access_client.request_access)
                self.assertEqual(missing.exception.stage, 'status-transport-unavailable')
                with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as server:
                    server.bind(endpoint)
                    with self.assertRaises(reconcile.ReconcileError) as refused:
                        reconcile.reconcile(pixel_access_client.request_access)
                    self.assertEqual(refused.exception.stage, 'status-transport-unavailable')

    def test_read_only_transport_failures_are_retryable_without_private_errors(self):
        for code in (errno.ENOENT, errno.ECONNREFUSED, errno.ECONNRESET, errno.ETIMEDOUT):
            calls = []
            def request(operation, body=None):
                calls.append(operation)
                raise OSError(code, 'private socket path')
            output = io.StringIO()
            with contextlib.redirect_stderr(output):
                self.assertEqual(reconcile.main(request, startup=True), 1)
            self.assertEqual(json.loads(output.getvalue())['stage'], 'status-transport-unavailable')
            self.assertNotIn('private socket path', output.getvalue())
            self.assertEqual(calls, ['status'])

    def test_permission_denial_is_not_classified_as_startup_delay(self):
        def request(operation, body=None):
            raise PermissionError(errno.EACCES, 'private path')
        with self.assertRaises(PermissionError):
            reconcile.reconcile(request)

    def test_transport_failure_after_change_is_never_retryable_preflight(self):
        calls = []
        def request(operation, body=None):
            calls.append(operation)
            if operation == 'status':
                return 200, projection(effective_mode='unknown', runtime_verified=False,
                                       reason='runtime-proof-required')
            raise ConnectionResetError(errno.ECONNRESET, 'private path')
        output = io.StringIO()
        with contextlib.redirect_stderr(output):
            self.assertEqual(reconcile.main(request, startup=True), 1)
        self.assertEqual(json.loads(output.getvalue())['stage'], 'client-exception')
        self.assertEqual(calls, ['status', 'change'])

    def test_startup_does_not_restore_even_a_pending_sandbox_transition(self):
        calls = []
        def request(operation, body=None):
            calls.append(operation)
            return 200, projection(effective_mode='unknown', runtime_verified=False,
                                   pending=True, reason='transition-recovery-required')
        with self.assertRaises(reconcile.ReconcileError):
            reconcile.reconcile(request, allow_safe_restore=False)
        self.assertEqual(calls, ['status'])

    def test_diagnostic_projection_retains_only_bounded_coordinator_error(self):
        diagnostic = reconcile.diagnostic_projection({
            "error": "runtime-proof-failed",
            "private": "discard-me",
        })
        self.assertEqual(diagnostic["coordinator_error"], "runtime-proof-failed")
        self.assertNotIn("private", diagnostic)
        self.assertNotIn("coordinator_error",
                         reconcile.diagnostic_projection({"error": "INVALID"}))

    def test_ready_projection_is_read_only(self):
        calls = []
        def request(operation, body=None):
            calls.append((operation, body))
            return 200, projection()
        value, changed = reconcile.reconcile(request)
        self.assertFalse(changed)
        self.assertEqual(value["effective_mode"], "sandboxed")
        self.assertEqual(calls, [("status", None)])

    def test_exact_unverified_projection_is_reproved_in_same_mode(self):
        calls = []
        before = projection(effective_mode="unknown", runtime_verified=False,
                            reason="runtime-proof-required")
        def request(operation, body=None):
            calls.append((operation, body))
            return (200, before) if operation == "status" else (200, projection())
        value, changed = reconcile.reconcile(request)
        self.assertTrue(changed)
        self.assertTrue(value["runtime_verified"])
        self.assertEqual(calls[1], ("change", {
            "mode": "sandboxed", "revision": "a" * 64, "confirmed": False}))

    def test_existing_full_access_uses_explicit_same_mode_confirmation(self):
        before = projection(configured_mode="full-access", effective_mode="unknown",
                            runtime_verified=False, reason="runtime-proof-required")
        after = projection(configured_mode="full-access", effective_mode="full-access")
        calls = []
        def request(operation, body=None):
            calls.append((operation, body))
            return (200, before) if operation == "status" else (200, after)
        _value, changed = reconcile.reconcile(request)
        self.assertTrue(changed)
        self.assertIs(calls[1][1]["confirmed"], True)

    def test_pending_sandboxed_transition_uses_coordinator_restore(self):
        before = projection(effective_mode="unknown", runtime_verified=False,
                            pending=True, reason="transition-recovery-required")
        calls = []
        def request(operation, body=None):
            calls.append((operation, body))
            return (200, before) if operation == "status" else (200, projection())
        value, changed = reconcile.reconcile(request)
        self.assertTrue(changed)
        self.assertTrue(value["runtime_verified"])
        self.assertEqual(calls[1], ("change", {
            "mode": "sandboxed", "revision": "a" * 64, "confirmed": False}))

    def test_pending_full_access_transition_never_mutates(self):
        before = projection(configured_mode="full-access", effective_mode="unknown",
                            runtime_verified=False, pending=True,
                            reason="transition-recovery-required")
        calls = []
        def request(operation, body=None):
            calls.append((operation, body))
            return 200, before
        with self.assertRaises(RuntimeError):
            reconcile.reconcile(request)
        self.assertEqual(calls, [("status", None)])

    def test_unsafe_or_ambiguous_states_never_mutate(self):
        base = projection(effective_mode="unknown", runtime_verified=False,
                          reason="runtime-proof-required")
        variants = [
            {"available": False}, {"busy": True}, {"pending": True, "scope": "edge"},
            {"configured_mode": "unknown"}, {"reason": "transition-recovery-required"},
            {"revision": "not-a-revision"}, {"scope": "edge"},
        ]
        for changes in variants:
            with self.subTest(changes=changes):
                value = copy.deepcopy(base)
                value.update(changes)
                calls = []
                def request(operation, body=None):
                    calls.append((operation, body))
                    return 200, value
                with self.assertRaises(RuntimeError):
                    reconcile.reconcile(request)
                self.assertEqual(calls, [("status", None)])

    def test_change_must_return_a_ready_projection(self):
        before = projection(effective_mode="unknown", runtime_verified=False,
                            reason="runtime-proof-required")
        def request(operation, _body=None):
            return (200, before) if operation == "status" else (200, before)
        with self.assertRaises(RuntimeError):
            reconcile.reconcile(request)

    def test_unsafe_state_retains_only_bounded_projection(self):
        unsafe = projection(effective_mode="unknown", runtime_verified=False,
                            reason="transition-recovery-required",
                            revision="b" * 64, private_value="do-not-log")
        with self.assertRaises(reconcile.ReconcileError) as raised:
            reconcile.reconcile(lambda _operation, _body=None: (200, unsafe))
        diagnostic = raised.exception.diagnostic()
        self.assertEqual(diagnostic["stage"], "unsafe-state")
        self.assertEqual(diagnostic["projection"]["reason"], "transition-recovery-required")
        self.assertNotIn("revision", diagnostic["projection"])
        self.assertNotIn("private_value", diagnostic["projection"])

    def test_main_emits_structured_change_failure_without_private_fields(self):
        before = projection(effective_mode="unknown", runtime_verified=False,
                            reason="runtime-proof-required")
        rejected = projection(effective_mode="unknown", runtime_verified=False,
                              reason="transition-recovery-required",
                              private_value="do-not-log")
        calls = []
        def request(operation, _body=None):
            calls.append(operation)
            return (200, before) if operation == "status" else (409, rejected)
        output = io.StringIO()
        with contextlib.redirect_stderr(output):
            self.assertEqual(reconcile.main(request), 1)
        diagnostic = json.loads(output.getvalue())
        self.assertEqual(diagnostic["error"], "pixel-access-reproof-failed")
        self.assertEqual(diagnostic["stage"], "change-failed")
        self.assertEqual(diagnostic["httpStatus"], 409)
        self.assertEqual(diagnostic["projection"]["reason"], "transition-recovery-required")
        self.assertNotIn("private_value", output.getvalue())
        self.assertEqual(calls, ["status", "change"])


if __name__ == "__main__":
    unittest.main()