Persistence¶
Pluggable operational data persistence -- protocol, configuration, and SQLite backend.
Protocol¶
protocol
¶
PersistenceBackend protocol -- lifecycle + repository access.
Application code depends on this protocol for storage lifecycle management. Repository protocols provide entity-level access.
PersistenceBackend
¶
Bases: Protocol
Lifecycle management for operational data storage.
Concrete backends implement this protocol to provide connection management, health monitoring, schema migrations, and access to entity-specific repositories.
Attributes:
| Name | Type | Description |
|---|---|---|
is_connected |
bool
|
Whether the backend has an active connection. |
backend_name |
NotBlankStr
|
Human-readable backend identifier. |
tasks |
TaskRepository
|
Repository for Task persistence. |
cost_records |
CostRecordRepository
|
Repository for CostRecord persistence. |
messages |
MessageRepository
|
Repository for Message persistence. |
lifecycle_events |
LifecycleEventRepository
|
Repository for AgentLifecycleEvent persistence. |
task_metrics |
TaskMetricRepository
|
Repository for TaskMetricRecord persistence. |
collaboration_metrics |
CollaborationMetricRepository
|
Repository for CollaborationMetricRecord persistence. |
parked_contexts |
ParkedContextRepository
|
Repository for ParkedContext persistence. |
audit_entries |
AuditRepository
|
Repository for AuditEntry persistence. |
users |
UserRepository
|
Repository for User persistence. |
api_keys |
ApiKeyRepository
|
Repository for ApiKey persistence. |
checkpoints |
CheckpointRepository
|
Repository for Checkpoint persistence. |
heartbeats |
HeartbeatRepository
|
Repository for Heartbeat persistence. |
agent_states |
AgentStateRepository
|
Repository for AgentRuntimeState persistence. |
settings |
SettingsRepository
|
Repository for namespaced settings persistence. |
artifacts |
ArtifactRepository
|
Repository for Artifact persistence. |
projects |
ProjectRepository
|
Repository for Project persistence. |
custom_presets |
PersonalityPresetRepository
|
Repository for custom personality preset persistence. |
workflow_definitions |
WorkflowDefinitionRepository
|
Repository for workflow definition persistence. |
workflow_executions |
WorkflowExecutionRepository
|
Repository for workflow execution persistence. |
collaboration_metrics
property
¶
Repository for CollaborationMetricRecord persistence.
workflow_definitions
property
¶
Repository for workflow definition persistence.
connect
async
¶
Establish connection to the storage backend.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If the connection cannot be established. |
disconnect
async
¶
health_check
async
¶
Check whether the backend is healthy and responsive.
Returns:
| Type | Description |
|---|---|
bool
|
|
migrate
async
¶
get_db
¶
Return the underlying database connection.
Returns:
| Type | Description |
|---|---|
Any
|
The raw database connection object (backend-specific). |
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not yet connected. |
get_setting
async
¶
Retrieve a setting value by key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
NotBlankStr
|
Setting key. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The setting value, or |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/protocol.py
set_setting
async
¶
Store a setting value.
Upserts -- creates or updates the key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
NotBlankStr
|
Setting key. |
required |
value
|
str
|
Setting value. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/protocol.py
Config¶
config
¶
Persistence configuration models.
Frozen Pydantic models for persistence backend selection and backend-specific settings.
SQLiteConfig
pydantic-model
¶
Bases: BaseModel
SQLite-specific persistence configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
NotBlankStr
|
Database file path. Use |
wal_mode |
bool
|
Whether to enable WAL journal mode for concurrent read performance. |
journal_size_limit |
int
|
Maximum WAL journal size in bytes (default 64 MB). |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
path(NotBlankStr) -
wal_mode(bool) -
journal_size_limit(int)
Validators:
-
_reject_traversal
PersistenceConfig
pydantic-model
¶
Bases: BaseModel
Top-level persistence configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
backend |
NotBlankStr
|
Backend name -- currently only |
sqlite |
SQLiteConfig
|
SQLite-specific settings (used when
|
Config:
frozen:Trueallow_inf_nan:False
Fields:
Validators:
-
_validate_backend_name
Repositories¶
repositories
¶
Repository protocols for operational data persistence.
Each entity type has its own protocol so that application code depends only on abstract interfaces, never on a concrete backend.
CollaborationMetricRepository
¶
Bases: Protocol
Append-only persistence + query for CollaborationMetricRecord.
save
async
¶
Persist a collaboration metric record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
CollaborationMetricRecord
|
The collaboration metric record to persist. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/hr/persistence_protocol.py
query
async
¶
Query collaboration metric records with optional filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
NotBlankStr | None
|
Filter by agent identifier. |
None
|
since
|
AwareDatetime | None
|
Include records after this time. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[CollaborationMetricRecord, ...]
|
Matching collaboration metric records. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/hr/persistence_protocol.py
LifecycleEventRepository
¶
Bases: Protocol
CRUD + query interface for AgentLifecycleEvent persistence.
save
async
¶
Persist a lifecycle event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
AgentLifecycleEvent
|
The lifecycle event to persist. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
list_events
async
¶
List lifecycle events with optional filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
NotBlankStr | None
|
Filter by agent identifier. |
None
|
event_type
|
LifecycleEventType | None
|
Filter by event type. |
None
|
since
|
AwareDatetime | None
|
Filter events after this timestamp. |
None
|
limit
|
int | None
|
Maximum number of events to return. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[AgentLifecycleEvent, ...]
|
Matching lifecycle events. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/hr/persistence_protocol.py
TaskMetricRepository
¶
Bases: Protocol
Append-only persistence + query for TaskMetricRecord.
save
async
¶
Persist a task metric record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
TaskMetricRecord
|
The task metric record to persist. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
query
async
¶
Query task metric records with optional filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
NotBlankStr | None
|
Filter by agent identifier. |
None
|
since
|
AwareDatetime | None
|
Include records after this time. |
None
|
until
|
AwareDatetime | None
|
Include records before this time. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[TaskMetricRecord, ...]
|
Matching task metric records. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/hr/persistence_protocol.py
TaskRepository
¶
Bases: Protocol
CRUD + query interface for Task persistence.
save
async
¶
Persist a task (insert or update).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
Task
|
The task to persist. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
get
async
¶
Retrieve a task by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
NotBlankStr
|
The task identifier. |
required |
Returns:
| Type | Description |
|---|---|
Task | None
|
The task, or |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
list_tasks
async
¶
List tasks with optional filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
TaskStatus | None
|
Filter by task status. |
None
|
assigned_to
|
NotBlankStr | None
|
Filter by assignee agent ID. |
None
|
project
|
NotBlankStr | None
|
Filter by project ID. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Task, ...]
|
Matching tasks as a tuple. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
delete
async
¶
Delete a task by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
NotBlankStr
|
The task identifier. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
CostRecordRepository
¶
Bases: Protocol
Append-only persistence + query/aggregation for CostRecord.
save
async
¶
Persist a cost record (append-only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
CostRecord
|
The cost record to persist. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
query
async
¶
Query cost records with optional filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
NotBlankStr | None
|
Filter by agent identifier. |
None
|
task_id
|
NotBlankStr | None
|
Filter by task identifier. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[CostRecord, ...]
|
Matching cost records as a tuple. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
aggregate
async
¶
Sum total cost_usd, optionally filtered by agent and/or task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
NotBlankStr | None
|
Filter by agent identifier. |
None
|
task_id
|
NotBlankStr | None
|
Filter by task identifier. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Total cost in USD (base currency). |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
MessageRepository
¶
Bases: Protocol
Write + history query interface for Message persistence.
save
async
¶
Persist a message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Message
|
The message to persist. |
required |
Raises:
| Type | Description |
|---|---|
DuplicateRecordError
|
If a message with the same ID exists. |
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
get_history
async
¶
Retrieve message history for a channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel
|
NotBlankStr
|
Channel name to query. |
required |
limit
|
int | None
|
Maximum number of messages to return (newest first). |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Message, ...]
|
Messages ordered by timestamp descending. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
ParkedContextRepository
¶
Bases: Protocol
CRUD interface for parked agent execution contexts.
save
async
¶
Persist a parked context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
ParkedContext
|
The parked context to persist. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
get
async
¶
Retrieve a parked context by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parked_id
|
NotBlankStr
|
The parked context identifier. |
required |
Returns:
| Type | Description |
|---|---|
ParkedContext | None
|
The parked context, or |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
get_by_approval
async
¶
Retrieve a parked context by approval ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
approval_id
|
NotBlankStr
|
The approval item identifier. |
required |
Returns:
| Type | Description |
|---|---|
ParkedContext | None
|
The parked context, or |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
get_by_agent
async
¶
Retrieve all parked contexts for an agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
NotBlankStr
|
The agent identifier. |
required |
Returns:
| Type | Description |
|---|---|
tuple[ParkedContext, ...]
|
Parked contexts for the agent. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
delete
async
¶
Delete a parked context by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parked_id
|
NotBlankStr
|
The parked context identifier. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
AuditRepository
¶
Bases: Protocol
Append-only persistence + query interface for AuditEntry.
Audit entries are immutable records of security evaluations. No update or delete operations are provided to preserve audit integrity.
save
async
¶
Persist an audit entry (append-only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entry
|
AuditEntry
|
The audit entry to persist. |
required |
Raises:
| Type | Description |
|---|---|
DuplicateRecordError
|
If an entry with the same ID exists. |
QueryError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
query
async
¶
query(
*,
agent_id=None,
action_type=None,
verdict=None,
risk_level=None,
since=None,
until=None,
limit=100,
)
Query audit entries with optional filters.
Filters are AND-combined. Results are ordered by timestamp descending (newest first).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
NotBlankStr | None
|
Filter by agent identifier. |
None
|
action_type
|
str | None
|
Filter by action type string. |
None
|
verdict
|
AuditVerdictStr | None
|
Filter by verdict string. |
None
|
risk_level
|
ApprovalRiskLevel | None
|
Filter by risk level. |
None
|
since
|
AwareDatetime | None
|
Only return entries at or after this timestamp. |
None
|
until
|
AwareDatetime | None
|
Only return entries at or before this timestamp. |
None
|
limit
|
int
|
Maximum number of entries to return (must be >= 1). |
100
|
Returns:
| Type | Description |
|---|---|
tuple[AuditEntry, ...]
|
Matching audit entries as a tuple. |
Raises:
| Type | Description |
|---|---|
QueryError
|
If the operation fails, limit < 1, or until is earlier than since. |
Source code in src/synthorg/persistence/repositories.py
UserRepository
¶
Bases: Protocol
CRUD interface for User persistence.
save
async
¶
Persist a user (insert or update).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user
|
User
|
The user to persist. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
get
async
¶
Retrieve a user by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
NotBlankStr
|
The user identifier. |
required |
Returns:
| Type | Description |
|---|---|
User | None
|
The user, or |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
get_by_username
async
¶
Retrieve a user by username.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
NotBlankStr
|
The login username. |
required |
Returns:
| Type | Description |
|---|---|
User | None
|
The user, or |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
list_users
async
¶
List all human users (excludes the system user).
Returns:
| Type | Description |
|---|---|
tuple[User, ...]
|
Human users as a tuple. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
count
async
¶
Count the number of human users (excludes the system user).
Returns:
| Type | Description |
|---|---|
int
|
Human user count. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
count_by_role
async
¶
Count users with a specific role.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role
|
HumanRole
|
The role to filter by. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of users with the given role. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
delete
async
¶
Delete a user by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
NotBlankStr
|
The user identifier. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
ApiKeyRepository
¶
Bases: Protocol
CRUD interface for API key persistence.
save
async
¶
Persist an API key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
ApiKey
|
The API key to persist. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
get
async
¶
Retrieve an API key by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_id
|
NotBlankStr
|
The key identifier. |
required |
Returns:
| Type | Description |
|---|---|
ApiKey | None
|
The API key, or |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
get_by_hash
async
¶
Retrieve an API key by its hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_hash
|
NotBlankStr
|
HMAC-SHA256 hex digest. |
required |
Returns:
| Type | Description |
|---|---|
ApiKey | None
|
The API key, or |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
list_by_user
async
¶
List API keys belonging to a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
NotBlankStr
|
The owner user ID. |
required |
Returns:
| Type | Description |
|---|---|
tuple[ApiKey, ...]
|
API keys for the user. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
delete
async
¶
Delete an API key by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_id
|
NotBlankStr
|
The key identifier. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
CheckpointRepository
¶
Bases: Protocol
CRUD interface for checkpoint persistence.
save
async
¶
Persist a checkpoint (insert or replace by ID).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint
|
Checkpoint
|
The checkpoint to persist. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
get_latest
async
¶
Retrieve the latest checkpoint by turn_number.
At least one filter (execution_id or task_id) is required.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
execution_id
|
NotBlankStr | None
|
Filter by execution identifier. |
None
|
task_id
|
NotBlankStr | None
|
Filter by task identifier. |
None
|
Returns:
| Type | Description |
|---|---|
Checkpoint | None
|
The checkpoint with the highest turn_number, or |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
ValueError
|
If neither filter is provided. |
Source code in src/synthorg/persistence/repositories.py
delete_by_execution
async
¶
Delete all checkpoints for an execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
execution_id
|
NotBlankStr
|
The execution identifier. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of checkpoints deleted. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
HeartbeatRepository
¶
Bases: Protocol
CRUD interface for heartbeat persistence.
save
async
¶
Persist a heartbeat (upsert by execution_id).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
heartbeat
|
Heartbeat
|
The heartbeat to persist. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
get
async
¶
Retrieve a heartbeat by execution ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
execution_id
|
NotBlankStr
|
The execution identifier. |
required |
Returns:
| Type | Description |
|---|---|
Heartbeat | None
|
The heartbeat, or |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
get_stale
async
¶
Retrieve heartbeats older than the threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
AwareDatetime
|
Heartbeats with |
required |
Returns:
| Type | Description |
|---|---|
tuple[Heartbeat, ...]
|
Stale heartbeats as a tuple. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
delete
async
¶
Delete a heartbeat by execution ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
execution_id
|
NotBlankStr
|
The execution identifier. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
AgentStateRepository
¶
Bases: Protocol
CRUD + query interface for agent runtime state persistence.
Provides a lightweight per-agent registry of execution state for dashboard queries, graceful shutdown discovery, and cross-restart recovery.
save
async
¶
Upsert an agent runtime state by agent_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
AgentRuntimeState
|
The agent runtime state to persist. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
get
async
¶
Retrieve an agent runtime state by agent ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
NotBlankStr
|
The agent identifier. |
required |
Returns:
| Type | Description |
|---|---|
AgentRuntimeState | None
|
The agent state, or |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
get_active
async
¶
Retrieve all non-idle agent states.
Returns states where status != 'idle', ordered by
last_activity_at descending (most recent first).
Returns:
| Type | Description |
|---|---|
tuple[AgentRuntimeState, ...]
|
Active agent states as a tuple. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
delete
async
¶
Delete an agent runtime state by agent ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
NotBlankStr
|
The agent identifier. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
SettingsRepository
¶
Bases: Protocol
CRUD interface for namespaced settings persistence.
Settings are stored as (namespace, key) composite-keyed
string values with updated_at timestamps.
get
async
¶
Retrieve a setting value and its timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
NotBlankStr
|
Setting namespace. |
required |
key
|
NotBlankStr
|
Setting key within the namespace. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, str] | None
|
|
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
get_namespace
async
¶
Retrieve all settings in a namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
NotBlankStr
|
Setting namespace. |
required |
Returns:
| Type | Description |
|---|---|
tuple[tuple[str, str, str], ...]
|
Tuple of |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
get_all
async
¶
Retrieve all settings across all namespaces.
Returns:
| Type | Description |
|---|---|
tuple[str, str, str, str]
|
Tuple of |
...
|
sorted by namespace then key. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
set
async
¶
Upsert a setting value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
NotBlankStr
|
Setting namespace. |
required |
key
|
NotBlankStr
|
Setting key within the namespace. |
required |
value
|
str
|
Setting value as a string. |
required |
updated_at
|
str
|
ISO 8601 timestamp of the change. |
required |
expected_updated_at
|
str | None
|
When provided, the row is only
updated if the current |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
delete
async
¶
Delete a setting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
NotBlankStr
|
Setting namespace. |
required |
key
|
NotBlankStr
|
Setting key within the namespace. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
delete_namespace
async
¶
Delete all settings in a namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
NotBlankStr
|
Setting namespace. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of settings deleted. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/repositories.py
ArtifactRepository
¶
Bases: Protocol
CRUD + query interface for Artifact persistence.
save
async
¶
Persist an artifact (insert or update).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artifact
|
Artifact
|
The artifact to persist. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
get
async
¶
Retrieve an artifact by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artifact_id
|
NotBlankStr
|
The artifact identifier. |
required |
Returns:
| Type | Description |
|---|---|
Artifact | None
|
The artifact, or |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/artifact_project_repos.py
list_artifacts
async
¶
List artifacts with optional filters.
Results are ordered by artifact ID ascending to ensure deterministic pagination across backends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
NotBlankStr | None
|
Filter by originating task ID. |
None
|
created_by
|
NotBlankStr | None
|
Filter by creator agent ID. |
None
|
artifact_type
|
ArtifactType | None
|
Filter by artifact type. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Artifact, ...]
|
Matching artifacts ordered by ID, as a tuple. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/artifact_project_repos.py
delete
async
¶
Delete an artifact by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artifact_id
|
NotBlankStr
|
The artifact identifier. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/artifact_project_repos.py
ProjectRepository
¶
Bases: Protocol
CRUD + query interface for Project persistence.
save
async
¶
Persist a project (insert or update).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project
|
Project
|
The project to persist. |
required |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
get
async
¶
Retrieve a project by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_id
|
NotBlankStr
|
The project identifier. |
required |
Returns:
| Type | Description |
|---|---|
Project | None
|
The project, or |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/artifact_project_repos.py
list_projects
async
¶
List projects with optional filters.
Results are ordered by project ID ascending to ensure deterministic pagination across backends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
ProjectStatus | None
|
Filter by project status. |
None
|
lead
|
NotBlankStr | None
|
Filter by project lead agent ID. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Project, ...]
|
Matching projects ordered by ID, as a tuple. |
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/artifact_project_repos.py
delete
async
¶
Delete a project by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_id
|
NotBlankStr
|
The project identifier. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
PersistenceError
|
If the operation fails. |
Source code in src/synthorg/persistence/artifact_project_repos.py
Factory¶
factory
¶
Factory for creating persistence backends from configuration.
Each company gets its own PersistenceBackend instance, which maps
to its own database. This enables multi-tenancy: one database per
company, selectable via the PersistenceConfig embedded in each
company's RootConfig.
create_backend
¶
Create a persistence backend from configuration.
Factory function that maps config.backend to the correct
concrete backend class. Each call returns a new, disconnected
backend instance -- the caller is responsible for calling
connect() and migrate().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
PersistenceConfig
|
Persistence configuration (includes backend selection and backend-specific settings). |
required |
Returns:
| Type | Description |
|---|---|
PersistenceBackend
|
A new, disconnected backend instance. |
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If the backend name is not recognized. |
Example::
config = PersistenceConfig(
backend="sqlite",
sqlite=SQLiteConfig(path="data/company-a.db"),
)
backend = create_backend(config)
await backend.connect()
await backend.migrate()
Source code in src/synthorg/persistence/factory.py
Errors¶
errors
¶
Persistence error hierarchy.
All persistence-related errors inherit from PersistenceError so
callers can catch the entire family with a single except clause.
PersistenceError
¶
Bases: Exception
Base exception for all persistence operations.
PersistenceConnectionError
¶
Bases: PersistenceError
Raised when a backend connection cannot be established or is lost.
MigrationError
¶
Bases: PersistenceError
Raised when a database migration fails.
RecordNotFoundError
¶
Bases: PersistenceError
Raised when a requested record does not exist.
Used by ArtifactStorageBackend.retrieve() when no content
exists for the given artifact ID. Repository get() methods
return None on miss instead of raising.
DuplicateRecordError
¶
Bases: PersistenceError
Raised when inserting a record that already exists.
QueryError
¶
Bases: PersistenceError
Raised when a query fails due to invalid parameters or backend issues.
VersionConflictError
¶
Bases: QueryError
Raised when an optimistic concurrency version check fails.
ArtifactTooLargeError
¶
Bases: PersistenceError
Raised when a single artifact exceeds the maximum allowed size.
ArtifactStorageFullError
¶
Bases: PersistenceError
Raised when total artifact storage exceeds capacity.
SQLite Backend¶
backend
¶
SQLite persistence backend implementation.
SQLitePersistenceBackend
¶
SQLite implementation of the PersistenceBackend protocol.
Uses a single aiosqlite.Connection with WAL mode enabled by
default for file-based databases (in-memory databases do not
support WAL). Configurable via SQLiteConfig.wal_mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
SQLiteConfig
|
SQLite-specific configuration. |
required |
Source code in src/synthorg/persistence/sqlite/backend.py
tasks
property
¶
Repository for Task persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
cost_records
property
¶
Repository for CostRecord persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
messages
property
¶
Repository for Message persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
lifecycle_events
property
¶
Repository for AgentLifecycleEvent persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
task_metrics
property
¶
Repository for TaskMetricRecord persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
collaboration_metrics
property
¶
Repository for CollaborationMetricRecord persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
parked_contexts
property
¶
Repository for ParkedContext persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
audit_entries
property
¶
Repository for AuditEntry persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
users
property
¶
Repository for User persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
api_keys
property
¶
Repository for ApiKey persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
checkpoints
property
¶
Repository for Checkpoint persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
heartbeats
property
¶
Repository for Heartbeat persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
agent_states
property
¶
Repository for AgentRuntimeState persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
settings
property
¶
Repository for namespaced settings persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
artifacts
property
¶
Repository for Artifact persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
projects
property
¶
Repository for Project persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
custom_presets
property
¶
Repository for custom personality preset persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
workflow_definitions
property
¶
Repository for workflow definition persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
workflow_executions
property
¶
Repository for workflow execution persistence.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
connect
async
¶
Open the SQLite database and configure WAL mode.
Source code in src/synthorg/persistence/sqlite/backend.py
get_db
¶
Return the shared database connection.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not yet connected. |
Source code in src/synthorg/persistence/sqlite/backend.py
disconnect
async
¶
Close the database connection.
Source code in src/synthorg/persistence/sqlite/backend.py
health_check
async
¶
Check database connectivity.
Source code in src/synthorg/persistence/sqlite/backend.py
migrate
async
¶
Apply the database schema.
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
MigrationError
|
If schema application fails. |
Source code in src/synthorg/persistence/sqlite/backend.py
get_setting
async
¶
Retrieve a setting value by key from the _system namespace.
Delegates to self.settings (the SettingsRepository).
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
Source code in src/synthorg/persistence/sqlite/backend.py
set_setting
async
¶
Store a setting value (upsert) in the _system namespace.
Delegates to self.settings (the SettingsRepository).
Raises:
| Type | Description |
|---|---|
PersistenceConnectionError
|
If not connected. |
Source code in src/synthorg/persistence/sqlite/backend.py
repositories
¶
SQLite repository implementations for Task, CostRecord, and Message.
HR-related repositories (LifecycleEvent, TaskMetric, CollaborationMetric)
are in hr_repositories.py within this package.
SQLiteTaskRepository
¶
SQLite implementation of the TaskRepository protocol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Connection
|
An open aiosqlite connection. |
required |
Source code in src/synthorg/persistence/sqlite/repositories.py
save
async
¶
Persist a task (upsert semantics).
Source code in src/synthorg/persistence/sqlite/repositories.py
get
async
¶
Retrieve a task by its ID.
Source code in src/synthorg/persistence/sqlite/repositories.py
list_tasks
async
¶
List tasks with optional filters.
Source code in src/synthorg/persistence/sqlite/repositories.py
delete
async
¶
Delete a task by ID.
Source code in src/synthorg/persistence/sqlite/repositories.py
SQLiteCostRecordRepository
¶
SQLite implementation of the CostRecordRepository protocol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Connection
|
An open aiosqlite connection. |
required |
Source code in src/synthorg/persistence/sqlite/repositories.py
save
async
¶
Persist a cost record (append-only).
Source code in src/synthorg/persistence/sqlite/repositories.py
query
async
¶
Query cost records with optional filters.
Source code in src/synthorg/persistence/sqlite/repositories.py
aggregate
async
¶
Sum total cost_usd, optionally filtered by agent and/or task.
Source code in src/synthorg/persistence/sqlite/repositories.py
SQLiteMessageRepository
¶
SQLite implementation of the MessageRepository protocol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Connection
|
An open aiosqlite connection. |
required |
Source code in src/synthorg/persistence/sqlite/repositories.py
save
async
¶
Persist a message.
Source code in src/synthorg/persistence/sqlite/repositories.py
get_history
async
¶
Retrieve message history for a channel, newest first.