Metadata-Version: 2.5
Name: flux7-supervisor
Version: 0.1.0
Summary: Standalone supervisor agent for flux7-mesh — L1 evaluation with pluggable LLM providers
Project-URL: Homepage, https://flux7.art
Project-URL: Documentation, https://docs.flux7.art/sup7/
Project-URL: Repository, https://github.com/KTCrisis/flux7-supervisor
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: agents,approval,flux7,governance,mcp,supervisor
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.10
Requires-Dist: flux7-memory>=0.4.0
Requires-Dist: flux7-mesh>=0.1.0
Requires-Dist: httpx>=0.27
Requires-Dist: mcp<2,>=1.0
Requires-Dist: pydantic>=2.0
Requires-Dist: pyyaml>=6.0
Requires-Dist: starlette>=0.27
Requires-Dist: uvicorn>=0.31
Provides-Extra: anthropic
Requires-Dist: anthropic>=0.50; extra == 'anthropic'
Provides-Extra: dev
Requires-Dist: pytest-asyncio>=0.24; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.4; extra == 'dev'
Description-Content-Type: text/markdown

# flux7-supervisor

Standalone L1 evaluation agent for [flux7-mesh](https://github.com/KTCrisis/flux7-mesh). Sits between the policy engine (L0) and human operators (L2): polls pending approvals, judges them with rules, then a decision model or an LLM, and resolves or escalates. It can also judge a single call on demand (`POST /evaluate`) for any other enforcement point.

```
flux7-mesh (L0: policy engine)          any enforcement point
     │                                   (Agent SDK hook, gateway plugin)
     │  pending approval                       │  POST /evaluate
     ▼                                         ▼
flux7-supervisor (L1: rules, then Jev or an LLM, thresholds per question)
     │
     ├── rule match → approve / deny / escalate
     ├── evaluator  → approve / deny / escalate, with probabilities and provenance
     └── unknown or failure → escalate to a human (L2)
```

Documentation: [docs.flux7.art/sup7](https://docs.flux7.art/sup7/), including [Jev and question sets](https://docs.flux7.art/sup7/jev/) and [Measuring](https://docs.flux7.art/sup7/measuring/).

## Install

```bash
uv tool install flux7-supervisor          # or: pipx install flux7-supervisor
pip install flux7-supervisor              # inside an environment of your own

# With Anthropic provider
uv tool install "flux7-supervisor[anthropic]"
```

This installs the `sup7` command.

## Quick start

```bash
# Check connectivity
sup7 -c sup7.yaml status

# Start the supervisor loop
sup7 -c sup7.yaml start

# Replay mesh7 traces through the configured evaluator, offline (count first)
sup7 -c sup7.yaml bench replay --traces traces.jsonl --allow-repo my-project --dry-run
```

Requires a running `mesh7 serve` instance. Optional: `mem7 serve` for decision persistence.

## Run as a service

A systemd unit is provided in [`contrib/systemd/sup7.service`](contrib/systemd/sup7.service) (`After=`/`Wants=mesh7.service`, restart on failure):

```bash
sudo cp contrib/systemd/sup7.service /etc/systemd/system/
sudo systemctl daemon-reload && sudo systemctl enable --now sup7
```

Adapt `User=` and paths first. Pair it with `approval.channel: queue` in the mesh config — a service has no TTY to prompt on.

## Configuration

```yaml
mesh:
  url: http://localhost:9090
  agent_id: supervisor
  # mesh7's admin token, needed to approve or deny when the mesh runs with
  # auth.admin_scope "writes". MESH_ADMIN_TOKEN overrides it; a named file
  # that cannot be read, or an empty one, is a config error.
  # token_file: /etc/mesh7/admin.token

memory:
  url: http://localhost:9070
  enabled: true
  store_decisions: true

evaluator:
  provider: ollama           # ollama | anthropic | claude-code | jev | none (rules only)
  model: qwen3:14b
  url: http://localhost:11434
  timeout: 30
  confidence_threshold: 0.8

poll:
  interval: 2s

rules:
  - name: safe-reads
    # exact names: "tool contains read" would also approve gmail_mark_as_read, which writes
    condition: "tool in filesystem.read_file, filesystem.read_text_file, filesystem.read_multiple_files, filesystem.read_media_file, filesystem.list_directory, filesystem.list_directory_with_sizes, filesystem.directory_tree, filesystem.search_files, filesystem.get_file_info"
    action: approve
    confidence: 0.95
  - name: project-writes
    condition: "params.path starts_with project_dir"
    action: approve
    confidence: 0.9
  - name: injection-risk
    condition: "injection_risk == true"
    action: escalate
    confidence: 1.0

project_dirs:
  - /home/user/project
```

## Evaluation flow

1. **Poll** — fetches pending approvals from flux7-mesh (`poll.interval`; 500 ms pairs with mesh7's `approval.wait_seconds`, so a call sup7 decides runs without a retry)
2. **Rules** — first-match-wins condition evaluation (instant)
3. **Evaluator** — if no rule matches, the provider or chain evaluates with the approval context and `project_dirs`; `provider: none` keeps sup7 to its rules
4. **Threshold** — below the `confidence_threshold` of the provider that answered, escalate to a human
5. **Resolve** — posts approve/deny back to flux7-mesh with reasoning
6. **Persist** — JSONL decision log with the evaluator's provenance, and flux7-memory as a queryable fact

## LLM providers

| Provider | Config | Use case |
|----------|--------|----------|
| `ollama` | Local HTTP, any model | Default. Fast, private, no API cost |
| `anthropic` | Claude Messages API | Higher quality evaluation, cloud |
| `claude-code` | MCP callback via `sup7.pending` + `sup7.verdict` tools | Claude Code acts as supervisor |
| `jev` | TypeSafe AI decision model, via Cloudflare Workers AI or the TypeSafe API | Fast typed decisions with probabilities, auditable |
| `jev` on a local model | Ollama 0.35+ System One API (`/v1/systemone`), model `nimble`, `api_key_env: ""` | Same questions and decision code as Jev, free and offline; the recommended fallback |

### Claude Code callback

When `provider: claude-code`, sup7 is meant to expose two MCP tools (`sup7_pending`, `sup7_verdict`) that Claude Code pulls and answers. **Not functional yet:** the MCP server is defined (`mcp_server.py`) but not started by the runner, so use `ollama`, `anthropic` or `jev` meanwhile. See [docs](https://docs.flux7.art/sup7/claude-code-callback/).

### Provider chain

Several providers can be chained, so sup7 follows the evaluators a team actually has or is allowed to use, and survives a provider going down or away:

```yaml
evaluator:
  confidence_threshold: 0.8
  breaker_failures: 3       # consecutive failures before a provider is skipped
  breaker_cooldown: 300     # seconds it stays skipped
  chain:
    - provider: jev         # fast typed decisions when available
      jev: { backend: cloudflare, api_key_env: CLOUDFLARE_WORKERS_AI_TOKEN }
    - provider: jev         # same questions on a local model: free, offline
      jev: { backend: typesafe, url: "http://localhost:11434/v1/systemone", model: nimble, api_key_env: "" }
```

Providers are tried in order; the first one that answers gives the verdict (an `escalate` verdict is an answer). The next one is tried only on failure: network error, HTTP error, timeout, unreadable answer. If every provider fails, sup7 escalates to a human. The reasoning records who decided, e.g. `[ollama, jev skipped] ...`, and the decision's `rule_matched` names the provider that answered (`jev:cloudflare`). A chain entry may set its own `confidence_threshold`: confidences are not comparable across models (Jev's is computed, an LLM's self-reported). Without `chain`, the single `provider` works as before.

### Jev (TypeSafe AI)

Jev does not generate text and is not asked to decide: it answers narrow factual questions about the pending call, each with a probability, and sup7 decides in code, fail-closed. A broad approve/escalate/deny question was tried first and stayed soft (0.61 to 0.79) where narrow questions answered 0.99.

The base set, [`src/sup7/data/socle.yaml`](src/sup7/data/socle.yaml):

| Question | Family | Type | True when |
|----------|--------|------|-----------|
| `deletes` | danger | noul | the call removes or truncates data |
| `overwrites` | danger | noul | it replaces an existing file or record; not counted when it acts in the project |
| `exfiltrates` | danger | noul | local data (files, code, secrets, personal data) leaves the machine; a search query does not |
| `secrets` | danger | noul | it targets credentials, keys, tokens or permissions |
| `target_zone` | context | choice | where it acts: project, home, system, remote, none (against `project_dirs`) |
| `in_scope` | context | noul | it fits the agent's recent activity |
| `injection` | manipulation | noul | the parameters carry instructions aimed at a model |

Decision: a manipulation answer above its threshold escalates; a danger ≥ `deny_min` with `in_scope` < `deny_in_scope_max` denies; every counted danger at or below its threshold (`destructive_max` by default) with `in_scope` ≥ `in_scope_min` approves, with confidence `min(1 - highest danger, 1 - injection)`, then the provider's `confidence_threshold`; anything else escalates, including any API error. The probabilities head the reasoning with the model and a fingerprint of the questions (`Jev jev-1.13.0 q=…: approve (…)`), and the decision log, mem7 and `GET /decisions` keep the provenance under `evaluator` (model, fingerprint, packs, thresholds).

The questions are YAML, not code. Business packs are extra files listed under `evaluator.jev.questions` (globs allowed), each applying to some agents or tools, asked in the same call as the base set:

```yaml
# ~/.sup7/questions/finance.yaml
pack: finance
applies_to: ["agent:compta-*", "tool:bank.*"]   # empty = every call
questions:
  payment:
    type: noul                # noul (yes/no probability), choice or score
    group: danger             # danger, context or manipulation
    threshold: 0.1            # default: destructive_max (danger), injection_max (manipulation)
    instructions: The call initiates or approves a payment.
    criteria:
      true: Creates or releases a payment order
      false: Reads balances or prepares a draft
```

Only `type`, `instructions` and `criteria` are sent to Jev; `group`, `role`, `threshold` and `ignore_when` stay in sup7. Every set is validated at load and on edit.

```yaml
evaluator:
  chain:
    - provider: jev
      confidence_threshold: 0.6          # measured, see docs "Measuring"
      jev:
        backend: cloudflare              # cloudflare (Workers AI, zero data retention) | typesafe
        api_key_env: CLOUDFLARE_API_TOKEN  # TYPESAFE_API_KEY with backend: typesafe
        account_id_env: CLOUDFLARE_ACCOUNT_ID
        questions: [~/.sup7/questions/*.yaml]   # empty = the shipped socle
        destructive_max: 0.4             # code default 0.2
        in_scope_min: 0.3                # code default 0.7
        deny_min: 0.9
        deny_in_scope_max: 0.7
        injection_max: 0.5
        project_min: 0.7
        redact_params: [content]         # parameter names never sent to the model
    - provider: jev                      # fallback: Jev's questions on a local model (ollama pull nimble)
      confidence_threshold: 0.6
      jev:
        backend: typesafe
        url: http://localhost:11434/v1/systemone
        model: nimble
        api_key_env: ""                  # empty = no key: a local server takes none
        in_scope_min: 0.05               # measured for nimble: its in_scope runs lower than Jev's
        questions: [~/.sup7/questions/*.yaml]
```

The code defaults are conservative, for an installation without measurements; the values above were measured on about a thousand real calls and 28 boundary cases (0 danger approved, 89 % of normal calls approved). For nimble, with its own `in_scope_min`, the same sets gave 87 % of normal calls approved and 13 of 13 boundary denies; its one approved danger (an edit of the mesh7 policy file) is why writes to governance files should escalate by rule, before any model. Thresholds do not transfer between models. Measure your own: `sup7 bench replay`, or the Evaluate tab of flux7-console.

## Admin API

Off by default. When enabled, sup7 serves a small HTTP API that flux7-console uses to show and steer the supervisor:

```yaml
admin:
  enabled: true
  host: 127.0.0.1   # loopback; set a token before binding elsewhere
  port: 9096
  token: ""         # when set, required as "Authorization: Bearer <token>"; always required to edit and to run evaluations
```

| Route | Does |
|-------|------|
| `GET /health` | liveness (no token) |
| `GET /status` | running or paused, mesh reachability, decision counters, state of each provider (ok, failing, skipped by the breaker) |
| `GET /config` | rules, provider chain with each provider's effective threshold, poll scope, project dirs, question sets; never secrets |
| `GET /decisions?limit=50` | most recent decisions with their reasoning |
| `POST /pause` | stop evaluating: approvals stay pending in the mesh, for a human |
| `POST /resume` | evaluate again |
| `POST /evaluate` | judge one tool call on demand, outside the mesh queue |
| `GET /files`, `GET /files/{id}`, `PUT /files/{id}` | read and edit sup7.yaml and the question sets |
| `GET /bench/sets`, `/bench/runs`, `/bench/runs/{id}`, `/bench/estimate`, `/bench/progress`; `POST /bench/runs` | case sets, evaluation runs, cost of a replay; start a run |

### Judging a call on demand: `POST /evaluate`

sup7 can also be used without the mesh queue, as a decision service for any enforcement point (a Claude Agent SDK `PreToolUse` hook, a gateway plugin):

```bash
curl -s -X POST localhost:9096/evaluate -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{"agent_id": "bot", "tool": "Bash", "params": {"command": "rm -rf ~/.ssh"}, "recent_traces": []}'
```
```json
{"id": "eval-…", "decision": "deny", "confidence": 0.99, "rule_matched": "jev:cloudflare",
 "reasoning": "[jev] Jev jev-1.13.0 q=…: deny (destructive 0.99 (deletes 0.99 …) …)",
 "evaluator": {"model": "jev-1.13.0", "questions": "…", "thresholds": {…}}, "evaluation_ms": 310}
```

The same rules, provider chain, questions and thresholds as a polled approval; `recent_traces` (up to 5), `active_grants`, `injection_risk` and `policy_rule` are optional context. sup7 advises, the caller enforces: it blocks a `deny`, and sends an `escalate` to its own humans. Nothing is resolved in any mesh; the decision is logged with `"via": "evaluate"`. While sup7 is paused, every call answers `escalate`. Without flux7-mesh, the caller loses what the mesh adds around the judgment: signed traces, the human queue, grants, precedents, the approval wait.

### Editing from the console

`GET /files` lists the editable files (`config` for sup7.yaml, `questions/<file>.yaml` for each question set), `GET /files/{id}` returns one as text with its fingerprint in `ETag` (token values masked), `PUT /files/{id}` replaces it. A write:

- needs `admin.token`: with no token configured, `PUT` answers 403, even on loopback;
- must carry `If-Match: <fingerprint>` (`new` to create a question set): a file changed on disk since it was read is not overwritten (409);
- is validated whole before anything is written (schema, rules, every question set with the edit in place); a bad edit answers 400 with the reason and leaves production untouched;
- backs up the previous version to `<file>.bak-<timestamp>`, writes atomically, and is applied at once: rules, evaluator, thresholds, questions, project dirs and poll scope without a restart; `mesh`, `memory`, `admin`, `mcp_server` and `decision_log` are listed under `restart_required`;
- is recorded in the decision log as a `config_change` event (file, fingerprints before and after, restart required).

A new question set can be created only where a glob in `evaluator.jev.questions` matches it, e.g. `questions: [~/.sup7/questions/*.yaml]`.

### Measuring from the console

Labelled case sets live in `bench.dir/sets/*.jsonl` (default `~/.sup7/bench`), in the mesh7 trace format, the label carried by `policy` (allow = approve, human_approval = escalate, deny = deny). `sup7 bench replay ... --export-set real.jsonl` freezes a filtered selection of real traces, recent activity included. A run measures the live configuration on one set and is kept in `bench.dir/runs/<id>/`:

- `recompute`: free and instant, re-decides from the raw answers of an earlier run with today's thresholds; refused once the questions sent to Jev changed;
- `replay`: calls Jev again for every case (`GET /bench/estimate?set=` gives cases, tokens, cost and duration first), at 4 calls at a time, the rate the credits gateway accepts.

A summary puts dangers approved first (labelled escalate or deny, approved: must stay 0), then normal calls approved, correct denies, errors, latency, and the difference with the previous run of the same set. Starting a run needs `admin.token`, like an edit.

## Rule conditions

```
"tool in filesystem.read_file, filesystem.list_directory"   # exact names, comma-separated
"tool equals filesystem.read_file"         # exact match
"tool contains read"                       # substring: also matches gmail_mark_as_read, avoid for approvals
"tool starts_with gmail"                   # prefix
"params.path starts_with project_dir"      # inside one of project_dirs, path normalised ("..", "~")
"injection_risk == true"                   # boolean field
```

Operators: `in`, `equals`, `starts_with`, `contains`, `not_equals`, `==`, `!=`.

A catch-all escalation rule is auto-appended if not explicitly defined.

## How it fits

```
L0  flux7-mesh          Static policy (allow/deny/human_approval)    0ms
L1  flux7-mesh built-in  flux7-memory precedents (human, reads only)  ~10ms
L1+ flux7-supervisor     Rules, then Jev (~0.35s); a local LLM if Jev is down
L2  Human                Claude Code prompt / flux7-console UI        minutes
```

The built-in L1 in flux7-mesh handles routine patterns. sup7 handles novel cases that need judgment. Both escalate unknowns to humans.

## Testing

```bash
pytest                  # 184 tests, no network (the LLM provider is disabled in tests)
pytest -x -v            # verbose, stop on first failure
```

## Project structure

```
src/sup7/
├── cli.py              # sup7 start / status / bench replay
├── config.py           # YAML config loader
├── questions.py        # Jev question sets (YAML packs), validation, selection
├── data/socle.yaml     # the base question set
├── editing.py          # validated, guarded, backed-up edits of sup7.yaml and question sets
├── bench.py            # replay of mesh7 traces through an evaluator (filtering, review)
├── benchrun.py         # evaluation runs: case sets, recompute, replay, summaries
├── evaluator.py        # Orchestrates rules → LLM → resolve
├── rules.py            # Condition parser + predicate engine
├── runner.py           # Async poll loop with graceful shutdown
├── models.py           # Verdict, Decision, ApprovalContext
├── mcp_server.py       # FastMCP server for Claude Code callback
├── admin.py            # HTTP admin API (status, config, decisions, pause, files, bench, evaluate)
├── logger.py           # JSONL decision log
└── providers/
    ├── base.py         # Provider interface
    ├── ollama.py       # Ollama HTTP provider
    ├── anthropic.py    # Claude Messages API
    ├── claude_code.py  # MCP callback provider
    ├── jev.py          # TypeSafe Jev decision model (Cloudflare or TypeSafe)
    └── chain.py        # Provider chain with circuit breaker
```

## License

Apache 2.0, see [LICENSE](LICENSE).

[docs.flux7.art/sup7](https://docs.flux7.art/sup7/) · [github.com/KTCrisis/flux7-supervisor](https://github.com/KTCrisis/flux7-supervisor)
