bricksentry 0.1.4__tar.gz → 0.1.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {bricksentry-0.1.4 → bricksentry-0.1.5}/CHANGELOG.md +4 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/PKG-INFO +2 -2
- {bricksentry-0.1.4 → bricksentry-0.1.5}/README.md +1 -1
- {bricksentry-0.1.4 → bricksentry-0.1.5}/pyproject.toml +1 -1
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/__init__.py +1 -1
- bricksentry-0.1.5/src/bricksentry/agent_tools.py +228 -0
- bricksentry-0.1.4/src/bricksentry/agent_tools.py +0 -139
- {bricksentry-0.1.4 → bricksentry-0.1.5}/.gitignore +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/CONTRIBUTING.md +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/LICENSE +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/SECURITY.md +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/action.yml +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/examples/databricks/bricksentry.yml +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/examples/databricks/bricksentry_quickstart.ipynb +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/examples/databricks/bricksentry_quickstart.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/examples/databricks/github-workflow.yml +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/examples/databricks/policy.yml +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/examples/demo/agent_loop_demo.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/scripts/build_notebook.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/assess.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/backends/__init__.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/backends/base.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/backends/databricks_backend.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/backends/duckdb_backend.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/changes.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/cli.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/config.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/demo.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/diff.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/explain.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/github.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/lineage.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/mcp_server.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/models.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/policy.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/preview.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/py.typed +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/src/bricksentry/report.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/tests/conftest.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/tests/test_assess_policy.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/tests/test_databricks_and_uc.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/tests/test_demo.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/tests/test_diff.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/tests/test_notebook.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/tests/test_preview_agents.py +0 -0
- {bricksentry-0.1.4 → bricksentry-0.1.5}/tests/test_sample_guard_and_store.py +0 -0
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.5 (2026-11-10)
|
|
4
|
+
|
|
5
|
+
- Clearer MCP tool definitions for agents. Each tool now says when to use it versus its sibling tools and what it returns, and every parameter has a description in the tool schema (for example the `candidates` map format for `assess_change`). No behavior changes.
|
|
6
|
+
|
|
3
7
|
## 0.1.4 (2026-11-10)
|
|
4
8
|
|
|
5
9
|
From a second end-to-end run on Databricks serverless against a 2-million-row table:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: bricksentry
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.5
|
|
4
4
|
Summary: Change-safety for Databricks pipelines and the AI agents that edit them: data diffs, Unity Catalog blast radius, policy verdicts and rollback plans.
|
|
5
5
|
Project-URL: Homepage, https://bricksentry.org
|
|
6
6
|
Project-URL: Documentation, https://bricksentry.org/docs
|
|
@@ -256,7 +256,7 @@ All conditions in a `when` must hold. The most severe matching rule sets the ver
|
|
|
256
256
|
- **Lineage is recorded at runtime.** Brand-new tables have no downstream history yet. Unity Catalog doesn't capture RDD operations, and UDFs hide column-level mapping.
|
|
257
257
|
- **Keys are required.** Tables without a stable key can't be diffed row by row yet (an aggregate-profile mode is planned).
|
|
258
258
|
- **Changed-table detection uses path globs** from config. It doesn't parse Asset Bundles yet.
|
|
259
|
-
- **Validated on one Databricks workspace so far** (serverless, November 2026, on a 2-million-row table): lineage, Delta rollback plans, previews on shallow clones, every verdict level and sampling. The automated tests use DuckDB and a mocked SDK, so please [open an issue](https://github.com/bricksentry/bricksentry/issues) if something behaves differently on yours.
|
|
259
|
+
- **Validated on one Databricks workspace so far** (serverless, November 2026, on a 2-million-row table with 0.1.4): lineage, Delta rollback plans, previews on shallow clones, every verdict level, and the sampling guard catching a single changed row. The automated tests use DuckDB and a mocked SDK, so please [open an issue](https://github.com/bricksentry/bricksentry/issues) if something behaves differently on yours.
|
|
260
260
|
|
|
261
261
|
## Roadmap
|
|
262
262
|
|
|
@@ -209,7 +209,7 @@ All conditions in a `when` must hold. The most severe matching rule sets the ver
|
|
|
209
209
|
- **Lineage is recorded at runtime.** Brand-new tables have no downstream history yet. Unity Catalog doesn't capture RDD operations, and UDFs hide column-level mapping.
|
|
210
210
|
- **Keys are required.** Tables without a stable key can't be diffed row by row yet (an aggregate-profile mode is planned).
|
|
211
211
|
- **Changed-table detection uses path globs** from config. It doesn't parse Asset Bundles yet.
|
|
212
|
-
- **Validated on one Databricks workspace so far** (serverless, November 2026, on a 2-million-row table): lineage, Delta rollback plans, previews on shallow clones, every verdict level and sampling. The automated tests use DuckDB and a mocked SDK, so please [open an issue](https://github.com/bricksentry/bricksentry/issues) if something behaves differently on yours.
|
|
212
|
+
- **Validated on one Databricks workspace so far** (serverless, November 2026, on a 2-million-row table with 0.1.4): lineage, Delta rollback plans, previews on shallow clones, every verdict level, and the sampling guard catching a single changed row. The automated tests use DuckDB and a mocked SDK, so please [open an issue](https://github.com/bricksentry/bricksentry/issues) if something behaves differently on yours.
|
|
213
213
|
|
|
214
214
|
## Roadmap
|
|
215
215
|
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "bricksentry"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.5"
|
|
8
8
|
description = "Change-safety for Databricks pipelines and the AI agents that edit them: data diffs, Unity Catalog blast radius, policy verdicts and rollback plans."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"""Agent tools as plain Python functions returning JSON-safe dicts.
|
|
2
|
+
|
|
3
|
+
The MCP server registers these, but they work in any agent framework
|
|
4
|
+
(LangGraph, OpenAI Agents SDK, Databricks Agent Framework...): import and wrap.
|
|
5
|
+
|
|
6
|
+
Errors come back as ``{"error": "..."}`` instead of exceptions, so an agent
|
|
7
|
+
can read them and recover.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import functools
|
|
13
|
+
from typing import Annotated, Any
|
|
14
|
+
|
|
15
|
+
from bricksentry.assess import Sentry
|
|
16
|
+
from bricksentry.explain import explain_diff as _explain_diff
|
|
17
|
+
|
|
18
|
+
try: # pydantic ships with the `mcp` extra; parameter descriptions are optional without it
|
|
19
|
+
from pydantic import Field as _Field
|
|
20
|
+
except ImportError: # pragma: no cover
|
|
21
|
+
_Field = None
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _desc(text: str):
|
|
25
|
+
"""Parameter description that MCP servers expose in the tool schema."""
|
|
26
|
+
return _Field(description=text) if _Field is not None else text
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
_SENTRY: Sentry | None = None
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def get_sentry() -> Sentry:
|
|
33
|
+
global _SENTRY
|
|
34
|
+
if _SENTRY is None:
|
|
35
|
+
_SENTRY = Sentry()
|
|
36
|
+
return _SENTRY
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def set_sentry(sentry: Sentry | None) -> None:
|
|
40
|
+
global _SENTRY
|
|
41
|
+
_SENTRY = sentry
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _safe(fn):
|
|
45
|
+
@functools.wraps(fn)
|
|
46
|
+
def wrapper(*args, **kwargs):
|
|
47
|
+
try:
|
|
48
|
+
return fn(*args, **kwargs)
|
|
49
|
+
except Exception as e:
|
|
50
|
+
return {"error": f"{type(e).__name__}: {e}"}
|
|
51
|
+
|
|
52
|
+
return wrapper
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@_safe
|
|
56
|
+
def whats_downstream(
|
|
57
|
+
table: Annotated[str, _desc("Fully qualified table name, e.g. main.sales.orders.")],
|
|
58
|
+
depth: Annotated[int, _desc("How many lineage hops to follow downstream (default 3).")] = 3,
|
|
59
|
+
) -> dict[str, Any]:
|
|
60
|
+
"""List what depends on a table, so you know the blast radius BEFORE you edit it.
|
|
61
|
+
|
|
62
|
+
Use this first, when planning a change. It reads Unity Catalog lineage and
|
|
63
|
+
returns every downstream table, job, notebook, pipeline, dashboard and query,
|
|
64
|
+
up to `depth` hops, with owners and tags. Dropped tables are filtered out.
|
|
65
|
+
Read-only; changes nothing. To check a change you have already made, use
|
|
66
|
+
assess_change instead.
|
|
67
|
+
|
|
68
|
+
Returns {table, owner, tags, count, downstream: [{kind, name, depth, via,
|
|
69
|
+
owner, tags}]}, or {error} if the table or lineage cannot be read."""
|
|
70
|
+
s = get_sentry()
|
|
71
|
+
nodes = s.downstream(table, depth)
|
|
72
|
+
owner, tags = s.lineage.owner_and_tags(table)
|
|
73
|
+
return {
|
|
74
|
+
"table": table,
|
|
75
|
+
"owner": owner,
|
|
76
|
+
"tags": tags,
|
|
77
|
+
"count": len(nodes),
|
|
78
|
+
"downstream": [n.to_dict() for n in nodes],
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@_safe
|
|
83
|
+
def assess_change(
|
|
84
|
+
tables: Annotated[list[str], _desc("Production tables your change affects, fully qualified.")],
|
|
85
|
+
candidates: Annotated[
|
|
86
|
+
dict[str, str] | None,
|
|
87
|
+
_desc(
|
|
88
|
+
"Optional map of production table -> candidate table your change built "
|
|
89
|
+
"(e.g. {'main.sales.orders': 'dev.sales.orders'}). Omit to use the "
|
|
90
|
+
"candidate pattern from bricksentry.yml."
|
|
91
|
+
),
|
|
92
|
+
] = None,
|
|
93
|
+
pr: Annotated[str | None, _desc("Optional pull request number or id, used to name candidates and the report.")] = None,
|
|
94
|
+
) -> dict[str, Any]:
|
|
95
|
+
"""Assess a change that has already been built into candidate tables.
|
|
96
|
+
|
|
97
|
+
Use this after your pipeline change has written candidate (dev/PR) tables.
|
|
98
|
+
For a single SQL write that has not run yet, use preview_write instead.
|
|
99
|
+
For each table it diffs production against the candidate (row, column and
|
|
100
|
+
schema changes, null-safe keys), walks downstream lineage and applies the
|
|
101
|
+
rules in policy.yml. The verdict is deterministic: rules decide, not an LLM.
|
|
102
|
+
Read-only on production and candidates.
|
|
103
|
+
|
|
104
|
+
Returns {id, verdict: pass|warn|needs_human|block, risk_tier, safe_to_ship,
|
|
105
|
+
notify, rule_hits, tables: [{table, verdict, summary, diff, downstream,
|
|
106
|
+
rollback}]}. Pass `id` to safe_to_ship (and optionally explain_diff)."""
|
|
107
|
+
a = get_sentry().assess(list(tables), candidates=candidates, pr=pr)
|
|
108
|
+
return a.to_dict()
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@_safe
|
|
112
|
+
def preview_write(
|
|
113
|
+
sql: Annotated[
|
|
114
|
+
str,
|
|
115
|
+
_desc(
|
|
116
|
+
"Exactly one INSERT, MERGE, UPDATE, DELETE or CREATE OR REPLACE TABLE "
|
|
117
|
+
"statement. Write {{target}} where the table name goes, or use the literal name."
|
|
118
|
+
),
|
|
119
|
+
],
|
|
120
|
+
target: Annotated[str, _desc("Fully qualified production table the statement would write to.")],
|
|
121
|
+
keys: Annotated[
|
|
122
|
+
list[str] | None, _desc("Optional key columns for the row diff. Omit to use keys from bricksentry.yml.")
|
|
123
|
+
] = None,
|
|
124
|
+
) -> dict[str, Any]:
|
|
125
|
+
"""Dry-run ONE write statement on a disposable shallow clone of `target`,
|
|
126
|
+
never on production, and report what it would change plus the policy verdict.
|
|
127
|
+
|
|
128
|
+
Use this before running any direct write. It clones the table, rewrites the
|
|
129
|
+
statement to hit the clone, runs it, diffs clone vs production, applies policy
|
|
130
|
+
and drops the clone, even on failure. DROP, TRUNCATE, ALTER, GRANT and
|
|
131
|
+
multiple statements are refused.
|
|
132
|
+
|
|
133
|
+
Returns the same shape as assess_change (with an `id` for safe_to_ship), or
|
|
134
|
+
{error} if the statement is refused or fails."""
|
|
135
|
+
return get_sentry().preview_write(sql, target, keys=keys)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
@_safe
|
|
139
|
+
def explain_diff(
|
|
140
|
+
assessment_id: Annotated[
|
|
141
|
+
str | None, _desc("Id returned by assess_change or preview_write. Give this or `table`.")
|
|
142
|
+
] = None,
|
|
143
|
+
table: Annotated[
|
|
144
|
+
str | None, _desc("Table name, to explain its most recent diff in this session. Give this or `assessment_id`.")
|
|
145
|
+
] = None,
|
|
146
|
+
) -> dict[str, Any]:
|
|
147
|
+
"""Explain in plain English what a diff changed, for a PR description or a
|
|
148
|
+
human reviewer.
|
|
149
|
+
|
|
150
|
+
Use after assess_change or preview_write when you need readable text rather
|
|
151
|
+
than raw numbers, e.g. "removed 1 row (0.00%); revenue sum -3.1%". Read-only.
|
|
152
|
+
Works across processes because assessments are saved to disk.
|
|
153
|
+
|
|
154
|
+
Returns {assessment_id, explanations: {table: text}} when given an id, or
|
|
155
|
+
{table, explanation} when given a table, or {error} if nothing matches."""
|
|
156
|
+
s = get_sentry()
|
|
157
|
+
if assessment_id:
|
|
158
|
+
a = s.get_assessment(assessment_id)
|
|
159
|
+
if a is None:
|
|
160
|
+
return {"error": f"unknown assessment id {assessment_id}"}
|
|
161
|
+
return {"assessment_id": a.id, "explanations": {t.table: t.summary for t in a.tables}}
|
|
162
|
+
if table:
|
|
163
|
+
d = s.get_diff(table)
|
|
164
|
+
if d is None:
|
|
165
|
+
return {"error": f"no diff for {table} yet; call assess_change or preview_write first"}
|
|
166
|
+
return {"table": table, "explanation": _explain_diff(d)}
|
|
167
|
+
return {"error": "pass assessment_id or table"}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
@_safe
|
|
171
|
+
def safe_to_ship(
|
|
172
|
+
assessment_id: Annotated[str, _desc("Id returned by assess_change or preview_write.")],
|
|
173
|
+
) -> dict[str, Any]:
|
|
174
|
+
"""Final gate: decide whether to merge or apply a change. Call this last,
|
|
175
|
+
and follow `next_step` exactly.
|
|
176
|
+
|
|
177
|
+
safe_to_ship is true only for pass and warn. For needs_human, do not merge:
|
|
178
|
+
open the PR with the report and request review from the listed owners. For
|
|
179
|
+
block, stop and fix the issues in rule_hits. Never bypass needs_human or
|
|
180
|
+
block. Read-only.
|
|
181
|
+
|
|
182
|
+
Returns {assessment_id, safe_to_ship: bool, verdict, risk_tier, rule_hits:
|
|
183
|
+
[{rule, verdict, table, reason}], notify, next_step}, or {error} if the id
|
|
184
|
+
is unknown."""
|
|
185
|
+
a = get_sentry().get_assessment(assessment_id)
|
|
186
|
+
if a is None:
|
|
187
|
+
return {"error": f"unknown assessment id {assessment_id}"}
|
|
188
|
+
v = a.verdict.value
|
|
189
|
+
next_step = {
|
|
190
|
+
"pass": "Proceed: open or merge the pull request.",
|
|
191
|
+
"warn": "Proceed, but include the warnings in the pull request description.",
|
|
192
|
+
"needs_human": "Do not merge. Open the pull request with the report and request review from: "
|
|
193
|
+
+ (", ".join(a.notify) or "a data owner") + ".",
|
|
194
|
+
"block": "Stop. Fix the blocking issues listed in rule_hits, rebuild the candidate and assess again.",
|
|
195
|
+
}[v]
|
|
196
|
+
return {
|
|
197
|
+
"assessment_id": a.id,
|
|
198
|
+
"safe_to_ship": v in ("pass", "warn"),
|
|
199
|
+
"verdict": v,
|
|
200
|
+
"risk_tier": a.verdict.risk_tier,
|
|
201
|
+
"rule_hits": [h.to_dict() for h in a.hits],
|
|
202
|
+
"notify": a.notify,
|
|
203
|
+
"next_step": next_step,
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
@_safe
|
|
208
|
+
def rollback_plan(
|
|
209
|
+
table: Annotated[str, _desc("Fully qualified Delta table name.")],
|
|
210
|
+
) -> dict[str, Any]:
|
|
211
|
+
"""Get the exact SQL to restore `table` to its current Delta version.
|
|
212
|
+
|
|
213
|
+
Call this BEFORE applying a change and keep the SQL (e.g. in the PR or job
|
|
214
|
+
log), so undo is one statement if the change goes wrong. Read-only: it reads
|
|
215
|
+
DESCRIBE HISTORY and runs nothing.
|
|
216
|
+
|
|
217
|
+
Returns {table, current_version, rollback_sql}, e.g. "RESTORE TABLE
|
|
218
|
+
main.sales.orders TO VERSION AS OF 42", or {error}."""
|
|
219
|
+
s = get_sentry()
|
|
220
|
+
return {
|
|
221
|
+
"table": table,
|
|
222
|
+
"current_version": s.backend.current_version(table),
|
|
223
|
+
"rollback_sql": s.rollback_plan(table),
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
TOOLS = [whats_downstream, assess_change, preview_write, explain_diff, safe_to_ship, rollback_plan]
|
|
228
|
+
READ_ONLY = {"whats_downstream", "assess_change", "explain_diff", "safe_to_ship", "rollback_plan"}
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
"""Agent tools as plain Python functions returning JSON-safe dicts.
|
|
2
|
-
|
|
3
|
-
The MCP server registers these, but they work in any agent framework
|
|
4
|
-
(LangGraph, OpenAI Agents SDK, Databricks Agent Framework...): import and wrap.
|
|
5
|
-
|
|
6
|
-
Errors come back as ``{"error": "..."}`` instead of exceptions, so an agent
|
|
7
|
-
can read them and recover.
|
|
8
|
-
"""
|
|
9
|
-
|
|
10
|
-
from __future__ import annotations
|
|
11
|
-
|
|
12
|
-
import functools
|
|
13
|
-
from typing import Any
|
|
14
|
-
|
|
15
|
-
from bricksentry.assess import Sentry
|
|
16
|
-
from bricksentry.explain import explain_diff as _explain_diff
|
|
17
|
-
|
|
18
|
-
_SENTRY: Sentry | None = None
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def get_sentry() -> Sentry:
|
|
22
|
-
global _SENTRY
|
|
23
|
-
if _SENTRY is None:
|
|
24
|
-
_SENTRY = Sentry()
|
|
25
|
-
return _SENTRY
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
def set_sentry(sentry: Sentry | None) -> None:
|
|
29
|
-
global _SENTRY
|
|
30
|
-
_SENTRY = sentry
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
def _safe(fn):
|
|
34
|
-
@functools.wraps(fn)
|
|
35
|
-
def wrapper(*args, **kwargs):
|
|
36
|
-
try:
|
|
37
|
-
return fn(*args, **kwargs)
|
|
38
|
-
except Exception as e:
|
|
39
|
-
return {"error": f"{type(e).__name__}: {e}"}
|
|
40
|
-
|
|
41
|
-
return wrapper
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
@_safe
|
|
45
|
-
def whats_downstream(table: str, depth: int = 3) -> dict[str, Any]:
|
|
46
|
-
"""List everything downstream of a table (tables, jobs, notebooks, pipelines,
|
|
47
|
-
dashboards, queries) with owners and tags, from lineage. Read-only."""
|
|
48
|
-
s = get_sentry()
|
|
49
|
-
nodes = s.downstream(table, depth)
|
|
50
|
-
owner, tags = s.lineage.owner_and_tags(table)
|
|
51
|
-
return {
|
|
52
|
-
"table": table,
|
|
53
|
-
"owner": owner,
|
|
54
|
-
"tags": tags,
|
|
55
|
-
"count": len(nodes),
|
|
56
|
-
"downstream": [n.to_dict() for n in nodes],
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
@_safe
|
|
61
|
-
def assess_change(
|
|
62
|
-
tables: list[str],
|
|
63
|
-
candidates: dict[str, str] | None = None,
|
|
64
|
-
pr: str | None = None,
|
|
65
|
-
) -> dict[str, Any]:
|
|
66
|
-
"""Assess a change: diff each table's production version against its candidate
|
|
67
|
-
(built by your change), walk downstream lineage and apply policy.
|
|
68
|
-
Returns a verdict (pass / warn / needs_human / block), a risk tier, reasons,
|
|
69
|
-
owners to notify and an assessment id for explain_diff / safe_to_ship. Read-only."""
|
|
70
|
-
a = get_sentry().assess(list(tables), candidates=candidates, pr=pr)
|
|
71
|
-
return a.to_dict()
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
@_safe
|
|
75
|
-
def preview_write(sql: str, target: str, keys: list[str] | None = None) -> dict[str, Any]:
|
|
76
|
-
"""Run ONE write statement (INSERT / MERGE / UPDATE / DELETE / CREATE OR REPLACE TABLE)
|
|
77
|
-
against a disposable sandbox clone of `target`, never production, and report
|
|
78
|
-
what it would change plus the policy verdict. Use {{target}} in the SQL or the
|
|
79
|
-
literal table name. The clone is dropped afterwards."""
|
|
80
|
-
return get_sentry().preview_write(sql, target, keys=keys)
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
@_safe
|
|
84
|
-
def explain_diff(assessment_id: str | None = None, table: str | None = None) -> dict[str, Any]:
|
|
85
|
-
"""Plain-English explanation of what changed, by assessment id or table name. Read-only."""
|
|
86
|
-
s = get_sentry()
|
|
87
|
-
if assessment_id:
|
|
88
|
-
a = s.get_assessment(assessment_id)
|
|
89
|
-
if a is None:
|
|
90
|
-
return {"error": f"unknown assessment id {assessment_id}"}
|
|
91
|
-
return {"assessment_id": a.id, "explanations": {t.table: t.summary for t in a.tables}}
|
|
92
|
-
if table:
|
|
93
|
-
d = s.get_diff(table)
|
|
94
|
-
if d is None:
|
|
95
|
-
return {"error": f"no diff for {table} yet; call assess_change or preview_write first"}
|
|
96
|
-
return {"table": table, "explanation": _explain_diff(d)}
|
|
97
|
-
return {"error": "pass assessment_id or table"}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
@_safe
|
|
101
|
-
def safe_to_ship(assessment_id: str) -> dict[str, Any]:
|
|
102
|
-
"""Final gate for an assessment: returns safe_to_ship (bool), the verdict,
|
|
103
|
-
the rules that fired and the next step the agent should take. Read-only."""
|
|
104
|
-
a = get_sentry().get_assessment(assessment_id)
|
|
105
|
-
if a is None:
|
|
106
|
-
return {"error": f"unknown assessment id {assessment_id}"}
|
|
107
|
-
v = a.verdict.value
|
|
108
|
-
next_step = {
|
|
109
|
-
"pass": "Proceed: open or merge the pull request.",
|
|
110
|
-
"warn": "Proceed, but include the warnings in the pull request description.",
|
|
111
|
-
"needs_human": "Do not merge. Open the pull request with the report and request review from: "
|
|
112
|
-
+ (", ".join(a.notify) or "a data owner") + ".",
|
|
113
|
-
"block": "Stop. Fix the blocking issues listed in rule_hits, rebuild the candidate and assess again.",
|
|
114
|
-
}[v]
|
|
115
|
-
return {
|
|
116
|
-
"assessment_id": a.id,
|
|
117
|
-
"safe_to_ship": v in ("pass", "warn"),
|
|
118
|
-
"verdict": v,
|
|
119
|
-
"risk_tier": a.verdict.risk_tier,
|
|
120
|
-
"rule_hits": [h.to_dict() for h in a.hits],
|
|
121
|
-
"notify": a.notify,
|
|
122
|
-
"next_step": next_step,
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
@_safe
|
|
127
|
-
def rollback_plan(table: str) -> dict[str, Any]:
|
|
128
|
-
"""The exact SQL to restore `table` to its current version if a change goes wrong.
|
|
129
|
-
Capture it BEFORE applying a change. Read-only."""
|
|
130
|
-
s = get_sentry()
|
|
131
|
-
return {
|
|
132
|
-
"table": table,
|
|
133
|
-
"current_version": s.backend.current_version(table),
|
|
134
|
-
"rollback_sql": s.rollback_plan(table),
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
TOOLS = [whats_downstream, assess_change, preview_write, explain_diff, safe_to_ship, rollback_plan]
|
|
139
|
-
READ_ONLY = {"whats_downstream", "assess_change", "explain_diff", "safe_to_ship", "rollback_plan"}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|