feat(c5): add provenance ledger and bridge gates
这个提交存在于:
父节点
44b9359d59
当前提交
159bf5a63d
共有 7 个文件被更改,包括 1408 次插入 和 6 次删除
|
|
@ -667,6 +667,18 @@
|
|||
"type": "bool",
|
||||
"default": true
|
||||
},
|
||||
"enable_p5_b1_recall_gate": {
|
||||
"description": "启用 P5-B1 召回闸门",
|
||||
"hint": "默认关闭。开启后普通记忆注入和主动 recall 需要陪伴侧当前请求的一次性 attestation;无效时返回明确 degraded。",
|
||||
"type": "bool",
|
||||
"default": false
|
||||
},
|
||||
"enable_p5_b1_bridge_gate": {
|
||||
"description": "启用 P5-B1 Bridge 闸门",
|
||||
"hint": "默认关闭。开启后 Bridge 搜索、上下文和序列化入口需要一次性 attestation;不会删除原版娱乐写入。",
|
||||
"type": "bool",
|
||||
"default": false
|
||||
},
|
||||
"schedule_fast_context_enabled": {
|
||||
"description": "启用日程快速上下文",
|
||||
"hint": "让陪伴插件读取日程连续性时使用本地只读候选,不调用 Embedding 或 Rerank;关闭后回退完整检索。",
|
||||
|
|
|
|||
|
|
@ -393,8 +393,16 @@ class MemoryCompanionBridge:
|
|||
*,
|
||||
session_context: SessionContext | dict[str, Any] | None = None,
|
||||
top_k: int | None = None,
|
||||
p5_attestation: Any = None,
|
||||
p5_attestation_consumer: Any = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
return await self._plugin.bridge_search(query, session_context=session_context, top_k=top_k)
|
||||
return await self._plugin.bridge_search(
|
||||
query,
|
||||
session_context=session_context,
|
||||
top_k=top_k,
|
||||
p5_attestation=p5_attestation,
|
||||
p5_attestation_consumer=p5_attestation_consumer,
|
||||
)
|
||||
|
||||
async def compose_injection(
|
||||
self,
|
||||
|
|
@ -405,6 +413,8 @@ class MemoryCompanionBridge:
|
|||
max_chars: int | None = None,
|
||||
companion_bot_mood: str = "",
|
||||
companion_bot_energy: float = 0.0,
|
||||
p5_attestation: Any = None,
|
||||
p5_attestation_consumer: Any = None,
|
||||
) -> str:
|
||||
return await self._plugin.bridge_compose_injection(
|
||||
query,
|
||||
|
|
@ -413,6 +423,8 @@ class MemoryCompanionBridge:
|
|||
max_chars=max_chars,
|
||||
companion_bot_mood=companion_bot_mood,
|
||||
companion_bot_energy=companion_bot_energy,
|
||||
p5_attestation=p5_attestation,
|
||||
p5_attestation_consumer=p5_attestation_consumer,
|
||||
)
|
||||
|
||||
async def compose_context(
|
||||
|
|
@ -425,6 +437,8 @@ class MemoryCompanionBridge:
|
|||
companion_bot_mood: str = "",
|
||||
companion_bot_energy: float = 0.0,
|
||||
retrieval_profile: str = "",
|
||||
p5_attestation: Any = None,
|
||||
p5_attestation_consumer: Any = None,
|
||||
) -> str:
|
||||
return await self._plugin.bridge_compose_context(
|
||||
query=query,
|
||||
|
|
@ -434,13 +448,74 @@ class MemoryCompanionBridge:
|
|||
companion_bot_mood=companion_bot_mood,
|
||||
companion_bot_energy=companion_bot_energy,
|
||||
retrieval_profile=retrieval_profile,
|
||||
p5_attestation=p5_attestation,
|
||||
p5_attestation_consumer=p5_attestation_consumer,
|
||||
)
|
||||
|
||||
async def remember(self, *, event: Any, content: str, note_type: str = "memory") -> dict[str, Any]:
|
||||
return await self._plugin.tool_remember(event, content, note_type=note_type)
|
||||
|
||||
async def recall(self, *, event: Any, query: str, top_k: int = 5) -> dict[str, Any]:
|
||||
return await self._plugin.tool_recall(event, query, top_k=top_k)
|
||||
async def recall(
|
||||
self,
|
||||
*,
|
||||
event: Any,
|
||||
query: str,
|
||||
top_k: int = 5,
|
||||
p5_attestation: Any = None,
|
||||
p5_attestation_consumer: Any = None,
|
||||
) -> dict[str, Any]:
|
||||
return await self._plugin.tool_recall(
|
||||
event,
|
||||
query,
|
||||
top_k=top_k,
|
||||
p5_attestation=p5_attestation,
|
||||
p5_attestation_consumer=p5_attestation_consumer,
|
||||
)
|
||||
|
||||
def p5_capability_status(self) -> dict[str, Any]:
|
||||
getter = getattr(self._plugin, "p5_capability_status", None)
|
||||
if not callable(getter):
|
||||
return {"state": "degraded", "error_code": "p5_status_unavailable"}
|
||||
try:
|
||||
result = getter()
|
||||
except Exception:
|
||||
return {"state": "degraded", "error_code": "p5_status_exception"}
|
||||
return dict(result) if isinstance(result, dict) else {"state": "degraded", "error_code": "p5_status_invalid"}
|
||||
|
||||
def provenance_snapshot(self) -> dict[str, Any]:
|
||||
getter = getattr(self._plugin, "provenance_snapshot", None)
|
||||
if not callable(getter):
|
||||
return {"records": {}, "operation_count": 0, "state": "degraded"}
|
||||
result = getter()
|
||||
return dict(result) if isinstance(result, dict) else {"records": {}, "operation_count": 0, "state": "degraded"}
|
||||
|
||||
def provenance_preview(self, candidates: list[dict[str, Any]], *, operation_ref_hash: str) -> dict[str, Any]:
|
||||
getter = getattr(self._plugin, "provenance_preview", None)
|
||||
if not callable(getter):
|
||||
return {"mode": "preview", "readonly": True, "write_count": 0, "error_codes": ["unavailable"]}
|
||||
result = getter(candidates, operation_ref_hash=operation_ref_hash)
|
||||
return dict(result) if isinstance(result, dict) else {"mode": "preview", "readonly": True, "write_count": 0, "error_codes": ["invalid_result"]}
|
||||
|
||||
async def provenance_apply(self, operation: dict[str, Any]) -> dict[str, Any]:
|
||||
getter = getattr(self._plugin, "provenance_apply", None)
|
||||
if not callable(getter):
|
||||
return {"ok": False, "state": "degraded", "error_code": "unavailable"}
|
||||
result = await getter(operation)
|
||||
return dict(result) if isinstance(result, dict) else {"ok": False, "state": "degraded", "error_code": "invalid_result"}
|
||||
|
||||
async def provenance_backup(self) -> dict[str, Any]:
|
||||
getter = getattr(self._plugin, "provenance_backup", None)
|
||||
if not callable(getter):
|
||||
return {"ok": False, "state": "degraded", "error_code": "unavailable"}
|
||||
result = await getter()
|
||||
return dict(result) if isinstance(result, dict) else {"ok": False, "state": "degraded", "error_code": "invalid_result"}
|
||||
|
||||
async def provenance_rollback(self, operation: dict[str, Any]) -> dict[str, Any]:
|
||||
getter = getattr(self._plugin, "provenance_rollback", None)
|
||||
if not callable(getter):
|
||||
return {"ok": False, "state": "degraded", "error_code": "unavailable"}
|
||||
result = await getter(operation)
|
||||
return dict(result) if isinstance(result, dict) else {"ok": False, "state": "degraded", "error_code": "invalid_result"}
|
||||
|
||||
async def create_note(self, *, event: Any, title: str, content: str = "") -> dict[str, Any]:
|
||||
return await self._plugin.tool_note_create(event, title, content)
|
||||
|
|
@ -574,6 +649,12 @@ class MemoryCompanionBridge:
|
|||
result["capability_schema_version"] = bot_personal_contract.BOT_PERSONAL_CAPABILITY_SCHEMA_VERSION
|
||||
result["payload_schema_version"] = bot_personal_contract.BOT_PERSONAL_PAYLOAD_SCHEMA_VERSION
|
||||
result["capability_state"] = "available"
|
||||
p5_status = getattr(self._plugin, "p5_capability_status", None)
|
||||
if callable(p5_status):
|
||||
try:
|
||||
result["p5"] = dict(p5_status() or {})
|
||||
except Exception:
|
||||
result["p5"] = {"state": "degraded", "error_code": "p5_status_exception"}
|
||||
self._capability_cache.mark_available(c4_snapshot)
|
||||
result.setdefault("warnings", [])
|
||||
return result
|
||||
|
|
@ -634,6 +715,12 @@ class MemoryCompanionBridge:
|
|||
"consume_context_projection",
|
||||
"read_bot_profile",
|
||||
"read_profile",
|
||||
"p5_capability_status",
|
||||
"provenance_snapshot",
|
||||
"provenance_preview",
|
||||
"provenance_apply",
|
||||
"provenance_backup",
|
||||
"provenance_rollback",
|
||||
"probe_capability_snapshot",
|
||||
"probe_bot_personal_memory_capabilities",
|
||||
],
|
||||
|
|
@ -697,6 +784,7 @@ class MemoryCompanionBridge:
|
|||
bot_personal_contract, "BOT_PERSONAL_PAYLOAD_SCHEMA_VERSION", ""
|
||||
)
|
||||
result["capability_state"] = "degraded"
|
||||
result["p5"] = {"state": "degraded", "error_code": reason}
|
||||
return result
|
||||
|
||||
def get_token_usage_summary(self) -> dict[str, Any]:
|
||||
|
|
|
|||
530
core/provenance.py
普通文件
530
core/provenance.py
普通文件
|
|
@ -0,0 +1,530 @@
|
|||
"""Pure chat-side provenance contract and in-memory migration projection.
|
||||
|
||||
The module accepts metadata only. It never reads or stores prompt text,
|
||||
conversation content, media, credentials, paths, or arbitrary source prose,
|
||||
and it performs no I/O. A later SQLite adapter may use the plans and CAS
|
||||
results, but this module deliberately has no knowledge of that adapter.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable, Mapping
|
||||
from copy import deepcopy
|
||||
from dataclasses import asdict, is_dataclass
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
|
||||
PROVENANCE_CONTRACT_NAME = "ops.p5.provenance.v1"
|
||||
PROVENANCE_CONTRACT_VERSION = "1.0"
|
||||
P3_CONTRACT_NAME = "ops.context_orchestration.v1"
|
||||
P3_CONTRACT_VERSION = "1.0"
|
||||
P3_CONTRACT_FINGERPRINT = "3bb7a12af05bb4d9a47cef9f31e78752cec512b90b1d50ae49feef54470974f8"
|
||||
COMPANION_ATTESTATION_ISSUER = "private_companion"
|
||||
ATTESTATION_SCHEMA_VERSION = "ops.p5.attestation.v1"
|
||||
|
||||
TRUST_LEVELS = ("T0", "T1", "T2", "T3", "T4")
|
||||
PROVENANCE_STATES = ("observed", "legacy_unresolved", "owner_recovered", "invalid")
|
||||
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"
|
||||
|
||||
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",
|
||||
)
|
||||
OPERATION_FIELDS = (
|
||||
"contract_name", "contract_version", "contract_fingerprint", "operation_kind",
|
||||
"operation_ref_hash", "memory_id", "expected_revision", "before_record_digest",
|
||||
"after_record_digest", "before_record", "after_record",
|
||||
)
|
||||
|
||||
_SOURCE_KIND_SET = frozenset(SOURCE_KINDS)
|
||||
_TRUST_SET = frozenset(TRUST_LEVELS)
|
||||
_FIREWALL_SET = frozenset(FIREWALL_STATUSES)
|
||||
_DISPOSITION_SET = frozenset(DISPOSITIONS)
|
||||
_SINKS = frozenset({
|
||||
"prompt_forward_quote", "prompt_vision_summary", "memory_recall",
|
||||
"bridge_serialization", "tool_retrieval", "external_export", "cross_user_read",
|
||||
})
|
||||
_MEMORY_ID_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._:-]{0,119}$")
|
||||
_SHA256_RE = re.compile(r"^[0-9a-fA-F]{64}$")
|
||||
_TOKEN_RE = re.compile(r"^[a-z][a-z0-9_]{0,63}$")
|
||||
_FORBIDDEN_RAW_FIELDS = frozenset({
|
||||
"body", "content", "text", "message", "prompt", "conversation", "media",
|
||||
"raw_id", "source_id", "source_event_id", "event_id", "attestation_id",
|
||||
"authority_attestation_id", "request_id", "session_id", "credential",
|
||||
"credentials", "password", "secret", "token", "path", "absolute_path", "url",
|
||||
})
|
||||
_SNAPSHOT_FIELDS = frozenset({
|
||||
"schema_version", "contract_name", "contract_version", "contract_fingerprint",
|
||||
"issuer", "issuer_epoch", "p3_contract_name", "p3_contract_version",
|
||||
"p3_contract_fingerprint", "source_kind", "source_trust", "trust", "firewall_status",
|
||||
"source_event_ref_hash", "source_hash", "authority_attestation_ref_hash",
|
||||
"attestation_ref_hash", "disposition", "reason_codes", "request_hash", "session_hash",
|
||||
"derived_hash", "derived_from_ref_hash", "provenance_state", "sink",
|
||||
})
|
||||
|
||||
|
||||
def _canonical_json(value: Any) -> str:
|
||||
return json.dumps(value, ensure_ascii=True, sort_keys=True, separators=(",", ":"))
|
||||
|
||||
|
||||
def _digest(value: Mapping[str, Any]) -> str:
|
||||
return hashlib.sha256(_canonical_json(value).encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def contract_descriptor() -> dict[str, Any]:
|
||||
"""Return a fresh, closed, JSON-safe descriptor."""
|
||||
|
||||
return {
|
||||
"contract_name": PROVENANCE_CONTRACT_NAME,
|
||||
"contract_version": PROVENANCE_CONTRACT_VERSION,
|
||||
"record_fields": list(RECORD_FIELDS),
|
||||
"provenance_states": list(PROVENANCE_STATES),
|
||||
"trust_levels": list(TRUST_LEVELS),
|
||||
"source_kinds": list(SOURCE_KINDS),
|
||||
"firewall_statuses": list(FIREWALL_STATUSES),
|
||||
"dispositions": list(DISPOSITIONS),
|
||||
"hash_format": HASH_FORMAT,
|
||||
"attestation_issuer": COMPANION_ATTESTATION_ISSUER,
|
||||
"p3_contract_name": P3_CONTRACT_NAME,
|
||||
"p3_contract_version": P3_CONTRACT_VERSION,
|
||||
}
|
||||
|
||||
|
||||
def contract_fingerprint() -> str:
|
||||
"""Return the canonical descriptor SHA-256 fingerprint."""
|
||||
|
||||
return _digest(contract_descriptor())
|
||||
|
||||
|
||||
provenance_contract_descriptor = contract_descriptor
|
||||
provenance_contract_fingerprint = contract_fingerprint
|
||||
|
||||
|
||||
def _safe_memory_id(value: Any) -> str:
|
||||
return value if isinstance(value, str) and _MEMORY_ID_RE.fullmatch(value) else ""
|
||||
|
||||
|
||||
def _safe_hash(value: Any) -> str:
|
||||
return value if isinstance(value, str) and _SHA256_RE.fullmatch(value) and value == value.lower() else ""
|
||||
|
||||
|
||||
def _normalize_hash(value: Any) -> str:
|
||||
if not isinstance(value, str):
|
||||
return ""
|
||||
raw = value[7:] if value.startswith("sha256:") else value
|
||||
return raw.lower() if _SHA256_RE.fullmatch(raw) else ""
|
||||
|
||||
|
||||
def _safe_revision(value: Any) -> int | None:
|
||||
return value if isinstance(value, int) and not isinstance(value, bool) and value >= 0 else None
|
||||
|
||||
|
||||
def _known(value: Any, allowed: frozenset[str] | tuple[str, ...]) -> bool:
|
||||
return isinstance(value, str) and value in allowed
|
||||
|
||||
|
||||
def _forbidden(value: Mapping[str, Any]) -> bool:
|
||||
return any(key in value for key in _FORBIDDEN_RAW_FIELDS)
|
||||
|
||||
|
||||
def _error_result(*codes: str) -> dict[str, Any]:
|
||||
return {"ok": False, "error_codes": sorted(set(codes))}
|
||||
|
||||
|
||||
def _blank_record(memory_id: Any = "", *, revision: int = 0, state: str = "invalid") -> dict[str, Any]:
|
||||
return {
|
||||
"contract_name": PROVENANCE_CONTRACT_NAME,
|
||||
"contract_version": PROVENANCE_CONTRACT_VERSION,
|
||||
"contract_fingerprint": contract_fingerprint(),
|
||||
"memory_id": _safe_memory_id(memory_id),
|
||||
"source_kind": "unknown",
|
||||
"source_trust": "T4",
|
||||
"firewall_status": "unknown",
|
||||
"source_event_ref_hash": "",
|
||||
"authority_attestation_ref_hash": "",
|
||||
"provenance_state": state,
|
||||
"migration_operation_ref": "",
|
||||
"recovery_operation_ref": "",
|
||||
"record_revision": revision if _safe_revision(revision) is not None else 0,
|
||||
}
|
||||
|
||||
|
||||
def validate_provenance_record(record: Mapping[str, Any] | None) -> dict[str, Any]:
|
||||
"""Return static validation errors or a deep-copied safe record.
|
||||
|
||||
Invalid input values are never returned, so a caller can safely serialize
|
||||
the result without leaking hostile identifiers or source prose.
|
||||
"""
|
||||
|
||||
if not isinstance(record, Mapping):
|
||||
return _error_result("record_not_mapping")
|
||||
errors: list[str] = []
|
||||
if _forbidden(record):
|
||||
errors.append("forbidden_raw_field")
|
||||
if set(record) != set(RECORD_FIELDS):
|
||||
errors.append("record_fields_mismatch")
|
||||
if record.get("contract_name") != PROVENANCE_CONTRACT_NAME:
|
||||
errors.append("contract_name_mismatch")
|
||||
if record.get("contract_version") != PROVENANCE_CONTRACT_VERSION:
|
||||
errors.append("contract_version_mismatch")
|
||||
if record.get("contract_fingerprint") != contract_fingerprint():
|
||||
errors.append("contract_fingerprint_mismatch")
|
||||
if not _safe_memory_id(record.get("memory_id")):
|
||||
errors.append("memory_id_invalid")
|
||||
if not _known(record.get("source_kind"), _SOURCE_KIND_SET):
|
||||
errors.append("source_kind_invalid")
|
||||
if not _known(record.get("source_trust"), _TRUST_SET):
|
||||
errors.append("source_trust_invalid")
|
||||
if not _known(record.get("firewall_status"), _FIREWALL_SET):
|
||||
errors.append("firewall_status_invalid")
|
||||
if not _known(record.get("provenance_state"), PROVENANCE_STATES):
|
||||
errors.append("provenance_state_invalid")
|
||||
if _safe_revision(record.get("record_revision")) is None:
|
||||
errors.append("record_revision_invalid")
|
||||
|
||||
source = record.get("source_event_ref_hash")
|
||||
authority = record.get("authority_attestation_ref_hash")
|
||||
source_hash = _safe_hash(source)
|
||||
authority_hash = _safe_hash(authority)
|
||||
if source != "" and not source_hash:
|
||||
errors.append("source_event_ref_hash_invalid")
|
||||
if authority != "" and not authority_hash:
|
||||
errors.append("authority_attestation_ref_hash_invalid")
|
||||
migration = record.get("migration_operation_ref")
|
||||
recovery = record.get("recovery_operation_ref")
|
||||
if migration != "" and not _safe_hash(migration):
|
||||
errors.append("migration_operation_ref_invalid")
|
||||
if recovery != "" and not _safe_hash(recovery):
|
||||
errors.append("recovery_operation_ref_invalid")
|
||||
|
||||
state = record.get("provenance_state")
|
||||
if state == "legacy_unresolved":
|
||||
if (record.get("source_kind"), record.get("source_trust"), record.get("firewall_status")) != ("unknown", "T4", "unknown"):
|
||||
errors.append("legacy_defaults_required")
|
||||
if source != "" or authority != "":
|
||||
errors.append("legacy_hashes_forbidden")
|
||||
elif state in {"observed", "owner_recovered"}:
|
||||
if record.get("source_kind") == "unknown":
|
||||
errors.append("observed_source_kind_required")
|
||||
if not source_hash or not authority_hash:
|
||||
errors.append("attestation_hashes_required")
|
||||
if source_hash and source_hash == authority_hash:
|
||||
errors.append("attestation_hashes_conflict")
|
||||
if state == "owner_recovered" and not _safe_hash(recovery):
|
||||
errors.append("recovery_operation_ref_required")
|
||||
elif state == "invalid" and (source_hash or authority_hash):
|
||||
errors.append("invalid_hashes_forbidden")
|
||||
if errors:
|
||||
return _error_result(*errors)
|
||||
return {"ok": True, "record": deepcopy(dict(record))}
|
||||
|
||||
|
||||
def legacy_unresolved(memory_id: str, *, record_revision: int = 0) -> dict[str, Any]:
|
||||
"""Represent a historical record without guessing its provenance."""
|
||||
|
||||
result = _blank_record(memory_id, revision=record_revision, state="legacy_unresolved")
|
||||
return result if result["memory_id"] and validate_provenance_record(result)["ok"] else _blank_record(state="invalid")
|
||||
|
||||
|
||||
def _snapshot_mapping(snapshot: Any) -> Mapping[str, Any] | None:
|
||||
if isinstance(snapshot, Mapping):
|
||||
return snapshot
|
||||
if is_dataclass(snapshot):
|
||||
value = asdict(snapshot)
|
||||
return value if isinstance(value, Mapping) else None
|
||||
return None
|
||||
|
||||
|
||||
def _paired_hash(snapshot: Mapping[str, Any], *keys: str) -> tuple[str, str]:
|
||||
values = [_normalize_hash(snapshot[key]) for key in keys if key in snapshot]
|
||||
if not values:
|
||||
return "missing", ""
|
||||
if not all(values):
|
||||
return "invalid", ""
|
||||
return ("present", values[0]) if len(set(values)) == 1 else ("conflict", "")
|
||||
|
||||
|
||||
def _paired_label(snapshot: Mapping[str, Any], allowed: frozenset[str], *keys: str) -> tuple[str, str]:
|
||||
values = [snapshot[key] for key in keys if key in snapshot]
|
||||
if not values:
|
||||
return "missing", ""
|
||||
if not all(_known(value, allowed) for value in values):
|
||||
return "invalid", ""
|
||||
return ("present", values[0]) if len(set(values)) == 1 else ("conflict", "")
|
||||
|
||||
|
||||
def _snapshot_safe(snapshot: Mapping[str, Any]) -> bool:
|
||||
if set(snapshot) - _SNAPSHOT_FIELDS or _forbidden(snapshot):
|
||||
return False
|
||||
if not _known(snapshot.get("disposition"), _DISPOSITION_SET):
|
||||
return False
|
||||
codes = snapshot.get("reason_codes", ())
|
||||
if not isinstance(codes, (list, tuple)) or len(codes) > 16:
|
||||
return False
|
||||
if not all(isinstance(code, str) and _TOKEN_RE.fullmatch(code) for code in codes):
|
||||
return False
|
||||
for key in ("request_hash", "session_hash", "issuer_epoch"):
|
||||
if not _normalize_hash(snapshot.get(key)):
|
||||
return False
|
||||
derived = [snapshot[key] for key in ("derived_hash", "derived_from_ref_hash") if key in snapshot and snapshot[key] != ""]
|
||||
if derived and (any(not _normalize_hash(value) for value in derived) or len({_normalize_hash(value) for value in derived}) != 1):
|
||||
return False
|
||||
return snapshot.get("provenance_state") == "observed" and _known(snapshot.get("sink"), _SINKS)
|
||||
|
||||
|
||||
def observed_from_companion_snapshot(memory_id: str, snapshot: Any, *, record_revision: int = 0) -> dict[str, Any]:
|
||||
"""Create ``observed`` only from a complete, attested Companion snapshot."""
|
||||
|
||||
safe_id = _safe_memory_id(memory_id)
|
||||
revision = _safe_revision(record_revision)
|
||||
data = _snapshot_mapping(snapshot)
|
||||
if not safe_id or revision is None or data is None or not _snapshot_safe(data):
|
||||
return _blank_record(safe_id, revision=revision or 0)
|
||||
expected = {
|
||||
"schema_version": ATTESTATION_SCHEMA_VERSION,
|
||||
"issuer": COMPANION_ATTESTATION_ISSUER,
|
||||
"contract_name": PROVENANCE_CONTRACT_NAME,
|
||||
"contract_version": PROVENANCE_CONTRACT_VERSION,
|
||||
"contract_fingerprint": contract_fingerprint(),
|
||||
"p3_contract_name": P3_CONTRACT_NAME,
|
||||
"p3_contract_version": P3_CONTRACT_VERSION,
|
||||
"p3_contract_fingerprint": P3_CONTRACT_FINGERPRINT,
|
||||
}
|
||||
if any(data.get(key) != value for key, value in expected.items()):
|
||||
return _blank_record(safe_id, revision=revision)
|
||||
source_status, source_hash = _paired_hash(data, "source_event_ref_hash", "source_hash")
|
||||
authority_status, authority_hash = _paired_hash(data, "authority_attestation_ref_hash", "attestation_ref_hash")
|
||||
trust_status, trust = _paired_label(data, _TRUST_SET, "source_trust", "trust")
|
||||
source_kind = data.get("source_kind")
|
||||
firewall = data.get("firewall_status")
|
||||
if (
|
||||
source_status != "present" or authority_status != "present" or trust_status != "present"
|
||||
or source_hash == authority_hash or not _known(source_kind, _SOURCE_KIND_SET - {"unknown"})
|
||||
or not _known(firewall, _FIREWALL_SET)
|
||||
):
|
||||
return _blank_record(safe_id, revision=revision)
|
||||
record = _blank_record(safe_id, revision=revision, state="observed")
|
||||
record.update({
|
||||
"source_kind": source_kind, "source_trust": trust, "firewall_status": firewall,
|
||||
"source_event_ref_hash": source_hash, "authority_attestation_ref_hash": authority_hash,
|
||||
})
|
||||
return record if validate_provenance_record(record)["ok"] else _blank_record(safe_id, revision=revision)
|
||||
|
||||
|
||||
def provenance_record_digest(record: Mapping[str, Any] | None) -> str:
|
||||
checked = validate_provenance_record(record)
|
||||
return _digest(checked["record"]) if checked["ok"] else ""
|
||||
|
||||
|
||||
def _record_or_none(value: Any) -> dict[str, Any] | None:
|
||||
checked = validate_provenance_record(value if isinstance(value, Mapping) else None)
|
||||
return deepcopy(checked["record"]) if checked["ok"] else None
|
||||
|
||||
|
||||
def _operation(*, operation_kind: str, operation_ref_hash: str, before_record: Mapping[str, Any] | None, after_record: Mapping[str, Any]) -> dict[str, Any] | None:
|
||||
before = _record_or_none(before_record)
|
||||
after = _record_or_none(after_record)
|
||||
reference = _safe_hash(operation_ref_hash)
|
||||
if operation_kind not in {"migration", "recovery"} or not reference or after is None:
|
||||
return None
|
||||
if before is not None and before["memory_id"] != after["memory_id"]:
|
||||
return None
|
||||
expected = before["record_revision"] if before is not None else after["record_revision"] - 1
|
||||
if expected < 0 or after["record_revision"] != expected + 1:
|
||||
return None
|
||||
operation = {
|
||||
"contract_name": PROVENANCE_CONTRACT_NAME, "contract_version": PROVENANCE_CONTRACT_VERSION,
|
||||
"contract_fingerprint": contract_fingerprint(), "operation_kind": operation_kind,
|
||||
"operation_ref_hash": reference, "memory_id": after["memory_id"], "expected_revision": expected,
|
||||
"before_record_digest": _digest(before) if before is not None else "",
|
||||
"after_record_digest": _digest(after), "before_record": before, "after_record": after,
|
||||
}
|
||||
return operation if validate_operation(operation)["ok"] else None
|
||||
|
||||
|
||||
def validate_operation(operation: Mapping[str, Any] | None) -> dict[str, Any]:
|
||||
if not isinstance(operation, Mapping):
|
||||
return _error_result("operation_not_mapping")
|
||||
errors: list[str] = []
|
||||
if _forbidden(operation):
|
||||
errors.append("forbidden_raw_field")
|
||||
if set(operation) != set(OPERATION_FIELDS):
|
||||
errors.append("operation_fields_mismatch")
|
||||
if operation.get("contract_name") != PROVENANCE_CONTRACT_NAME:
|
||||
errors.append("contract_name_mismatch")
|
||||
if operation.get("contract_version") != PROVENANCE_CONTRACT_VERSION:
|
||||
errors.append("contract_version_mismatch")
|
||||
if operation.get("contract_fingerprint") != contract_fingerprint():
|
||||
errors.append("contract_fingerprint_mismatch")
|
||||
if operation.get("operation_kind") not in {"migration", "recovery"}:
|
||||
errors.append("operation_kind_invalid")
|
||||
if not _safe_hash(operation.get("operation_ref_hash")):
|
||||
errors.append("operation_ref_invalid")
|
||||
if not _safe_memory_id(operation.get("memory_id")):
|
||||
errors.append("memory_id_invalid")
|
||||
expected = _safe_revision(operation.get("expected_revision"))
|
||||
if expected is None:
|
||||
errors.append("expected_revision_invalid")
|
||||
before = _record_or_none(operation.get("before_record"))
|
||||
after = _record_or_none(operation.get("after_record"))
|
||||
if operation.get("before_record") is not None and before is None:
|
||||
errors.append("before_record_invalid")
|
||||
if after is None:
|
||||
errors.append("after_record_invalid")
|
||||
if before is None and operation.get("before_record_digest") != "":
|
||||
errors.append("before_digest_required_empty")
|
||||
if before is not None and operation.get("before_record_digest") != _digest(before):
|
||||
errors.append("before_digest_mismatch")
|
||||
if after is not None and operation.get("after_record_digest") != _digest(after):
|
||||
errors.append("after_digest_mismatch")
|
||||
if after is not None:
|
||||
if operation.get("memory_id") != after["memory_id"]:
|
||||
errors.append("memory_id_mismatch")
|
||||
if expected is not None and after["record_revision"] != expected + 1:
|
||||
errors.append("revision_progression_invalid")
|
||||
ref_field = "migration_operation_ref" if operation.get("operation_kind") == "migration" else "recovery_operation_ref"
|
||||
if after.get(ref_field) != operation.get("operation_ref_hash"):
|
||||
errors.append("operation_ref_not_bound")
|
||||
if before is not None and after is not None:
|
||||
if before["memory_id"] != after["memory_id"]:
|
||||
errors.append("before_after_memory_mismatch")
|
||||
if expected is not None and before["record_revision"] != expected:
|
||||
errors.append("before_revision_mismatch")
|
||||
return _error_result(*errors) if errors else {"ok": True, "operation": deepcopy(dict(operation))}
|
||||
|
||||
|
||||
def plan_legacy_migration(records: Iterable[Mapping[str, Any]], *, operation_ref_hash: str) -> dict[str, Any]:
|
||||
"""Return a non-mutating preview for unresolved legacy records."""
|
||||
|
||||
reference = _safe_hash(operation_ref_hash)
|
||||
result: dict[str, Any] = {
|
||||
"mode": "preview", "readonly": True, "write_count": 0,
|
||||
"operation_ref_hash": reference, "operations": [], "skipped": [], "error_codes": [],
|
||||
}
|
||||
if not reference:
|
||||
result["error_codes"].append("operation_ref_invalid")
|
||||
return result
|
||||
if isinstance(records, (str, bytes)) or not isinstance(records, Iterable):
|
||||
result["error_codes"].append("records_not_iterable")
|
||||
return result
|
||||
for candidate in records:
|
||||
if not isinstance(candidate, Mapping) or _forbidden(candidate):
|
||||
result["error_codes"].append("candidate_invalid")
|
||||
continue
|
||||
existing = _record_or_none(candidate)
|
||||
if existing is not None:
|
||||
result["skipped"].append({"memory_id": existing["memory_id"], "reason_code": "already_provenanced"})
|
||||
continue
|
||||
if set(candidate) != {"memory_id", "record_revision"}:
|
||||
result["error_codes"].append("candidate_fields_mismatch")
|
||||
continue
|
||||
memory_id = _safe_memory_id(candidate.get("memory_id"))
|
||||
revision = _safe_revision(candidate.get("record_revision"))
|
||||
if not memory_id or revision is None:
|
||||
result["error_codes"].append("candidate_invalid")
|
||||
continue
|
||||
after = legacy_unresolved(memory_id, record_revision=revision + 1)
|
||||
after["migration_operation_ref"] = reference
|
||||
operation = _operation(operation_kind="migration", operation_ref_hash=reference, before_record=None, after_record=after)
|
||||
if operation is None:
|
||||
result["error_codes"].append("operation_invalid")
|
||||
else:
|
||||
result["operations"].append(operation)
|
||||
result["error_codes"] = sorted(set(result["error_codes"]))
|
||||
return result
|
||||
|
||||
|
||||
def _minimal_current(current: Mapping[str, Any] | None) -> tuple[str, int | None, dict[str, Any] | None, str]:
|
||||
if not isinstance(current, Mapping) or _forbidden(current):
|
||||
return "", None, None, "current_invalid"
|
||||
record = _record_or_none(current)
|
||||
if record is not None:
|
||||
return record["memory_id"], record["record_revision"], record, ""
|
||||
if set(current) != {"memory_id", "record_revision"}:
|
||||
return "", None, None, "current_fields_mismatch"
|
||||
return _safe_memory_id(current.get("memory_id")), _safe_revision(current.get("record_revision")), None, ""
|
||||
|
||||
|
||||
def apply_planned_operation(current: Mapping[str, Any] | None, operation: Mapping[str, Any] | None) -> dict[str, Any]:
|
||||
"""Apply a plan to an in-memory mapping with exact revision CAS."""
|
||||
|
||||
checked = validate_operation(operation)
|
||||
if not checked["ok"]:
|
||||
return {"ok": False, "mode": "apply", "status": "invalid_operation", "error_codes": checked["error_codes"]}
|
||||
op = checked["operation"]
|
||||
memory_id, revision, current_record, error = _minimal_current(current)
|
||||
if error or not memory_id or revision is None:
|
||||
return {"ok": False, "mode": "apply", "status": "invalid_current", "error_codes": [error or "current_invalid"]}
|
||||
if current_record is not None and _digest(current_record) == op["after_record_digest"]:
|
||||
return {"ok": True, "mode": "apply", "status": "idempotent", "record": deepcopy(op["after_record"]), "changed": False}
|
||||
if memory_id != op["memory_id"] or revision != op["expected_revision"]:
|
||||
return {"ok": False, "mode": "apply", "status": "revision_conflict", "error_codes": ["revision_or_memory_mismatch"]}
|
||||
before = op["before_record"]
|
||||
if before is None:
|
||||
if current_record is not None:
|
||||
return {"ok": False, "mode": "apply", "status": "revision_conflict", "error_codes": ["unexpected_existing_provenance"]}
|
||||
elif current_record is None or _digest(current_record) != op["before_record_digest"]:
|
||||
return {"ok": False, "mode": "apply", "status": "revision_conflict", "error_codes": ["before_record_mismatch"]}
|
||||
return {"ok": True, "mode": "apply", "status": "applied", "record": deepcopy(op["after_record"]), "changed": True}
|
||||
|
||||
|
||||
def rollback_planned_operation(current: Mapping[str, Any] | None, operation: Mapping[str, Any] | None) -> dict[str, Any]:
|
||||
"""Rollback only when the current record still matches the plan's CAS."""
|
||||
|
||||
checked = validate_operation(operation)
|
||||
if not checked["ok"]:
|
||||
return {"ok": False, "mode": "rollback", "status": "invalid_operation", "error_codes": checked["error_codes"]}
|
||||
op = checked["operation"]
|
||||
memory_id, revision, current_record, error = _minimal_current(current)
|
||||
if error or current_record is None or revision is None:
|
||||
return {"ok": False, "mode": "rollback", "status": "invalid_current", "error_codes": [error or "current_invalid"]}
|
||||
if memory_id != op["memory_id"] or revision != op["after_record"]["record_revision"] or _digest(current_record) != op["after_record_digest"]:
|
||||
return {"ok": False, "mode": "rollback", "status": "revision_conflict", "error_codes": ["concurrent_update_protected"]}
|
||||
before = op["before_record"]
|
||||
return {"ok": True, "mode": "rollback", "status": "rolled_back", "record": deepcopy(before) if before is not None else None, "removed": before is None, "changed": True}
|
||||
|
||||
|
||||
def plan_owner_recovery(current: Mapping[str, Any], snapshot: Mapping[str, Any], *, recovery_operation_ref_hash: str) -> dict[str, Any]:
|
||||
"""Plan attested owner recovery for one legacy-unresolved record."""
|
||||
|
||||
existing = _record_or_none(current)
|
||||
reference = _safe_hash(recovery_operation_ref_hash)
|
||||
if existing is None or existing["provenance_state"] != "legacy_unresolved" or not reference:
|
||||
return {"ok": False, "mode": "preview", "error_codes": ["recovery_precondition_failed"]}
|
||||
after = observed_from_companion_snapshot(existing["memory_id"], snapshot, record_revision=existing["record_revision"] + 1)
|
||||
if after["provenance_state"] != "observed":
|
||||
return {"ok": False, "mode": "preview", "error_codes": ["companion_snapshot_invalid"]}
|
||||
after["provenance_state"] = "owner_recovered"
|
||||
after["migration_operation_ref"] = existing["migration_operation_ref"]
|
||||
after["recovery_operation_ref"] = reference
|
||||
operation = _operation(operation_kind="recovery", operation_ref_hash=reference, before_record=existing, after_record=after)
|
||||
if operation is None:
|
||||
return {"ok": False, "mode": "preview", "error_codes": ["operation_invalid"]}
|
||||
return {"ok": True, "mode": "preview", "readonly": True, "write_count": 0, "operation": operation}
|
||||
|
||||
|
||||
__all__ = [
|
||||
"ATTESTATION_SCHEMA_VERSION", "COMPANION_ATTESTATION_ISSUER", "DISPOSITIONS",
|
||||
"FIREWALL_STATUSES", "HASH_FORMAT", "OPERATION_FIELDS", "P3_CONTRACT_FINGERPRINT",
|
||||
"P3_CONTRACT_NAME", "P3_CONTRACT_VERSION", "PROVENANCE_CONTRACT_NAME",
|
||||
"PROVENANCE_CONTRACT_VERSION", "PROVENANCE_STATES", "RECORD_FIELDS", "SOURCE_KINDS",
|
||||
"TRUST_LEVELS", "apply_planned_operation", "contract_descriptor", "contract_fingerprint",
|
||||
"legacy_unresolved", "observed_from_companion_snapshot", "plan_legacy_migration",
|
||||
"plan_owner_recovery", "provenance_contract_descriptor", "provenance_contract_fingerprint",
|
||||
"provenance_record_digest", "rollback_planned_operation", "validate_operation",
|
||||
"validate_provenance_record",
|
||||
]
|
||||
280
core/provenance_store.py
普通文件
280
core/provenance_store.py
普通文件
|
|
@ -0,0 +1,280 @@
|
|||
"""Small append-only provenance ledger for the chat-side memory plugin.
|
||||
|
||||
The ledger stores only validated provenance records and migration audit
|
||||
metadata. It never accepts prompt text, conversation content, media,
|
||||
credentials, or arbitrary source prose. Writes are atomic and every mutation
|
||||
creates a backup before the current projection changes.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from copy import deepcopy
|
||||
import json
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import threading
|
||||
import time
|
||||
from typing import Any, Mapping
|
||||
|
||||
from .provenance import (
|
||||
apply_planned_operation,
|
||||
plan_legacy_migration,
|
||||
provenance_record_digest,
|
||||
rollback_planned_operation,
|
||||
validate_operation,
|
||||
validate_provenance_record,
|
||||
)
|
||||
|
||||
|
||||
LEDGER_SCHEMA_VERSION = "ops.p5.provenance.ledger.v1"
|
||||
|
||||
|
||||
class ProvenanceLedger:
|
||||
"""Persist validated provenance projections with CAS-protected writes."""
|
||||
|
||||
def __init__(self, path: Path):
|
||||
self.path = Path(path)
|
||||
self._lock = threading.RLock()
|
||||
|
||||
def _empty(self) -> dict[str, Any]:
|
||||
return {"schema_version": LEDGER_SCHEMA_VERSION, "records": {}, "operations": [], "observations": []}
|
||||
|
||||
def _load_locked(self) -> dict[str, Any]:
|
||||
if not self.path.is_file():
|
||||
return self._empty()
|
||||
try:
|
||||
raw = json.loads(self.path.read_text(encoding="utf-8"))
|
||||
except (OSError, ValueError, TypeError):
|
||||
return self._empty()
|
||||
if not isinstance(raw, dict) or raw.get("schema_version") != LEDGER_SCHEMA_VERSION:
|
||||
return self._empty()
|
||||
records: dict[str, dict[str, Any]] = {}
|
||||
raw_records = raw.get("records")
|
||||
if isinstance(raw_records, dict):
|
||||
for memory_id, value in raw_records.items():
|
||||
checked = validate_provenance_record(value if isinstance(value, Mapping) else None)
|
||||
if checked.get("ok") and checked.get("record", {}).get("memory_id") == memory_id:
|
||||
records[memory_id] = deepcopy(checked["record"])
|
||||
operations: list[dict[str, Any]] = []
|
||||
raw_operations = raw.get("operations")
|
||||
if isinstance(raw_operations, list):
|
||||
for value in raw_operations:
|
||||
checked = validate_operation(value if isinstance(value, Mapping) else None)
|
||||
if checked.get("ok"):
|
||||
operations.append(deepcopy(checked["operation"]))
|
||||
elif isinstance(value, dict) and value.get("kind") == "rollback":
|
||||
safe = self._safe_rollback_audit(value)
|
||||
if safe is not None:
|
||||
operations.append(safe)
|
||||
observations: list[dict[str, Any]] = []
|
||||
raw_observations = raw.get("observations")
|
||||
if isinstance(raw_observations, list):
|
||||
for value in raw_observations:
|
||||
safe = self._safe_observation(value)
|
||||
if safe is not None:
|
||||
observations.append(safe)
|
||||
return {
|
||||
"schema_version": LEDGER_SCHEMA_VERSION,
|
||||
"records": records,
|
||||
"operations": operations,
|
||||
"observations": observations,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _safe_observation(value: Any) -> dict[str, Any] | None:
|
||||
if not isinstance(value, Mapping) or set(value) != {
|
||||
"memory_id", "record_digest", "record_revision", "source_event_ref_hash",
|
||||
"authority_attestation_ref_hash", "provenance_state",
|
||||
}:
|
||||
return None
|
||||
if (
|
||||
not isinstance(value.get("memory_id"), str)
|
||||
or not value["memory_id"]
|
||||
or not isinstance(value.get("record_digest"), str)
|
||||
or len(value["record_digest"]) != 64
|
||||
or not isinstance(value.get("source_event_ref_hash"), str)
|
||||
or len(value["source_event_ref_hash"]) != 64
|
||||
or not isinstance(value.get("authority_attestation_ref_hash"), str)
|
||||
or len(value["authority_attestation_ref_hash"]) != 64
|
||||
or not isinstance(value.get("record_revision"), int)
|
||||
or value["record_revision"] < 1
|
||||
or value.get("provenance_state") not in {"observed", "owner_recovered"}
|
||||
):
|
||||
return None
|
||||
return {
|
||||
"memory_id": value["memory_id"][:120],
|
||||
"record_digest": value["record_digest"].lower(),
|
||||
"record_revision": value["record_revision"],
|
||||
"source_event_ref_hash": value["source_event_ref_hash"].lower(),
|
||||
"authority_attestation_ref_hash": value["authority_attestation_ref_hash"].lower(),
|
||||
"provenance_state": value["provenance_state"],
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _safe_rollback_audit(value: Mapping[str, Any]) -> dict[str, Any] | None:
|
||||
memory_id = value.get("memory_id")
|
||||
before_digest = value.get("before_digest")
|
||||
after_digest = value.get("after_digest")
|
||||
from_revision = value.get("from_revision")
|
||||
to_revision = value.get("to_revision")
|
||||
if (
|
||||
not isinstance(memory_id, str)
|
||||
or not memory_id
|
||||
or not isinstance(before_digest, str)
|
||||
or len(before_digest) != 64
|
||||
or not isinstance(after_digest, str)
|
||||
or len(after_digest) != 64
|
||||
or not isinstance(from_revision, int)
|
||||
or from_revision < 0
|
||||
or not isinstance(to_revision, int)
|
||||
or to_revision < 0
|
||||
):
|
||||
return None
|
||||
return {
|
||||
"kind": "rollback",
|
||||
"memory_id": memory_id[:120],
|
||||
"before_digest": before_digest.lower(),
|
||||
"after_digest": after_digest.lower(),
|
||||
"from_revision": from_revision,
|
||||
"to_revision": to_revision,
|
||||
}
|
||||
|
||||
def _write_locked(self, document: Mapping[str, Any]) -> None:
|
||||
self.path.parent.mkdir(parents=True, exist_ok=True)
|
||||
temporary = self.path.with_name(f".{self.path.name}.{threading.get_ident()}.tmp")
|
||||
temporary.write_text(
|
||||
json.dumps(document, ensure_ascii=True, sort_keys=True, separators=(",", ":")),
|
||||
encoding="utf-8",
|
||||
)
|
||||
temporary.replace(self.path)
|
||||
|
||||
def _backup_locked(self) -> bool:
|
||||
if not self.path.is_file():
|
||||
return False
|
||||
backup = self.path.with_name(f"{self.path.name}.backup.{time.time_ns()}")
|
||||
try:
|
||||
shutil.copy2(self.path, backup)
|
||||
return True
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def _minimal_current(memory_id: str, record: Mapping[str, Any] | None) -> dict[str, Any]:
|
||||
if isinstance(record, Mapping):
|
||||
return deepcopy(dict(record))
|
||||
return {"memory_id": memory_id, "record_revision": 0}
|
||||
|
||||
def snapshot(self) -> dict[str, Any]:
|
||||
with self._lock:
|
||||
document = self._load_locked()
|
||||
return {
|
||||
"schema_version": LEDGER_SCHEMA_VERSION,
|
||||
"records": deepcopy(document["records"]),
|
||||
"operation_count": len(document["operations"]),
|
||||
"observation_count": len(document["observations"]),
|
||||
"path_present": self.path.is_file(),
|
||||
}
|
||||
|
||||
def preview_legacy(self, candidates: list[Mapping[str, Any]], *, operation_ref_hash: str) -> dict[str, Any]:
|
||||
return plan_legacy_migration(candidates, operation_ref_hash=operation_ref_hash)
|
||||
|
||||
def backup(self) -> dict[str, Any]:
|
||||
with self._lock:
|
||||
if not self.path.is_file():
|
||||
return {"ok": True, "state": "nothing_to_backup", "created": False}
|
||||
created = self._backup_locked()
|
||||
return {"ok": created, "state": "backed_up" if created else "degraded", "created": created}
|
||||
|
||||
def apply(self, operation: Mapping[str, Any]) -> dict[str, Any]:
|
||||
checked = validate_operation(operation)
|
||||
if not checked.get("ok"):
|
||||
return {"ok": False, "state": "invalid", "error_codes": checked.get("error_codes", [])}
|
||||
op = checked["operation"]
|
||||
with self._lock:
|
||||
document = self._load_locked()
|
||||
current = self._minimal_current(op["memory_id"], document["records"].get(op["memory_id"]))
|
||||
result = apply_planned_operation(current, op)
|
||||
if not result.get("ok") or not result.get("changed"):
|
||||
return {**result, "backup_created": False}
|
||||
backup_created = self._backup_locked()
|
||||
document["records"][op["memory_id"]] = deepcopy(result["record"])
|
||||
document["operations"].append(deepcopy(op))
|
||||
self._write_locked(document)
|
||||
return {**result, "backup_created": backup_created, "operation_count": len(document["operations"])}
|
||||
|
||||
def rollback(self, operation: Mapping[str, Any]) -> dict[str, Any]:
|
||||
checked = validate_operation(operation)
|
||||
if not checked.get("ok"):
|
||||
return {"ok": False, "state": "invalid", "error_codes": checked.get("error_codes", [])}
|
||||
op = checked["operation"]
|
||||
with self._lock:
|
||||
document = self._load_locked()
|
||||
current = document["records"].get(op["memory_id"])
|
||||
result = rollback_planned_operation(current, op)
|
||||
if not result.get("ok"):
|
||||
return {**result, "backup_created": False}
|
||||
backup_created = self._backup_locked()
|
||||
if result.get("removed"):
|
||||
document["records"].pop(op["memory_id"], None)
|
||||
else:
|
||||
document["records"][op["memory_id"]] = deepcopy(result["record"])
|
||||
document["operations"].append(
|
||||
{
|
||||
"kind": "rollback",
|
||||
"memory_id": op["memory_id"],
|
||||
"before_digest": op["after_record_digest"],
|
||||
"after_digest": provenance_record_digest(result.get("record")) if result.get("record") else "0" * 64,
|
||||
"from_revision": op["after_record"]["record_revision"],
|
||||
"to_revision": result.get("record", {}).get("record_revision", 0) if result.get("record") else 0,
|
||||
}
|
||||
)
|
||||
self._write_locked(document)
|
||||
return {**result, "backup_created": backup_created, "operation_count": len(document["operations"])}
|
||||
|
||||
def record_observed(self, memory_ids: list[str], snapshot: Any) -> dict[str, Any]:
|
||||
"""Append observed metadata for selected memories without raw payloads."""
|
||||
|
||||
from .provenance import observed_from_companion_snapshot
|
||||
|
||||
safe_ids = [item for item in memory_ids if isinstance(item, str) and item][:64]
|
||||
if not safe_ids:
|
||||
return {"ok": False, "state": "invalid", "error_code": "memory_ids_empty", "written": 0}
|
||||
with self._lock:
|
||||
document = self._load_locked()
|
||||
updates: dict[str, dict[str, Any]] = {}
|
||||
for memory_id in safe_ids:
|
||||
current = document["records"].get(memory_id)
|
||||
revision = int(current.get("record_revision", 0)) if isinstance(current, dict) else 0
|
||||
record = observed_from_companion_snapshot(memory_id, snapshot, record_revision=revision + 1)
|
||||
checked = validate_provenance_record(record)
|
||||
if not checked.get("ok"):
|
||||
continue
|
||||
if isinstance(current, dict) and current == checked["record"]:
|
||||
continue
|
||||
updates[memory_id] = deepcopy(checked["record"])
|
||||
if not updates:
|
||||
return {"ok": True, "state": "deduplicated", "written": 0, "backup_created": False}
|
||||
backup_created = self._backup_locked()
|
||||
document["records"].update(updates)
|
||||
for record in updates.values():
|
||||
document["observations"].append(
|
||||
{
|
||||
"memory_id": record["memory_id"],
|
||||
"record_digest": provenance_record_digest(record),
|
||||
"record_revision": record["record_revision"],
|
||||
"source_event_ref_hash": record["source_event_ref_hash"],
|
||||
"authority_attestation_ref_hash": record["authority_attestation_ref_hash"],
|
||||
"provenance_state": record["provenance_state"],
|
||||
}
|
||||
)
|
||||
self._write_locked(document)
|
||||
return {
|
||||
"ok": True,
|
||||
"state": "written",
|
||||
"written": len(updates),
|
||||
"memory_ids": list(updates),
|
||||
"backup_created": backup_created,
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["LEDGER_SCHEMA_VERSION", "ProvenanceLedger"]
|
||||
229
core/service.py
229
core/service.py
|
|
@ -11,7 +11,7 @@ import inspect
|
|||
import re
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from typing import Any, Mapping
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from .astrbot_compat import (
|
||||
|
|
@ -51,6 +51,8 @@ from .operations import (
|
|||
detect_preset,
|
||||
persist_runtime_config,
|
||||
)
|
||||
from .provenance import observed_from_companion_snapshot
|
||||
from .provenance_store import ProvenanceLedger
|
||||
from .qq_history import QQHistoryReader
|
||||
from .reply_chain import ReplyChainResolver
|
||||
from .retrieval import RetrievalEngine
|
||||
|
|
@ -178,6 +180,152 @@ class MemoryCompanionService:
|
|||
self._relationship_phase_state: dict[str, dict[str, Any]] = {}
|
||||
self._RELATIONSHIP_PHASE_FILE = self.data_dir / "memory_companion_relationship_phase.json"
|
||||
self._load_relationship_phase_state()
|
||||
self.provenance_ledger = ProvenanceLedger(self.data_dir / "provenance_ledger.json")
|
||||
self._last_p5_gate_status: dict[str, Any] = {"state": "disabled", "enabled": False}
|
||||
|
||||
def _p5_gate_enabled(self, sink: str) -> bool:
|
||||
key = (
|
||||
"private_companion_bridge.enable_p5_b1_recall_gate"
|
||||
if sink == "memory_recall"
|
||||
else "private_companion_bridge.enable_p5_b1_bridge_gate"
|
||||
)
|
||||
return self.config.bool(key, False)
|
||||
|
||||
async def _p5_gate(
|
||||
self,
|
||||
*,
|
||||
event: Any = None,
|
||||
sink: str,
|
||||
attestation: Any = None,
|
||||
consumer: Any = None,
|
||||
) -> dict[str, Any]:
|
||||
if not self._p5_gate_enabled(sink):
|
||||
if attestation is not None and callable(consumer):
|
||||
try:
|
||||
consumed = consumer(attestation, sink)
|
||||
if inspect.isawaitable(consumed):
|
||||
await consumed
|
||||
except Exception:
|
||||
pass
|
||||
result = {"ok": True, "state": "legacy", "legacy": True, "enabled": False}
|
||||
self._last_p5_gate_status = result
|
||||
return result
|
||||
if attestation is None and event is not None:
|
||||
issuer = getattr(event, "private_companion_p5_issue_attestation", None)
|
||||
if callable(issuer):
|
||||
try:
|
||||
issued = issuer(sink)
|
||||
except Exception:
|
||||
issued = None
|
||||
if isinstance(issued, tuple) and len(issued) == 2:
|
||||
attestation, consumer = issued
|
||||
if attestation is None or not callable(consumer):
|
||||
result = {
|
||||
"ok": False,
|
||||
"state": "degraded",
|
||||
"enabled": True,
|
||||
"error_code": "p5_attestation_required",
|
||||
"warnings": ["legacy_call_not_attested"],
|
||||
}
|
||||
self._last_p5_gate_status = result
|
||||
return result
|
||||
try:
|
||||
snapshot = consumer(attestation, sink)
|
||||
if inspect.isawaitable(snapshot):
|
||||
snapshot = await snapshot
|
||||
except Exception:
|
||||
snapshot = None
|
||||
snapshot_record = observed_from_companion_snapshot("p5_gate", snapshot)
|
||||
snapshot_sink = getattr(snapshot, "sink", None)
|
||||
if snapshot_sink is None and isinstance(snapshot, Mapping):
|
||||
snapshot_sink = snapshot.get("sink")
|
||||
if snapshot_record.get("provenance_state") != "observed" or snapshot_sink != sink:
|
||||
result = {
|
||||
"ok": False,
|
||||
"state": "denied",
|
||||
"enabled": True,
|
||||
"error_code": "p5_attestation_invalid",
|
||||
"warnings": ["invalid_or_replayed_attestation"],
|
||||
}
|
||||
self._last_p5_gate_status = result
|
||||
return result
|
||||
disposition = str(getattr(snapshot, "disposition", "") or "")
|
||||
trust = str(getattr(snapshot, "source_trust", "") or "")
|
||||
if isinstance(snapshot, Mapping):
|
||||
disposition = str(snapshot.get("disposition") or disposition)
|
||||
trust = str(snapshot.get("source_trust") or snapshot.get("trust") or trust)
|
||||
if disposition == "deny_high_risk":
|
||||
state = "denied"
|
||||
error_code = "p5_high_risk_denied"
|
||||
allowed = False
|
||||
elif disposition != "allow" or trust in {"T3", "T4"}:
|
||||
state = "shadow"
|
||||
error_code = "p5_source_shadowed"
|
||||
allowed = False
|
||||
else:
|
||||
state = "allowed"
|
||||
error_code = ""
|
||||
allowed = True
|
||||
result = {
|
||||
"ok": allowed,
|
||||
"state": state,
|
||||
"enabled": True,
|
||||
"snapshot": snapshot,
|
||||
"error_code": error_code or None,
|
||||
"warnings": [] if allowed else [error_code],
|
||||
}
|
||||
self._last_p5_gate_status = {
|
||||
key: value for key, value in result.items() if key != "snapshot"
|
||||
}
|
||||
return result
|
||||
|
||||
def p5_capability_status(self) -> dict[str, Any]:
|
||||
ledger = getattr(self, "provenance_ledger", None)
|
||||
snapshot = ledger.snapshot() if isinstance(ledger, ProvenanceLedger) else {"operation_count": 0, "observation_count": 0, "path_present": False}
|
||||
return {
|
||||
"schema_version": "ops.p5.provenance.v1",
|
||||
"recall_gate": self._p5_gate_enabled("memory_recall"),
|
||||
"bridge_gate": self._p5_gate_enabled("bridge_serialization"),
|
||||
"last_gate_state": str(getattr(self, "_last_p5_gate_status", {}).get("state") or "disabled"),
|
||||
"provenance_records": len(snapshot.get("records", {})),
|
||||
"provenance_operations": int(snapshot.get("operation_count", 0) or 0),
|
||||
"provenance_observations": int(snapshot.get("observation_count", 0) or 0),
|
||||
"provenance_file_present": bool(snapshot.get("path_present", False)),
|
||||
}
|
||||
|
||||
async def _p5_record_observed(self, memory_ids: list[str], snapshot: Any) -> dict[str, Any]:
|
||||
ledger = getattr(self, "provenance_ledger", None)
|
||||
if not isinstance(ledger, ProvenanceLedger):
|
||||
return {"ok": False, "state": "degraded", "error_code": "provenance_ledger_unavailable"}
|
||||
return await asyncio.to_thread(ledger.record_observed, memory_ids, snapshot)
|
||||
|
||||
def provenance_snapshot(self) -> dict[str, Any]:
|
||||
ledger = getattr(self, "provenance_ledger", None)
|
||||
return ledger.snapshot() if isinstance(ledger, ProvenanceLedger) else {"records": {}, "operation_count": 0}
|
||||
|
||||
def provenance_preview(self, candidates: list[Mapping[str, Any]], *, operation_ref_hash: str) -> dict[str, Any]:
|
||||
ledger = getattr(self, "provenance_ledger", None)
|
||||
if not isinstance(ledger, ProvenanceLedger):
|
||||
return {"mode": "preview", "readonly": True, "write_count": 0, "error_codes": ["ledger_unavailable"]}
|
||||
return ledger.preview_legacy(candidates, operation_ref_hash=operation_ref_hash)
|
||||
|
||||
async def provenance_backup(self) -> dict[str, Any]:
|
||||
ledger = getattr(self, "provenance_ledger", None)
|
||||
if not isinstance(ledger, ProvenanceLedger):
|
||||
return {"ok": False, "state": "degraded", "error_code": "ledger_unavailable"}
|
||||
return await asyncio.to_thread(ledger.backup)
|
||||
|
||||
async def provenance_apply(self, operation: Mapping[str, Any]) -> dict[str, Any]:
|
||||
ledger = getattr(self, "provenance_ledger", None)
|
||||
if not isinstance(ledger, ProvenanceLedger):
|
||||
return {"ok": False, "state": "degraded", "error_code": "ledger_unavailable"}
|
||||
return await asyncio.to_thread(ledger.apply, operation)
|
||||
|
||||
async def provenance_rollback(self, operation: Mapping[str, Any]) -> dict[str, Any]:
|
||||
ledger = getattr(self, "provenance_ledger", None)
|
||||
if not isinstance(ledger, ProvenanceLedger):
|
||||
return {"ok": False, "state": "degraded", "error_code": "ledger_unavailable"}
|
||||
return await asyncio.to_thread(ledger.rollback, operation)
|
||||
|
||||
def preview_historical_chat_upload(
|
||||
self,
|
||||
|
|
@ -917,10 +1065,23 @@ class MemoryCompanionService:
|
|||
*,
|
||||
session_context: SessionContext | dict[str, Any] | None = None,
|
||||
top_k: int | None = None,
|
||||
p5_attestation: Any = None,
|
||||
p5_attestation_consumer: Any = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
gate = await self._p5_gate(
|
||||
sink="bridge_serialization",
|
||||
attestation=p5_attestation,
|
||||
consumer=p5_attestation_consumer,
|
||||
)
|
||||
if not gate.get("ok"):
|
||||
return []
|
||||
ctx = self.session_context_from_bridge(session_context)
|
||||
results = await self.search(query, ctx, top_k or self.config.int("memory_injection.top_k", 6))
|
||||
return [serialize_memory(item.memory, item.score, item.reason) for item in results]
|
||||
serialized = [serialize_memory(item.memory, item.score, item.reason) for item in results]
|
||||
snapshot = gate.get("snapshot")
|
||||
if snapshot is not None:
|
||||
await self._p5_record_observed([item.memory.id for item in results], snapshot)
|
||||
return serialized
|
||||
|
||||
async def bridge_compose_injection(
|
||||
self,
|
||||
|
|
@ -931,7 +1092,18 @@ class MemoryCompanionService:
|
|||
max_chars: int | None = None,
|
||||
companion_bot_mood: str = "",
|
||||
companion_bot_energy: float = 0.0,
|
||||
p5_attestation: Any = None,
|
||||
p5_attestation_consumer: Any = None,
|
||||
_p5_gate_passed: bool = False,
|
||||
) -> str:
|
||||
if not _p5_gate_passed:
|
||||
gate = await self._p5_gate(
|
||||
sink="bridge_serialization",
|
||||
attestation=p5_attestation,
|
||||
consumer=p5_attestation_consumer,
|
||||
)
|
||||
if not gate.get("ok"):
|
||||
return ""
|
||||
ctx = self.session_context_from_bridge(session_context)
|
||||
query_text = clean_text(query or ctx.message_text, 1400)
|
||||
if not query_text:
|
||||
|
|
@ -957,7 +1129,16 @@ class MemoryCompanionService:
|
|||
companion_bot_mood: str = "",
|
||||
companion_bot_energy: float = 0.0,
|
||||
retrieval_profile: str = "",
|
||||
p5_attestation: Any = None,
|
||||
p5_attestation_consumer: Any = None,
|
||||
) -> str:
|
||||
gate = await self._p5_gate(
|
||||
sink="bridge_serialization",
|
||||
attestation=p5_attestation,
|
||||
consumer=p5_attestation_consumer,
|
||||
)
|
||||
if not gate.get("ok"):
|
||||
return ""
|
||||
profile = clean_text(retrieval_profile, 40).lower()
|
||||
fast_profile_enabled = {
|
||||
"schedule_fast": self.config.bool(
|
||||
|
|
@ -986,6 +1167,7 @@ class MemoryCompanionService:
|
|||
max_chars=max_chars,
|
||||
companion_bot_mood=companion_bot_mood,
|
||||
companion_bot_energy=companion_bot_energy,
|
||||
_p5_gate_passed=True,
|
||||
)
|
||||
|
||||
async def _bridge_compose_schedule_fast_context(
|
||||
|
|
@ -3379,14 +3561,39 @@ class MemoryCompanionService:
|
|||
self._schedule_memory_embedding(memory_id, record)
|
||||
return {"ok": True, "memory_id": memory_id, "review_status": record.review_status}
|
||||
|
||||
async def tool_recall(self, event: Any, query: str, top_k: int = 5) -> dict[str, Any]:
|
||||
async def tool_recall(
|
||||
self,
|
||||
event: Any,
|
||||
query: str,
|
||||
top_k: int = 5,
|
||||
*,
|
||||
p5_attestation: Any = None,
|
||||
p5_attestation_consumer: Any = None,
|
||||
) -> dict[str, Any]:
|
||||
ctx = await self.identity.resolve_event_context(event)
|
||||
query = clean_text(query, 1000)
|
||||
if not query:
|
||||
return {"ok": False, "error": "empty query", "memories": []}
|
||||
p5_gate = await self._p5_gate(
|
||||
event=event,
|
||||
sink="memory_recall",
|
||||
attestation=p5_attestation,
|
||||
consumer=p5_attestation_consumer,
|
||||
)
|
||||
if not p5_gate.get("ok"):
|
||||
return {
|
||||
"ok": False,
|
||||
"state": p5_gate.get("state", "degraded"),
|
||||
"error": p5_gate.get("error_code", "p5_attestation_required"),
|
||||
"memories": [],
|
||||
}
|
||||
results = await self.search(query, ctx, max(1, min(10, int(top_k or 5))))
|
||||
snapshot = p5_gate.get("snapshot")
|
||||
if snapshot is not None:
|
||||
await self._p5_record_observed([item.memory.id for item in results], snapshot)
|
||||
return {
|
||||
"ok": True,
|
||||
"state": p5_gate.get("state", "legacy"),
|
||||
"memories": [serialize_memory(item.memory, item.score, item.reason) for item in results],
|
||||
}
|
||||
|
||||
|
|
@ -4519,6 +4726,22 @@ class MemoryCompanionService:
|
|||
if self.config.bool("memory_injection.debug_log_injection_enabled", False):
|
||||
logger.info("[MemoryCompanion] 记忆注入已关闭: session=%s", ctx.session_id)
|
||||
return
|
||||
p5_gate = await self._p5_gate(event=event, sink="memory_recall")
|
||||
if not p5_gate.get("ok"):
|
||||
self._mark_memory_companion_injection_state(
|
||||
event,
|
||||
req,
|
||||
injected=False,
|
||||
conversation_memory=False,
|
||||
slot_map={},
|
||||
)
|
||||
logger.info(
|
||||
"[MemoryCompanion] P5 召回闸门未放行本轮记忆注入: session=%s state=%s error=%s",
|
||||
ctx.session_id,
|
||||
p5_gate.get("state"),
|
||||
p5_gate.get("error_code"),
|
||||
)
|
||||
return
|
||||
self._sanitize_request_history_for_companion(ctx, req)
|
||||
recent_fact_context = await self._recent_fact_guard_context(ctx)
|
||||
recent_cross_window_context = await self._recent_cross_window_context(ctx)
|
||||
|
|
|
|||
129
tests/test_c5_bridge_gate.py
普通文件
129
tests/test_c5_bridge_gate.py
普通文件
|
|
@ -0,0 +1,129 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
COMPANION_ROOT = Path(__file__).resolve().parents[2] / "astrbot_plugin_private_companion-main" / "astrbot_plugin_private_companion-main"
|
||||
if str(COMPANION_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(COMPANION_ROOT))
|
||||
|
||||
from p5_attestation import P5AttestationRegistry # noqa: E402
|
||||
|
||||
from core.config import ConfigView # noqa: E402
|
||||
from core.provenance_store import ProvenanceLedger # noqa: E402
|
||||
from core.service import MemoryCompanionService # noqa: E402
|
||||
|
||||
|
||||
def run(coro):
|
||||
return asyncio.run(coro)
|
||||
|
||||
|
||||
def make_service(*, bridge_gate: bool = True, recall_gate: bool = True):
|
||||
service = object.__new__(MemoryCompanionService)
|
||||
service.config = ConfigView(
|
||||
{
|
||||
"private_companion_bridge": {
|
||||
"enable_p5_b1_bridge_gate": bridge_gate,
|
||||
"enable_p5_b1_recall_gate": recall_gate,
|
||||
}
|
||||
}
|
||||
)
|
||||
service._last_p5_gate_status = {}
|
||||
return service
|
||||
|
||||
|
||||
def mint(registry, *, request, event, p3, source_kind="current_user_intent", trust="T2", disposition="allow", sink="bridge_serialization"):
|
||||
handle = registry.mint(
|
||||
request,
|
||||
event,
|
||||
p3,
|
||||
request_hash="a" * 64,
|
||||
session_hash="b" * 64,
|
||||
source_kind=source_kind,
|
||||
source_trust=trust,
|
||||
firewall_status="allowed",
|
||||
disposition=disposition,
|
||||
reason_codes=("evidence_only_nonexecuting",) if disposition == "allow" else ("untrusted_source_shadowed", "p5_nonexecuting"),
|
||||
source_event_ref_hash="c" * 64,
|
||||
sinks=(sink,),
|
||||
)
|
||||
return handle, lambda candidate, requested_sink=sink: registry.consume(candidate, request, event, p3, requested_sink)
|
||||
|
||||
|
||||
class C5BridgeGateTests(unittest.TestCase):
|
||||
def test_bridge_gate_accepts_current_user_attestation_and_rejects_replay(self):
|
||||
service = make_service()
|
||||
registry = P5AttestationRegistry()
|
||||
request, event, p3 = object(), object(), object()
|
||||
handle, consumer = mint(registry, request=request, event=event, p3=p3)
|
||||
allowed = run(service._p5_gate(sink="bridge_serialization", attestation=handle, consumer=consumer))
|
||||
replay = run(service._p5_gate(sink="bridge_serialization", attestation=handle, consumer=consumer))
|
||||
self.assertTrue(allowed["ok"])
|
||||
self.assertEqual(allowed["state"], "allowed")
|
||||
self.assertFalse(replay["ok"])
|
||||
self.assertEqual(replay["error_code"], "p5_attestation_invalid")
|
||||
|
||||
def test_shadow_source_is_not_allowed_to_expand_recall(self):
|
||||
service = make_service()
|
||||
registry = P5AttestationRegistry()
|
||||
request, event, p3 = object(), object(), object()
|
||||
handle, consumer = mint(
|
||||
registry,
|
||||
request=request,
|
||||
event=event,
|
||||
p3=p3,
|
||||
source_kind="forwarded_text",
|
||||
trust="T3",
|
||||
disposition="shadow_quarantine",
|
||||
)
|
||||
result = run(service._p5_gate(sink="bridge_serialization", attestation=handle, consumer=consumer))
|
||||
self.assertFalse(result["ok"])
|
||||
self.assertEqual(result["state"], "shadow")
|
||||
|
||||
def test_disabled_gate_preserves_legacy_behavior(self):
|
||||
service = make_service(bridge_gate=False, recall_gate=False)
|
||||
result = run(service._p5_gate(sink="memory_recall"))
|
||||
self.assertEqual(result, {"ok": True, "state": "legacy", "legacy": True, "enabled": False})
|
||||
|
||||
def test_ledger_preview_apply_backup_and_cas_rollback(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
ledger = ProvenanceLedger(Path(directory) / "provenance_ledger.json")
|
||||
preview = ledger.preview_legacy(
|
||||
[{"memory_id": "memory-1", "record_revision": 0}],
|
||||
operation_ref_hash="d" * 64,
|
||||
)
|
||||
self.assertTrue(preview["readonly"])
|
||||
self.assertEqual(preview["write_count"], 0)
|
||||
operation = preview["operations"][0]
|
||||
applied = ledger.apply(operation)
|
||||
self.assertTrue(applied["ok"])
|
||||
self.assertEqual(applied["status"], "applied")
|
||||
self.assertTrue(ledger.backup()["created"])
|
||||
self.assertEqual(ledger.snapshot()["records"]["memory-1"]["provenance_state"], "legacy_unresolved")
|
||||
conflict = ledger.rollback({**operation, "after_record_digest": "e" * 64})
|
||||
self.assertFalse(conflict["ok"])
|
||||
rolled_back = ledger.rollback(operation)
|
||||
self.assertTrue(rolled_back["ok"])
|
||||
self.assertEqual(rolled_back["status"], "rolled_back")
|
||||
|
||||
def test_dual_plugin_snapshot_is_recorded_without_raw_payload(self):
|
||||
service = make_service(bridge_gate=True, recall_gate=False)
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
service.provenance_ledger = ProvenanceLedger(Path(directory) / "provenance_ledger.json")
|
||||
registry = P5AttestationRegistry()
|
||||
request, event, p3 = object(), object(), object()
|
||||
handle, consumer = mint(registry, request=request, event=event, p3=p3)
|
||||
gate = run(service._p5_gate(sink="bridge_serialization", attestation=handle, consumer=consumer))
|
||||
result = run(service._p5_record_observed(["memory-1"], gate["snapshot"]))
|
||||
snapshot = service.provenance_snapshot()
|
||||
self.assertTrue(result["ok"])
|
||||
self.assertEqual(snapshot["records"]["memory-1"]["provenance_state"], "observed")
|
||||
self.assertNotIn("content", str(snapshot))
|
||||
self.assertNotIn("prompt", str(snapshot))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
140
tests/test_c5_provenance.py
普通文件
140
tests/test_c5_provenance.py
普通文件
|
|
@ -0,0 +1,140 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import hashlib
|
||||
import json
|
||||
import unittest
|
||||
|
||||
from core.provenance import (
|
||||
ATTESTATION_SCHEMA_VERSION,
|
||||
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,
|
||||
rollback_planned_operation,
|
||||
validate_provenance_record,
|
||||
)
|
||||
|
||||
|
||||
def _hash(value: str) -> str:
|
||||
return hashlib.sha256(value.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def _snapshot(**overrides: object) -> dict[str, object]:
|
||||
value: dict[str, object] = {
|
||||
"schema_version": ATTESTATION_SCHEMA_VERSION,
|
||||
"contract_name": PROVENANCE_CONTRACT_NAME,
|
||||
"contract_version": PROVENANCE_CONTRACT_VERSION,
|
||||
"contract_fingerprint": contract_fingerprint(),
|
||||
"issuer": COMPANION_ATTESTATION_ISSUER,
|
||||
"issuer_epoch": _hash("issuer-epoch"),
|
||||
"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"),
|
||||
"authority_attestation_ref_hash": _hash("authority-attestation"),
|
||||
"request_hash": _hash("request"),
|
||||
"session_hash": _hash("session"),
|
||||
"derived_from_ref_hash": "",
|
||||
"provenance_state": "observed",
|
||||
"sink": "memory_recall",
|
||||
}
|
||||
value.update(overrides)
|
||||
return value
|
||||
|
||||
|
||||
class C5ProvenanceTests(unittest.TestCase):
|
||||
def test_descriptor_fingerprint_is_closed_and_stable(self) -> None:
|
||||
descriptor = contract_descriptor()
|
||||
canonical = json.dumps(descriptor, ensure_ascii=True, sort_keys=True, separators=(",", ":"))
|
||||
self.assertEqual(_hash(canonical), contract_fingerprint())
|
||||
self.assertEqual(13, len(descriptor["record_fields"]))
|
||||
json.dumps(descriptor, ensure_ascii=True)
|
||||
|
||||
def test_legacy_unresolved_is_conservative_and_valid(self) -> None:
|
||||
record = legacy_unresolved("memory-42", record_revision=4)
|
||||
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"])
|
||||
|
||||
def test_valid_snapshot_becomes_observed_and_invalid_snapshot_is_safe(self) -> None:
|
||||
record = observed_from_companion_snapshot("memory-42", _snapshot(), record_revision=7)
|
||||
self.assertEqual("observed", record["provenance_state"])
|
||||
self.assertTrue(validate_provenance_record(record)["ok"])
|
||||
self.assertEqual(_hash("source-event"), record["source_event_ref_hash"])
|
||||
|
||||
invalid = observed_from_companion_snapshot("memory-42", _snapshot(source_trust="T9"))
|
||||
self.assertEqual("invalid", invalid["provenance_state"])
|
||||
invalid = observed_from_companion_snapshot("memory-42", _snapshot(content="raw prose"))
|
||||
self.assertEqual("invalid", invalid["provenance_state"])
|
||||
|
||||
def test_record_and_preview_reject_raw_fields_without_echoing_them(self) -> None:
|
||||
record = legacy_unresolved("memory-42")
|
||||
record["content"] = "do not retain this"
|
||||
result = validate_provenance_record(record)
|
||||
rendered = json.dumps(result, ensure_ascii=False, sort_keys=True)
|
||||
self.assertFalse(result["ok"])
|
||||
self.assertIn("forbidden_raw_field", result["error_codes"])
|
||||
self.assertNotIn("do not retain this", rendered)
|
||||
|
||||
hostile = [{"memory_id": "memory-secret", "record_revision": 0, "prompt": "secret prompt"}]
|
||||
preview = plan_legacy_migration(hostile, operation_ref_hash=_hash("migration"))
|
||||
self.assertEqual([], preview["operations"])
|
||||
self.assertNotIn("secret prompt", json.dumps(preview, ensure_ascii=False))
|
||||
|
||||
def test_preview_is_immutable_and_apply_returns_a_new_record(self) -> None:
|
||||
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"]))
|
||||
operation = preview["operations"][0]
|
||||
applied = apply_planned_operation({"memory_id": "memory-42", "record_revision": 4}, operation)
|
||||
self.assertEqual((True, "applied", True), (applied["ok"], applied["status"], applied["changed"]))
|
||||
self.assertIsNot(applied["record"], operation["after_record"])
|
||||
|
||||
def test_apply_is_idempotent_and_rollback_uses_cas(self) -> None:
|
||||
operation = plan_legacy_migration(
|
||||
[{"memory_id": "memory-42", "record_revision": 4}], operation_ref_hash=_hash("migration-42")
|
||||
)["operations"][0]
|
||||
applied = apply_planned_operation({"memory_id": "memory-42", "record_revision": 4}, operation)
|
||||
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
|
||||
conflict = rollback_planned_operation(concurrent, operation)
|
||||
self.assertEqual((False, "revision_conflict"), (conflict["ok"], conflict["status"]))
|
||||
|
||||
rolled_back = rollback_planned_operation(applied["record"], operation)
|
||||
self.assertEqual((True, "rolled_back", True), (rolled_back["ok"], rolled_back["status"], rolled_back["removed"]))
|
||||
self.assertIsNone(rolled_back["record"])
|
||||
|
||||
def test_owner_recovery_is_preview_only_and_requires_attested_snapshot(self) -> None:
|
||||
legacy = legacy_unresolved("memory-42", record_revision=2)
|
||||
from core.provenance import plan_owner_recovery
|
||||
|
||||
planned = plan_owner_recovery(legacy, _snapshot(), recovery_operation_ref_hash=_hash("recovery"))
|
||||
self.assertTrue(planned["ok"])
|
||||
self.assertEqual("owner_recovered", planned["operation"]["after_record"]["provenance_state"])
|
||||
rejected = plan_owner_recovery(
|
||||
legacy, _snapshot(p3_contract_fingerprint=_hash("wrong-p3")), recovery_operation_ref_hash=_hash("recovery-2")
|
||||
)
|
||||
self.assertEqual(["companion_snapshot_invalid"], rejected["error_codes"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
正在加载…
添加表格
在新工单中引用