Storage Layer
dir_core.storage
dir_core.storage — pluggable persistence layer for DIR modules.
Quick start::
from dir_core.storage import sqlite_storage, memory_storage
# All modules backed by a single SQLite file
stores = sqlite_storage("data/my_app.db")
registry = AgentRegistry(storage=stores.agent_registry)
context = ContextStore(storage=stores.context)
# Fully in-memory (great for unit tests)
stores = memory_storage()
Custom backend example::
from dir_core.storage.base import AgentRegistryStorage
class MyPostgresAgentStorage:
def init_schema(self) -> None: ...
def upsert_agent(self, agent_id, contract_json, priority,
status, agent_version, session_token) -> None: ...
# ... implement all methods of AgentRegistryStorage
registry = AgentRegistry(storage=MyPostgresAgentStorage())
AgentRegistryStorage
Bases: Protocol
Source code in src/dir_core/storage/base.py
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 | |
get_agent(agent_id)
Return a dict with keys agent_id/contract/priority/status/ agent_version/session_token, or None if not found.
Source code in src/dir_core/storage/base.py
59 60 61 62 | |
get_status(agent_id)
Return (status, suspension_reason) or None if agent does not exist.
Source code in src/dir_core/storage/base.py
70 71 72 | |
init_schema()
Create or migrate the underlying schema (called once on construction).
Source code in src/dir_core/storage/base.py
43 44 45 | |
list_active_agents()
Return agent_ids where status == 'ACTIVE'.
Source code in src/dir_core/storage/base.py
74 75 76 | |
update_status(agent_id, status, suspension_reason)
Update status and suspension_reason. Return True if a row was changed.
Source code in src/dir_core/storage/base.py
64 65 66 67 68 | |
upsert_agent(agent_id, contract_json, priority, status, agent_version, session_token)
Insert or replace an agent record.
Source code in src/dir_core/storage/base.py
47 48 49 50 51 52 53 54 55 56 57 | |
AuditStore
Repository helper: append-only audit rows plus idempotent replay (DIR §7).
Combines :class:DecisionAuditStorage (decision_audit_events) and
:class:IdempotencyStorage (idempotency_cache). Construct from a
:class:~dir_core.storage.StorageBundle as
AuditStore(bundle.decision_audit, bundle.idempotency).
Source code in src/dir_core/storage/base.py
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 210 211 212 213 | |
close()
No-op when backends use short-lived connections per call.
Source code in src/dir_core/storage/base.py
182 183 184 | |
ContextStorage
Bases: Protocol
Source code in src/dir_core/storage/base.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
get_session(dfid)
Return JSON-encoded session data for dfid, or None.
Source code in src/dir_core/storage/base.py
88 89 90 | |
get_state(agent_id)
Return JSON-encoded state for agent_id, or None.
Source code in src/dir_core/storage/base.py
96 97 98 | |
set_session(dfid, data_json)
Persist JSON-encoded session data for dfid.
Source code in src/dir_core/storage/base.py
92 93 94 | |
set_state(agent_id, data_json)
Persist JSON-encoded state for agent_id.
Source code in src/dir_core/storage/base.py
100 101 102 | |
DecisionAuditStorage
Bases: Protocol
Source code in src/dir_core/storage/base.py
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 | |
all_events_chronological()
Return all events in insertion order.
Source code in src/dir_core/storage/base.py
158 159 160 | |
events_for_dfid(dfid)
Return events for dfid in insertion order (details = dict per row).
Source code in src/dir_core/storage/base.py
154 155 156 | |
record(dfid, event, *, step_id='', state='', details=None)
Append one DFID-scoped audit row.
details is persisted as JSON. Implementations MUST use the same
encoding as :func:~dir_core.storage.json_util.dumps_json_dict
(sort_keys=True, default=str) so SQLite, PostgreSQL, and any
other backend produce comparable detail_json / detail_json-text
column values and do not fail on non-JSON-native values.
Source code in src/dir_core/storage/base.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
EscalationStorage
Bases: Protocol
Source code in src/dir_core/storage/base.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | |
get_pending_requests()
Return list of pending requests as dicts with dfid/agent_id/reason/context/proposal/impact keys.
Source code in src/dir_core/storage/base.py
352 353 354 355 | |
get_window_count(agent_id, since_str)
Count budget tokens for agent_id after since_str (ISO/SQLite timestamp).
Source code in src/dir_core/storage/base.py
322 323 324 | |
insert_request(dfid, agent_id, reason, context_json, proposal_json, impact)
Persist a new escalation request with PENDING status.
Source code in src/dir_core/storage/base.py
330 331 332 333 334 335 336 337 338 339 340 | |
record_budget_token(agent_id)
Record one escalation token consumption for agent_id.
Source code in src/dir_core/storage/base.py
326 327 328 | |
resolve_request(dfid, resolved_at, decision, proposal_json)
Mark escalation as RESOLVED with human decision.
Source code in src/dir_core/storage/base.py
342 343 344 345 346 347 348 349 350 | |
IdempotencyStorage
Bases: Protocol
Source code in src/dir_core/storage/base.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
get(key)
Return cached result for key, or None on cache miss.
Source code in src/dir_core/storage/base.py
112 113 114 | |
set(key, result)
Store result under key.
Implementations SHOULD persist JSON with the same encoding rules as
:func:~dir_core.storage.json_util.dumps_json_dict (stable key order,
default=str) so disk backends stay interchangeable.
Source code in src/dir_core/storage/base.py
116 117 118 119 120 121 122 123 | |
IntentRetryStorage
Bases: Protocol
Source code in src/dir_core/storage/base.py
298 299 300 301 302 303 304 305 306 307 308 309 310 | |
delete(dfid)
Remove dfid record (called when flow reaches terminal state).
Source code in src/dir_core/storage/base.py
308 309 310 | |
get_count(dfid)
Return current rejection count for dfid (0 if unseen).
Source code in src/dir_core/storage/base.py
300 301 302 | |
set_count(dfid, count)
Persist rejection count for dfid.
Source code in src/dir_core/storage/base.py
304 305 306 | |
LifecycleStorage
Bases: Protocol
Source code in src/dir_core/storage/base.py
363 364 365 366 367 368 369 | |
record_transition(dfid, from_status, to_status)
Append a flow transition record.
Source code in src/dir_core/storage/base.py
365 366 367 368 369 | |
MemoryAgentRegistryStorage
In-memory backend for AgentRegistry.
Source code in src/dir_core/storage/memory.py
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 66 67 68 69 70 71 72 73 74 75 | |
MemoryContextStorage
In-memory backend for ContextStore.
Source code in src/dir_core/storage/memory.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
MemoryDecisionAuditStorage
In-memory backend for DFID-scoped decision audit rows.
Source code in src/dir_core/storage/memory.py
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 | |
MemoryEscalationStorage
In-memory backend for EscalationManager.
Source code in src/dir_core/storage/memory.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
MemoryIdempotencyStorage
In-memory backend for IdempotencyGuard.
Source code in src/dir_core/storage/memory.py
111 112 113 114 115 116 117 118 119 120 121 | |
MemoryIntentRetryStorage
In-memory backend for IntentRetryGovernor.
Source code in src/dir_core/storage/memory.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
MemoryLifecycleStorage
In-memory backend for lifecycle.transition.
Source code in src/dir_core/storage/memory.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
get_transitions(dfid=None)
Helper: return all transitions, optionally filtered by dfid.
Source code in src/dir_core/storage/memory.py
361 362 363 364 365 | |
MemoryResourceLockStorage
In-memory backend for ResourceLockManager.
Thread-safe via a reentrant lock.
Source code in src/dir_core/storage/memory.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
get_locked_amount(resource_id, exclude_dfid)
Return total locked amount for resource_id (excluding exclude_dfid).
Source code in src/dir_core/storage/memory.py
214 215 216 217 218 219 220 221 | |
MemorySagaStorage
In-memory backend for SagaCompensation.
Source code in src/dir_core/storage/memory.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
ResourceContentionError
Bases: StorageError
Raised when an exclusive lock cannot be acquired within the timeout.
Source code in src/dir_core/storage/base.py
32 33 | |
ResourceLockStorage
Bases: Protocol
Source code in src/dir_core/storage/base.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
acquire_batch(dfid, resources, timeout_sec)
Atomically write all locks for dfid.
The availability check is performed by :class:ResourceLockManager
before calling this method. Implementations must ensure the write
is atomic so that two concurrent callers cannot both see "enough room"
and both succeed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dfid
|
str
|
Flow identifier claiming the locks. |
required |
resources
|
Dict[str, float]
|
Mapping of resource_id -> requested amount. |
required |
timeout_sec
|
float
|
Maximum time to wait for exclusive write access. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
could not be obtained within timeout_sec |
bool
|
( |
Note
Implementations that guarantee atomic check-and-set (e.g. a
Postgres INSERT ... WHERE available - locked >= requested)
may perform the availability check themselves and raise
:exc:InsufficientCapacityError when capacity is exceeded.
Source code in src/dir_core/storage/base.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
get_locked_amount(resource_id, exclude_dfid)
Return the total reserved amount for resource_id, excluding any lock already held by exclude_dfid (so re-acquiring is idempotent).
Source code in src/dir_core/storage/base.py
251 252 253 254 255 | |
release(dfid)
Release all locks held by dfid.
Source code in src/dir_core/storage/base.py
288 289 290 | |
SagaStorage
Bases: Protocol
Source code in src/dir_core/storage/base.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
clear_dirty(dfid)
Remove dfid from dirty state after successful compensation.
Source code in src/dir_core/storage/base.py
237 238 239 | |
get_dirty_flows()
Return all dfids currently in dirty state.
Source code in src/dir_core/storage/base.py
229 230 231 | |
get_dirty_state(dfid)
Return dict with 'failed_step' and 'partial_state' keys, or None.
Source code in src/dir_core/storage/base.py
233 234 235 | |
mark_dirty(dfid, failed_step, partial_state_json)
Record dfid as PARTIAL_SUCCESS_DIRTY with the given state snapshot.
Source code in src/dir_core/storage/base.py
225 226 227 | |
SqliteAgentRegistryStorage
SQLite backend for AgentRegistry (DIR §2.3).
Source code in src/dir_core/storage/sqlite.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 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 | |
SqliteContextStorage
SQLite backend for ContextStore (DIR §8).
Source code in src/dir_core/storage/sqlite.py
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 210 211 212 213 214 215 216 | |
SqliteDecisionAuditStorage
SQLite backend for append-only decision_audit_events.
Source code in src/dir_core/storage/sqlite.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
SqliteEscalationStorage
SQLite backend for EscalationManager (DIR §9).
Source code in src/dir_core/storage/sqlite.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 | |
SqliteIdempotencyStorage
SQLite backend for IdempotencyGuard (DIR §7).
Source code in src/dir_core/storage/sqlite.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
SqliteIntentRetryStorage
SQLite backend for IntentRetryGovernor (DIR §6.2).
Source code in src/dir_core/storage/sqlite.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | |
SqliteLifecycleStorage
SQLite backend for lifecycle.transition (DIR §4.3).
Source code in src/dir_core/storage/sqlite.py
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 | |
SqliteResourceLockStorage
SQLite backend for ResourceLockManager (DIR §6.2).
acquire_batch uses BEGIN IMMEDIATE to guarantee that the
check-and-insert performed by :class:ResourceLockManager is not
interleaved with another concurrent acquire_batch.
Source code in src/dir_core/storage/sqlite.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
acquire_batch(dfid, resources, timeout_sec)
Atomically write all locks using BEGIN IMMEDIATE.
Returns True if written, False if contention persisted beyond timeout.
Source code in src/dir_core/storage/sqlite.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | |
get_locked_amount(resource_id, exclude_dfid)
Return total locked amount for resource_id (excluding exclude_dfid).
Source code in src/dir_core/storage/sqlite.py
422 423 424 425 426 427 428 429 430 431 | |
SqliteSagaStorage
SQLite backend for SagaCompensation (DIR §7).
Source code in src/dir_core/storage/sqlite.py
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
StorageBundle
dataclass
Holds one storage backend instance per dir_core module.
Use :func:sqlite_storage or :func:memory_storage to obtain a bundle,
or construct one manually to mix backends.
Source code in src/dir_core/storage/__init__.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
StorageError
Bases: Exception
Base exception raised by storage backends.
Source code in src/dir_core/storage/base.py
28 29 | |
dumps_json_dict(data, *, sort_keys=True)
Serialize a dict for TEXT / JSONB columns.
Uses stable key order and default=str so values that are not native
JSON types (e.g. datetime, UUID) encode consistently in
:class:~dir_core.storage.sqlite.SqliteDecisionAuditStorage and in the
sample PostgreSQL repository (samples/shared/storage/pg_repo.py).
Source code in src/dir_core/storage/json_util.py
14 15 16 17 18 19 20 21 22 | |
ensure_db(path, create_tables=None)
Create parent dirs and an empty DB file if needed, then run optional schema callback.
Used by samples that need SQLite before wiring storage backends. Returns
the resolved :class:~pathlib.Path to the database file.
Source code in src/dir_core/storage/sqlite.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
memory_storage()
Return a :class:StorageBundle backed entirely in process memory.
No data survives process exit. Useful for tests and ephemeral pipelines.
Source code in src/dir_core/storage/__init__.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
sqlite_storage(db_path)
Return a :class:StorageBundle where every module is backed by one SQLite file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
str
|
Path to the SQLite database file (created if absent). |
required |
Source code in src/dir_core/storage/__init__.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |