Python API Reference¶
Top-level exports¶
groundcrew
¶
groundcrew - Deterministic state oracle and semantic action codec for computer-use agents.
ChainVerification(is_valid, chain_length, broken_at=None, errors=list(), summary='')
dataclass
¶
Result of verifying a receipt chain.
Attributes:
| Name | Type | Description |
|---|---|---|
is_valid |
bool
|
True if the chain is unbroken. |
chain_length |
int
|
Number of receipts in the chain. |
broken_at |
int | None
|
Index of the first broken link, or None if the chain is valid. |
errors |
list[str]
|
List of human-readable error descriptions. |
summary |
str
|
One-line summary of the verification result. |
ClosedLoopError
¶
Bases: ValueError
Raised when the gate refuses empty, unusable, or empty-effect receipts.
GateOutcome(ok, verdict, reason, exit_code, receipt_count=0, total_changed_paths=0, empty_effect_ids=(), dead_path_ids=(), dead_paths=(), human_required=False, risk=None, inventory_count=0, action=None)
dataclass
¶
Result of a closed-loop read of groundcrew receipts or destructive gates.
Attributes:
| Name | Type | Description |
|---|---|---|
ok |
bool
|
True only when a pipeline may continue (PASS). |
verdict |
str
|
|
reason |
str
|
Human-readable explanation (always non-empty). |
exit_code |
int
|
0 PASS, 1 FAIL (action failed), 2 FAIL_LOUD (empty/no side effects). |
receipt_count |
int
|
Number of receipts examined. |
total_changed_paths |
int
|
Distinct changed paths across examined receipts. |
empty_effect_ids |
tuple[str, ...]
|
Receipt IDs that claimed success with zero changes. |
dead_path_ids |
tuple[str, ...]
|
Receipt IDs with success but paths that fail disk verify. |
dead_paths |
tuple[str, ...]
|
Sample of claimed paths that are dead on disk. |
human_required |
bool
|
True when a human must approve before proceeding. |
risk |
str | None
|
|
inventory_count |
int
|
Count of named targets that will be destroyed (if gated). |
action |
str | None
|
Canonical action / verb that was gated (destructive path). |
to_dict()
¶
Serialise for JSON reports (eagle-eyes dogfood, CI artifacts).
Source code in src/groundcrew/closed_loop.py
ActionReceipt(spec, before_id, after_id, diff, success, timestamp)
dataclass
¶
A verifiable record pairing an action spec with the state change it produced.
ActionSpec(verb, target, params)
dataclass
¶
A semantic description of an action: a verb applied to a target with params.
ContentDiff(file_diffs=list(), total_added=0, total_removed=0)
dataclass
¶
Aggregated line-level diff across all changed files.
Attributes:
| Name | Type | Description |
|---|---|---|
file_diffs |
list[FileDiff]
|
Per-file diff results. |
total_added |
int
|
Sum of added lines across all files. |
total_removed |
int
|
Sum of removed lines across all files. |
FileDiff(path, before_lines, after_lines, added_lines, removed_lines, unified_diff, is_binary=False, is_approximate=False)
dataclass
¶
Line-level diff for a single file.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
Relative path to the file. |
before_lines |
int | None
|
Number of lines in the before version (0 for new files,
|
after_lines |
int
|
Number of lines in the after version (0 for deleted files). |
added_lines |
int
|
Number of lines added. |
removed_lines |
int
|
Number of lines removed. |
unified_diff |
str
|
Standard unified diff string. |
is_binary |
bool
|
True if the file was detected as binary. |
is_approximate |
bool
|
True when the before content is unavailable (modified files). In this case the diff shows all current lines as added because the before state cannot be reconstructed from hashes alone. |
Oracle(root, spec=None)
¶
Context manager that snapshots a root before and after a block of work.
Source code in src/groundcrew/oracle.py
record(spec)
¶
Build an ActionReceipt for spec from the captured before/after state.
Source code in src/groundcrew/oracle.py
ReceiptStore(path)
¶
A SQLite-backed store for persisting and retrieving action receipts.
Source code in src/groundcrew/oracle.py
FileState(path, size, sha256)
dataclass
¶
The recorded state of a single file: relative path, size, and digest.
SnapshotDiff(snapshot_a_id, snapshot_b_id, added, removed, modified)
dataclass
¶
The structural delta between two snapshots: added, removed, modified files.
Attributes:
| Name | Type | Description |
|---|---|---|
added |
list[FileState]
|
|
removed |
list[FileState]
|
|
modified |
list[tuple[FileState, FileState]]
|
|
StateSnapshot(id, timestamp, root, files)
dataclass
¶
A content-addressed snapshot of every file beneath a root directory.
PlannedToolCall(call_id, name, arguments=dict())
dataclass
¶
One proposed tool invocation (pre-execution).
ToolMisuseReport(validity_ids, over_call_ids, missing_tools, call_count, classes)
dataclass
¶
Classified misuse findings for a plan.
ToolSchema(name, required_args=(), arg_types=dict())
dataclass
¶
Required argument contract for a named tool.
DirectoryWatcher(root, authorized_paths=None, interval_seconds=5.0)
¶
Polls a directory for changes and fires callbacks on unexpected mutations.
Typical usage::
watcher = DirectoryWatcher(root="/path/to/dir", interval_seconds=5.0)
watcher.take_baseline()
changes = watcher.check()
if changes:
print("Unexpected changes:", changes)
Attributes:
| Name | Type | Description |
|---|---|---|
root |
The directory being watched. |
|
authorized_paths |
set[str]
|
If provided, changes to these paths are considered authorized and will not be reported. |
interval_seconds |
Polling interval used by :meth: |
Source code in src/groundcrew/watcher.py
take_baseline()
¶
Capture the current state of the directory as the authorized baseline.
Returns:
| Type | Description |
|---|---|
StateSnapshot
|
The captured :class: |
Source code in src/groundcrew/watcher.py
check()
¶
Check for changes since the baseline was taken.
Compares the current directory state against the stored baseline and
returns a list of human-readable change descriptions for all changes
that are not in :attr:authorized_paths.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of change descriptions. Empty if no unauthorized changes. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If :meth: |
Source code in src/groundcrew/watcher.py
watch(callback, max_checks=10)
¶
Poll for changes and invoke callback on unexpected mutations.
Polls up to max_checks times, sleeping :attr:interval_seconds
between each poll. This is intentionally non-infinite so it remains
testable and composable. Use a loop around :meth:watch for indefinite
monitoring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
Callable[[list[str]], None]
|
Called with a list of change description strings whenever unauthorized changes are detected. |
required |
max_checks
|
int
|
Maximum number of polls before returning. |
10
|
Source code in src/groundcrew/watcher.py
build_chain_report(receipts)
¶
Build a human-readable chain-of-custody report for a sequence of receipts.
The report lists each receipt's action, state transition, outcome, and timestamp, followed by an overall chain verification result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
receipts
|
list[ActionReceipt]
|
Ordered list of :class: |
required |
Returns:
| Type | Description |
|---|---|
str
|
A formatted multi-line string suitable for printing or logging. |
Source code in src/groundcrew/chain.py
verify_chain(receipts)
¶
Verify that a sequence of receipts forms an unbroken chain.
The chain is valid if for every consecutive pair:
receipts[n].after_id == receipts[n+1].before_id
An empty list is considered trivially valid (length 0). A single-receipt list is also valid (nothing to check).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
receipts
|
list[ActionReceipt]
|
Ordered list of :class: |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
ChainVerification
|
class: |
Source code in src/groundcrew/chain.py
assert_not_destructive(verb='', **kwargs)
¶
Raise :class:ClosedLoopError unless :func:gate_destructive is ok.
Source code in src/groundcrew/closed_loop.py
assert_side_effects(source, **kwargs)
¶
Gate receipts and raise :class:ClosedLoopError unless outcome is ok.
Source code in src/groundcrew/closed_loop.py
dead_paths_for_receipt(receipt, root)
¶
Return claimed side-effect paths that do not match the live workspace.
D-GCROOT / L10 harden: a success receipt can invent FileState rows so
changed_paths is non-empty while nothing real happened. When a workspace
root is known, every path in the structural diff must match disk:
- added / modified: file must exist under
root - removed: file must not exist under
root
Returns the list of dead (mismatch) relative paths (may be empty).
Source code in src/groundcrew/closed_loop.py
gate_destructive(verb='', *, target='', sql=None, command=None, params=None, inventory=None, approved=False, approval_token=None, environment='production', require_inventory=True)
¶
Block unattended destructive tools (Replit DB wipe / Antigravity / AgentWard).
Load-bearing controls (all required for destructive ops in strict envs):
- Classify - verb / SQL / shell must be detected as destructive.
- Inventory - named targets that will be destroyed (tables, paths, DBs). Empty inventory = agent does not know what it is wiping → FAIL_LOUD.
- Approval - human token / approved flag. Missing → FAIL_LOUD
(
human_required=True).
Non-destructive calls PASS without inventory or approval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verb
|
str
|
Tool verb (e.g. |
''
|
target
|
str
|
Logical target name (db, table, path). |
''
|
sql
|
str | None
|
Free-form SQL if the tool accepts queries. |
None
|
command
|
str | None
|
Shell command string if applicable. |
None
|
params
|
dict[str, Any] | None
|
Extra tool params (may embed |
None
|
inventory
|
Sequence[str] | None
|
Explicit list of objects that will be destroyed. |
None
|
approved
|
bool
|
True when a human (or humanproof session) already approved. |
False
|
approval_token
|
str | None
|
Opaque owner-issued token id/secret handle. |
None
|
environment
|
str
|
|
'production'
|
require_inventory
|
bool
|
If True (default), destructive ops need non-empty inventory even when approved. |
True
|
Returns:
| Type | Description |
|---|---|
GateOutcome
|
class: |
Source code in src/groundcrew/closed_loop.py
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 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 613 | |
gate_destructive_receipt(receipt, *, inventory=None, approved=False, approval_token=None, environment='production', require_inventory=True)
¶
Gate an :class:ActionReceipt for destructive verbs (pre- or post-exec).
Uses receipt.spec.verb/target/params plus optional explicit inventory.
When inventory is omitted, falls back to receipt.diff.changed_paths
(filesystem-class wipes) then spec.target.
Source code in src/groundcrew/closed_loop.py
gate_receipts(source, *, require_side_effects=True, require_any_success=True, root=None, verify_disk=None)
¶
Read receipts and fail loudly when success has no filesystem side effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
ReceiptStore | Sequence[ActionReceipt] | str | Path
|
Open :class: |
required |
require_side_effects
|
bool
|
If True (L10 default), any receipt with
|
True
|
require_any_success
|
bool
|
If True, a non-empty set of receipts where every
receipt has |
True
|
root
|
str | Path | None
|
Workspace directory for D-GCROOT dead-path verification. When set
(or when |
None
|
verify_disk
|
bool | None
|
Force on/off disk verification. Default: True when |
None
|
Returns:
| Type | Description |
|---|---|
GateOutcome
|
class: |
Source code in src/groundcrew/closed_loop.py
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 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 342 343 344 345 346 347 348 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 | |
is_destructive(verb='', *, target='', sql=None, command=None, params=None)
¶
Classify a tool call as destructive (irreversible data/schema/file loss).
Checks (any match → destructive):
1. Verb in :data:DESTRUCTIVE_VERBS (exact or prefix verb:scope)
2. SQL payload via :func:sql_is_destructive
3. Shell command via :func:shell_is_destructive
4. params['sql'] / params['command'] / params['query']
Source code in src/groundcrew/closed_loop.py
shell_is_destructive(command)
¶
Return True if command looks like rm -rf / shred / dd overwrite class.
Source code in src/groundcrew/closed_loop.py
sql_is_destructive(sql)
¶
Return True if sql contains irreversible DROP/TRUNCATE/DELETE-class ops.
Public incidents (Replit AI production DB wipe, AgentWard file wipe) start with free-form SQL tools that accept any string. Classifiers must refuse before execution - not after a success receipt is written.
Source code in src/groundcrew/closed_loop.py
analyze_tool_misuse(calls, *, schemas=None, tools_required=False, required_tools=None, tools_forbidden=False, max_calls=None)
¶
Classify validity / over-calling / missing failures on a tool plan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
calls
|
Sequence[PlannedToolCall | dict[str, Any]] | None
|
Planned tool calls (may be empty). |
required |
schemas
|
Sequence[ToolSchema | dict[str, Any]] | Mapping[str, ToolSchema | dict[str, Any]] | None
|
Per-tool required-arg contracts (list or name→schema map). |
None
|
tools_required
|
bool
|
If True and calls empty → missing class. |
False
|
required_tools
|
Iterable[str] | None
|
Tool names that must appear at least once. |
None
|
tools_forbidden
|
bool
|
If True, any call is over-calling (answer-only turn). |
False
|
max_calls
|
int | None
|
Soft cap; excess calls tagged over-calling. |
None
|
Source code in src/groundcrew/tool_misuse.py
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 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 236 237 238 239 240 241 242 243 244 245 246 | |
assert_tool_misuse_ok(calls=None, **kwargs)
¶
Raise :class:ClosedLoopError unless :func:gate_tool_misuse is ok.
Source code in src/groundcrew/tool_misuse.py
call_is_valid(call, schema)
¶
True when required args present and types match schema (if provided).
Source code in src/groundcrew/tool_misuse.py
gate_tool_misuse(calls=None, *, schemas=None, tools_required=False, required_tools=None, tools_forbidden=False, max_calls=None, refuse_validity=True, refuse_over_calling=True, refuse_missing=True)
¶
Refuse plans with PRISMS-class tool misuse (arXiv 2608.00218).
Rules:
- Invalid args (validity) → FAIL
- Over-calling when tools forbidden / over max_calls → FAIL
- Missing required tools / empty when tools_required → FAIL_LOUD (missing is pre-generation boundary class - empty inventory)
- Clean plan → PASS
Source code in src/groundcrew/tool_misuse.py
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 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 342 343 344 345 346 347 348 349 350 351 352 | |