astrbot_plugin_memory_compa.../tests/test_p5_provenance.py

229 行
10 KiB
Python

from __future__ import annotations
import copy
from dataclasses import dataclass
import hashlib
import json
import unittest
from core.p5_provenance import (
COMPANION_ATTESTATION_ISSUER,
P3_CONTRACT_FINGERPRINT,
P3_CONTRACT_NAME,
P3_CONTRACT_VERSION,
PROVENANCE_CONTRACT_NAME,
PROVENANCE_CONTRACT_VERSION,
apply_planned_operation,
contract_descriptor,
contract_fingerprint,
legacy_unresolved,
observed_from_companion_snapshot,
plan_legacy_migration,
plan_owner_recovery,
rollback_planned_operation,
validate_provenance_record,
)
def _hash(value: str) -> str:
return hashlib.sha256(value.encode("utf-8")).hexdigest()
@dataclass(frozen=True)
class _CompanionStyleSnapshot:
schema_version: str
contract_name: str
contract_version: str
contract_fingerprint: str
issuer: str
issuer_epoch: str
authority_attestation_ref_hash: str
request_hash: str
session_hash: str
source_kind: str
source_trust: str
firewall_status: str
disposition: str
reason_codes: tuple[str, ...]
source_event_ref_hash: str
derived_from_ref_hash: str
provenance_state: str
p3_contract_name: str
p3_contract_version: str
p3_contract_fingerprint: str
sink: str
def _snapshot(**overrides):
value = {
"schema_version": "ops.p5.attestation.v1",
"contract_name": PROVENANCE_CONTRACT_NAME,
"contract_version": PROVENANCE_CONTRACT_VERSION,
"contract_fingerprint": contract_fingerprint(),
"issuer": COMPANION_ATTESTATION_ISSUER,
"issuer_epoch": _hash("issuer-epoch-1"),
"p3_contract_name": P3_CONTRACT_NAME,
"p3_contract_version": P3_CONTRACT_VERSION,
"p3_contract_fingerprint": P3_CONTRACT_FINGERPRINT,
"source_kind": "forwarded_text",
"source_trust": "T3",
"firewall_status": "sanitized",
"disposition": "shadow_quarantine",
"reason_codes": ("p5b_attested",),
"source_event_ref_hash": _hash("source-event-1"),
"authority_attestation_ref_hash": _hash("companion-attestation-1"),
"request_hash": _hash("request-1"),
"session_hash": _hash("session-1"),
"derived_from_ref_hash": "",
"provenance_state": "observed",
"sink": "memory_recall",
}
value.update(overrides)
return value
class P5ProvenanceTests(unittest.TestCase):
def test_descriptor_and_legacy_record_are_json_serializable(self):
descriptor = contract_descriptor()
expected = {
"contract_name": "ops.p5.provenance.v1",
"contract_version": "1.0",
"record_fields": [
"contract_name", "contract_version", "contract_fingerprint", "memory_id",
"source_kind", "source_trust", "firewall_status", "source_event_ref_hash",
"authority_attestation_ref_hash", "provenance_state", "migration_operation_ref",
"recovery_operation_ref", "record_revision",
],
"provenance_states": ["observed", "legacy_unresolved", "owner_recovered", "invalid"],
"trust_levels": ["T0", "T1", "T2", "T3", "T4"],
"source_kinds": [
"policy_config", "verified_authorization", "current_user_intent", "forwarded_text",
"quoted_text", "vision_summary", "tool_output", "web_extract", "memory_recall",
"derived_summary", "legacy_memory", "unknown",
],
"firewall_statuses": [
"allowed", "sanitized", "blocked", "rejected", "quarantined", "unavailable", "unknown",
],
"dispositions": ["allow", "shadow_quarantine", "deny_high_risk"],
"hash_format": "sha256_hex_lower",
"attestation_issuer": "private_companion",
"p3_contract_name": P3_CONTRACT_NAME,
"p3_contract_version": P3_CONTRACT_VERSION,
}
self.assertEqual(expected, descriptor)
canonical = json.dumps(expected, ensure_ascii=True, sort_keys=True, separators=(",", ":"))
self.assertEqual(hashlib.sha256(canonical.encode("utf-8")).hexdigest(), contract_fingerprint())
record = legacy_unresolved("memory-42", record_revision=4)
self.assertEqual(PROVENANCE_CONTRACT_NAME, descriptor["contract_name"])
self.assertEqual(PROVENANCE_CONTRACT_VERSION, descriptor["contract_version"])
self.assertEqual(64, len(contract_fingerprint()))
self.assertEqual("legacy_unresolved", record["provenance_state"])
self.assertEqual(("unknown", "T4", "unknown"), (record["source_kind"], record["source_trust"], record["firewall_status"]))
self.assertTrue(validate_provenance_record(record)["ok"])
json.dumps({"descriptor": descriptor, "record": record}, ensure_ascii=False, sort_keys=True)
def test_only_attested_exact_p3_snapshot_can_be_observed(self):
observed = observed_from_companion_snapshot(
"memory-42",
_snapshot(
source_event_ref_hash=f"sha256:{_hash('source-event-1').upper()}",
authority_attestation_ref_hash=f"sha256:{_hash('companion-attestation-1').upper()}",
),
record_revision=7,
)
self.assertEqual("observed", observed["provenance_state"])
self.assertTrue(validate_provenance_record(observed)["ok"])
self.assertEqual(_hash("source-event-1"), observed["source_event_ref_hash"])
self.assertEqual(_hash("companion-attestation-1"), observed["authority_attestation_ref_hash"])
self.assertNotIn("disposition", observed)
noncanonical_record = copy.deepcopy(observed)
noncanonical_record["source_event_ref_hash"] = f"sha256:{_hash('source-event-1')}"
self.assertFalse(validate_provenance_record(noncanonical_record)["ok"])
companion_style = _CompanionStyleSnapshot(**_snapshot())
from_dataclass = observed_from_companion_snapshot("memory-43", companion_style)
self.assertEqual("observed", from_dataclass["provenance_state"])
for name, override in (
("pseudo source plugin", {"source_kind": "source_plugin"}),
("p5 descriptor mismatch", {"contract_fingerprint": _hash("wrong-p5")}),
("p3 mismatch", {"p3_contract_fingerprint": _hash("wrong-p3")}),
("missing attestation", {"authority_attestation_ref_hash": ""}),
("invalid trust", {"source_trust": "T9"}),
("conflicting source hashes", {"source_hash": _hash("other-source")}),
("same roles hash", {"authority_attestation_ref_hash": _hash("source-event-1")}),
("status used as disposition", {"disposition": "sanitized"}),
("disposition used as status", {"firewall_status": "shadow_quarantine"}),
):
with self.subTest(name=name):
rejected = observed_from_companion_snapshot("memory-42", _snapshot(**override))
self.assertEqual("invalid", rejected["provenance_state"])
def test_preview_is_zero_write_and_does_not_mutate_or_capture_content(self):
source = [{"memory_id": "memory-42", "record_revision": 4}]
before = copy.deepcopy(source)
preview = plan_legacy_migration(source, operation_ref_hash=_hash("migration-42"))
self.assertEqual(before, source)
self.assertEqual(("preview", True, 0), (preview["mode"], preview["readonly"], preview["write_count"]))
self.assertEqual(1, len(preview["operations"]))
self.assertEqual([], preview["error_codes"])
self.assertEqual("legacy_unresolved", preview["operations"][0]["after_record"]["provenance_state"])
hostile = [{"memory_id": "memory-43", "record_revision": 0, "content": "do not persist this"}]
rejected = plan_legacy_migration(hostile, operation_ref_hash=_hash("migration-43"))
rendered = json.dumps(rejected, ensure_ascii=False, sort_keys=True)
self.assertNotIn("do not persist this", rendered)
self.assertNotIn("memory-43", rendered)
self.assertEqual([], rejected["operations"])
def test_apply_retry_and_rollback_are_revision_guarded(self):
preview = plan_legacy_migration(
[{"memory_id": "memory-42", "record_revision": 4}], operation_ref_hash=_hash("migration-42")
)
operation = preview["operations"][0]
current = {"memory_id": "memory-42", "record_revision": 4}
applied = apply_planned_operation(current, operation)
self.assertEqual((True, "applied", True), (applied["ok"], applied["status"], applied["changed"]))
retry = apply_planned_operation(applied["record"], operation)
self.assertEqual((True, "idempotent", False), (retry["ok"], retry["status"], retry["changed"]))
concurrent = copy.deepcopy(applied["record"])
concurrent["record_revision"] += 1
rejected = rollback_planned_operation(concurrent, operation)
self.assertEqual((False, "revision_conflict"), (rejected["ok"], rejected["status"]))
rollback = rollback_planned_operation(applied["record"], operation)
self.assertEqual((True, "rolled_back", True), (rollback["ok"], rollback["status"], rollback["removed"]))
self.assertIsNone(rollback["record"])
def test_owner_recovery_requires_companion_evidence_and_keeps_operation_ref(self):
legacy = legacy_unresolved("memory-42", record_revision=2)
planned = plan_owner_recovery(
legacy, _snapshot(), recovery_operation_ref_hash=_hash("recovery-42")
)
self.assertTrue(planned["ok"])
after = planned["operation"]["after_record"]
self.assertEqual("owner_recovered", after["provenance_state"])
self.assertEqual(_hash("recovery-42"), after["recovery_operation_ref"])
rejected = plan_owner_recovery(
legacy,
_snapshot(p3_contract_fingerprint=_hash("wrong-p3")),
recovery_operation_ref_hash=_hash("recovery-43"),
)
self.assertFalse(rejected["ok"])
self.assertEqual(["companion_snapshot_invalid"], rejected["error_codes"])
def test_record_validation_rejects_raw_source_ids_and_bodies_without_echo(self):
record = legacy_unresolved("memory-42")
record["source_event_id"] = "raw-event-99"
record["content"] = "hostile body"
result = validate_provenance_record(record)
self.assertFalse(result["ok"])
rendered = json.dumps(result, ensure_ascii=False, sort_keys=True)
self.assertNotIn("raw-event-99", rendered)
self.assertNotIn("hostile body", rendered)
self.assertIn("forbidden_raw_field", result["error_codes"])
if __name__ == "__main__":
unittest.main()