Skip to main content

Context Engine (context) API

Table of Contents

Top

upsquad/context/v1/context.proto

AssembleContextRequest

AssembleContextRequest is the input to the primary hot-path RPC. The engine assembles a cache-friendly prompt from immutable layers, pinned chunks, and retrieved semantic context within the token budget.

FieldTypeLabelDescription
scopeScopePathscope is the caller's position in the tenant hierarchy (from JWT).
session_idstringsession_id identifies the active conversation session.
task_descriptionstringtask_description is the current agent task used to drive semantic retrieval.
token_budgetint32token_budget is the maximum number of tokens the assembled prompt may use.
model_idstringmodel_id identifies the target LLM so token counting uses the correct tokenizer.
hintsAssembleContextRequest.HintsEntryrepeatedhints carry optional directives such as pinned_chunk_ids or must_include keys.

AssembleContextRequest.HintsEntry

FieldTypeLabelDescription
keystring
valuestring

AssembleContextResponse

AssembleContextResponse is the result of context assembly. It includes the assembled prompt, cache boundary metadata, quality attestation, and savings metrics.

FieldTypeLabelDescription
prompt_idstringprompt_id is the SHA-256 content hash of the assembled prompt, usable as a stable cache key for provider-side prompt caching.
assembled_promptstringassembled_prompt is the full prompt string ready for LLM dispatch. The stable prefix (layers L1–L4 + pinned chunks) is placed first to maximise provider cache hit rate.
cache_prefix_bytesint32cache_prefix_bytes is the byte offset marking the boundary between the stable prefix and the unstable tail. Callers should apply cache_control up to this boundary.
assembled_tokensint32assembled_tokens is the token count of the assembled prompt.
baseline_tokensint32baseline_tokens is the naive token count (no optimisation) sampled on a percentage of calls. Zero when not sampled this call.
savings_ratiodoublesavings_ratio is (baseline_tokens - assembled_tokens) / baseline_tokens. Zero when not sampled.
version_hashstringversion_hash is the Merkle hash of all included chunks; used for delta reuse.
chunk_idsstringrepeatedchunk_ids lists the IDs of every chunk included in the assembled prompt.
qualityQualityAttestationquality contains attestation data about retrieval confidence.

CompactRequest

CompactRequest triggers synchronous compaction for a session. Async compaction is triggered automatically via Redis Streams when session tokens exceed 80% of the token budget.

FieldTypeLabelDescription
scopeScopePathscope is the caller's position in the tenant hierarchy.
session_idstringsession_id identifies the session to compact.
strategystringstrategy selects the compaction algorithm. Only 'sliding_window' is supported in MVP.

CompactResponse

CompactResponse reports compaction results including quality metrics.

FieldTypeLabelDescription
version_hashstringversion_hash is the content hash of the post-compaction session state.
original_tokensint32original_tokens is the token count before compaction.
compacted_tokensint32compacted_tokens is the token count after compaction.
fact_retention_scoredoublefact_retention_score is the self-evaluated quality score in [0, 1]. Compaction is rejected (QUALITY_GUARDRAIL_TRIGGERED) if this falls below 0.98.

CreateShareRequest

CreateShareRequest grants a grantee scope access to a specific resource. The grantor scope is inferred from the caller's JWT claims. Requires clearance L3+; cross-pillar shares require L4+.

FieldTypeLabelDescription
scopeScopePathscope is the grantor's position in the tenant hierarchy.
resource_idstringresource_id is the UUID of the resource (chunk, snapshot, layer) to share.
resource_typestringresource_type identifies the kind of resource: 'chunk', 'snapshot', 'layer'.
grantee_scopestringgrantee_scope is the hierarchical scope type of the grantee: 'org', 'pillar', 'team', or 'agent'.
grantee_scope_idstringgrantee_scope_id is the UUID of the grantee entity within grantee_scope.
permissionsstringrepeatedpermissions is the list of allowed operations: 'read', 'use_in_assembly'.
reasonstringreason is an optional audit-trail annotation for the share grant.

CreateShareResponse

CreateShareResponse confirms the share grant creation.

FieldTypeLabelDescription
share_idstringshare_id is the UUID of the created share grant record.
created_atgoogle.protobuf.Timestampcreated_at is the server-side timestamp of the grant.

CreateSnapshotRequest

CreateSnapshotRequest creates a named, immutable snapshot of the current session state. Requires clearance L2 or above.

FieldTypeLabelDescription
scopeScopePathscope is the caller's position in the tenant hierarchy.
session_idstringsession_id identifies the session to snapshot.
snapshot_namestringsnapshot_name is the human-readable label for this snapshot.

CreateSnapshotResponse

CreateSnapshotResponse confirms snapshot creation.

FieldTypeLabelDescription
snapshot_idstringsnapshot_id is the UUID of the created snapshot record.
version_hashstringversion_hash is the content hash of the snapshotted state.
created_atgoogle.protobuf.Timestampcreated_at is the server-side timestamp of the snapshot.

EmbedRequest

EmbedRequest generates embeddings for one or more text inputs. Results are cached by content hash (30-day TTL).

FieldTypeLabelDescription
scopeScopePathscope is the caller's position in the tenant hierarchy.
textsstringrepeatedtexts is the list of strings to embed. Batches up to 100 items.
model_versionstringmodel_version selects the embedding model revision. An empty string uses the configured default (text-embedding-3-small).

EmbedResponse

EmbedResponse returns one embedding vector per input text.

FieldTypeLabelDescription
embeddingsEmbeddingVectorrepeatedembeddings is the list of float32 vectors, in the same order as the request texts.
model_versionstringmodel_version records which model revision produced these embeddings.

EmbeddingVector

EmbeddingVector holds a single dense embedding vector.

FieldTypeLabelDescription
valuesfloatrepeatedvalues is the float32 representation. Dimension is model-dependent (default 1536).
cachedboolcached is true when this embedding was served from the Redis embedding cache.

GuardrailLayer

GuardrailLayer describes one immutable guardrail layer (L1-L4) as it stands right now in the guardrail store. The version is the freshness oracle the orchestrator uses to decide whether to re-pin at a turn boundary.

FieldTypeLabelDescription
layer_levelint32layer_level is the chain-of-trust level, 1..4 (L1 platform, L2 tenant, L3 team, L4 agent).
content_hashstringcontent_hash is the current SHA-256 of the active layer content.
versionint64version is monotonic per (scope, layer_level): a higher version is always a strictly later revision. Re-pin only auto-adopts forward versions.
effective_atint64effective_at is the unix-milliseconds timestamp at which this version became active (derived from the layer's signed_at).

IngestEventRequest

IngestEventRequest persists a single conversation event.

FieldTypeLabelDescription
scopeScopePathscope is the caller's position in the tenant hierarchy.
session_idstringsession_id identifies the active conversation session.
event_typestringevent_type categorises the event: 'message', 'tool_output', 'workflow_event'.
contentbytescontent is the raw event payload; encoding is caller-defined (typically UTF-8 JSON).
metadataIngestEventRequest.MetadataEntryrepeatedmetadata carries optional key-value annotations (e.g. role, tool_name, latency_ms).

IngestEventRequest.MetadataEntry

FieldTypeLabelDescription
keystring
valuestring

IngestEventResponse

IngestEventResponse confirms ingestion and returns the content hash for deduplication and delta tracking.

FieldTypeLabelDescription
event_idstringevent_id is the UUID of the persisted event record.
version_hashstringversion_hash is the SHA-256 of the event content; used for deduplication.
token_countint32token_count is the tiktoken-computed token count of the content.

ListSharesRequest

ListSharesRequest retrieves all share grants visible to the caller.

FieldTypeLabelDescription
scopeScopePathscope is the caller's position in the tenant hierarchy.
pageint32page is the 1-based page number for pagination.
page_sizeint32page_size is the maximum number of results per page. Max 100.

ListSharesResponse

ListSharesResponse returns a paginated list of share grants.

FieldTypeLabelDescription
sharesShareGrantrepeatedshares is the list of share grant records visible to the caller.
total_countint32total_count is the total number of matching grants (for pagination).

PullContextRequest

PullContextRequest retrieves a memory snapshot from the version store. Always reads from primary DB — never from cache.

FieldTypeLabelDescription
scopeScopePathscope is the caller's position in the tenant hierarchy.
agent_idstringagent_id identifies the agent whose memory is being retrieved.
session_idstringsession_id optionally scopes the pull to a specific session.
snapshot_namestringsnapshot_name optionally targets a named snapshot instead of the latest version.

PullContextResponse

PullContextResponse returns the memory snapshot payload.

FieldTypeLabelDescription
version_hashstringversion_hash identifies the version of the returned snapshot.
memory_typestringmemory_type categorises the returned content.
contentbytescontent is the serialised memory payload.
created_atgoogle.protobuf.Timestampcreated_at is the timestamp of the snapshot.
metadataPullContextResponse.MetadataEntryrepeatedmetadata carries annotations stored with the snapshot.

PullContextResponse.MetadataEntry

FieldTypeLabelDescription
keystring
valuestring

PushContextRequest

PushContextRequest persists a memory snapshot to the version store.

FieldTypeLabelDescription
scopeScopePathscope is the caller's position in the tenant hierarchy.
session_idstringsession_id identifies the session whose memory is being pushed.
memory_typestringmemory_type categorises the content: 'episodic', 'semantic', 'working'.
contentbytescontent is the serialised memory payload.
metadataPushContextRequest.MetadataEntryrepeatedmetadata carries optional annotations.

PushContextRequest.MetadataEntry

FieldTypeLabelDescription
keystring
valuestring

PushContextResponse

PushContextResponse confirms the push and returns the new version hash.

FieldTypeLabelDescription
version_hashstringversion_hash is the SHA-256 of the pushed content; acts as a version tag.
created_atgoogle.protobuf.Timestampcreated_at is the server-side timestamp of the push.

QualityAttestation

QualityAttestation captures retrieval quality signals attached to every AssembleContextResponse. Used by the guardrail engine to decide whether to expand retrieval or fall back to recency-only.

FieldTypeLabelDescription
confidence_scoredoubleconfidence_score is the mean retrieval similarity score in [0, 1].
expansion_appliedboolexpansion_applied is true when the engine broadened the retrieval pool due to a low initial confidence score.
fallback_usedboolfallback_used is true when the engine fell back to recency-only retrieval because semantic retrieval failed or returned no results.
fallback_reasonstringfallback_reason describes why the fallback was triggered.

ResolveGuardrailLayersRequest

ResolveGuardrailLayersRequest asks the engine for the current guardrail layer set for the caller's scope. It carries only the scope; the engine resolves the active L1-L4 layers under the same RLS GUC as ValidatePrompt.

FieldTypeLabelDescription
scopeScopePathscope is the caller's position in the tenant hierarchy (from JWT).

ResolveGuardrailLayersResponse

ResolveGuardrailLayersResponse returns the current L1-L4 guardrail layers, each labelled with its monotonic store version. Layers are returned in ascending layer_level order (L1..L4). A layer that has no active row for the scope is simply absent from the list.

FieldTypeLabelDescription
layersGuardrailLayerrepeatedlayers is the current guardrail set, L1-L4, ascending by layer_level.

RevokeShareRequest

RevokeShareRequest removes an existing share grant. Requires clearance L3+ or being the original grant creator.

FieldTypeLabelDescription
scopeScopePathscope is the caller's position in the tenant hierarchy.
share_idstringshare_id is the UUID of the share grant to revoke.
reasonstringreason is an optional audit-trail annotation for the revocation.

RevokeShareResponse

RevokeShareResponse confirms share grant revocation.

FieldTypeLabelDescription
revoked_atgoogle.protobuf.Timestamprevoked_at is the server-side timestamp of the revocation.

RollbackContextRequest

RollbackContextRequest reverts a session to a previous version. Requires clearance L3 or above (team lead).

FieldTypeLabelDescription
scopeScopePathscope is the caller's position in the tenant hierarchy.
session_idstringsession_id identifies the session to roll back.
target_version_hashstringtarget_version_hash is the content hash of the version to restore.

RollbackContextResponse

RollbackContextResponse confirms the rollback and reports the restored state.

FieldTypeLabelDescription
restored_version_hashstringrestored_version_hash is the hash of the now-active version after rollback.
rolled_back_atgoogle.protobuf.Timestamprolled_back_at is the server-side timestamp of the rollback operation.

ScopePath

ScopePath identifies the caller's position in the tenant hierarchy. Every RPC must carry a ScopePath extracted from the caller's JWT claims. The engine validates that the scope in the request matches the JWT scope and enforces hierarchical isolation: Org > Pillar > Team > Agent.

FieldTypeLabelDescription
org_idstringorg_id is always required; identifies the tenant organisation.
pillar_idstringpillar_id is empty when operating at org level.
team_idstringteam_id is empty when operating at pillar level or above.
agent_idstringagent_id is empty when operating at team level or above.

SearchRequest

SearchRequest performs a hybrid (vector + BM25 + recency) search.

FieldTypeLabelDescription
scopeScopePathscope is the caller's position in the tenant hierarchy.
querystringquery is the natural-language search string.
domainstringdomain optionally restricts the search to a specific knowledge domain.
top_kint32top_k is the maximum number of results to return. Default: 10.
min_scoredoublemin_score is the minimum similarity threshold for inclusion. Default: 0.7.

SearchResponse

SearchResponse wraps a ranked list of search results.

FieldTypeLabelDescription
resultsSearchResultrepeatedresults is the ranked list of matching chunks.

SearchResult

SearchResult is a single retrieved chunk with its provenance and score.

FieldTypeLabelDescription
chunk_idstringchunk_id is the UUID of the stored chunk.
contentstringcontent is the decoded text content of the chunk.
scoredoublescore is the final hybrid relevance score in [0, 1].
sourcestringsource identifies the origin document or event that produced this chunk.
metadataSearchResult.MetadataEntryrepeatedmetadata carries additional key-value annotations on the chunk.

SearchResult.MetadataEntry

FieldTypeLabelDescription
keystring
valuestring

ShareGrant

ShareGrant is a single share grant record as returned by ListShares.

FieldTypeLabelDescription
share_idstringshare_id is the UUID of this grant.
resource_idstringresource_id is the UUID of the shared resource.
resource_typestringresource_type identifies the kind of resource.
grantor_scope_idstringgrantor_scope_id is the UUID of the entity that created the grant.
grantee_scopestringgrantee_scope is the hierarchical scope type of the grantee.
grantee_scope_idstringgrantee_scope_id is the UUID of the grantee entity.
permissionsstringrepeatedpermissions is the list of allowed operations.
reasonstringreason is the audit annotation recorded at grant creation.
created_atgoogle.protobuf.Timestampcreated_at is when the grant was created.

ValidatePromptRequest

ValidatePromptRequest submits an assembled prompt for tampering and injection detection against the 4-layer hash chain.

FieldTypeLabelDescription
scopeScopePathscope is the caller's position in the tenant hierarchy.
promptstringprompt is the assembled prompt string to validate.
layer_hashesstringrepeatedlayer_hashes contains the expected SHA-256 hashes of layers L1–L4 in order. The engine verifies the prompt contains these layers verbatim.

ValidatePromptResponse

ValidatePromptResponse reports the validation result.

FieldTypeLabelDescription
validboolvalid is true when the prompt passes all validation checks.
violation_codestringviolation_code is set when valid is false; maps to the error code taxonomy.
violation_detailstringviolation_detail provides a human-readable explanation of the violation.

ContextEngine

ContextEngine is the central gRPC service for all context, memory, and retrieval operations. It sits between the Agent Runtime and the data layer.

--- Hot path (called before every LLM call) ---

Method NameRequest TypeResponse TypeDescription
AssembleContextAssembleContextRequestAssembleContextResponseAssembleContext assembles an optimised prompt from the 4-layer immutable chain of trust plus retrieved semantic chunks. Target latency: p95 < 500ms.
IngestEventIngestEventRequestIngestEventResponseIngestEvent persists a conversation event, computes its content hash, counts tokens, and enqueues async embedding.
CompactCompactRequestCompactResponseCompact summarises older session history via the compaction LLM (Gemini 2.5 Flash-Lite) when session tokens exceed 80% of the token budget. Only the sliding_window strategy is supported in MVP.
PushContextPushContextRequestPushContextResponsePushContext persists a memory snapshot to the version-controlled store.
PullContextPullContextRequestPullContextResponsePullContext retrieves a memory snapshot. Reads from primary DB (never cache).
RollbackContextRollbackContextRequestRollbackContextResponseRollbackContext reverts a session to a previous version hash. Requires clearance L3 or above.
CreateSnapshotCreateSnapshotRequestCreateSnapshotResponseCreateSnapshot creates a named, immutable snapshot of the current session state. Requires clearance L2 or above.
ValidatePromptValidatePromptRequestValidatePromptResponseValidatePrompt checks an assembled prompt against the 4-layer hash chain to detect tampering and prompt-injection patterns.
SearchSearchRequestSearchResponseSearch performs a hybrid (vector + BM25 + recency) search across the caller's accessible context corpus.
EmbedEmbedRequestEmbedResponseEmbed generates embeddings for the provided texts using the configured embedding model (default: text-embedding-3-small). Results are cached by content hash with a 30-day TTL.
CreateShareCreateShareRequestCreateShareResponseCreateShare grants a named grantee scope access to a specific resource. Requires clearance L3+; cross-pillar shares require L4+.
RevokeShareRevokeShareRequestRevokeShareResponseRevokeShare removes an existing share grant. Requires clearance L3+ or being the original grant creator.
ListSharesListSharesRequestListSharesResponseListShares returns share grants where the caller is owner or grantee.
ResolveGuardrailLayersResolveGuardrailLayersRequestResolveGuardrailLayersResponseResolveGuardrailLayers returns the current 4-layer guardrail set (L1-L4) for the caller's scope, each labelled with its monotonic store version. This is the same layer set ValidatePrompt compares against, but exposes the version that already exists in the guardrail store so the orchestrator can distinguish a forward (tightening) update from a backward/sideways mutation at a turn boundary. Additive; ValidatePrompt is unchanged. Resolves under the same RLS GUC (app.org_id) as every other RPC.

Top

upsquad/context/v1/stream.proto

AuditAccessEvent

AuditAccessEvent is the payload carried in audit:access stream messages. Published by any service that performs a cross-scope access operation. Consumed by the Audit Writer for async persistence to the audit log.

FieldTypeLabelDescription
actor_scopeScopePathactor_scope is the scope of the entity performing the access.
target_resource_idstringtarget_resource_id is the UUID of the accessed resource.
target_resource_typestringtarget_resource_type identifies the kind of accessed resource.
operationstringoperation describes the action performed (e.g. 'read', 'create_share').
share_idstringshare_id is set when the access was via a share grant.
outcomestringoutcome is 'allowed' or 'denied'.
denial_reasonstringdenial_reason is set when outcome is 'denied'.
occurred_atgoogle.protobuf.Timestampoccurred_at is the timestamp of the access attempt.

BaselineMetricEvent

BaselineMetricEvent is the payload carried in metrics:baseline stream messages. Published by the Assembly Pipeline on sampled calls to measure naive token usage. Consumed by the Baseline Calculator for savings ratio computation.

FieldTypeLabelDescription
prompt_idstringprompt_id is the content hash of the assembled prompt being measured.
scopeScopePathscope is the caller's position in the tenant hierarchy.
session_idstringsession_id identifies the session.
assembled_tokensint32assembled_tokens is the optimised token count.
naive_tokensint32naive_tokens is the unoptimised (baseline) token count.
model_idstringmodel_id is the target LLM that will consume this prompt.
sampled_atgoogle.protobuf.Timestampsampled_at is when this sample was taken.

CompactionJob

CompactionJob is the payload carried in compaction:{org_id} stream messages. Published by the Assembly Pipeline when session tokens exceed 80% of budget. Consumed by the Compaction Worker Pool.

FieldTypeLabelDescription
session_idstringsession_id identifies the session that requires compaction.
scopeScopePathscope is the session owner's position in the tenant hierarchy.
current_token_countint32current_token_count is the token count that triggered the compaction.
token_budgetint32token_budget is the configured budget that was exceeded.
strategystringstrategy is the requested compaction strategy (currently 'sliding_window').
triggered_atgoogle.protobuf.Timestamptriggered_at is the timestamp when the threshold was crossed.

EmbeddingJob

EmbeddingJob is the payload carried in embedding:{org_id} stream messages. Published by the Ingestion Service for async chunk embedding. Consumed by the Embedding Pipeline.

FieldTypeLabelDescription
event_idstringevent_id is the UUID of the ingested event whose content needs embedding.
chunk_idsstringrepeatedchunk_ids are the UUIDs of the text chunks to embed.
scopeScopePathscope is the caller's position in the tenant hierarchy.
model_versionstringmodel_version selects the embedding model revision. Empty = default.
queued_atgoogle.protobuf.Timestampqueued_at is the timestamp when this job was enqueued.

ReindexJob

ReindexJob is the payload carried in reindex:{org_id} stream messages. Published by the Admin API when the embedding model version changes or when corruption is detected. Consumed by the Reindex Worker.

FieldTypeLabelDescription
scopeScopePathscope identifies which org/pillar/team scope needs re-embedding.
old_model_versionstringold_model_version is the model revision being replaced.
new_model_versionstringnew_model_version is the target model revision.
initiated_bystringinitiated_by is the user/agent ID that triggered the reindex.
initiated_atgoogle.protobuf.Timestampinitiated_at is the timestamp of the reindex request.

StreamEnvelope

StreamEnvelope is the common wrapper for every message published to a Redis Stream. It carries routing metadata (message_id, stream name, ScopePath) alongside the opaque serialised payload.

Stream → Payload type mapping: compaction:{org_id} → CompactionJob embedding:{org_id} → EmbeddingJob reindex:{org_id} → ReindexJob metrics:baseline → BaselineMetricEvent audit:access → AuditAccessEvent

FieldTypeLabelDescription
message_idstringmessage_id is the unique identifier for this stream message (UUID). Consumers use this for idempotency and ACK tracking.
streamstringstream is the full Redis Stream key this message was published to (e.g. "compaction:org-123").
scopeScopePathscope is the ScopePath of the originating caller; used by consumers to re-establish tenant context without decoding the payload.
created_atgoogle.protobuf.Timestampcreated_at is the server-side timestamp of publication.
payloadbytespayload is the stream-specific Protobuf message serialised to bytes. Consumers must decode it based on the stream field.

Scalar Value Types

.proto TypeNotesC++JavaPythonGoC#PHPRuby
doubledoubledoublefloatfloat64doublefloatFloat
floatfloatfloatfloatfloat32floatfloatFloat
int32Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead.int32intintint32intintegerBignum or Fixnum (as required)
int64Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead.int64longint/longint64longinteger/stringBignum
uint32Uses variable-length encoding.uint32intint/longuint32uintintegerBignum or Fixnum (as required)
uint64Uses variable-length encoding.uint64longint/longuint64ulonginteger/stringBignum or Fixnum (as required)
sint32Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s.int32intintint32intintegerBignum or Fixnum (as required)
sint64Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s.int64longint/longint64longinteger/stringBignum
fixed32Always four bytes. More efficient than uint32 if values are often greater than 2^28.uint32intintuint32uintintegerBignum or Fixnum (as required)
fixed64Always eight bytes. More efficient than uint64 if values are often greater than 2^56.uint64longint/longuint64ulonginteger/stringBignum
sfixed32Always four bytes.int32intintint32intintegerBignum or Fixnum (as required)
sfixed64Always eight bytes.int64longint/longint64longinteger/stringBignum
boolboolbooleanbooleanboolboolbooleanTrueClass/FalseClass
stringA string must always contain UTF-8 encoded or 7-bit ASCII text.stringStringstr/unicodestringstringstringString (UTF-8)
bytesMay contain any arbitrary sequence of bytes.stringByteStringstr[]byteByteStringstringString (ASCII-8BIT)