crv.core.grammar
Experimental API
Grammar synchronization and EBNF may change as expressivity is extended. Since: 2025-09-25.
crv.core.grammar
Canonical grammar and normalization helpers.
Defines enums (actions, channels, visibility, patch operations, representation edge kinds, exchange kinds) and helpers to normalize/validate enum-like strings. Includes the authoritative lower_snake EBNF. Zero‑IO; source of truth for naming.
See Also
schema.md, versioning.md
crv.core.grammar.EBNF_GRAMMAR
module-attribute
crv.core.grammar.PARSED_GRAMMAR
module-attribute
PARSED_GRAMMAR: typing.Final[crv.core.grammar.ParsedGrammar] = (
crv.core.grammar.ParsedGrammar.from_text(crv.core.grammar.EBNF_GRAMMAR)
)
crv.core.grammar.GrammarProduction
dataclass
Parsed production with convenient accessors.
Source code in src/crv/core/grammar.py
crv.core.grammar.GrammarProduction.leading_terminals
instance-attribute
crv.core.grammar.ParsedGrammar
dataclass
Container for parsed grammar productions.
Source code in src/crv/core/grammar.py
crv.core.grammar.ParsedGrammar.productions
instance-attribute
crv.core.grammar.ParsedGrammar.production
crv.core.grammar.ParsedGrammar.lower_snake_terminals
crv.core.grammar.ParsedGrammar.from_text
classmethod
Source code in src/crv/core/grammar.py
crv.core.grammar.ActionKind
Bases: enum.Enum
All concrete action verbs an agent (or venue) may emit.
Serialized values are used in
- DecisionHead.action_candidates[].action_type (schema.DecisionHead)
- Event envelopes payloads written by world
- EBNF terminals
Notes
Table mappings: * messages (send_chat_message, publish_announcement) * exchange (post/cancel/order, trade, swap, peer exchange, vote, gift) * scenarios_seen (as part of context reconstruction) * decisions (chosen_action and action_candidates)
Source code in src/crv/core/grammar.py
crv.core.grammar.ActionKind.ACQUIRE_TOKEN
class-attribute
instance-attribute
crv.core.grammar.ActionKind.RELINQUISH_TOKEN
class-attribute
instance-attribute
crv.core.grammar.ActionKind.RELATE_AGENT
class-attribute
instance-attribute
crv.core.grammar.ActionKind.RELATE_OTHER_AGENTS
class-attribute
instance-attribute
crv.core.grammar.ActionKind.ENDORSE_TOKEN
class-attribute
instance-attribute
crv.core.grammar.ActionKind.COENDORSE_TOKEN_WITH_AGENTS
class-attribute
instance-attribute
crv.core.grammar.ActionKind.EXPOSE_SIGNAL_ABOUT_TOKEN
class-attribute
instance-attribute
crv.core.grammar.ActionKind.DECLARE_COOCCURRENCE
class-attribute
instance-attribute
crv.core.grammar.ActionKind.PROPOSE_PEER_EXCHANGE
class-attribute
instance-attribute
crv.core.grammar.ActionKind.ACCEPT_PEER_EXCHANGE
class-attribute
instance-attribute
crv.core.grammar.ActionKind.REJECT_PEER_EXCHANGE
class-attribute
instance-attribute
crv.core.grammar.ActionKind.SETTLE_PEER_EXCHANGE
class-attribute
instance-attribute
crv.core.grammar.ActionKind.POST_ORDER_TO_VENUE
class-attribute
instance-attribute
crv.core.grammar.ActionKind.CANCEL_ORDER_AT_VENUE
class-attribute
instance-attribute
crv.core.grammar.ActionKind.SETTLE_TRADE_FROM_VENUE
class-attribute
instance-attribute
crv.core.grammar.ActionKind.SWAP_AT_VENUE
class-attribute
instance-attribute
crv.core.grammar.ActionKind.CAST_VOTE_AT_VENUE
class-attribute
instance-attribute
crv.core.grammar.ActionKind.PUBLISH_VOTE_OUTCOME
class-attribute
instance-attribute
crv.core.grammar.ActionKind.SEND_CHAT_MESSAGE
class-attribute
instance-attribute
crv.core.grammar.ChannelType
Bases: enum.Enum
Logical channel families for routing & visibility.
Serialized values appear in
- messages.channel_name (prefix like 'group:G1', 'room:market')
- event envelopes channel field
Source code in src/crv/core/grammar.py
crv.core.grammar.Visibility
Bases: enum.Enum
Rendered visibility on observations or messages.
Serialized values appear in
- messages.visibility_scope
- event envelopes visibility field
- scenarios_seen.visibility_scope
Source code in src/crv/core/grammar.py
crv.core.grammar.PatchOp
Bases: enum.Enum
Atomic edit operations over the identity/affect representation.
Canonical-only policy
Only the following operations are valid and accepted: - set_identity_edge_weight - adjust_identity_edge_weight - decay_identity_edges - remove_identity_edge
Notes
Use the canonical operations together with explicit edge_kind and fields, e.g., object_to_object with subject_id/object_id, or object_to_positive_valence / object_to_negative_valence with token_id.
Table mappings: * identity_edges (rows produced AFTER patches are applied) — we log the post-barrier state, not the patch instruction itself.
Source code in src/crv/core/grammar.py
crv.core.grammar.PatchOp.SET_IDENTITY_EDGE_WEIGHT
class-attribute
instance-attribute
crv.core.grammar.PatchOp.ADJUST_IDENTITY_EDGE_WEIGHT
class-attribute
instance-attribute
crv.core.grammar.PatchOp.DECAY_IDENTITY_EDGES
class-attribute
instance-attribute
crv.core.grammar.RepresentationEdgeKind
Bases: enum.Enum
Identity/affect edge kinds INSIDE an agent's representation.
Use these values in
- identity_edges.edge_kind
Notes
Table mappings: * identity_edges: one row per edge snapshot/delta written AFTER the barrier applies representation patches.
Psych/math mapping: These correspond to the following psychological constructs and math symbols. Valence is modeled as dual implicit channels per object (r^+{i,o}, r^-{i,o}) and dual "friend/foe" channels per other agent (u^+{i,j}, u^-{i,j}) from self i's perspective. a_{i,j} is an additional primitive self–other tie; anchors s_i^+, s_i^- are slow self-evaluations.
- self_to_positive_valence -> s^+_{i} (self anchor)
- self_to_negative_valence -> s^-_{i} (self anchor)
- self_to_object -> s_{i,o}
- self_to_agent -> a_{i,j} (primitive self–other tie)
- agent_to_positive_valence -> u^+_{i,j} (self's positive feeling toward agent j)
- agent_to_negative_valence -> u^-_{i,j} (self's negative feeling toward agent j)
- agent_to_object -> b_{i,j,o}
- agent_to_agent -> d_{i,k,l}
- agent_pair_to_object -> q_{i,k,l,o}
- object_to_positive_valence -> r^+_{i,o}
- object_to_negative_valence -> r^-_{i,o}
- object_to_object -> c_{i,o,o'}
Source code in src/crv/core/grammar.py
crv.core.grammar.RepresentationEdgeKind.SELF_TO_POSITIVE_VALENCE
class-attribute
instance-attribute
crv.core.grammar.RepresentationEdgeKind.SELF_TO_NEGATIVE_VALENCE
class-attribute
instance-attribute
crv.core.grammar.RepresentationEdgeKind.SELF_TO_OBJECT
class-attribute
instance-attribute
crv.core.grammar.RepresentationEdgeKind.SELF_TO_AGENT
class-attribute
instance-attribute
crv.core.grammar.RepresentationEdgeKind.AGENT_TO_POSITIVE_VALENCE
class-attribute
instance-attribute
crv.core.grammar.RepresentationEdgeKind.AGENT_TO_NEGATIVE_VALENCE
class-attribute
instance-attribute
crv.core.grammar.RepresentationEdgeKind.AGENT_TO_OBJECT
class-attribute
instance-attribute
crv.core.grammar.RepresentationEdgeKind.AGENT_TO_AGENT
class-attribute
instance-attribute
crv.core.grammar.RepresentationEdgeKind.AGENT_PAIR_TO_OBJECT
class-attribute
instance-attribute
crv.core.grammar.RepresentationEdgeKind.OBJECT_TO_POSITIVE_VALENCE
class-attribute
instance-attribute
crv.core.grammar.RepresentationEdgeKind.OBJECT_TO_NEGATIVE_VALENCE
class-attribute
instance-attribute
crv.core.grammar.TopologyEdgeKind
Bases: enum.Enum
World topology edge kinds (STRUCTURE OUTSIDE the agent).
These DO NOT go into identity_edges. In a topology/world table, use these values for that table's edge_kind (e.g., 'world_topology_edges').
Examples:
- is_neighbor: grid/graph adjacency
- follows: directed social following
- in_group: static group membership edge
Notes
Table mappings: * world_topology_edges (future) — NOT part of identity_edges.
Source code in src/crv/core/grammar.py
crv.core.grammar.TopologyEdgeKind.IS_NEIGHBOR
class-attribute
instance-attribute
crv.core.grammar.ExchangeKind
Bases: enum.Enum
Generalized ownership-exchange events (beyond finance).
Serialized values appear in
- exchange.exchange_event_type
- EBNF terminals in the 'exchange' section
Notes
Table mappings: * exchange (one row per event): - price/quantity for trade-like events - baseline_value if venue emits an index (price / poll % / trend) - additional_payload for venue specifics
Source code in src/crv/core/grammar.py
crv.core.grammar.ExchangeKind.ORDER_POST
class-attribute
instance-attribute
crv.core.grammar.ExchangeKind.ORDER_CANCEL
class-attribute
instance-attribute
crv.core.grammar.ExchangeKind.PEER_OFFER
class-attribute
instance-attribute
crv.core.grammar.ExchangeKind.PEER_ACCEPT
class-attribute
instance-attribute
crv.core.grammar.ExchangeKind.PEER_REJECT
class-attribute
instance-attribute
crv.core.grammar.TableName
Bases: enum.Enum
Canonical Parquet table names. Enforced by crv.io.tables.
Source code in src/crv/core/grammar.py
crv.core.grammar.TableName.IDENTITY_EDGES
class-attribute
instance-attribute
crv.core.grammar.TableName.SCENARIOS_SEEN
class-attribute
instance-attribute
crv.core.grammar.TableName.ORACLE_CALLS
class-attribute
instance-attribute
crv.core.grammar.TableDescriptor
dataclass
Frozen descriptor for a canonical CRV table.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
crv.core.grammar.TableName
|
Canonical table identifier (lower_snake serialized). |
columns |
dict[str, str]
|
Mapping of lower_snake column_name -> dtype where dtype ∈ {"i64","f64","str","struct","list[struct]"}. |
partitioning |
list[str]
|
Partition columns (always ["bucket"]). |
required |
list[str]
|
Columns that must exist and be populated (non-null). |
nullable |
list[str]
|
Columns permitted to contain nulls. Validations for combinations are enforced by Pydantic row models downstream. |
version |
crv.core.versioning.SchemaVersion
|
Schema version pinned to crv.core.versioning.SCHEMA_V. |
Notes
- Core remains zero-IO; this type describes the schema contract only.
- IO layers (crv.io) materialize, validate, and append frames against these descriptors.
Source code in src/crv/core/grammar.py
crv.core.grammar.is_lower_snake
Check whether a string is lower_snake.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Candidate string to validate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if value matches lower_snake (e.g., "acquire_token"), False otherwise. |
Examples:
Source code in src/crv/core/grammar.py
crv.core.grammar.assert_lower_snake
Validate that a string is lower_snake.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Candidate string to validate. |
required |
what
|
str
|
Human-friendly label used in the error message. |
'value'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If value is not lower_snake. |
Source code in src/crv/core/grammar.py
crv.core.grammar.action_value
Get the serialized (lower_snake) value for an ActionKind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
crv.core.grammar.ActionKind
|
Action enum. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Lower_snake serialized value (e.g., "acquire_token"). |
Source code in src/crv/core/grammar.py
crv.core.grammar.action_kind_from_value
Parse a lower_snake action string into an ActionKind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
Lower_snake action string. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ActionKind |
crv.core.grammar.ActionKind
|
Parsed action kind. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If s is not lower_snake or is not a known action. |
Source code in src/crv/core/grammar.py
crv.core.grammar.exchange_value
Get the serialized (lower_snake) value for an ExchangeKind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
crv.core.grammar.ExchangeKind
|
Exchange enum. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Lower_snake serialized value (e.g., "trade"). |
Source code in src/crv/core/grammar.py
crv.core.grammar.exchange_kind_from_value
Parse a lower_snake exchange string into an ExchangeKind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
Lower_snake exchange kind string. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ExchangeKind |
crv.core.grammar.ExchangeKind
|
Parsed exchange kind. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If s is not lower_snake or is not a known exchange kind. |
Source code in src/crv/core/grammar.py
crv.core.grammar.edge_value
Get the serialized (lower_snake) value for a RepresentationEdgeKind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
crv.core.grammar.RepresentationEdgeKind
|
Edge kind enum. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Lower_snake serialized value (e.g., "object_to_object"). |
Source code in src/crv/core/grammar.py
crv.core.grammar.edge_kind_from_value
Parse a lower_snake edge_kind string into a RepresentationEdgeKind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
Lower_snake edge kind string. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RepresentationEdgeKind |
crv.core.grammar.RepresentationEdgeKind
|
Parsed edge kind. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If s is not lower_snake or is not a known edge kind. |
Source code in src/crv/core/grammar.py
crv.core.grammar.normalize_visibility
Normalize a free-form visibility token to canonical lower_snake.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vis
|
str
|
Candidate visibility token. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
One of {"public","group","room","dm"}. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the token does not match a known visibility class. |
Notes
Used by: - messages.visibility_scope - scenarios_seen.visibility_scope - event envelopes visibility field
Source code in src/crv/core/grammar.py
crv.core.grammar.normalize_channel_type
Normalize a free-form channel type token to canonical lower_snake.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ch
|
str
|
Candidate channel type token. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
One of {"public","group","room","dm"}. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the token is not a recognized channel type. |
Notes
Typically used only for validation when parsing a channel prefix.
Source code in src/crv/core/grammar.py
crv.core.grammar.canonical_action_key
Build a compact, human-friendly log key for an action candidate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action_type
|
crv.core.grammar.ActionKind
|
Action type to label. |
required |
**params
|
object
|
Optional parameters to include in the label. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Key formatted as "action:sorted_k=v|k=v". |
Examples:
>>> canonical_action_key(ActionKind.ACQUIRE_TOKEN, token_id="Alpha")
'acquire_token:token_id=Alpha'
>>> canonical_action_key(ActionKind.SEND_CHAT_MESSAGE, channel="group:G1")
'send_chat_message:channel=group:G1'
Notes
The key is for logs and dashboards only; program logic must use structured fields rather than parsing this string.
Used by: - decisions.action_candidates[].key (string) - logs / dashboards (viz) as human-readable labels
Source code in src/crv/core/grammar.py
crv.core.grammar.ensure_all_enum_values_lower_snake
Assert that every enum member's value is lower_snake.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enums
|
collections.abc.Iterable[type[enum.Enum]]
|
Iterable of Enum classes to inspect. |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If any enum member has a non-lower_snake value. |
Examples:
>>> ensure_all_enum_values_lower_snake([
... ActionKind, ChannelType, Visibility,
... PatchOp, RepresentationEdgeKind, ExchangeKind, TableName
... ])