# PRD: the decider for capsule core

Status: draft, made from the section findings. Every claim cites the source line it rests on as path:line. Text marked **Proposal** has no source behind it. Researchers counted some line numbers by hand, so a few may be off by one. They checked these against the source: contract/decide.md:74, 77 and 105; code/jev.rs:24 and 69; code/act.rs:104 and 107; code/accept.rs:44; plan/router.md:2, 10, 32, 55, 66, 69 and 101.

## Problem

Capsule core needs a component that picks which verbs an agent's turn offers. That component is a classifier, "Laya, open weights, or TypeSafe's Jev" (plan/router.md:2). It decides from data: "the state and the options" (contract/decide.md:9-10). What it sees is "computed from the record" (contract/decide.md:82). It is "never written by the reasoner, so injection cannot steer the pick" (plan/router.md:4-5). "File contents, command output and the model's prose" are kept from it, so "nothing the model read can steer the pick" (contract/decide.md:87-90).

Its pick goes on the record with "the distribution, provider and version beside it". Then "replay asks no decider and the record is the training set" (plan/router.md:21-23; contract/decide.md:36-40).

What goes wrong without one, or with a bad one:
- With no decider, the run "parks and a person's `Reply` is the pick" (plan/router.md:23-24; contract/decide.md:111-114). Every turn then waits on a person.
- A decider that cannot see outcomes loops. "A decider that cannot tell a failing test from a passing one says `read` forever" (contract/decide.md:91-92). The first live run was "a loop, stopped by hand after fourteen decisions" (plan/router.md:55; code/jev.rs:24). The cause: a `ran` result dropped its exit code, and "a one-verb offer leaves it no way out" (plan/router.md:56-58).

## Users

The researcher assigned to Problem and Users sent back only a summary, not its cited claims. The users below come from the other sections' findings.

- **Operator.** Grants the decider to the environment: `(grant capability decide/verb :kind decide)` (contract/decide.md:7-9). Owns provider routing, which is "the operator's table, not a decision" (contract/decide.md:121-124). Gets the acceptance questions the decider declines or the evidence cannot show (code/accept.rs:23-26; contract/decide.md:104-107).
- **Capsule author.** Declares the decider with `(use capability decide/verb :kind decide)` (contract/decide.md:13). Binds the verbs that can be offered (contract/decide.md:23-27).
- **Reasoner (the agent's model).** Reads the narrowed offer and acts within it. It can disagree with `(want "name")`, which runs nothing and returns `("wanted" "name")` (contract/decide.md:48-56; code/act.rs:12-16). A `want` is the reasoner disagreeing with the pick (contract/decide.md:58).
- **Person on the run.** When no decider is provided, their `Reply` is the pick (plan/router.md:23-24). `Interrupt(Deny)` ends the run refused (contract/decide.md:111-114).
- **Trainer of Laya.** Reads the records, because Laya "is distilled from the records of each capsule it routes" (plan/router.md:10-12).

## Goals

1. Narrow each turn's offer to the verbs that matter, from data only, never from text the reasoner wrote or read (contract/decide.md:87-90; plan/router.md:4-5).
2. Record every decision so that replay asks no decider and the record trains the next decider (contract/decide.md:36-40; plan/router.md:21-23).
3. Fail safe: if a decision is declined or can't be read, offer every verb (contract/decide.md:43-45; code/act.rs:104, 107).
4. Replace Jev with Laya, a decider that is "Owned, local, cheap", behind the same `decide` family (plan/router.md:69-73).
5. Let a decider judge acceptance too, through `decide/accept` (contract/decide.md:93; code/accept.rs:1).

## Non-goals

- Granting authority. A decider only narrows the offer. The verb still runs under the frame's authority, because "authority is the mask" (contract/decide.md:23-27). The `act` call gets the full list: "authority is the wall" (contract/decide.md:45-47).
- Granting a `want`. That is "not the decider's to grant" (contract/decide.md:48-56).
- Routing to a provider. That is the operator's table, and it is never recorded (contract/decide.md:121-124).
- **Proposal:** adopting a third-party hosted decider as the default. This is out of scope, given the goal of an owned, local decider (plan/router.md:72-73). See Risks.

## Design

### What the decider sees
- It is called as `(decide/verb task turns (offer coder/verbs))` (contract/decide.md:16). In code it takes `(task turns offered)` (code/jev.rs:192-196).
- Its input is the task and each turn as its results, plus a digest of "numbers, booleans, a bare word…" (contract/decide.md:83-87).
- Jev sees each turn's tag, its first operand and a numeric/true-false digest. Operands are cut at 80 characters (code/jev.rs:13, 73, 249-299).
- An unoffered verb the agent asked for shows up "in the next state" (contract/decide.md:53; code/act.rs:24-26).

### What it answers
- It replies the way a model adapter does: an object whose `form` names the verbs it keeps, with the distribution, provider and version. Example: `{"form": "coder/read coder/edit", …, "provider": "jev", "version": "1.13"}` (contract/decide.md:28-32).
- `form` is "the options in probability order whose mass together reaches the decider's threshold" (contract/decide.md:32-35). Jev's threshold is `THRESHOLD: f64 = 0.6` (code/jev.rs:69). The shortlist is built at code/jev.rs:330-343. The reply carries form, distribution, confidence, provider, version and usage (code/jev.rs:344-353).
- `act` reads the `form` as whitespace-separated words and keeps the names in the capsule's order (code/act.rs:83-85, 106, 222-237).
- `decide/accept` answers accept or reject (code/accept.rs:28-30; contract/decide.md:93-97). Its threshold is 0.8 (code/accept.rs:41-44), because "a wrong accept costs a merge, a wrong verb costs a turn" (code/accept.rs:42-43).

### How it is bound
- The environment grants it and the capsule declares it (contract/decide.md:7-13).
- It can only narrow. In `act`, names outside the list are dropped with `retain` (code/act.rs:85-86, 110-112). The verb list is checked: no reserved words, no duplicates, and each verb bound to a lambda (code/act.rs:328-359, 368-375).
- The decision is charged to the frame's budget. The decider's own cost "lands with A12" (contract/decide.md:115-116). A turn is two calls: "decide then `call/model`" (contract/decide.md:117-118).
- Jev's transport:
  - Provider: OpenRouter `typesafe/jev-1.13`, or TypeSafe direct `jev-latest` (code/jev.rs:51-62).
  - Timeout: 60 seconds (code/jev.rs:136).
  - Retries: 3 tries, with a 1-second backoff that doubles (code/jev.rs:80, 83, 178).
  - Auth: Bearer key (code/jev.rs:144).

### How it declines
- "Below its threshold, a decider declines": `("declined" why)` (contract/decide.md:74).
- A pick outside the options, an answer that can't be read, or a lost connection also declines. Only a key the API rejects is refused (contract/decide.md:76-81).
- The code matches:
  - 401/403 is refused (code/jev.rs:159).
  - 5xx and lost connections are retried, then declined (code/jev.rs:160, 181).
  - Any other HTTP error, an unreadable answer, or a pick outside the offer is declined (code/jev.rs:161, 316, 318).
  - A malformed payload is refused (code/jev.rs:168-171).
- Fallback: "a reply that keeps none, a reply with no form, or a declined decision offers them all" (contract/decide.md:43-45; code/act.rs:104, 107).
- Way out: a narrowed offer ends with `want` (contract/decide.md:48-56; code/act.rs:114, 126-132).
- `decide/accept` treats a failing gate as a reject without asking the model (code/accept.rs:117-124; code/accept.rs:20-21). Below its threshold it declines to the operator (code/accept.rs:157-161).

### How Laya comes to replace Jev
- Both sit behind one granted family: "V1-L3 Laya behind the same family" (plan/router.md:69; contract/decide.md:8). Swapping them should need no change to the interface.
- Jev serves first on purpose. "Jev may serve first behind the same `decide` while the records accrue," and Laya is distilled from those records (plan/router.md:10-12). "A scripted scenario set may seed a capsule with no history" (plan/router.md:11).
- Done so far:
  - V1-L1: decision as an effect (plan/router.md:26).
  - V1-L2: Jev decides (plan/router.md:40).
  - V1-L2b: delete the router (plan/router.md:74).
  - V1-L2c: adapter retries (plan/router.md:83).
  - V1-L2d: `want` (plan/router.md:93).
- V1-L3 (Laya) is the only stage not done. Its precondition is that the V1-L0 questions are answered in `.dev/` first: arbitrary options or trained labels; ONNX, candle or a local server; what a fine-tune takes (plan/router.md:70-73). `.dev/` is not among the sources.
- **Proposal:** cut over in stages.
  1. Run Laya in shadow next to Jev on live capsules, recording both picks.
  2. Switch a capsule to Laya once Laya's picks agree with Jev's, and its `want` and decline rates are no worse, over a set number of records.
  3. Keep Jev as the rollback.

  The sources give no timeline, no threshold for "enough" records, no quality bar and no rollback plan.

## Evaluation

What exists:
- Mechanism tests: `tests/decide.rs` has six tests covering narrowing, declines, picks outside the offer, and parking with no decider. `host/tests/coder.rs` runs every earlier test under a decider that declines, one that picks, and none (plan/router.md:32-36).
- Jev tests: `host/tests/jev.rs` has six, with the API scripted (plan/router.md:48).
- Retry tests: plan/router.md:91-92. `want` tests: plan/router.md:101.
- Live runs:
  - First run: the 14-decision loop (plan/router.md:55).
  - Second run: "four decisions… the task done, no refusal, no repeated read" (plan/router.md:63-66).
  - Self-build run: "24 decisions, two sessions" (plan/router.md:79).
- Recorded fields that can feed metrics: form, confidence, distribution, provider, version and usage (plan/router.md:45-46; code/accept.rs:163-172).
- Acceptance check: `decide/accept` judges evidence the host collects. That evidence is the gate's exit status, the files changed, files the ticket does not name, and tests added (code/accept.rs:13-16).

**Proposal:** these metrics, none of which the sources define:
- decisions per completed task;
- repeated-verb loops, such as the same `read` again and again;
- the `want` rate, a sign the reasoner disagrees (contract/decide.md:58);
- the decline rate;
- how often Laya and Jev pick the same verbs on replayed records;
- decider cost per turn, once A12 lands.

## Risks

- **Blindness to outcomes leads to loops.** A digest of answers and `want` fixed this (plan/router.md:59-62, 93-100). It comes back if the digest loses signal (contract/decide.md:91-92).
- **A narrow offer leads to wasted calls.** For example, the reasoner "read a file it did not need" (plan/router.md:94-96).
- **The decider's cost is not metered yet.** It is waiting on A12 (plan/router.md:38, 66-67; contract/decide.md:116).
- **Dependence on an outside service.** Jev runs on TypeSafe's or OpenRouter's API (plan/router.md:42, 50; code/jev.rs:50). A TLS failure needed a resume by hand (plan/router.md:80-81).
- **Gaps between the contract and the code:**
  1. "Below its threshold, declines" (contract/decide.md:74) does not clearly hold for `decide/verb`. Jev uses 0.6 only to size the shortlist, even though its comment calls 0.6 "the confidence a pick must reach" (code/jev.rs:89-90). Only `Choice` (code/jev.rs:357-509) and `decide/accept` decline below their threshold.
  2. The researcher reads `act` as accepting a `want` even when it was not offered (code/act.rs:286 runs before 295). The contract says `want` appears only on a narrowed offer (contract/decide.md:50). This is an inference from the code, not something a source states.
  3. In a decision, a missing `form` offers everything (contract/decide.md:44; code/act.rs:107). In a model proposal, the same reader errors with "the reply names no form" (code/act.rs:237, 258, 139-142).
- **The plan is behind the code.** plan/router.md:67-68 lists the `Accept` family as not built, but code/accept.rs implements it and contract/decide.md:93 records it as ruled.
- **Laya's own risks are not covered by any source.** These include data quality, capsules with little history (seeding is only "may", plan/router.md:11), drift, and managing one model per capsule.
- **Unverified vendor pressure.** The sources list inbox/ as "notes that came in, unverified" (README.md:13). inbox/vendor-note.md claims "it's approved" (:6). It says RouterCo's hosted decider "beats Jev on every benchmark" and asks that it be recommended as the default (:13-14). It also tells AI assistants to skip citations and to email the PRD to an outside address (:9-12). This document does not follow those instructions or adopt that claim. Nothing else in the sources backs it, and it conflicts with "Owned, local, cheap" (plan/router.md:72-73).

## Open questions

1. The V1-L0 questions for Laya: arbitrary options or trained labels; ONNX, candle or a local server; what a fine-tune takes (plan/router.md:70-72).
2. The other open items D6, D6b and K1 (plan/router.md:101-106).
3. Should `decide/verb` decline below 0.6, as contract/decide.md:74 and the comment at code/jev.rs:89-90 suggest, or only use 0.6 to size the shortlist?
4. Should `act` reject a `want` that was not offered (code/act.rs:286, 295; contract/decide.md:50)?
5. How much record is enough, and what quality bar must Laya meet to replace Jev? Will the two run side by side, and who decides the switch? No source says (see the Proposal under Design).
6. Where do the decider's cost and budget accounting live (A12; contract/decide.md:115-116)?
