Metadata-Version: 2.5
Name: jev-decide
Version: 0.1.0
Summary: The jev decision service: page state in, one typed operation and target out.
License-Expression: MIT
Requires-Python: >=3.12
Requires-Dist: httpx[http2]<1,>=0.28
Description-Content-Type: text/markdown

# jev-decide

**One decision, no browser.** `jev decide` reads a page state and answers with a single
typed operation and target that the caller executes. It observes nothing, executes
nothing, and never generates text.

It is built for an agent that already drives a browser: page state in, one action out.

## Install

```bash
uv tool install --editable .     # tracks this checkout; right while developing
uv tool install --no-cache .     # frozen copy of the current source
```

Pass `--no-cache` for a frozen copy: `uv tool install --force .` can reuse a cached wheel
and silently install older code than the checkout.

The only runtime dependency is `httpx`. There is no browser driver here.

## Use

Feed it the output of `ziniao-cli page snapshot` verbatim:

```bash
ziniao-cli page snapshot --store-id "$STORE" --max-items 200 \
  | jev decide --goal "Open the orders page"
```

`page` is not optional and not implicit. The command is `ziniao-cli page snapshot`;
`ziniao-cli snapshot` answers `unknown command "snapshot" for "ziniao-cli"`, and the store
browser has to be open already.

```json
{
  "status": "ok",
  "operation": "CLICK",
  "choice": "e30",
  "target": {
    "index": "13",
    "ref": "e30",
    "selector": "#nav-orders",
    "role": "link",
    "label": "Returns & Orders"
  },
  "needs_text": false,
  "confidence": 0.99,
  "operation_probabilities": {"CLICK": 1.0, "TYPE_TEXT": 0.0, "WAIT": 0.0, "DONE": 0.0, "SCROLL_DOWN": 0.0, "BLOCKED": 0.0},
  "target_probabilities": {"13": 0.95, "11": 0.03, "12": 0.02},
  "elements": [{"index": "13", "ref": "e30", "selector": "#nav-orders", "role": "link", "label": "Returns & Orders", "operations": ["CLICK"]}]
}
```

That response is real, not illustrative: the captured `ziniao-cli page snapshot` of a
public amazon.com page (`tests/fixtures/ziniao_page_snapshot.json`) with the goal *"Open
the orders page"* — `jev-1.13.0`, 2724 input tokens, 935 ms. The fixture ships in this
repo, so the shape above is reproducible offline.

- `--observation FILE` reads a file instead of stdin; `--history FILE` passes previous
  steps as a JSON list; `--compact` emits one line.
- stdout is **always** JSON, including failures, so a caller parses one stream.
- Exit codes: `0` a decision, `2` the observation was unusable, `3` the model or its
  credential failed.

Every response carries the whole `elements` index table with a `ref` and a `selector` for
each target, so the caller can execute or re-check without re-snapshotting.

### Execute the decision

| Decision | How the caller runs it |
| --- | --- |
| `CLICK` | `ziniao-cli page click --store-id "$STORE" --ref <ref>` (or `--selector`) |
| `TYPE_TEXT` | the caller writes the value, then `ziniao-cli page input --store-id "$STORE" --selector <selector> --text <value> --clear` |
| `SELECT` | set the value at the returned `selector` (`page input` has no `--ref`) |
| `SCROLL_DOWN` / `SCROLL_UP` | `ziniao-cli page scroll --store-id "$STORE" --y ±560` |
| `WAIT` | observe again without acting |
| `DONE` / `BLOCKED` | stop. `DONE` is the model's choice, not proof: verify the outcome yourself |

**TYPE_TEXT never comes with text.** The service names the field and stops; the calling
agent supplies the value from its own context. That is why this package needs no text
model and no `TEXT_MODEL_API_KEY`.

## The observation

`page snapshot` is the only `ziniao-cli` page command that returns an indexed element
table with both semantic identity (role, accessible name, context, geometry) **and**
actionable locators. `page content --content-format structured` reads better but its
`selectorHint` is often just a tag name and form controls carry no locator, so it cannot
drive a choice.

The current `ziniao-page` skill draws the same line from the other side: it treats
`page snapshot` as a local fallback — for upload controls, custom or icon-only controls,
and candidates that survive `page query` and open-shadow-DOM checks — and prefers
`page content --content-format structured` for *understanding* a page. That preference is
about reading, not about choosing: only the snapshot carries a `ref` and a real
`selector`, so a decision still needs one. Take a snapshot when a decision is due; do not
turn it into a page poll.

- Supply `--max-items` generously. The cap is the observation, so a low cap hides targets —
  and a small value is not a reliable way to keep the observation short: measured against a
  live store, `--max-items 3` and `--max-items 5` both returned 10 elements while `20`
  returned 20.
- A `ref` belongs to one store, tab, and page, and expires on reload or any URL change.
  Re-snapshot after a navigation instead of reusing `ref`; `--target-id` picks the tab in a
  multi-tab store.
- Off-canvas elements are dropped the same way the browser-side reader drops them. An
  element hidden with `left:-9999px` has real width and height, so geometry is the only
  signal; without it, "skip to content" links become targets.
- A `<select>` is offered as a dropdown to open, because the snapshot names it without
  listing its options. Add an `options` array to that element in a normalized observation
  to get the `SELECT` operation.
- A page snapshot carries no scroll position, so the service offers scrolling down only.
  Add `scroll: {"y":…, "height":…, "viewport":…}` to make both directions exact.
- Page text is synthesized from the element table when the caller supplies none; merge a
  `text` field into the payload, or send a normalized observation, for real page prose.

## Credentials

`TYPESAFE_API_KEY` is required. The process environment wins, then files fill in what is
missing, most specific first:

| Order | Location |
| --- | --- |
| 1 | the process environment |
| 2 | `./.env` |
| 3 | `~/.config/jev/.env` (or `$XDG_CONFIG_HOME/jev/.env`) |

The third location exists so a CLI started by a daemon, a GUI, or another agent finds its
credentials without a shell rc file — **an agent process does not read `~/.bashrc`**.
A missing key needs no separate check command: `decide` answers with exit `3` and a
parseable `{"error":{"type":"credential"}}`.

The decision endpoint is configuration, not a credential, but it resolves through the same
three locations: `TYPESAFE_BASE_URL` overrides the default
`https://api.typesafe.ai/v1/systemone`, and an empty value means the default. `doctor`
reports the endpoint it would use in both the `credentials` and `model` checks.

```bash
mkdir -p ~/.config/jev && cp .env.example ~/.config/jev/.env
```

## Doctor

```bash
jev doctor
```

One read-only self-check for the whole install, and the fastest way to answer "why did
`decide` exit 3?":

| Check | What it reports |
| --- | --- |
| `runtime` | the interpreter actually running, and the version this package requires |
| `install` | the version, and whether this is the editable checkout or a frozen install |
| `credentials` | **where** the key came from (process environment, `./.env`, `~/.config/jev/.env`) and **how long** it is — never the key itself |
| `model` | one minimal model request that proves the service answers |

The last check is a real request on purpose: an unreachable service and a wrong decision
look identical from the caller's side, so `doctor` asks a question, names the answer it
expects, and fails if something else comes back — a reachable endpoint answering with the
wrong model is a failure, not a pass. The request carries no page state and no goal and
costs roughly 400 input tokens.

The same run, one line per check, detail trimmed:

```json
{"status": "ok", "command": "doctor", "checks": [
  {"name": "runtime", "status": "ok", "detail": {"python": "3.13.12", "required": "3.12"}},
  {"name": "install", "status": "ok", "detail": {"version": "0.1.0", "mode": "editable"}},
  {"name": "credentials", "status": "ok", "detail": {"source": "~/.config/jev/.env", "key": {"present": true, "length": 108}, "endpoint": "https://api.typesafe.ai/v1/systemone"}},
  {"name": "model", "status": "ok", "detail": {"endpoint": "https://api.typesafe.ai/v1/systemone", "answer": "PONG", "model": "jev-1.13.0", "latency_ms": 1025}}
], "summary": {"passed": 4, "failed": 0, "skipped": 0}}
```

Exit `0` when every check passes, `3` when this install cannot decide. Same rule as
`decide`: stdout is always one parseable JSON document, and `--compact` emits it on one
line. `doctor` never reads an observation, so it needs no stdin.

## Development

```bash
uv run ruff check .
uv run pytest
uv build
```

Tests are offline: they use a captured page snapshot and a fake model call. Nothing in
`pytest` reaches the network, opens a browser, or spends money.
