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π
- INV-CONTEXTUNIT-EXPLICIT-ADAPTER: Protobuf messages cross into typed SDK/domain
ContextUnitmodels only at an explicit adapter boundary (to_protobuf/from_protobuf); domain code never passes raw protobuf messages.
Key symbols and inputs/outputsπ
| Symbol | Role | Inputs / Outputs |
|---|---|---|
to_protobuf | Domain-to-wire ContextUnit encoder | Pydantic 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_protobuf | Wire-to-domain decoder | Protobuf ContextUnit -> validated Pydantic ContextUnit |
verify_unit_access | Unit 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.shThe script compiles all core protobufs, fixes generated package imports, and formats the generated modules. Commit contract and generated stub changes together.