Metadata-Version: 2.5
Name: stratyfy-cli
Version: 0.4.1
Summary: The pre command: describe a database, train a PRE model, read its rules
Requires-Python: <4,>=3.11
Requires-Dist: attrs>=23
Requires-Dist: httpx<1,>=0.27
Requires-Dist: psycopg[binary]<4,>=3.1
Requires-Dist: python-dateutil>=2.8
Requires-Dist: sqlalchemy<3,>=2.0
Requires-Dist: typer>=0.12
Description-Content-Type: text/markdown

# pre-cli

The `pre` command. A coding agent runs it to describe the user's database, train a PRE model, read its rules and
score cases.
It talks to the PRE API through `packages/pre-api-client`. It never imports PRE. Its sentences come from
`packages/pre-words`, so they match the report.

## Install

```bash
pipx install stratyfy-cli          # or: uv tool install stratyfy-cli
```

For development, run `poetry install` in this folder, then `poetry run pre --help`. `pre --version` prints the
version.

The wheel and the sdist bundle `pre_api_client`, `pre_words` and `pre_report` from `packages/`, so the install
needs nothing else from this repo. `hatch_build.py` copies them in at build time. The CLI uses hatchling to build,
because poetry-core cannot include files from outside this folder. Poetry still manages the development env.

## Commands

Each command prints a short text result and a next step. Add `--json` to get one JSON object instead. On
failure the command exits with 1 and prints `code`, `message` and `next_step`.

| Command | What it does |
| --- | --- |
| `pre login [--no-wait] [--api-url <url>]` | Signs in as `gh auth login` does. It prints a link and a code. The user signs in with an email code and approves the device. Then it writes a new key for this machine to `.pre/config.json`. With `--no-wait` it prints the link and exits; the next `pre login` waits for that same sign-in. That way an agent can show the user the link first. The default API is `https://api.stratyfy.ai/v1`. |
| `pre logout` | Revokes the key on the API and removes it from `.pre/config.json`. If the API does not answer, it still removes the key and names its id for an admin to revoke. |
| `pre try [name]` | Lists the sample apps, or downloads one into `./<name>/`: its SQLite database, a README, a copy of this folder's key, and a `CLAUDE.md` and the skill so Claude Code started there knows what to do. |
| `pre keys list` | Lists the workspace's live keys. The key in use here is marked. |
| `pre keys create <name> [--to-file <path>]` | Makes a key for a job or an app, not tied to this machine. It is shown once. Inside a coding agent it is written only to a file, never to the screen. |
| `pre keys revoke <id>` | Revokes one key. It stops working at once, everywhere. |
| `pre usage [--days 30]` | The workspace's use per day: rows scored and calls. |
| `pre delete-data [--yes]` | Deletes every dataset and model in the workspace, on the API. It asks first and refuses inside a coding agent. Keys, outcomes and local files stay. See the docs page "Your data". |
| `pre delete-account` | Prints the page that deletes the account and everything in it, after a code sent by email. No key needed. |
| `pre version` | Prints the version, as `pre --version` does. |
| `pre guide` | Prints the method a coding agent follows, from set-up to the report. It is the plugin's `onboard` skill, bundled in the CLI. |
| `pre init --api-key <key> [--api-url <url>]` | Writes a key you already have to `.pre/config.json`. The key can also come from `PRE_API_KEY`. |

`PRE_API_KEY` and `PRE_API_URL` win over `.pre/config.json` for every call, so a job on a server needs no config. `pre logout` still revokes only the folder's own key.
| `pre connect <db>` | Describes a SQLite or Postgres database: tables, row counts, columns, types, null share, sample values and foreign keys. It opens the database read-only. |
| `pre setup claude [--yes] [--dry-run] [--local]` | For Claude Code. Run it in your own terminal. It writes the PRE skill to `.claude/skills/pre-onboard/`, and adds the API host and a remote Postgres host to Claude Code's sandbox network allowlist. It shows the change and asks first. It refuses to write when an agent runs it. See [Sandboxes](#sandboxes). |
| `pre status` | Checks that the API answers and accepts the key, and names the workspace. Lists the datasets and models this folder made. |
| `pre outcome define --name <slug> --sql <query> [--case account --cases accounts --event churn --event-past churned]` | Runs the outcome query and stores it. The query returns one row per case: the case id, then a 0/1 label. It refuses no rows, a label that is not 0 or 1, a repeated case id and a label that is always the same. The words go into every sentence the CLI prints. It guesses each word you leave out from the column names and says so. |
| `pre outcome show` | Lists the stored outcomes and their queries. |
| `pre extract --outcome <slug> --features <query>` | Runs the features query (the case id, then the features), joins it to the label and writes `.pre/data/<slug>.csv`. Nothing is sent. |
| `pre extract --outcome <slug> --upload [--refresh]` | Sends the CSV the dry run wrote, unchanged. With `--refresh`, or with no CSV yet, it runs the queries again first. |
| `pre train --outcome <slug> [--max-rules N] [--max-conditions N] [--no-wait] [--timeout S]` | Trains on the outcome's uploaded dataset. Every column but `id` is an input. It waits until the model is ready or failed and prints progress to stderr. When ready, it says how well the model predicts on held-out cases: the AUC and how many times the average rate the top 10% hold. |
| `pre rules --outcome <slug>` or `pre rules --model <id>` | Prints the rules as sentences, strongest first, with points and direction. The baseline shows as the starting rate. `--json` gives the API's rules. |
| `pre predict --outcome <slug> [--features <query>]` | Scores cases with the outcome's model. The query returns the case id, then the model inputs as of today, for example for the accounts still active. Without `--features` it scores the training cases as of their snapshot date and says so. It writes `.pre/data/<slug>.scores.csv`: the case id, the score, the points, the rules that fired as sentences and `act`, highest score first. `act` is yes or no when `pre threshold` chose a threshold for this model, and empty otherwise. It also writes `.pre/data/<slug>.scores.json` for `pre report`: the same scores with the rule ids that fired. It refuses a query that lacks an input or repeats a case id. |
| `pre threshold --outcome <slug> --value <v> [--cost <c>]` | Asks the API for the score at which acting on a case pays most, on the held-out cases. `--value` is what acting on a case that has the outcome gains; `--cost` is what one action costs. It prints the cut-off and the cut-offs for the top 5, 10, 20, 50 and 100 percent, and stores the threshold on the outcome for `pre predict`. It sends nothing from the database. |
| `pre report --outcome <slug> [--actions <file.json>] [--labels <file.json>] [--top N]` | Writes `.pre/reports/<slug>.html` and `.pre/reports/<slug>.csv` from the scores `pre predict` saved. It sends nothing. The page says how well the model predicts, lists the top rules as sentences and the `N` cases most likely to have the outcome, with their reasons. The CSV holds the same cases. `--actions` maps a rule id to one suggested action. `--labels` maps an input column to a plain label. It refuses a rule id or a column the model lacks. When the scores came from the training cases, the page says so. When `pre predict` used a threshold from `pre threshold`, the page and the CSV list every case at or above it instead of the top `N`, and the page names the value and the cost. |

`<db>` is a SQLite file path, `sqlite:///path`, or `postgresql://user:password@host:5432/db`. Use `--schema` for
a Postgres schema other than `public`.

A `<query>` is the SQL itself or a path that ends in `.sql`. It must be one SELECT or WITH statement. The CLI
opens SQLite with `mode=ro` and runs Postgres in a read-only transaction, so a query cannot change data. For
Postgres, a database user with read access only is safer still.

## What leaves the machine

The CSV that `pre extract --upload` sends, and the rows that `pre predict` sends. Before it writes the CSV, `pre extract`:

1. Drops a column when every word of its name is an identifier word and one of them is a core word. Core words
   are `email`, `phone`, `mobile`, `ip`, `address`, `street`, `ssn`, `name` and `username`. The other identifier
   words are `mail`, `number`, `first`, `last`, `full`, `user`, `contact`, `home` and `work`. So `work_email` and
   `first_name` go, and `plan_name` and `address_verified` stay.
2. Drops a column when a text value holds an email address or looks like a phone number.
3. Replaces the case id with an HMAC of it in a column named `id`. The key is in `.pre/secret` (mode 600).
   `.pre/data/<slug>.ids.csv` maps each hash back to the case id. It stays on this machine.

The output names each dropped column and the reason.

`pre predict` sends each case as the same HMAC of its id and the model's inputs, in batches of 1,000. It sends no
other column. It maps the hashes back to the case ids in memory.

## Sandboxes

`pre setup` writes the smallest change it can. It merges into the file, so a second run changes nothing.

It writes `.claude/settings.local.json`, key `sandbox.network.allowedDomains`. That is Claude Code's personal
settings file; the team's `.claude/settings.json` stays as it is. Do not commit it. For a Postgres on this machine, `--local`
also sets `sandbox.network.allowLocalBinding`, which opens every local port. It never sets `allowAllUnixSockets`.
Claude Code is the only agent at launch; others are on the roadmap. It also writes the onboard skill to
`.claude/skills/pre-onboard/SKILL.md`. It records the copy's SHA-256 in `.pre/config.json`, and later replaces only
that copy: a skill you edited stays as it is. Commit the skill to share it with the team. Its source is
`tools/agent-plugin`.

These key names come from secondary sources. They live in `pre_cli/sandbox.py`.

## Local config

`.pre/config.json` holds the API URL, the key and its id, the database URL, the outcome queries and the dataset
and model ids that later commands create. While a `pre login --no-wait` waits, it also holds that sign-in. The API
can also list every dataset and model in the workspace. Each command that writes the config also writes
`.pre/.gitignore`, so git ignores the folder. Set `PRE_DIR` to use another folder.

## Tests

`scripts/check pre-cli` runs ruff and pytest. The `pre status` tests start a Prism mock of
`services/pre-api/openapi.yaml` with `npx`. They skip when `npx` is missing.

`tests/test_package.py` builds the sdist, a wheel from that sdist and a wheel from this folder. It fails if one
holds a file outside `pre_cli` and the three bundled packages, a `services`, `apps` or `ground_truth` path, a
`seed.py` or `check.py`, or the name of the PRE library. PRE must never reach PyPI.

## Release

The CLI is on PyPI as `stratyfy-cli`. The command it installs is `pre`. `.github/workflows/release-cli.yml`
publishes it with trusted publishing, so no PyPI token is stored.

1. Set the new `version` in `pyproject.toml`. It is the only place that holds the version.
2. Merge that change to `master`. CI runs `scripts/check pre-cli`, which builds the package and checks its files.
3. Tag the merge commit and push the tag: `git tag stratyfy-cli-v<version>`, then
   `git push origin stratyfy-cli-v<version>`.
4. The workflow checks that the tag matches the version, builds, installs the wheel and runs `pre --version`. Then
   it publishes in the GitHub environment `pypi`.

Users install it with Python 3.11 or later: `pipx install stratyfy-cli`.
