Kernel Services
Agent Registry
dir_core.agent_registry
Agent Registry: contract, handshake, lookup by agent_id.
DIR §2.3. Maintains a registry of active agents, their capability contracts, and metadata. Handshake with SemVer alignment; schema serving for Context compilation.
AgentRegistry
Registry of active agents with SemVer handshake (DIR §2.3).
Storage backend is pluggable. Pass storage= for a custom backend, or
db_path= to use the built-in SQLite backend (default behaviour).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
Optional[str]
|
Path to SQLite database. Used when |
None
|
supported_versions
|
str
|
SemVer constraint for handshake (e.g. |
'1.x'
|
storage
|
Optional[AgentRegistryStorage]
|
Custom :class: |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When neither |
Source code in src/dir_core/agent_registry.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
get_agent_contract(agent_id)
Retrieve agent capability contract.
Source code in src/dir_core/agent_registry.py
162 163 164 165 | |
get_agent_manifest(agent_id)
Retrieve agent capability contract. Deprecated: use get_agent_contract.
Source code in src/dir_core/agent_registry.py
167 168 169 | |
get_agent_priority(agent_id)
Retrieve agent priority (default 0).
Source code in src/dir_core/agent_registry.py
171 172 173 174 | |
get_agent_status(agent_id)
Return (status, suspension_reason) if the agent exists, else None.
Source code in src/dir_core/agent_registry.py
207 208 209 | |
get_schema(agent_id, schema_kind=None)
Return schema from contract: schema_kind=None -> contract['schema']; else -> contract['schemas'][kind] or contract['schema'].
Source code in src/dir_core/agent_registry.py
124 125 126 127 128 129 130 131 132 133 134 | |
handshake(agent_id, contract, agent_version, priority=0)
Handshake with version check. REJECT on VERSION_MISMATCH. Returns ACCEPTED with session_token or REJECTED with reason.
Source code in src/dir_core/agent_registry.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
list_agents()
List all active agent IDs.
Source code in src/dir_core/agent_registry.py
176 177 178 | |
register_agent(agent_id, contract, priority=0)
Register or update an agent with capability contract.
Deprecated: Use handshake() for version checking and session tokens (DIR §2.3). register_agent does not enforce SemVer compatibility.
Source code in src/dir_core/agent_registry.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
set_agent_status(agent_id, status, suspension_reason=None)
Transition agent lifecycle status (e.g. ACTIVE -> SUSPENDED).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Registered agent identifier. |
required |
status
|
str
|
New status value (e.g. 'SUSPENDED', 'ACTIVE'). |
required |
suspension_reason
|
Optional[str]
|
Optional machine-oriented reason (audit / ops). |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if a row was updated. |
Source code in src/dir_core/agent_registry.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
HandshakeResult
dataclass
Result of agent handshake (DIR §2.3).
Source code in src/dir_core/agent_registry.py
25 26 27 28 29 30 31 | |
Context Store
dir_core.context_store
Context Store (DIR §8) - Manages multi-layered context for agents.
Layers: 1. Session (Ephemeral): Context specific to the current DecisionFlow (dfid). 2. State (Authoritative): Long-lived agent state (policy versions, trajectory). 3. Memory (Long-term): Vector DB or archival storage (Stub for MVP). 4. Artifacts (Reference): Static docs/rules (Stub).
Provides compile_working_context to assemble a frozen view for decision making.
Implementation note: Memory and Artifacts layers are stubs (return {}). Full implementation requires: Memory (vector DB / archival), Artifacts (RAG / static docs). See DIR §8.1, ROA §7.2 for layer definitions.
ContextStore
Multi-layered context store for agent state (DIR §8).
Storage backend is pluggable. Pass storage= for a custom backend, or
db_path= to use the built-in SQLite backend (default behaviour).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
Optional[str]
|
Path to SQLite database. Used when |
None
|
storage
|
Optional[ContextStorage]
|
Custom :class: |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When neither |
Source code in src/dir_core/context_store.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
compile_working_context(agent_id, dfid)
Assemble all layers into a single Working Context. Returns immutable dictionary (snapshot).
Source code in src/dir_core/context_store.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
update_session(dfid, updates)
Merge updates into existing session.
Source code in src/dir_core/context_store.py
66 67 68 69 70 | |
update_state(agent_id, updates)
Merge updates into existing state.
Source code in src/dir_core/context_store.py
80 81 82 83 84 | |
Idempotency
dir_core.idempotency
Idempotency Guard (DIR §7) - ensures operations are executed exactly once.
Key is formed from: - DFID (decision flow ID) - Step ID (unique within flow) - Canonical parameters (sorted JSON)
Backend can be in-memory (testing) or persistent (production). The built-in
backends are :class:MemoryBackend and :class:SQLiteBackend; custom backends
must satisfy the :class:IdempotencyBackend protocol.
IdempotencyBackend
Bases: Protocol
Protocol satisfied by all idempotency storage backends.
Source code in src/dir_core/idempotency.py
32 33 34 35 36 | |
IdempotencyGuard
Guard that checks cache before execution and records result after.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
IdempotencyBackend
|
Any object satisfying :class: |
required |
Source code in src/dir_core/idempotency.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
run(dfid, step_id, params, func)
Run func(params) with idempotency protection.
Source code in src/dir_core/idempotency.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
idempotency_key(dfid, step_id, params)
Compute deterministic key: SHA256(dfid|step_id|canonical_params).
Source code in src/dir_core/idempotency.py
25 26 27 28 29 | |
Escalation
dir_core.escalation
Escalation Manager (DIR §9).
Human-in-the-Loop: request escalation, budget (token bucket), resolve.
EscalationManager
Manages escalation requests, budget, and resolution (DIR §9).
Storage backend is pluggable. Pass storage= for a custom backend, or
db_path= to use the built-in SQLite backend (default behaviour).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
Optional[str]
|
Path to SQLite database. Used when |
None
|
max_escalations_per_hour
|
int
|
Token-bucket capacity per agent per window. |
3
|
refill_interval_sec
|
int
|
Window length in seconds (default 3600 = 1 hour). |
3600
|
storage
|
Optional[EscalationStorage]
|
Custom :class: |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When neither |
Source code in src/dir_core/escalation.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
get_pending()
Return list of pending escalation requests.
Source code in src/dir_core/escalation.py
176 177 178 | |
request_escalation(dfid, agent_id, reason, context, proposal, impact)
Request escalation. Returns GRANTED or BUDGET_EXHAUSTED. On BUDGET_EXHAUSTED, agent should be demoted to PASSIVE and flow aborted.
Source code in src/dir_core/escalation.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
request_from_model(escalation)
Request escalation from EscalationRequest model (DIR §5.3).
Maps EscalationRequest to request_escalation API. Converts severity (LOW/MEDIUM/HIGH/CRITICAL) to ImpactCategory (LOW_IMPACT/HIGH_IMPACT). If original_policy is Policy, converts to PolicyProposal for storage.
Source code in src/dir_core/escalation.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
resolve_escalation(dfid, decision, modified_proposal=None)
Record human decision: OVERRIDE, MODIFY, or ABORT.
Source code in src/dir_core/escalation.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
EscalationOutcome
Bases: StrEnum
Result of request_escalation.
Source code in src/dir_core/escalation.py
28 29 30 31 32 | |
ImpactCategory
Bases: StrEnum
Impact level for escalation (DIR §9.4).
Source code in src/dir_core/escalation.py
21 22 23 24 25 | |
Resource Lock
dir_core.resource_lock
Resource Locking / Semantic Locking (DIR §6.2, §2.3).
Reservation locks for shared resources (capital, API throughput). Linear Lock Acquisition (alphabetical order) to prevent deadlocks.
LockResult
Bases: StrEnum
Result of acquire attempt.
Source code in src/dir_core/resource_lock.py
19 20 21 22 23 24 | |
ResourceLockManager
Manages reservation locks for shared resources (DIR §6.2).
Responsibility split:
- Manager: checks domain availability (via
availability_provider), enforces alphabetical lock ordering to prevent deadlocks, retries on contention. - Storage backend: provides atomic batch-write of acquired locks and returns the currently locked amount per resource.
Storage backend is pluggable. Pass storage= for a custom backend, or
db_path= to use the built-in SQLite backend (default behaviour).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
Optional[str]
|
Path to SQLite database. Used when |
None
|
availability_provider
|
Optional[Callable[[str], float]]
|
|
None
|
storage
|
Optional[ResourceLockStorage]
|
Custom :class: |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When |
Source code in src/dir_core/resource_lock.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
acquire(dfid, resources, timeout_sec=5.0)
Acquire locks for all resources in sorted order (DIR Topologies §6.4).
Flow:
1. Check domain availability for each resource via
availability_provider. Return INSUFFICIENT_LIQUIDITY
immediately if any resource is over-allocated.
2. Ask the storage backend to write the locks atomically. The backend
may retry internally (SQLite uses BEGIN IMMEDIATE); if it cannot
obtain exclusive write access within timeout_sec, return
RESOURCE_CONTENTION_TIMEOUT.
Source code in src/dir_core/resource_lock.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
release(dfid)
Release all locks held by dfid.
Source code in src/dir_core/resource_lock.py
114 115 116 | |
JIT State Verifier
dir_core.jit
Just-In-Time (JIT) State Verification (DIR §6.5, Topologies §2.4, §3.2).
Fast-pass checks before execution: verify state has not drifted since snapshot. Does NOT re-evaluate reasoning — only compares snapshot vs live state.
JITStateVerifier
JIT State Verifier for Topology B (SDS).
Validates that the environment has not drifted since the agent's snapshot. Domain-specific logic (which keys, hard limits) is passed via callbacks.
Source code in src/dir_core/jit.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
verify(snapshot, live, keys_to_compare=None, tolerance=None)
Run drift verification.
Returns:
| Type | Description |
|---|---|
ValidationResult
|
("ACCEPT", reason) if no drift, else ("REJECT", reason). |
Source code in src/dir_core/jit.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
verify_drift(snapshot, live, keys_to_compare=None, tolerance=None)
Verify that live state has not drifted beyond tolerance since snapshot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snapshot
|
Dict[str, Any]
|
State at snapshot time (when agent reasoned). |
required |
live
|
Dict[str, Any]
|
Current live state. |
required |
keys_to_compare
|
Optional[List[str]]
|
Keys to check. If None, compare all keys in snapshot. |
None
|
tolerance
|
Optional[Dict[str, float]]
|
Optional dict of key -> max allowed delta for numeric values. If key not in tolerance, exact match is required. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[bool, str]
|
(True, "") if no drift, else (False, reason). |
Source code in src/dir_core/jit.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
Arbitration
dir_core.arbitration
Priority-Based Arbitration (DIR Topologies §2.4).
Selects the winning proposal from parallel agents using a priority matrix. Lower priority number = higher precedence (e.g. Risk > Strategy).
select_winner(proposals, priority_matrix=None)
Select winning proposal using Priority Matrix (Topologies §2.4).
Lower priority number = higher precedence. If no matrix provided, uses DEFAULT_PRIORITY_MATRIX. Unknown policy_kind gets priority 10.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
proposals
|
List[PolicyProposal]
|
List of policy proposals from parallel agents. |
required |
priority_matrix
|
Optional[Dict[str, int]]
|
Optional mapping policy_kind -> priority (lower = higher). |
None
|
Returns:
| Type | Description |
|---|---|
Optional[PolicyProposal]
|
The winning proposal, or None if proposals list is empty. |
Source code in src/dir_core/arbitration.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |