Home/Docs/Architecture & Contracts/ContextUnit Protocol

ContextUnit protocolπŸ”—

Envelope and provenanceπŸ”—

ContextUnit is the canonical context envelope shared by service SDKs. Its Pydantic model is packages/core/src/contextunity/core/sdk/contextunit.py; its wire contract is packages/core/protos/contextunit.proto.

The model carries identity, tenant scope, content, metadata, provenance, and timestamps. provenance is always a list and defaults to an empty list. A producer must add or extend provenance when a meaningful transformation or cross-service boundary requires attribution; the model does not fabricate a non-empty chain for raw local construction.

Boundary ruleπŸ”—

Important
  • INV-CONTEXTUNIT-EXPLICIT-ADAPTER: Protobuf messages cross into typed SDK/domain ContextUnit models only at an explicit adapter boundary (to_protobuf / from_protobuf); domain code never passes raw protobuf messages.

Key symbols and inputs/outputsπŸ”—

SymbolRoleInputs / Outputs
to_protobufDomain-to-wire ContextUnit encoderPydantic ContextUnit (unit_id, trace_id, parent_unit_id, modality, open payload carrying domain keys, provenance, chain_of_thought, metrics, security read/write SecurityScopes lists, created_at) -> protobuf ContextUnit
from_protobufWire-to-domain decoderProtobuf ContextUnit -> validated Pydantic ContextUnit
verify_unit_accessUnit authorization guard(token, unit, *, operation="read") -> None, ConfigurationError on invalid operation, or permission denial when unauthorized

Application code uses the Pydantic model. gRPC adapters convert explicitly at the wire boundary with to_protobuf and from_protobuf. Do not pass protobuf messages through domain code or rely on implicit conversion.

Tests must cover round-trip conversion, tenant preservation, metadata and timestamp fidelity, empty provenance, and extended provenance.

Regenerating protobuf codeπŸ”—

From the repository root:

cd packages/core
./compile_protos.sh

The script compiles all core protobufs, fixes generated package imports, and formats the generated modules. Commit contract and generated stub changes together.