diff --git a/CHANGELOG.md b/CHANGELOG.md index 853f0ca..427a4b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,28 @@ All notable changes to LLeMbas CLI. The format follows ## [Unreleased] +Harness spec 3.9.0: the model last used in a directory, the two types of schedule, a permission rule set right. + +### Added + +- **Harness spec 3.9.0: the model last used in a directory** (`harness/acp.md`). A new agent + session in a directory starts on the model last used in exactly that directory — from the + terminal, the web, the desktop app or a phone — instead of the server's default. The memory is + this machine's (`models.json` in the state directory) and the server keeps no record of it. + `_lembas/directories` tells the server each directory's model (`model`, capability + `directory_model`) and answers for one directory alone with `list: false`. +- **Harness spec 3.9.0: schedules are Chat or Agentic** — an Agentic schedule runs on one linked + computer in one directory with the agent's tools, on the model the schedule names or else the + one last used in that directory; a scheduled run never changes what the directory remembers. + +### Fixed + +- **`.env.example` is read without asking after a broad allow, as before it.** With `read: allow` + made in a menu, by an "always" answer or in a project, the defaults' `*.env.*: ask` and + `*.env.example: allow` both counted as narrower rules and the stricter won, so the allow + *started* a question about every example file. A narrower rule that a later one narrows again + is now set aside (`harness/permission/rules.md` §2, spec 3.9.0); `.env` and `.env.local` still ask. + ## [2.0.0-beta.11] — 2026-11-13 Harness spec 3.8.1: the text that tells a model when to write a page and when to show a file. diff --git a/harness/README.md b/harness/README.md index c733e1e..d1c20cc 100644 --- a/harness/README.md +++ b/harness/README.md @@ -31,11 +31,11 @@ page *Harness-parity* in LLeMbas CLI's wiki. | `progress.md` | Progress (3.4.0): the thirteen phases every client shows the same way, the data each carries, their texts, and where they are sent | | `reports.md` | Reports (3.8.0): a schedule's run files a report with `report_write`, or leaves a history line; notify-when (`always`, `when`, `never`) and the instruction for each; what a run leaves; the retired `[SILENT]`; a run on a linked computer | | `pages.md` | HTML pages in answers (3.8.0, experimental): the `html-view` fence, what the model is told, the note and the text projection for a reader that cannot show a page, when pages are offered, the session note, the frame | -| `permission/rules.md` | Which permission rule decides (3.8.0): the layers, the candidate, the floors a later-made rule gives way to, and what making a rule does | +| `permission/rules.md` | Which permission rule decides (3.8.0): the layers, the candidate, the floors a later-made rule gives way to (3.9.0: a narrower rule that a later one narrows again is set aside), and what making a rule does | | `display.md` | Display (3.7.0): one display in every client — a tool call's title, its four states, the duration and the facts beside it, the kinds of block; how `stopped` and `failed` stay on a finished reply; what the approval, question and plan cards offer; numbers in the client's locale | | `quirks.json` | What real servers do, and how both harnesses cope | | `conformance/.json` | Cases with their expected results, run by both test suites | -| `acp.md` | The link between them: ACP plus every `_lembas/*` method, field and capability | +| `acp.md` | The link between them: ACP plus every `_lembas/*` method, field and capability; scheduled work on a linked computer (3.5.0) and the two types of schedule, Chat and Agentic (3.9.0); the model last used in a directory (3.9.0) | ## Vocabulary @@ -78,6 +78,38 @@ page *Harness-parity* in LLeMbas CLI's wiki. approval in plan mode and answers its `unavailable` text outside it. Their permission key is `plan` (`permission/defaults.json`), which a rule written for the former `todo` key still reaches. +## 3.9.0 + +Written ahead of the implementations; the CLI carries it in the same round. A new capability and +a field, a rule amended, no tool and no text for a model. + +- **The model last used in a directory** (`acp.md`, section of that name; capability + `directory_model`; `conformance/last_model.json`). A new agent session in a directory starts on + the model last used in exactly that directory — from the terminal, the web, the desktop app or + a phone — instead of the instance's default. The memory is the device's and stays on it + (`models.json` in the CLI's state): the server keeps no record. The device writes it when a + person's turn begins and when the person changes the session's model; never for a subagent, a + turn the harness started, a command's own model, a one-shot run or a schedule's session. A + session of the device's own starts by: the model asked for, the project's setting, **the + directory's model** (while it is served), the global setting, the instance's default. + `_lembas/directories` carries it — `model` on each `Dir` and on the answered directory — and + takes `list: false` to describe one directory without listing it. The client's rule: a new + agent chat on (device, directory) with no model chosen starts on that model when the person + still has it, else as before; an older device sends no `model`, which reads as *as before*. +- **Schedules have two types, Chat and Agentic** (`acp.md` *Scheduled work on a linked + computer*): Agentic is a schedule with `runs_on` — one device, one directory, the agent's tools + there — and Chat one without; no new field. **The run's model** of an Agentic schedule: the + model the schedule names, else the model last used in its directory on that device (asked of + the device as the run starts) when the person still has it, else as before. **A scheduled run + never becomes the directory's model.** A device that is away was already specified (3.5.0: the + run waits, then is missed); the model is resolved only once it is linked. +- **Permission rules** (`permission/rules.md` §2 rule 2; six cases in `conformance/rules.json`): + among the narrower rules a later-made candidate gives way to, one that a later rule narrows + again is set aside. The defaults ask about `*.env.*` and then allow `*.env.example`; after a + broad `read: allow` both counted and the strictest won, so making the allow *started* asking + about `.env.example`. Now the example file is read without asking after it as before it. + LLeMbas: the same change in its rule lookup, if it has the same floor. + ## 3.8.1 A text. `prompts/blocks/html-pages.md` (`pages.md` §2) now says which of the two ways of diff --git a/harness/VERSION b/harness/VERSION index f280719..a5c4c76 100644 --- a/harness/VERSION +++ b/harness/VERSION @@ -1 +1 @@ -3.8.1 +3.9.0 diff --git a/harness/acp.md b/harness/acp.md index 271ef18..f853425 100644 --- a/harness/acp.md +++ b/harness/acp.md @@ -68,6 +68,7 @@ change to the protocol is a change here first, then in both projects. | `agents` | spec 3.4.0: the session's Subagents setting `{ min, max }` is read from `_meta.lembas.agents` on `session/new`, `session/prompt` and `_lembas/configure` (*Subagents in an agent chat*, below) | | `tree` | spec 3.4.0 (`subagents.md` §5–§13): the `team` event; `_lembas/team`, `_lembas/team/transcript`, `_lembas/team/findings`, `_lembas/team/stop`; `subagent` on `session/request_permission` | | `scheduled` | spec 3.5.0 (*Scheduled work on a linked computer*, below): `scheduled` in `_meta.lembas` of `session/new` and `session/prompt` read, and the run's outcome in the prompt's answer; `scheduled` on `session/request_permission`; `_lembas/monitor`; `limits.scheduled` in `_lembas/hello` and `initialize`. Listed by every build that has it, whatever the setting says: whether this device *accepts* scheduled work is `limits.scheduled` | +| `directory_model` | spec 3.9.0 (*The model last used in a directory*, below): `model` on a `Dir` of `_lembas/directories`' answer; `list: false` in its params | | `reports` | spec 3.8.0 (`reports.md` §6): `scheduled.notify` in `_meta.lembas` of `session/new` and `session/prompt` read; `report_write` offered to the model of a schedule's run that notifies; the report the run wrote carried in the prompt's answer (`scheduled.report`); the status `silent` no longer given to a turn whose request said `notify` | ## Standard ACP, as used here @@ -133,6 +134,9 @@ device never answers on a model of its own instead, and never falls back on its is true for every session the client opened, and a client has nothing to say about a device having "answered on its own model": that notice is gone. A session a terminal shares (`announce`) keeps the terminal's model, and its `instance` says which it is. +A session opened with neither `lembas_model` nor `model` starts as a session of the device's own +does (*The model last used in a directory*: the project's setting, else the model last used in +`cwd`, else the device's global setting, else the instance's default). The id is the device's store id and lasts: a session the client names later that is not in memory is opened from the store under the same rules (capability `sessions`). With capability `commands`, an `available_commands_update` follows the answer. @@ -334,6 +338,20 @@ server. The server decides *when*; the device decides *whether*, and *how far* chat has still holds, and the ones below are added. Numbers and texts: `loop.json` `scheduled`. Conformance areas `scheduled`, `silent`, `monitor`. +### Two types of schedule: Chat and Agentic (spec 3.9.0) + +Every client presents a schedule as one of two types, and the type is what `runs_on` says — +there is no field of its own: + +| Type | `runs_on` | A run is | +|---|---|---| +| **Chat** | `null` | a turn on the server, with the server's tools, as a chat's | +| **Agentic** | `{ device, cwd, mode, approval_wait_minutes }` | an agent turn on **that device**, in **that directory**, with the agent's tools there (the shell, the files), under the device's limits and the schedule's mode | + +An Agentic schedule is tied to one device and one directory; it has no meaning without them, and +a client does not offer the type while no device of the person's accepts scheduled work +(`limits.scheduled`). Everything below is about the Agentic type. + ### The device's setting: Accept scheduled work - **`remote.scheduled`** in the device's global config, **off by default**; shown as **Accept @@ -374,6 +392,27 @@ session again on its next run; a fresh run opens a new one. The prompt's text is for a scheduled run — the instruction, the run's time, and for a schedule that notifies only when it matters the `[SILENT]` hint — composed exactly as for a run on the server; the device adds nothing. +**The run's model** (spec 3.9.0; `conformance/last_model.json`, `kind: client`). The client +resolves it for every run that opens a session, when the run starts, and sends it as +`lembas_model`: + +1. **the model the schedule names**, when it names one; +2. else **the model last used in the schedule's directory on that device** — asked of the device + then (`_lembas/directories` with `path: cwd, list: false`, capability `directory_model`) — + when the person still has it; +3. else as before 3.9.0: the default a schedule without a model runs on. + +So an Agentic schedule that names no model follows the directory: change the model you work with +there, and the next run uses it. A schedule that continues one conversation keeps its session's +model from run to run; the client changes it (`_lembas/configure`) only when the schedule names a +model and the session is on another. The device is linked whenever this is asked — a run whose +device is away is `waiting` before anything is resolved (*When the device is away*). + +**A scheduled run never becomes the directory's model.** The device records nothing for a session +a schedule opened — not the run's turn, not a `_lembas/configure` on it, not a turn the person +writes into the run's chat afterwards (`kind: record`). Otherwise a schedule that names a small +model for a nightly check would decide what the person's next chat in that folder starts on. + **The mode** (`scheduled.mode`; `_meta.mode` is ignored for a scheduled session): `manual`, `edit` or `auto`, and **`edit` when absent** — so a run is never wider than Edit unless the person picked Auto on that schedule. `plan` is refused (`scheduled.texts.plan`, `data: { reason: "plan" }`): @@ -476,6 +515,82 @@ tool reads them — followed by a newline and the exit status as decimal digits the device hashes the output as it streams, however long). Nothing is normalised: a watch command that prints a time changes every time, and should not. +## The model last used in a directory (spec 3.9.0) + +**A new agent session in a directory starts on the model last used in that directory**, wherever +it is started from — the terminal, the web, the desktop app, a phone — instead of the instance's +default. What is remembered is **the device's, and stays on the device**: the server keeps no +record of it, so removing a device, or LLeMbas from it, leaves nothing of it behind on the +server. Capability `directory_model`; conformance area `last_model`. + +### What the device remembers + +- **One model per directory**: the ref (`provider/model`, the served id) of the model last used in + a session whose working directory is exactly that directory. The key is the directory's absolute + path as the file system has it — symlinks followed, no trailing slash — compared exactly: + **a directory does not inherit its parent's model, nor a parent its child's.** +- **Where**: in the device's own state (`models.json` in the CLI's state directory, written + atomically, mode 0600; local mode keeps it in the local root). Never in the project, never in + a session, never on the server. `lembas uninstall --purge` removes it with the rest of the + state; a plain uninstall keeps it with the settings and sessions, as it keeps those. The file + holds at most 500 directories — the ones used longest ago go first — and a directory that no + longer exists is dropped when the file is next written. +- **When it is written** (`conformance/last_model.json`, `kind: record`) — for the main agent of a + session, in a terminal, on the link (`lembas service`) and on `serve --stdio` alike: + 1. **a turn a person started begins**: the model the turn runs on is the directory's; + 2. **the person changes the session's model** between turns or during one (`/model`, a setting, + `_lembas/configure` with a `model`): the new model is the directory's at once. + + And **not**: a turn the harness started (`machine`: a job's note, a subagent's message); a + subagent's model; a command's own `model`, which holds for that prompt only; a one-shot + `lembas run`; and **anything in a session a schedule opened for its run** — a scheduled run + uses the directory's model (below) and never changes it, whichever model the schedule names. + +### How a session of the device's own starts + +The first of these that names a model (`kind: start`): + +| # | Source | | +|---|---|---| +| 1 | the model asked for at the start | `--model`; `model` / `lembas_model` in `session/new` | +| 2 | the project's setting | `model` in a trusted project's configuration | +| 3 | **the model last used in this directory** | only while the login still serves it; otherwise skipped, silently, and kept | +| 4 | the device's global setting | `model` in the global configuration — what a login writes as its default | +| 5 | the instance's default | | + +A project's setting comes first because it is written down on purpose and travels with the +project; the directory's memory comes before the global setting because the global setting is the +default for every directory that has none. A session opened from the store again starts by the +same order; so does a one-shot `lembas run`, which records nothing. + +### What the device tells the client + +`_lembas/directories` (below) says the remembered model of each directory it describes: `model` +on a `Dir`, and on the answered directory itself — present only where one is remembered **and** +the directory is `accepted`. It is the ref as recorded; the device does not vouch that the +instance still serves it. To ask about one directory without listing it, `list: false`. + +A device without the capability (before 3.9.0) sends no `model` and ignores `list`: the client +then finds no `model` in the answer and starts as it did before. + +### The client's rule + +**A new agent chat on (device, directory) for which the person chose no model starts on the +directory's `model`, when the person still has that model** (it is one the instance serves to +them now); otherwise as before 3.9.0 — the person's or the instance's default. The same holds +for a schedule's run on a device (*Scheduled work*, *The run's model*). + +- The client asks when the chat is made (`_lembas/directories` with the chat's `path` and + `list: false`; a directory dialog that has just listed the folder already holds the answer) and + may show the model as the choice it starts from. A device that is not linked then, or has no + capability, or answers without `model`: as before. +- A model the person chose is the chat's whatever the directory remembers — and, once a turn runs + on it, it is what the directory remembers next. +- **The client does not keep it**: the `model` of a directory is used for the session being + opened and stored nowhere but as that chat's own model. The device is asked again next time. + (`kind: client` — the model a chat or a run starts on, from what was named, what the directory + says, the models the person has and the default.) + ## `_lembas/*` methods Direction is who sends it. "→ result" marks a request; the others are notifications. @@ -540,10 +655,39 @@ of the output as it streams and keeps at most `scheduled.monitor.output_max_byte ### `_lembas/directories` — client → agent → result -Params `{ path?: string }`. Without a path: `{ path: "", places: Dir[] }`, the remote roots and every -trusted directory inside them. With one (absolute, inside the roots): `{ …Dir, parent, entries: Dir[], error? }`. -`Dir = { path, name, accepted: boolean, reason? }` — whether `session/new` would accept it. -Only on the link (an error on `serve --stdio`). +Params `{ path?: string, list?: boolean }`. Only on the link (an error on `serve --stdio`). + +`Dir = { path, name, accepted: boolean, reason?, model? }` — `accepted`: whether `session/new` +would accept it (`reason`: why not); **`model`** (capability `directory_model`, spec 3.9.0): the +ref of the model last used in exactly that directory, absent where none is remembered or the +directory is not accepted (*The model last used in a directory*). + +- **Without a path**: `{ path: "", places: Dir[] }`, the remote roots and every trusted directory + inside them. (`list` is ignored.) +- **With one** (absolute, inside the roots): `{ …Dir, parent, entries: Dir[], error? }` — the + directory itself, its parent (`""` at a root), and its subdirectories (hidden ones and + `node_modules` left out, at most 300, by name). +- **With one and `list: false`** (capability `directory_model`): `{ …Dir, parent }` — the directory + alone; nothing is listed, and `entries` is absent. This is how a client asks for one + directory's `model`. + +```json +→ { "path": "/home/user" } +← { "path": "/home/user", "name": "user", "accepted": false, "reason": "…", "parent": "", + "entries": [ + { "path": "/home/user/notes", "name": "notes", "accepted": true }, + { "path": "/home/user/project", "name": "project", "accepted": true, "model": "deepseek/deepseek-flash" } ] } + +→ { "path": "/home/user/project", "list": false } +← { "path": "/home/user/project", "name": "project", "accepted": true, + "model": "deepseek/deepseek-flash", "parent": "/home/user" } + +→ { "path": "/home/user/notes", "list": false } +← { "path": "/home/user/notes", "name": "notes", "accepted": true, "parent": "/home/user" } +``` + +A device before 3.9.0 ignores `list` and answers the second request with `entries` and without +`model`; a client reads `model` only, and an absent one means *start as before*. ### `_lembas/configure` — client → agent → result @@ -882,7 +1026,8 @@ Besides `/v1/models`, the chat completions, `/api/v1/instance` and `/mcp`: `instruction`, `rule`, `description`, `next_runs`, `timezone`. A run's `status` may also be **`unchanged`** (monitor mode: the watch saw nothing new; no model ran), and `waiting_for` may be **`device`**. A schedule that runs on a linked computer has - **`runs_on: { device, device_name, cwd, mode, approval_wait_minutes }`** (else `null`); one that + **`runs_on: { device, device_name, cwd, mode, approval_wait_minutes }`** (else `null`) — the + **Agentic** type, `null` being **Chat** (spec 3.9.0, *Two types of schedule*); one that watches has **`watch: { kind: "url", url } | { kind: "command", command, timeout_s }`** (else `null`). Creating one with the device's token, `runs_on.device: "this"` names the calling device. A server without these answers 401 or 403 to the device's token, and the CLI says the server is diff --git a/harness/conformance/last_model.json b/harness/conformance/last_model.json new file mode 100644 index 0000000..8e0a3b0 --- /dev/null +++ b/harness/conformance/last_model.json @@ -0,0 +1,312 @@ +{ + "about": "Spec 3.9.0 (acp.md *The model last used in a directory*; *Scheduled work*, *The run's model*). Three kinds. `kind: start` — the device: which model a session of its own starts on. `asked`: the model asked for at the start (--model; model / lembas_model in session/new); `project`: `model` in a trusted project's configuration; `directory`: the model remembered for exactly this directory; `global`: `model` in the device's global configuration; `served`: the ids the login serves now; `default`: the instance's default. → `{model, from}`, `from` one of `asked`, `project`, `directory`, `global`, `default`. Only the directory's model is checked against `served` and skipped when it is not there; the others are taken as written (a model nobody serves is that source's own error, as before). `kind: record` — the device: whether something makes the session's model the directory's. `session`: `terminal`, `link` (a session the client opened, or a terminal's shared one), `stdio`, `run` (one-shot lembas run) or `scheduled` (a session a schedule opened for its run); `agent`: `main` or `subagent`; `what`: `turn` (a turn begins; `machine: true` when the harness started it), `change` (the person changes the session's model: /model, a setting, _lembas/configure) or `command` (a command's own model, for its prompt). → true or false. `kind: client` — the client (LLeMbas): the model a new agent chat on a device and directory, or a schedule's run there, starts on. `named`: the model the person chose for the chat, or the schedule names (absent: none); `directory`: `model` of the directory in the device's _lembas/directories answer (absent: none, an older device, or a device not asked); `has`: the models the person has now; `default`: what a chat or schedule without a model started on before 3.9.0. → `{model, from}`, `from` one of `named`, `directory`, `default`. CLI: src/session/lastmodel.ts.", + "cases": [ + { + "about": "Nothing but the instance's default.", + "kind": "start", + "served": [ + "deepseek/deepseek-flash", + "qwen/qwen3" + ], + "default": "deepseek/deepseek-flash", + "expect": { + "model": "deepseek/deepseek-flash", + "from": "default" + } + }, + { + "about": "The global setting — what a login writes — over the instance's default.", + "kind": "start", + "global": "qwen/qwen3", + "served": [ + "deepseek/deepseek-flash", + "qwen/qwen3" + ], + "default": "deepseek/deepseek-flash", + "expect": { + "model": "qwen/qwen3", + "from": "global" + } + }, + { + "about": "The model last used in this directory over the global setting.", + "kind": "start", + "directory": "mistral/devstral", + "global": "qwen/qwen3", + "served": [ + "deepseek/deepseek-flash", + "qwen/qwen3", + "mistral/devstral" + ], + "default": "deepseek/deepseek-flash", + "expect": { + "model": "mistral/devstral", + "from": "directory" + } + }, + { + "about": "The project's setting over the directory's memory: it is written down on purpose.", + "kind": "start", + "project": "qwen/qwen3", + "directory": "mistral/devstral", + "global": "deepseek/deepseek-flash", + "served": [ + "deepseek/deepseek-flash", + "qwen/qwen3", + "mistral/devstral" + ], + "default": "deepseek/deepseek-flash", + "expect": { + "model": "qwen/qwen3", + "from": "project" + } + }, + { + "about": "The model asked for over everything.", + "kind": "start", + "asked": "deepseek/deepseek-flash", + "project": "qwen/qwen3", + "directory": "mistral/devstral", + "global": "qwen/qwen3", + "served": [ + "deepseek/deepseek-flash", + "qwen/qwen3", + "mistral/devstral" + ], + "default": "qwen/qwen3", + "expect": { + "model": "deepseek/deepseek-flash", + "from": "asked" + } + }, + { + "about": "The remembered model is no longer served: skipped, the global setting decides.", + "kind": "start", + "directory": "mistral/devstral", + "global": "qwen/qwen3", + "served": [ + "deepseek/deepseek-flash", + "qwen/qwen3" + ], + "default": "deepseek/deepseek-flash", + "expect": { + "model": "qwen/qwen3", + "from": "global" + } + }, + { + "about": "…and with no global setting, the instance's default.", + "kind": "start", + "directory": "mistral/devstral", + "served": [ + "deepseek/deepseek-flash", + "qwen/qwen3" + ], + "default": "deepseek/deepseek-flash", + "expect": { + "model": "deepseek/deepseek-flash", + "from": "default" + } + }, + { + "about": "A global setting is not checked against the list here: it is taken as written.", + "kind": "start", + "global": "old/model", + "served": [ + "deepseek/deepseek-flash" + ], + "default": "deepseek/deepseek-flash", + "expect": { + "model": "old/model", + "from": "global" + } + }, + { + "about": "A person's turn in a terminal.", + "kind": "record", + "session": "terminal", + "agent": "main", + "what": "turn", + "expect": true + }, + { + "about": "A person's turn in a session the client opened.", + "kind": "record", + "session": "link", + "agent": "main", + "what": "turn", + "expect": true + }, + { + "about": "An editor's session.", + "kind": "record", + "session": "stdio", + "agent": "main", + "what": "turn", + "expect": true + }, + { + "about": "The person switches model.", + "kind": "record", + "session": "terminal", + "agent": "main", + "what": "change", + "expect": true + }, + { + "about": "The chat's model is changed from the client between turns.", + "kind": "record", + "session": "link", + "agent": "main", + "what": "change", + "expect": true + }, + { + "about": "A turn the harness started — a finished job's note — is nobody's choice.", + "kind": "record", + "session": "terminal", + "agent": "main", + "what": "turn", + "machine": true, + "expect": false + }, + { + "about": "A subagent's model is not the directory's.", + "kind": "record", + "session": "terminal", + "agent": "subagent", + "what": "turn", + "expect": false + }, + { + "about": "A command's own model holds for its prompt only.", + "kind": "record", + "session": "terminal", + "agent": "main", + "what": "command", + "expect": false + }, + { + "about": "A one-shot run from a script.", + "kind": "record", + "session": "run", + "agent": "main", + "what": "turn", + "expect": false + }, + { + "about": "A scheduled run never becomes the directory's model.", + "kind": "record", + "session": "scheduled", + "agent": "main", + "what": "turn", + "expect": false + }, + { + "about": "…nor does a model change on a schedule's session.", + "kind": "record", + "session": "scheduled", + "agent": "main", + "what": "change", + "expect": false + }, + { + "about": "A new agent chat, no model chosen, the directory remembers one the person has.", + "kind": "client", + "directory": "mistral/devstral", + "has": [ + "deepseek/deepseek-flash", + "mistral/devstral" + ], + "default": "deepseek/deepseek-flash", + "expect": { + "model": "mistral/devstral", + "from": "directory" + } + }, + { + "about": "The person chose a model: theirs, whatever the directory remembers.", + "kind": "client", + "named": "deepseek/deepseek-flash", + "directory": "mistral/devstral", + "has": [ + "deepseek/deepseek-flash", + "mistral/devstral" + ], + "default": "deepseek/deepseek-flash", + "expect": { + "model": "deepseek/deepseek-flash", + "from": "named" + } + }, + { + "about": "The person no longer has the remembered model: as before.", + "kind": "client", + "directory": "mistral/devstral", + "has": [ + "deepseek/deepseek-flash" + ], + "default": "deepseek/deepseek-flash", + "expect": { + "model": "deepseek/deepseek-flash", + "from": "default" + } + }, + { + "about": "An older device, or one that remembers nothing there: as before.", + "kind": "client", + "has": [ + "deepseek/deepseek-flash", + "mistral/devstral" + ], + "default": "deepseek/deepseek-flash", + "expect": { + "model": "deepseek/deepseek-flash", + "from": "default" + } + }, + { + "about": "An Agentic schedule that names no model follows the directory.", + "kind": "client", + "directory": "qwen/qwen3", + "has": [ + "deepseek/deepseek-flash", + "qwen/qwen3" + ], + "default": "deepseek/deepseek-flash", + "expect": { + "model": "qwen/qwen3", + "from": "directory" + } + }, + { + "about": "A schedule that names one runs on it.", + "kind": "client", + "named": "deepseek/deepseek-flash", + "directory": "qwen/qwen3", + "has": [ + "deepseek/deepseek-flash", + "qwen/qwen3" + ], + "default": "qwen/qwen3", + "expect": { + "model": "deepseek/deepseek-flash", + "from": "named" + } + }, + { + "about": "The directory's model is compared exactly: another spelling is not the model.", + "kind": "client", + "directory": "Mistral/Devstral", + "has": [ + "deepseek/deepseek-flash", + "mistral/devstral" + ], + "default": "deepseek/deepseek-flash", + "expect": { + "model": "deepseek/deepseek-flash", + "from": "default" + } + } + ] +} diff --git a/harness/conformance/rules.json b/harness/conformance/rules.json index 0e7bbd1..0395185 100644 --- a/harness/conformance/rules.json +++ b/harness/conformance/rules.json @@ -1,5 +1,5 @@ { - "about": "Which permission rule decides (permission/rules.md, spec 3.8.0). The rules by layer: `defaults` true — permission/defaults.json as the `default` layer; `user` and `project` — a configuration's permission map (a key's value an action, or a map of pattern → action, read in the order written); `made` — rules made during the session, in order, each `by` an `always` answer or a `menu`. With `request` (`permission`, `pattern`: what one call asks for): expect the action that decides, or null where no rule matches. With `set` (a rule a person makes now, by a menu): expect `result` — `unchanged` (the action is already in force), `refused` with the rule in its way as `by`, or `made` with `still`, the stricter narrower rules that still apply, in the order read. The same answers for a rule typed into a file as for one a menu wrote. LLeMbas: security/permission rules; CLI: src/permission/evaluate.ts.", + "about": "Which permission rule decides (permission/rules.md, spec 3.8.0; rule 2's set-aside 3.9.0). The rules by layer: `defaults` true — permission/defaults.json as the `default` layer; `user` and `project` — a configuration's permission map (a key's value an action, or a map of pattern → action, read in the order written); `made` — rules made during the session, in order, each `by` an `always` answer or a `menu`. With `request` (`permission`, `pattern`: what one call asks for): expect the action that decides, or null where no rule matches. With `set` (a rule a person makes now, by a menu): expect `result` — `unchanged` (the action is already in force), `refused` with the rule in its way as `by`, or `made` with `still`, the stricter narrower rules that still apply, in the order read. The same answers for a rule typed into a file as for one a menu wrote. LLeMbas: security/permission rules; CLI: src/permission/evaluate.ts.", "cases": [ { "about": "The defaults alone: a file is read, a .env asks.", @@ -322,6 +322,113 @@ }, "expect": null }, + { + "about": "Spec 3.9.0: a narrower rule that a later one narrows again does not count. The defaults ask about *.env.* and then allow *.env.example; after a broad allow made in a menu the example file is still read without asking.", + "defaults": true, + "made": [ + { + "permission": "read", + "pattern": "*", + "action": "allow", + "by": "menu" + } + ], + "request": { + "permission": "read", + "pattern": ".env.example" + }, + "expect": "allow" + }, + { + "about": "The same under a project's read: allow, in a subdirectory.", + "defaults": true, + "project": { + "read": "allow" + }, + "request": { + "permission": "read", + "pattern": "config/.env.example" + }, + "expect": "allow" + }, + { + "about": "The same under the broadest rule, made by an \"always\" answer.", + "defaults": true, + "made": [ + { + "permission": "*", + "pattern": "*", + "action": "allow", + "by": "always" + } + ], + "request": { + "permission": "read", + "pattern": ".env.example" + }, + "expect": "allow" + }, + { + "about": "…and the sibling the defaults do not except still asks: only *.env.* speaks of it.", + "defaults": true, + "made": [ + { + "permission": "read", + "pattern": "*", + "action": "allow", + "by": "menu" + } + ], + "request": { + "permission": "read", + "pattern": ".env.production" + }, + "expect": "ask" + }, + { + "about": "Order matters as in a file: the narrow allow is read before the ask that takes it in, so nothing is set aside and the ask decides — as it does without the broad rule.", + "project": { + "read": { + "notes.private.md": "allow", + "*.private.*": "ask" + } + }, + "made": [ + { + "permission": "read", + "pattern": "*", + "action": "allow", + "by": "menu" + } + ], + "request": { + "permission": "read", + "pattern": "notes.private.md" + }, + "expect": "ask" + }, + { + "about": "A later, narrower rule that is stricter is the one that counts just the same.", + "project": { + "bash": { + "git *": "ask", + "git push --force *": "deny" + } + }, + "made": [ + { + "permission": "bash", + "pattern": "*", + "action": "allow", + "by": "menu" + } + ], + "request": { + "permission": "bash", + "pattern": "git push --force origin main" + }, + "expect": "deny" + }, { "about": "Choosing the action already in force changes nothing.", "defaults": true, diff --git a/harness/permission/rules.md b/harness/permission/rules.md index 9ddf3ef..45f76df 100644 --- a/harness/permission/rules.md +++ b/harness/permission/rules.md @@ -1,6 +1,6 @@ # Permission rules: which one decides -Spec 3.8.0. The rules are data (`defaults.json`, the person's own config, a project's, and those +Spec 3.8.0; §2 rule 2 amended in 3.9.0. The rules are data (`defaults.json`, the person's own config, a project's, and those made while a session runs); the modes say what happens to a call no rule settles (`modes.json`); the hardline is under all of it (`hardline.json`). This says **which rule decides a call when several match** — in both implementations, for a rule a person typed into a file exactly as for @@ -38,6 +38,15 @@ than it (`deny` over `ask` over `allow`) — the strictest of them decides: `read *.env: ask` asking, and `bash: allow` leaves `bash "git push *": ask`: a later, broader rule changes what a broader rule decided, never what somebody wrote for the narrower case. To allow the narrow thing, name it. + + **A narrower rule that a later one narrows again does not count** (spec 3.9.0): among those + rules, one is set aside when a rule read after it also matches the call and is narrower than + it — that later rule is what was written for the case. The defaults say `read *.env.*: ask` + and then `read *.env.example: allow`; under a candidate `read *` only the second counts for + `.env.example`, so an example file is read without asking after a broad allow exactly as + before it. (Before 3.9.0 both counted and the strictest won: making `read: allow` *started* + asking about `.env.example`.) Order still matters as it does in a file: a narrower rule read + *before* the rule it narrows sets nothing aside. 3. **The person's own answer.** What the `user` layer alone says of the call — its last matching rule, of any breadth. A project, an "always" answer and a menu cannot loosen what the person's own configuration says. @@ -66,4 +75,5 @@ When a person makes a rule — an "always" answer, or one set in a menu: every call the same way. - The person is told what it did: where it holds, whether it was saved, and **which stricter rules still apply** — every rule of the `default`, `user` or `project` layer that is narrower - than the new rule and stricter than it. + than the new rule and stricter than it. (A rule set aside for every call it could decide is + still listed: the list is of rules, not of calls.) diff --git a/src/permission/evaluate.ts b/src/permission/evaluate.ts index 45824d0..28c492e 100644 --- a/src/permission/evaluate.ts +++ b/src/permission/evaluate.ts @@ -93,14 +93,18 @@ export function ruleAction(permission: string, pattern: string, rules: Rule[]): /** The rule that decides (harness permission/rules.md §2). The last matching rule is the * candidate. One made after the written ones — a project's, an "always" answer, one set in a * menu — gives way to whichever of these is stricter than it, the strictest deciding: - * a narrower written rule (`read: allow` leaves `read *.env: ask` asking); the person's own + * a narrower written rule (`read: allow` leaves `read *.env: ask` asking) — unless a later + * written rule narrows that one again, which then speaks for the case; the person's own * answer (their config's last matching rule, of any breadth); and, for an "always" answer, a * written deny. A candidate from the defaults or the person's own config decides as it is. */ function lookupRule(permission: string, pattern: string, rules: Rule[]): Rule | undefined { const matches = (r: Rule) => match(permission, r.permission) && match(pattern, r.pattern) const hit = rules.findLast(matches) if (!hit || !(hit.source === "project" || hit.learned)) return hit - const floors: Rule[] = rules.filter((r) => r !== hit && !r.learned && matches(r) && narrowerThan(r, hit)) + // A narrower rule that a later one narrows again is set aside (spec 3.9.0): `read *.env.*: ask` + // gives way to `read *.env.example: allow` written after it, as it does without the candidate. + const narrower = rules.filter((r) => r !== hit && !r.learned && matches(r) && narrowerThan(r, hit)) + const floors: Rule[] = narrower.filter((r, i) => !narrower.slice(i + 1).some((later) => narrowerThan(later, r))) const own = rules.findLast((r) => r.source === "global" && !r.learned && matches(r)) if (own) floors.push(own) if (hit.learned) { diff --git a/src/session/lastmodel.ts b/src/session/lastmodel.ts new file mode 100644 index 0000000..056a6d5 --- /dev/null +++ b/src/session/lastmodel.ts @@ -0,0 +1,115 @@ +// The model last used in a directory (harness acp.md, spec 3.9.0): one model per directory, +// remembered on this machine and nowhere else — not in the project, not in a session, not on the +// server — so that a new session in a directory starts on what was last used there, wherever it +// is started from. The rules are three small functions (conformance/last_model.json); the rest is +// one file in the state directory. +import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs" +import { dirname, join, resolve } from "node:path" +import { paths } from "../config/paths.ts" +import { realPath } from "../permission/evaluate.ts" + +export type StartSource = "asked" | "project" | "directory" | "global" | "default" + +/** Which model a session of this device's own starts on: the first source that names one. Only + * the directory's is checked against what the login serves now — a model that has gone is + * skipped, silently; the others are taken as written, and a wrong one is theirs to report. */ +export function startModel(o: { asked?: string; project?: string; directory?: string; global?: string; served: readonly string[]; default?: string }): { model: string | undefined; from: StartSource } { + if (o.asked) return { model: o.asked, from: "asked" } + if (o.project) return { model: o.project, from: "project" } + if (o.directory && o.served.includes(o.directory)) return { model: o.directory, from: "directory" } + if (o.global) return { model: o.global, from: "global" } + return { model: o.default, from: "default" } +} + +export type SessionKind = "terminal" | "link" | "stdio" | "run" | "scheduled" + +/** Whether something makes the session's model the directory's: a turn a person started, or the + * person changing the model — of the main agent, and never in a one-shot run or a session a + * schedule opened. */ +export function records(o: { session: SessionKind; agent: "main" | "subagent"; what: "turn" | "change" | "command"; machine?: boolean }): boolean { + if (o.agent !== "main" || o.session === "run" || o.session === "scheduled") return false + if (o.what === "command") return false + return !(o.what === "turn" && o.machine === true) +} + +/** The client's rule (LLeMbas applies it; kept here so both suites answer the same cases): the + * model a new agent chat on a device and directory, or a schedule's run there, starts on. */ +export function clientModel(o: { named?: string; directory?: string; has: readonly string[]; default?: string }): { model: string | undefined; from: "named" | "directory" | "default" } { + if (o.named) return { model: o.named, from: "named" } + if (o.directory && o.has.includes(o.directory)) return { model: o.directory, from: "directory" } + return { model: o.default, from: "default" } +} + +/** At most this many directories are kept; the ones used longest ago go first. */ +export const MAX_DIRECTORIES = 500 + +interface Entry { + model: string + /** When it was written, ms since the epoch. */ + at: number +} +interface File { + version: 1 + directories: Record +} + +/** `models.json` in the state directory (local mode: the local root's). */ +export const lastModelFile = (): string => join(paths.state, "models.json") + +/** A directory as the memory names it: absolute, symlinks followed, no trailing slash. */ +export function directoryKey(dir: string): string { + return realPath(resolve(dir)) +} + +function read(): File { + try { + const v = JSON.parse(readFileSync(lastModelFile(), "utf8")) as File + if (v && typeof v === "object" && v.directories && typeof v.directories === "object") return { version: 1, directories: v.directories } + } catch {} + return { version: 1, directories: {} } +} + +/** The model last used in exactly `dir`, or undefined. Never a parent's, never a child's. */ +export function lastModel(dir: string): string | undefined { + const e = read().directories[directoryKey(dir)] + return e && typeof e.model === "string" && e.model ? e.model : undefined +} + +/** Every remembered directory's model, for many lookups at once (a listing). */ +export function lastModels(): (dir: string) => string | undefined { + const all = read().directories + return (dir) => { + const e = all[directoryKey(dir)] + return e && typeof e.model === "string" && e.model ? e.model : undefined + } +} + +const isDirectory = (p: string): boolean => { + try { + return statSync(p).isDirectory() + } catch { + return false + } +} + +/** Make `ref` the model of `dir`. Written atomically, read first, so two sessions writing for two + * directories keep both; directories that are gone are dropped, and the oldest beyond the cap. + * A failure is nobody's problem: the memory is a convenience, never in a turn's way. */ +export function rememberModel(dir: string, ref: string, now = Date.now()): void { + if (!ref) return + try { + const key = directoryKey(dir) + const file = read() + if (file.directories[key]?.model === ref) return + file.directories[key] = { model: ref, at: now } + const kept = Object.entries(file.directories) + .filter(([d, e]) => e && typeof e.model === "string" && (d === key || isDirectory(d))) + .sort((a, b) => (b[1].at ?? 0) - (a[1].at ?? 0)) + .slice(0, MAX_DIRECTORIES) + const path = lastModelFile() + if (!existsSync(dirname(path))) mkdirSync(dirname(path), { recursive: true, mode: 0o700 }) + const tmp = `${path}.${process.pid}.tmp` + writeFileSync(tmp, `${JSON.stringify({ version: 1, directories: Object.fromEntries(kept) }, null, 2)}\n`, { mode: 0o600 }) + renameSync(tmp, path) + } catch {} +} diff --git a/tests/conformance.ts b/tests/conformance.ts index ff0c3ad..1374a06 100644 --- a/tests/conformance.ts +++ b/tests/conformance.ts @@ -38,6 +38,8 @@ import { checkMode, requestRefusal, tighten, whoDecides, type Grant } from "../s import { fillPrompt as fillText } from "../src/prompt/fill.ts" import { isSilent, judgeCase, monitorHash, notifyInstruction, reportResult, reportTool, runLeaves, scheduledMode, stripSentinel } from "../src/acp/scheduled.ts" +import { clientModel, records, startModel, type SessionKind } from "../src/session/lastmodel.ts" + type Case = Record /** Whether a path is at `ro` or above under `grants` (fs_access's rule: the most specific grant on @@ -53,6 +55,14 @@ function readableUnder(grants: Record | undefined, path: string) } export const AREAS: Record unknown> = { + // Spec 3.9.0: the model last used in a directory (src/session/lastmodel.ts). + last_model: (c) => { + const str = (k: string) => (typeof c[k] === "string" ? (c[k] as string) : undefined) + if (c.kind === "start") return startModel({ asked: str("asked"), project: str("project"), directory: str("directory"), global: str("global"), served: c.served as string[], default: str("default") }) + if (c.kind === "record") return records({ session: c.session as SessionKind, agent: c.agent as "main" | "subagent", what: c.what as "turn" | "change" | "command", machine: c.machine === true }) + if (c.kind === "client") return clientModel({ named: str("named"), directory: str("directory"), has: c.has as string[], default: str("default") }) + throw new Error(`last_model: unknown kind ${String(c.kind)}`) + }, // Spec 3.5.0: scheduled work on a linked computer (src/acp/scheduled.ts). scheduled: (c) => scheduledMode({ accept: c.accept === true, mode: c.mode, max_mode: c.max_mode as Mode, approval_wait_s: c.approval_wait_s }), silent: (c) => ({ silent: isSilent(c.text as string), stripped: stripSentinel(c.text as string) }),