Guardrails¶
deep_db_agents.guardrails ¶
Hard guardrails enforced by the tool wrapper (not by the agent).
The defense hierarchy encoded here: limit and paginate, explore before extracting, estimate cost via EXPLAIN, allow only whitelisted operations, enforce a per-session row budget.
GuardrailConfig
dataclass
¶
GuardrailConfig(default_rows: int = 100, hard_max_rows: int = 1000, explain_row_threshold: int = 1000000, query_timeout_s: int = 30, allowed_statements: frozenset[str] = frozenset({'SELECT'}), row_budget: int | None = 10000000, max_materialized_bytes: int = 10485760)
Safety thresholds that cannot be bypassed by the agent.
Attributes:
| Name | Type | Description |
|---|---|---|
default_rows |
int
|
LIMIT applied automatically when the agent does not specify one. |
hard_max_rows |
int
|
Hard cap on rows per single query, never exceedable. |
explain_row_threshold |
int
|
If the row estimate from |
query_timeout_s |
int
|
Maximum execution timeout for a query, enforced by the driver/database. |
allowed_statements |
frozenset[str]
|
Whitelist of allowed statement types (only |
row_budget |
int | None
|
Cumulative row budget returned per session ( |
max_materialized_bytes |
int
|
Hard cap on the size (in bytes) of a single materialized
file. The materialization tools write whole records only while the cumulative
size stays below this ceiling and stop before exceeding it, so a file is never
larger than this value. Unlike |
check_estimate ¶
check_estimate(estimated_rows: int, metrics: SessionMetrics | None = None) -> None
Block the query if the EXPLAIN estimate exceeds the threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimated_rows
|
int
|
The estimated number of rows the query would return,
typically obtained via |
required |
metrics
|
SessionMetrics | None
|
Optional session counters to update when the query is blocked. |
None
|
Raises:
| Type | Description |
|---|---|
EstimateExceededError
|
If |
Source code in src/deep_db_agents/guardrails.py
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 | |
clamp_limit ¶
clamp_limit(requested: int | None) -> int
Compute the effective LIMIT to apply, capped by hard_max_rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
requested
|
int | None
|
The row limit requested by the agent, or |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The effective limit, never greater than |
Source code in src/deep_db_agents/guardrails.py
56 57 58 59 60 61 62 63 64 65 66 67 68 | |
SessionBudget
dataclass
¶
SessionBudget(budget: int | None = None, metrics: SessionMetrics | None = None)
Tracks row consumption in a session and enforces its budget.
If a SessionMetrics is associated, session counters are updated as well.
Attributes:
| Name | Type | Description |
|---|---|---|
budget |
int | None
|
Maximum cumulative rows allowed for the session, or |
metrics |
SessionMetrics | None
|
Optional session counters updated alongside the budget. |
consumed |
int
|
Rows consumed so far in the session (not set at init). |
charge ¶
charge(rows: int) -> None
Charge rows against the session budget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
int
|
Number of rows just returned, to add to the consumed total. |
required |
Raises:
| Type | Description |
|---|---|
RowBudgetExceededError
|
If the cumulative consumed rows exceed |
Source code in src/deep_db_agents/guardrails.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |