Metadata-Version: 2.5
Name: domdrift
Version: 0.1.0
Summary: Fail-closed HTML and JSON structural contract guard with soft-block detection and <2KB redacted drift snapshots for CLI and MCP agents.
Project-URL: Homepage, https://github.com/ByKite/domdrift
Project-URL: Repository, https://github.com/ByKite/domdrift
Project-URL: Issues, https://github.com/ByKite/domdrift/issues
Author-email: Kite <192934406+ByKite@users.noreply.github.com>
License-Expression: MIT
License-File: LICENSE
Keywords: ai-agents,cli,css-selectors,dom-diff,json-schema,mcp,model-context-protocol,web-scraping
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet :: WWW/HTTP :: Indexing/Search
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Software Development :: Testing
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown

# domdrift

domdrift checks saved HTML and JSON responses against a structural contract and tells a scraper when it got a challenge page instead.

```sh
uvx domdrift diff yesterday.html today.html
```

Save a contract from a known-good response. Successful checks print nothing:

```console
$ printf '<main><span class="price">$12</span></main>' > good.html
$ uvx domdrift init good.html --select 'price=main > .price' -o contract.json
$ uvx domdrift check -c contract.json good.html; echo $?
0
$ printf '<main><span class="sale-price">$10</span></main>' | uvx domdrift check -c contract.json --max-bytes 512 > repair.txt
$ echo $?
2
$ wc -c < repair.txt
256
$ cat repair.txt
DRIFT_DETECTED
violations: rule=price selector=main > .price reason=matched 0, need >= 1
survivors: selector=main count=1 broken=.price
candidates: selector=main > .sale-price count=1
candidates: selector=main > span count=1
  <main>
    <span.sale-price>
$ printf '<title>Just a moment...</title>' | uvx domdrift check -c contract.json
SOFT_BLOCK_DETECTED
Cloudflare: challenge page title. Keep existing selectors.
$ echo $?
3
```

For an MCP client that launches local stdio servers:

```json
{
  "mcpServers": {
    "domdrift": { "command": "uvx", "args": ["domdrift", "mcp"] }
  }
}
```

`domdrift_init` takes baseline content or a public URL. Pass its contract to `domdrift_check` with the next response. `domdrift_diff` compares two content strings directly. Check and diff return `status`, `exit_code` and `report`.

## Installation

Run without installing via [`uv`](https://docs.astral.sh/uv/):

```sh
uvx domdrift --version
```

Or install into a persistent environment:

```sh
uv tool install domdrift
# or
pip install domdrift
```

Requires Python 3.11+ and uses only the Python standard library (`dependencies = []`).

## Contracts

`init` accepts a file path, an HTTP(S) URL or stdin. Without `--select`, it looks for landmarks, repeating HTML items and their fields, or infers JSON paths and types. Use `--auto-landmarks` to add automatic HTML rules to explicit selections.

```sh
uvx domdrift init catalog.json --select 'price=$.data.items[*].unit_price' -o catalog.contract
uvx domdrift check -c catalog.contract next.json --format json --max-bytes 1024
```

HTML rules support `min_count`, `max_count`, `min_non_empty_ratio`, `required_attr` and a Python regex `pattern` on the attribute or visible text. CSS supports groups, descendant, child and sibling combinators, attribute operators, `:not`, `:has`, `:contains`, and first/last/nth-child. The parser works on supplied markup; it does not execute JavaScript.

JSON paths start at `$` and use `.key` and `[*]`. Inferred rules record types, nullability, field presence, non-empty ratios and minimum array sizes. A check permits value changes that satisfy those rules. A snapshot diff also reports inventory additions, removals, count changes, moves between HTML containers and type changes in optional JSON fields.

## Exit codes and limits

| Code | Meaning |
| ---: | --- |
| 0 | Contract holds, or no structural change in a diff. |
| 1 | Invalid arguments, contract or input, or a read error. |
| 2 | Structural drift. Inspect the surviving prefix, candidate and skeleton. |
| 3 | Likely challenge, access gate, rate limit or empty hydration shell. Keep the current selectors. |

Reports default to 2048 UTF-8 bytes. `--max-bytes` accepts budgets of 256 bytes or more. JSON output stays valid when details are trimmed. Evidence uses structural labels and JSON types rather than body text or JSON scalar values. Keep contracts from trusted sources: selectors and regexes are executable checks on incoming documents.

Input reads are capped at 10 MiB. URL connections use a 15-second socket timeout and check public addresses on each redirect and connection. `--allow-private-net` permits local HTTP fixtures on the CLI. MCP URL inputs always require public HTTP(S) addresses.

From a source checkout, run `uv run domdrift --help`. Python 3.11 or newer is required; the installed package uses only the standard library.
