diff --git a/DECISIONS.md b/DECISIONS.md index e86b1844..10043439 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -19839,3 +19839,485 @@ name as the owner of its windows, so a name check missed exactly the copy TST Desk ships. The executable check must not catch TST Desk's own windows, which share that owner name. The lock covers set-up only, never the command, so concurrent sessions still act in parallel. + +## 2026-11-20 — TD-5237: three narrow verbs edit the model catalog; rename is the label only (Class B) + +**Decision:** Settings → Model copies, relabels and removes catalog entries +with `duplicate_preset {source, label}`, `set_preset_label {name, label, +description?}` and `delete_preset {name}`, each acked with `setup_state`. +The writers live in `core/tstd/preset_write.py`. A copy is written from the +loaded entry with every role spelled out. "Rename" changes `label` and never +the entry's key. Every would-be file is loaded with `config_from_data` before +it is written. `delete_preset` is in `LOOPBACK_VERBS`; the other two are open +to a paired phone like `add_model`, and none is a machine-peer verb. + +**Rationale:** the key is an id other things store: `sessions.json` +(TD-1721), scheduled-job pins (TD-3812) and +`computer_use.local_worker_preset`. Changing it would need every one of +those rewritten in step, for no gain a person can see, since the picker shows +the label. A copy written from text would repeat the source's anchor names, +and YAML then rebinds every later alias to the copy; writing it expanded +keeps the two entries independent, which is the point of copying. Loading +before writing is what catches the one edit a line-level writer cannot judge +by itself: removing an entry whose anchor another entry uses. + +**Rejected:** one generic "edit preset" verb carrying a whole entry (a +free-form body is where a secret could ride in; the narrow verbs cannot +carry one); renaming the key and migrating its references; deleting the keys +an entry used along with it (a key is shared between entries and is managed +in Settings → API keys); letting a phone remove a model (it is the +computer's config, and nothing on a phone needs it). + +## 2026-11-20 — TD-5234: a model's `overrides` are a six-section whitelist, merged in one resolver (Class B) + +**Decision:** a catalog entry may carry `overrides` for six global sections: +`session` (`default_effort`, `default_ultracode` only), `clear_tool_results`, +`compaction`, `consult`, `workflow`, and `computer_use` (`max_prompt_images` +only). `preset_apply.effective_config(config, name)` is the one place they +are applied, and `_bind_session_preset` is its one caller for sessions. The +merge is per key, one level deep: a key the entry sets wins, an unset or +null key keeps the global value, a list replaces the global list whole. +Each merged section is rebuilt by the global section's own model, and every +entry is merged once at load so a bad value fails there. Switching a chat +to an entry that names an effort or Ultracode default sets the chat's to it +(owner, 2026-11-20); an entry that names neither leaves the chat's own. No +protocol message or field is added. + +**Rationale:** a session already holds a snapshot of the config it runs +with, and each turn re-reads the snapshot for clearing, consult, the image +cap, the workflow limits and the summarizer. Applying the overrides where +that snapshot is taken reaches all of them with no change at the call +sites, and keeps the TD-1721 rule that an open chat is not rewritten by a +Settings edit. The whitelist is what suits a model's size and speed. +Everything that decides what the agent may do is left out, so choosing a +model can never loosen the trust surface. The override models repeat each +key's limits (so the editor shows them and a bad value names its key), and +a test pins each one to the global key it overrides. + +**Rejected:** a free-form override of any top-level key (it would let a +catalog entry carry `policy`, `shell` or `mcp`); deep or list merging (a +model that wants no excluded tools could not say so); applying the entry's +effort only to new chats (the owner wants one click to set everything the +entry defines); `project_context` and `embeddings` in the list +(`project_context.token_budget` is read per workspace, and the embeddings +endpoint is not a property of the chat model); a separate `builds:` layer +that points at entries (a second catalog to keep in step). + +## 2026-11-20 — TD-5239: an entry names its engine; a Grok entry's roles are optional as a group (Class B) + +**Decision:** `Preset.engine` is `native`, `grok` or unset (unset means +`engine.kind`). `brain`, `worker` and `validator` are optional as a group: +all three or none, and none only with `engine: grok`. `overrides` and `fast` +on a Grok entry are a load error. A session's engine is set once, at +creation: the caller's (a scheduled pin), else the entry's, else the global +one. `set_session_preset` across engines, or onto an entry with no model +from a session on TST Desk's own loop, is refused with `engine_mismatch`. +For an entry with no roles, `ModelConfig.tiers()` returns the empty-catalog +stand-ins (TD-5172), and every walk over the catalog's roles goes through +`Preset.tier_map()`, which is empty for it. Work no chat owns (memory +distill, the doctor's probe) uses `ModelConfig.native_default()`: the active +entry if it has a model, else the first that does, else it is skipped +(owner, 2026-11-20: no `helper` key). `setup_state.preset_engines` is the one +additive wire field; there is no new client message. + +**Rationale:** the Grok CLI brings its own model and sign-in, so requiring +three roles on its entry would mean writing three that are never used. One +catalog with an optional group keeps one YAML block, one picker list and one +set of writers. The stand-in path already existed and is already safe: a +turn that reaches it fails with the "add a model" message before any request +is built. A session's loop cannot be swapped under a conversation, so the +engine is fixed and a cross-engine switch is refused rather than half done; +TD-5235 turns that refusal into "start a new chat on it". Refusing +`overrides` and `fast` on a Grok entry, rather than ignoring them, keeps a +setting that does nothing from sitting in the file looking active. + +**Rejected:** a separate `builds:` layer pointing at entries (a second +catalog to keep in step); dummy roles on a Grok entry; a `helper` key naming +the entry for off-turn work (one more thing to set, and the fallback is +predictable); changing an open session's engine in place; making +`setup_state.engine` mean the active entry's engine (Settings → Engine shows +that field as the global setting, so the window computes a new chat's engine +from `preset_engines` instead). + +## 2026-11-20 — TD-5258: one narrow verb puts Grok on the model list (Class B) + +**Decision:** `add_grok_preset {label?}` writes one catalog entry that is a +label, a fixed description and `engine: grok`, and is acked with +`setup_state`. The daemon refuses it with `grok_unavailable` when the Grok +CLI is not found and with `bad_request` when an entry already names the Grok +engine. It does not write `active_preset`. Settings → Engine always renders +the button and disables it, with the reason, when the CLI is missing. + +**Rationale:** the entry has nothing to choose but its name, so the verb +carries nothing else and cannot carry a key or a URL. Checking for the CLI +in the daemon keeps a client from adding an entry that could only fail. +Leaving the active model alone matches `duplicate_preset`: adding to the +list and choosing from it are separate acts. A button that appears only +where the CLI is installed would make the UI walk's census differ between a +developer's machine and CI. + +**Rejected:** reusing `add_model` with an engine field (it requires a URL +and a model and makes the new entry active); allowing several Grok entries +(they would be the same thing twice on the picker; a copy can still be made +with Duplicate). + +## 2026-11-20 — TD-5235: `switch_preset` for the chat, the default or both; a mid-turn switch waits for `turn_complete` (Class B) + +**Decision:** `switch_preset {name, session_id?, make_default}` is the +picker's verb. It validates everything first, writes the default first (the +one step that can still fail), then switches the chat. A chat with a turn +running stores the name in `Session.pending_preset`; the daemon binds it in +`_on_session_event` when that session's `turn_complete` arrives, and +`tier_state.pending_preset` tells the window. A chat with no live loop has +the name recorded. `new_session` takes an optional `preset`. A picker click +sends `make_default: true`; "Only this chat" sends false (owner, +2026-11-20). `set_session_preset` and `set_preset` are unchanged. A pending +switch is not persisted. + +**Rationale:** the complaint was that new chats went back to the old model, +so the plain click sets both and the narrower act is the secondary one. One +verb means "both" cannot half-apply. Binding at `turn_complete` is safe +because `SessionEventLog.add` awaits its subscribers, in registration +order, before it returns to the loop: the session's own subscriber has +already lowered the open-turn count, and the loop has not yet taken the +next message, so that message, a queued one included, runs on the new +entry while the turn that was running keeps the config it started with. +Waiting for the session to be idle instead would never fire while messages +are queued. A chat's engine cannot change (TD-5239), so the other engine's +entries are reached by a new chat, which `new_session.preset` makes one +message. + +**Rejected:** changing `set_session_preset` to queue mid-turn (older +clients and the phone rely on the refusal); persisting a pending switch +(a field on the session record for a rare case); a machine-peer +`switch_preset` (it can write this computer's config; a hub keeps the +older, session-only verb); having the daemon open the new chat itself on an +engine clash (the window decides what a click on such a row means, and the +refusal stays honest for other clients). + +## 2026-11-20 — TD-5236: the daemon names each model's picker section; slots use ⌘⌥ digit; catalog changes are broadcast (Class B) + +**Decision:** `Preset.group` (free text, up to 20 characters) and +`Preset.slot` (1 to 9, unique) are new optional keys. +`ModelConfig.preset_group()` gives every entry a section: its own `group`, +else `Grok` for the Grok engine, `Local` when every role resolves to a +loopback URL, else `API`. `setup_state` carries `preset_groups`, +`picker_groups` (the built-in order) and `preset_slots`. The quick-switch +chord is ⌘⌥1…9 on macOS and Ctrl+Alt+1…9 elsewhere (owner, 2026-11-20), read +from `KeyboardEvent.code`. After `add_model`, `set_preset`, `switch_preset` +with `make_default`, and the four catalog-edit verbs, the daemon broadcasts +`setup_state` to this computer's other windows, leaving out the connection +it is answering. + +**Rationale:** "Local" cannot be read off a URL by the window: a server on +the person's own network is theirs, but nothing in the address says so, and +a bound key can send a loopback-looking role to a provider (TD-1718). So the +daemon decides, with the conservative default (not loopback means API), and +the entry can say otherwise. ⌘1…9 already selects a session, so the model +slots add Option; on a Mac Option changes the character a digit key types, +which is why the physical key is read. `setup_state` was only ever a reply +to the asker, so a second window showed a stale list and default; the +presence refresh (TD-5015) already broadcasts the same event to loopback +windows, and this reuses that audience. The asker is left out because its +own reply already carries the state, and a second copy breaks a client that +pairs one reply with one request. + +**Rejected:** inferring the section in the window from the endpoint (the +window must not derive what it was not sent); treating private-network +addresses as Local automatically (a corporate gateway on 10.x is not the +person's own machine); a broadcast to phones as well (a phone asks when it +shows the list, and `setup_state` for a remote peer is trimmed per +connection, which a broadcast would bypass); slots without Option (taken). + +## 2026-11-20 — TD-5240: readiness is three local checks, asked when the picker opens, and never a gate (Class B) + +**Decision:** `check_presets` (no fields) is answered with +`preset_readiness`: per entry, `ready`, `no_key`, `server_down` or +`no_grok` and one sentence. A key is checked by keychain presence. A role +whose resolved URL is loopback is asked for `/models` with no key and a 1.5 s +limit, and any HTTP reply counts. A provider's server is not contacted. An +entry on the Grok engine is checked for the CLI only. The window sends it +each time the picker opens and shows the sentence on the row; it never +disables the row. + +**Rationale:** these are the three causes of "the switch worked and the +next message failed" that can be known without spending anything or +sending a key. Asking a provider would be a request to a third party the +person did not make (directive 3), and a key's presence is what can be +known without one. Opening the picker is the person's act, which is what +makes the loopback ask theirs too, as "Find models on this computer" is +(TD-5180). It is a warning because the check can be wrong in the harmless +direction: a server that is still loading answers nothing for a moment. + +**Rejected:** checking as part of `switch_preset` and refusing the switch +(a model whose server is starting could not be selected ahead of time); +probing on a timer (traffic nobody asked for); validating a key with a live +call (that is the existing Test button's job, on a click). + +## 2026-11-20 — TD-5241: a read-only `get_preset_roles` for the entry being edited; `setup_state` is unchanged (Class B) + +**Decision:** `get_preset_roles {name}` is answered with `preset_roles`, +which carries for that entry the four maps `setup_state` carries for the +active one. The Settings store keeps the chosen entry (`editPreset`) and its +roles (`editRoles`) beside the active entry's, never over them, and every +write names `editTarget()`. The write verbs already took a `preset`. + +**Rationale:** the alternative was to make `setup_state`'s `tier_*` fields +describe "whichever entry this connection is looking at", which would turn +a stateless reply into per-connection state and break Settings → API keys +and older clients, which read those fields as the active entry's. A separate +read keeps `setup_state` meaning one thing. Showing nothing until the answer +arrives is the "window never derives what it was not sent" rule: the active +entry's roles must not appear under another entry's name. + +**Rejected:** sending every entry's roles in every `setup_state` (it grows +with the catalog and is sent on every Settings write); making the entry +active in order to edit it (that is the behaviour being fixed). + +## 2026-11-20 — TD-5242: a phone hears catalog changes; it cannot remove a model or have this computer's servers asked (Class B) + +**Decision:** the catalog broadcast (TD-5236) also reaches remote +non-machine peers, with `setup_state` passed through `scrub_setup_state` +exactly as a remote peer's own reply is. `WebSocketServer.broadcast` takes +`remote_payload` for that. `check_presets` from a remote peer skips the +loopback ask and reports those servers as ready. `delete_preset` stays +loopback-only (TD-5237); the other model verbs are open to a phone. + +**Rationale:** a phone shows the same picker, so a list that does not +follow the computer is the same fault a second window had. The scrub keeps +the one rule about what a remote peer may see in one place. TD-5180 decided +that probing this computer's model ports is started by the person at it; +`check_presets` asks only configured servers rather than scanning, but from +a phone the same rule is kept to the letter, and a key's presence and the +Grok CLI are still worth telling. Reporting an unasked server as ready, +rather than unknown, keeps the wire's four statuses and matches what a +phone could know before this story: nothing about servers. + +**Rejected:** refusing `check_presets` from a phone (the picker would raise +an error each time it opened there); a fifth `unknown` status (a new state +for every client to handle, to say what an empty warning already says). + +## 2026-11-20 — TD-5276: the MCP page is loopback-only (Class B; removes a phone capability) + +**Decision:** `set_mcp_server`, `delete_mcp_server`, `set_mcp_secret` and +`delete_mcp_secret` join `LOOPBACK_VERBS`. A paired phone, the older remote +token and another computer are refused with `loopback_only`. Off the +computer, Settings → MCP servers renders the list read-only. A phone can no +longer switch a server on or off. + +**Rationale:** a stdio server is an argv the daemon spawns at once and at +every session start, outside the classifier. `peer_policy.check` allows a +remote non-machine peer anything not listed, and the handler did not look at +who was asking, so a paired phone could run a command on the computer with +no confirmation there, and it persisted after the phone was removed. With +the verb reopened in a test, the command ran. The file's own comment already +gave "the commands this computer runs (hooks, language and MCP servers, the +engine)" as the reason the config editor is loopback-only; this makes the +MCP page agree with it. A server's values go with it: they are that +command's environment or the headers sent to its host. + +**Rejected:** letting a phone change only `enabled` (re-enabling a server +the person switched off still runs a command, and the rule would have to +live in the handler, away from the one policy table); a confirm-at-the- +computer card for a phone's request (a feature with a new event, a pending +store and UI on both sides, not a fix); leaving the page interactive on a +phone and relying on the refusal (the page would offer what it cannot do). + +**Not decided here:** the other verbs a phone may send that change what the +computer does or where a key goes (`set_skip_all_approvals`, `shutdown`, +`set_credential`, `set_email_notify`, `start_autonomy`, the computer-use +policy verbs, `add_model`). They need the owner's call one by one; an +explicit phone allowlist with a test that classifies every verb would stop +the next one being missed. + +## 2026-11-20 — TD-5268: an optional `worker` on `approval_request`, and who is asking in the summary (Class B) + +**Decision:** `ApprovalRequest` gains `worker: ApprovalWorker | None`, +filled where a child's dispatcher is built (`_name_the_child`), only when +the approval handler is the parent session's own. The child's `summary` is +prefixed with who is asking. `Session.request_approval` takes a +keyword-only `worker` and withholds the Always-allow proposal for a named +worker and for a path tool in a child's own worktree. + +**Rationale:** the child is deliberately not a registered session, so the +parent's event is the only place its identity can travel. Putting it in +`summary` as well as in a field means every surface that already shows the +summary is correct today, including a phone app built before the field +existed. Always allow keeps its workspace scope (a per-worker allow would +be a loosening for one worker, and worker rules are tighten-only), so the +two cases where a workspace rule would be saved and then not apply are +simply not offered. + +**Rejected:** registering children as sessions to give them cards of their +own (reverses TD-4602); deriving the child from the tool-call id's prefix +in the window (the window would be parsing an id format it was never +promised). + +## 2026-11-20 — TD-5279: phone permissions, with MCP servers as the first (Class B; supersedes part of TD-5276) + +**Decision:** a paired phone may send `set_mcp_server`, `delete_mcp_server`, +`set_mcp_secret` and `delete_mcp_secret` only while the capability +`mcp_servers` is switched on, and it is switched on only by +`set_phone_permission`, a new verb in `LOOPBACK_VERBS`. The four verbs leave +`LOOPBACK_VERBS` for a new table, `PHONE_OPT_IN` (verb to capability). +`peer_policy.check` gains `grants`, defaulting to none, and `PeerView` gains +`phone`. Without the grant the refusal is `phone_permission_off`. Grants +live in `/phone-permissions.yaml` and may carry an expiry. Each +use by a phone sends `phone_change` to the computer's windows and records +the server's origin in `/mcp-origins.yaml`. Removing a phone +offers to remove what it added. Doctor gains a `warn` row status and uses +it while any phone permission is on. + +**Rationale:** this is the owner's decision, relayed by the planning +session: the ability stays, optional, default off, with the risk stated +before it is switched on. Three rules make that safe to offer. Only the +computer can grant, which is why the switch is the one verb with no +setting. Only a paired phone can use a grant: the older remote token is no +device, so nothing switches these verbs on for it, and another computer +stays deny-by-default. And use is visible and leaves a trail. The grants +are in the data dir, beside `remote-attach.yaml` and `approvals.yaml`, and +not a `config.yaml` key, so no repository and no config editor can set +one. Everything about the file fails closed. + +**What the default changes for a phone:** nothing since TD-5276 (refused); +against v0.4.6, a phone can no longer add or change an MCP server until +the computer allows it. + +**Rejected:** a single "trust my phones" switch (one sentence cannot +disclose six different risks, and the owner asked for this not to be +built); a permanent never-list for the most dangerous verbs (the owner +decided every one is opt-in, with stronger words for the two that can +send a stored secret to a host the phone names); keeping the grant in +`config.yaml` (the workspace layer and the config editor could then reach +it); an audit-database record now (every record is keyed to a session; +the log line and the origins file are the trail until that is designed). + +**Wording:** the refusal and the phone's page say "Settings, Phones and +devices", the section's real name, where the design note said "Settings, +Devices". + +**Still to do, one story each:** the same switch for turning approvals +off, model and key setup, computer-use control, unattended runs, the +connector sign-ins, `shutdown`, releasing the computer-use stop, changing +a saved key's host and changing the mail server; then the inventory that +makes a new verb start refused for a phone. + +## 2026-11-20 — TD-5259: memory distill runs on the chat's own entry (Class B) + +**Decision:** `_run_distill` picks its model with `_distill_entry(session)`: +the entry the chat is on when distill runs, if the chat is on TST Desk's +own loop and that entry still exists and names a model; otherwise the +TD-5239 rule (the active entry when it names a model, else the first that +does, else skip). + +**Rationale:** distill sends the conversation to a model server. Sending +it to the daemon's active entry meant a chat the person had put on a local +model could be sent to a hosted one, with nothing on screen saying so. The +chat's own entry is the one place its text was already going. + +**Rejected:** the entry most of the chat's turns ran on (needs per-turn +bookkeeping, and is wrong the moment a person moves a chat off a server on +purpose); applying the entry's context overrides to distill (distill has +its own settings and none of the six override sections govern it). + +## 2026-11-20 — TD-5281: Always allow is never offered from, nor saved for, a child's call (Class B; narrows TD-5268) + +**Decision:** `request_approval` proposes no rule when a `worker` is given, +and marks the parked call `from_worker`. The `always_allow` handler, which +rebuilds the rule from the parked call, approves a child's call once and +answers `not_always_allowable` without saving. + +**Rationale:** the owner's decision. The rule is workspace-wide; rules +cannot yet be scoped to one worker, so the honest options were a loud +label (TD-5268) or no button. Withholding only the card's proposal would +have left the verb able to save the rule from an older window. + +**Rejected:** refusing the frame and leaving the call parked (the person +did say yes to this call, and the handler's own pattern for a rule it +cannot save is "approved once"); a per-worker rule now (worker rules are +tighten-only; a loosening one is a design of its own). + +## 2026-11-20 — TD-5283: turning approvals off from a phone is a phone permission (Class B; removes a phone default) + +**Decision:** `set_skip_all_approvals` joins `PHONE_OPT_IN` under a new +capability, `approval_switches`. `required_capability` returns none for a +message that only tightens (`enabled: false`), so a phone can always bring +the prompts back. A phone's use sends `phone_change`. + +**Rationale:** the owner's decision that every standing change a phone can +make is opt-in, one capability at a time, each with its own disclosure. A +phone can already approve call by call; skip-all is the same power with +nobody looking at each call, and it outlives the phone. + +**What changes for a phone:** it can no longer turn approvals off until +the computer allows it. It can still approve and deny each call, and turn +approvals back on. + +**Rejected:** putting the on/off distinction in the handler (the message's +own field decides it, so it belongs in the one pure table); one capability +for "approvals and autonomy" together (two different risks in one +sentence). + +## 2026-11-20 — TD-5254: a model tool for the Artifacts pane (Class B; ends TD-3201's "no model tool") + +**Decision:** a builtin `show_artifact` with two modes, a workspace text +file by path (no copy) or text with a name (kept in the session's data +dir). `ArtifactStore.record` gains `name`, `caption`, `tool_call_id`, +an upsert that keeps the row's id, a count cap and the session quota. A +static classifier rule `show-artifact` puts the builtin in Class A; it is +on the plan-mode list; children do not get it. + +**Rationale:** TD-3201 rejected "a model tool this story", not for good; +its own docstring expects a later caller. Class A because the call writes +only the session's own data dir, capped, and the name of the file written +is the daemon's. A call with text names no path, so without a rule it +would fall to the worker classifier and ask every time. This is the +reasoning already recorded for `send_file`. + +**Rejected:** copying a `path` artifact on every call (that is +`send_file`, and it breaks "open in editor"); `oneOf` in the schema +(gateways differ); letting the name be a path under the session dir (a +label can never then be a traversal); offering it to children (only the +session's own agent publishes to its pane). + +## 2026-11-20 — TD-5284: a phone is refused any verb nobody placed for it (Class B) + +**Decision:** `PHONE_OPEN` is an explicit list. `check` refuses a remote +non-machine peer any verb that is not in `PHONE_OPEN`, `PHONE_OPT_IN` or +`MACHINE_VERBS` (`LOOPBACK_VERBS` are refused earlier), with +`loopback_only`. A test partitions every client message type. + +**Rationale:** default-open is how `set_mcp_server` reached a phone. The +machine-peer policy has been default-closed since TD-5015 with a test +that walks every type; this is the same rule for the other kind of remote +peer. The list was generated from what was open the day before, so the +change is to the default for future verbs only. + +**Rejected:** a new refusal code for "not placed" (to a phone it is the +same fact as `loopback_only`: do it at the computer); deciding the +remaining sensitive verbs in the same change (each needs its own +disclosure and is a behaviour change for phone users). + +## 2026-11-20 — TD-5253: MCP servers from pasted JSON, re-parsed by the daemon (Class B) + +**Decision:** two verbs, `preview_mcp_import` and `import_mcp_servers`, +both under the `mcp_servers` phone permission, and one event, +`mcp_import_preview`, that never carries a value. A pure parser +(`mcp_import.py`) classifies each value; `mcp_import_apply.py` stores +secrets first and then writes config, undoing the secrets if the write +fails. An existing id is refused. + +**Rationale:** the result is exactly what the Add form and the token box +make by hand, so nothing new exists to secure afterwards. The daemon, not +the window, decides what may be a literal, and defaults to the keychain. +Parsing the text a second time on import means a doctored preview changes +nothing. Refusing an existing id avoids deciding which of the old +server's keychain items survive a replacement. + +**Rejected:** putting the verbs in `LOOPBACK_VERBS` as the note first +said (they are the same act as `set_mcp_server`, which the owner made a +phone permission); expanding `${VAR}` from the daemon's environment (a +desktop-launched daemon does not have the shell's variables, and it would +read secrets from a place the person did not point at); importing SSE +endpoints as HTTP (the client speaks Streamable HTTP only). + diff --git a/README.md b/README.md index c12dc434..0cce7e9e 100644 --- a/README.md +++ b/README.md @@ -181,8 +181,18 @@ directives in [`AGENTS.md`](AGENTS.md) §2 and enforced in code. of that socket, and the other half of this repo. - **No subscription.** You pay your model provider per token and nobody else. There is no billing code in this repository because there is nothing to bill. -- **No telemetry.** No analytics, no phone-home, no crash reporting. The only outbound HTTP the - daemon makes is to the `base_url` you configured: there is exactly one chat client +- **No telemetry.** No analytics, no phone-home, no crash reporting. Nothing is sent to the + authors of TST Desk or to a service of ours, because there is none. Every host the daemon + does contact is named in your `config.yaml`: + - each model's `base_url`, for chat and, once a day, to re-read that model's price and + context size (`model_refresh`; `auto: false` turns it off); + - the search pages `web_search` reads (`search.base_url` and its fallbacks) and the pages + `web_fetch` opens, under the same approval rules as any other tool call; + - what you switch on yourself: notification channels, connectors, and an MCP server you + marked `allow_remote`. + + The packaged app, not the daemon, also asks GitHub releases for a newer version when it + opens and every four hours ([Updates](#updates)). There is exactly one chat client ([`provider.py`](core/tstd/provider.py)), and its endpoint comes from config, never from a literal in the source. Model discovery refuses any non-loopback endpoint *before* it sends ([`discovery.py`](core/tstd/discovery.py)). @@ -212,6 +222,13 @@ directives in [`AGENTS.md`](AGENTS.md) §2 and enforced in code. original. The copy is your file verbatim; it is not redacted, and it never goes to a log or the audit database. Limits: 512 MiB per file and 4 GiB per session (`session.send_file_max_bytes`, `session.send_file_session_max_bytes`). +- **What the agent shows in the Artifacts pane is text it was allowed to read, or text it + wrote.** `show_artifact` either points at a text file inside the workspace (no copy is made) + or keeps text the agent hands over under `/sessions//artifacts/`, + owner-only, under a name the daemon makes. It writes nowhere else and contacts no host. An + HTML artifact is shown as a static picture of itself: no scripts, no links, no remote + images. Limits: 2 MiB per artifact and 200 per session + (`session.artifact_max_bytes`, `session.artifact_max_count`). - **The agent cannot rewrite its own rules.** `AGENTS.md`, `CLAUDE.md` and `.tst/rules/**` are refused by the filesystem tool itself ([`tools/boundary.py`](core/tstd/tools/boundary.py)), ahead of the workspace check and ahead of any approval — so no `writable_paths` grant can open diff --git a/android/README.md b/android/README.md index 3734ef12..10a64eb0 100644 --- a/android/README.md +++ b/android/README.md @@ -24,6 +24,16 @@ Still works as a fallback. On the computer, turn on **Allow remote attach** in S The shell refuses any host that is not a Tailscale address (`100.64.0.0/10`, `fd7a:115c:a1e0::/48`) or a `*.ts.net` name. It does not listen on the network. The UI is served to the WebView on `127.0.0.1` only, on the same port each launch when it is free. +Some things stay with the computer and are refused from a phone: the raw Settings → +Configuration editor, trusting a workspace's own config, accepting an instruction-file +edit, and removing a model. + +Adding, changing or removing an MCP server or its token is off for phones unless you turn +it on at the computer (**Settings → Phones and devices → What paired phones may change**). +An MCP server is a command the computer runs, so read what that switch says before +turning it on. While it is off the phone shows the MCP list read-only. A phone cannot turn +it on, and the computer's window is told each time a phone changes a server (TD-5279). + ## Build Needs JDK 17+, the Android SDK (platform 35, build-tools 35), and the UI's `npm` dependencies. diff --git a/core/scripts/generate_protocol_fixtures.py b/core/scripts/generate_protocol_fixtures.py index 37b1d89d..27cc093f 100644 --- a/core/scripts/generate_protocol_fixtures.py +++ b/core/scripts/generate_protocol_fixtures.py @@ -19,6 +19,7 @@ from tstd.protocol import ( CHUNK_MAX, PROTOCOL_VERSION, + AddGrokPreset, AddMachine, AddModel, AddPin, @@ -49,6 +50,7 @@ CharterDocument, CheckCuPermissions, CheckpointNotice, + CheckPresets, CloseWorktree, CommandEntry, CommandList, @@ -74,6 +76,7 @@ DeleteJobTemplate, DeleteMcpSecret, DeleteMcpServer, + DeletePreset, DeleteSession, Deny, DenyVerify, @@ -85,6 +88,7 @@ DiagnosticsReport, Drain, DrainResult, + DuplicatePreset, EmailTestResult, EndpointModel, EndpointModels, @@ -100,6 +104,7 @@ GetConfigEditor, GetHooks, GetInstructionStack, + GetPresetRoles, GetSessionJobSource, GetSetupState, GetUsage, @@ -125,6 +130,7 @@ HookInfo, HookRun, HooksState, + ImportMcpServers, InstructionFileEntry, InstructionFiles, InstructionStack, @@ -157,6 +163,10 @@ LoosenedSetting, MachineList, MachineSummary, + McpImportChoice, + McpImportPreview, + McpImportServer, + McpImportValue, McpServerSummary, McpValueSummary, MemoryAccept, @@ -180,12 +190,17 @@ PairingCode, PairingCompleted, ParseJob, + PhoneChange, Ping, PlanPinned, PlanReview, PlanReviewed, PolicyRules, PolicyRuleSummary, + PresetReadiness, + PresetReady, + PresetRoles, + PreviewMcpImport, PreviewRestoreFiles, Question, QuestionAnswer, @@ -247,8 +262,10 @@ SetLoadGlobalMemory, SetMcpSecret, SetMcpServer, + SetPhonePermission, SetPlan, SetPreset, + SetPresetLabel, SetRemoteAttach, SetSessionPreset, SetSessionStar, @@ -277,6 +294,7 @@ StopCheck, StopCheckItem, SubmitLineComments, + SwitchPreset, TaskList, TaskListItem, TestEmail, @@ -582,6 +600,19 @@ ), "test_model": TestModel(), "set_preset": SetPreset(name="tst-default"), + # TD-5237: copy, relabel and remove a catalog entry + "duplicate_preset": DuplicatePreset(source="tst-default", label="My copy"), + "set_preset_label": SetPresetLabel( + name="tst-default", label="House model", description="General coding and chat" + ), + "delete_preset": DeletePreset(name="budget"), + "check_presets": CheckPresets(), + # TD-5241: the roles of the entry Settings → Model is editing + "get_preset_roles": GetPresetRoles(name="budget"), + # TD-5235: this chat, the model new chats use, or both + "switch_preset": SwitchPreset(name="budget", session_id="sess-1", make_default=True), + # TD-5258: put the Grok CLI on the model list + "add_grok_preset": AddGrokPreset(), "set_engine": SetEngine(kind="grok"), "set_grok_mode": SetGrokMode(session_id="sess-1", mode="plan"), "run_grok_command": RunGrokCommand(session_id="sess-1", name="compact", argument="keep tests"), @@ -627,6 +658,16 @@ "start_pairing": StartPairing(), "cancel_pairing": CancelPairing(), "remove_remote_device": RemoveRemoteDevice(device_id="dev_0123456789ab"), + # Pasted MCP JSON (TD-5253). The text here holds no token. + "preview_mcp_import": PreviewMcpImport(text='{"mcpServers": {"files": {"command": "uvx"}}}'), + "import_mcp_servers": ImportMcpServers( + text='{"mcpServers": {"files": {"command": "uvx"}}}', + servers=[McpImportChoice(source_name="files", id="files")], + ), + # What a paired phone may change (TD-5279): on for an hour. + "set_phone_permission": SetPhonePermission( + capability="mcp_servers", enabled=True, expires_in_seconds=3600 + ), # Other computers (TD-5015). Loopback only; the code is a placeholder. "add_machine": AddMachine(link="tstdesk://pair?h=100.64.1.5&p=39079&c=ABCD2345&n=tower"), "add_machine_address": AddMachine(host="100.64.1.5", port=39079, code="ABCD-2345"), @@ -1567,6 +1608,21 @@ id="dev_0123456789ab", name="Pixel", paired_at="2026-10-14T10:41:00Z" ) ), + "mcp_import_preview": McpImportPreview( + servers=[ + McpImportServer( + source_name="GitHub", + id="github", + command=["npx", "-y", "server-github"], + values=[McpImportValue(name="GITHUB_TOKEN", target="env", disposition="keychain")], + ) + ] + ), + "phone_change": PhoneChange( + device_name="Pixel", + capability="mcp_servers", + summary="added MCP server 'files': npx files-server", + ), # One connected computer and one self-pair that is unreachable (TD-5015). "machine_list": MachineList( machines=[ @@ -1622,6 +1678,23 @@ error="The server refused the key. Check it and try again.", ), "model_tested": ModelTested(preset="tst-default", ok=True, reply="I am working."), + "preset_roles": PresetRoles( + preset="budget", + tier_slugs={"brain": "vendor/model-a", "worker": None}, + tier_credentials={"brain": "openrouter", "worker": None}, + tier_loopback={"brain": False, "worker": True}, + ), + # TD-5240: whether each model on the picker can run right now + "preset_readiness": PresetReadiness( + rows=[ + PresetReady(preset="tst-default", status="ready"), + PresetReady( + preset="local", + status="server_down", + detail="Nothing is answering at 127.0.0.1:11434. Start the model server.", + ), + ] + ), # TD-5203: a two-model slice of the editor's schema, and an edit's ack. "config_editor": ConfigEditor( root="ModelConfig", diff --git a/core/tests/test_add_grok_preset.py b/core/tests/test_add_grok_preset.py new file mode 100644 index 00000000..79798b35 --- /dev/null +++ b/core/tests/test_add_grok_preset.py @@ -0,0 +1,164 @@ +"""Put the Grok CLI on the model list from Settings → Engine (TD-5258). + +One narrow verb writes an entry that is ``engine: grok`` and nothing else +(TD-5239). It is offered only where the Grok CLI is found, and once. +""" + +from __future__ import annotations + +from pathlib import Path + +import pytest + +from tests.catalog_helpers import catalog_config_yaml +from tests.test_add_model import _ask, _start, _stop +from tstd.config import ConfigError, default_config_yaml, load_config +from tstd.mock import MockProvider, Script +from tstd.peer_policy import MACHINE_PEER_VERBS +from tstd.preset_write import add_grok_preset + +DESCRIPTION = "The Grok CLI, with its own model" + + +def _write(tmp_path: Path, text: str) -> Path: + path = tmp_path / "config.yaml" + path.write_text(text, encoding="utf-8") + return path + + +class TestTheWriter: + def test_into_the_shipped_empty_catalog(self, tmp_path: Path) -> None: + shipped = default_config_yaml() + path = _write(tmp_path, shipped) + add_grok_preset("grok", "Grok", DESCRIPTION, path) + block = ( + '\npresets:\n grok:\n label: "Grok"\n' + f' description: "{DESCRIPTION}"\n engine: grok\n' + ) + assert path.read_text(encoding="utf-8") == shipped.replace("\npresets: {}\n", block, 1) + config = load_config(path) + entry = config.presets["grok"] + assert entry.engine == "grok" and entry.has_tiers is False + assert entry.label == "Grok" and entry.description == DESCRIPTION + # The only entry, so the loader makes it the one new chats use. + assert config.active_preset == "grok" and config.has_model() is True + assert config.requires_api_key() is False + + def test_after_the_last_entry_of_a_catalog_and_nothing_else_changes( + self, tmp_path: Path + ) -> None: + before = catalog_config_yaml() + path = _write(tmp_path, before) + was_active = load_config(path).active_preset + add_grok_preset("grok", "Grok", DESCRIPTION, path) + after = path.read_text(encoding="utf-8").split("\n") + start = after.index(" grok:") + assert after[start : start + 4] == [ + " grok:", + ' label: "Grok"', + f' description: "{DESCRIPTION}"', + " engine: grok", + ] + assert "\n".join(after[:start] + after[start + 4 :]) == before + config = load_config(path) + assert list(config.presets)[-1] == "grok" + assert config.active_preset == was_active + + def test_a_file_with_no_catalog_at_all(self, tmp_path: Path) -> None: + path = _write(tmp_path, default_config_yaml().replace("\npresets: {}\n", "\n", 1)) + add_grok_preset("grok", "Grok", "", path) + entry = load_config(path).presets["grok"] + assert entry.engine == "grok" and entry.description is None + + def test_a_label_cannot_inject_yaml(self, tmp_path: Path) -> None: + path = _write(tmp_path, default_config_yaml()) + add_grok_preset("grok", 'G"\n engine: native', DESCRIPTION, path) + entry = load_config(path).presets["grok"] + assert entry.label == 'G"\n engine: native' and entry.engine == "grok" + + def test_once_is_enough(self, tmp_path: Path) -> None: + path = _write(tmp_path, default_config_yaml()) + add_grok_preset("grok", "Grok Build", DESCRIPTION, path) + text = path.read_text(encoding="utf-8") + with pytest.raises(ConfigError, match=r"already one of your models \(Grok Build\)"): + add_grok_preset("grok-2", "Grok", DESCRIPTION, path) + assert path.read_text(encoding="utf-8") == text + + @pytest.mark.parametrize( + ("name", "label", "says"), + [ + ("local", "Grok", "already exists"), + ("Not A Slug", "Grok", "lowercase slug"), + ("grok", " ", "Give the model a name"), + ], + ) + def test_refusals_write_nothing(self, tmp_path: Path, name: str, label: str, says: str) -> None: + path = _write(tmp_path, catalog_config_yaml()) + with pytest.raises(ConfigError, match=says): + add_grok_preset(name, label, DESCRIPTION, path) + assert path.read_text(encoding="utf-8") == catalog_config_yaml() + + +def _mock() -> MockProvider: + return MockProvider(default=Script(kind="text")) + + +def _grok(monkeypatch: pytest.MonkeyPatch, *, found: bool) -> None: + result = (Path("/usr/bin/grok"), True) if found else (None, False) + monkeypatch.setattr("tstd.daemon._probe_grok", lambda _configured: result) + + +class TestOverTheWire: + async def test_it_adds_the_entry_and_leaves_the_active_model( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + _grok(monkeypatch, found=True) + daemon, task, ws = await _start(tmp_path, _mock()) + try: + before = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + assert before["preset_engines"] == {} + state = await _ask(ws, {"type": "add_grok_preset"}, "setup_state") + assert state["type"] == "setup_state" + assert state["presets"] == sorted([*before["presets"], "grok"]) + assert state["preset_engines"] == {"grok": "grok"} + assert state["preset_labels"]["grok"] == "Grok" + assert state["preset_descriptions"]["grok"] == DESCRIPTION + assert state["active_preset"] == before["active_preset"] + assert state["engine"] == before["engine"] + on_disk = load_config(daemon.config_path).presets["grok"] + assert on_disk.engine == "grok" and on_disk.has_tiers is False + finally: + await _stop(task, ws) + + async def test_a_second_ask_is_refused( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + _grok(monkeypatch, found=True) + daemon, task, ws = await _start(tmp_path, _mock()) + try: + await _ask(ws, {"type": "add_grok_preset", "label": "Grok Build"}, "setup_state") + text = daemon.config_path.read_text(encoding="utf-8") + reply = await _ask(ws, {"type": "add_grok_preset"}, "setup_state") + assert reply["type"] == "error" and reply["code"] == "bad_request" + assert "already one of your models (Grok Build)" in reply["message"] + assert daemon.config_path.read_text(encoding="utf-8") == text + finally: + await _stop(task, ws) + + async def test_it_is_refused_where_the_grok_cli_is_not_found( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + _grok(monkeypatch, found=False) + daemon, task, ws = await _start(tmp_path, _mock()) + try: + text = daemon.config_path.read_text(encoding="utf-8") + reply = await _ask(ws, {"type": "add_grok_preset"}, "setup_state") + assert reply["type"] == "error" and reply["code"] == "grok_unavailable" + assert "not found" in reply["message"] + assert daemon.config_path.read_text(encoding="utf-8") == text + finally: + await _stop(task, ws) + + +def test_another_computers_hub_may_not_send_it() -> None: + assert "add_grok_preset" not in MACHINE_PEER_VERBS diff --git a/core/tests/test_approval_worker.py b/core/tests/test_approval_worker.py new file mode 100644 index 00000000..cfbb87f1 --- /dev/null +++ b/core/tests/test_approval_worker.py @@ -0,0 +1,301 @@ +"""An approval card says which child is asking (TD-5268). + +A ``delegate`` or ``workflow`` child's call parks on its parent's session. +The card used to read as if the session's own agent had asked. These cases +run a real child against the session's own approval path and read the +event a window would get. +""" + +from __future__ import annotations + +import asyncio +import json +from pathlib import Path +from typing import Any + +import pytest + +from tests.test_delegate import _bind_runtime, _parent_dispatcher, _workspace +from tstd.mock import MockProvider, Script +from tstd.policy import PolicyConfig +from tstd.protocol import ApprovalRequest, ApprovalWorker, parse_daemon_event +from tstd.session import Session +from tstd.tools.delegate import ChildTask, run_child_task, worker_summary + +SHELL = Script(kind="tool_call", tool_name="shell", tool_arguments='{"command": "make test"}') +DONE = Script(kind="text", content="ran it") + + +async def _asking_parent(tmp_path: Path) -> tuple[Session, Any]: + """A parent whose dispatcher asks through the session, as the daemon wires it.""" + ws = _workspace(tmp_path) + parent = Session(str(ws)) + dispatcher = _parent_dispatcher(ws) + dispatcher.policy = PolicyConfig() + parent.policy = dispatcher.policy + dispatcher.approval_handler = parent.request_approval + # A card parks a session that is in a turn. + await parent.set_state("running") + return parent, dispatcher + + +async def _first_card(parent: Session) -> ApprovalRequest: + for _ in range(300): + cards = [e for e in parent.event_log.all_events if isinstance(e, ApprovalRequest)] + if cards: + return cards[0] + await asyncio.sleep(0.01) + raise AssertionError("no approval card was raised") + + +async def _run_and_approve(parent: Session, spec: ChildTask) -> ApprovalRequest: + running = asyncio.create_task(run_child_task(parent, spec)) + try: + card = await _first_card(parent) + parent.resolve_approval(card.tool_call_id, approved=True) + await asyncio.wait_for(running, 10) + finally: + if not running.done(): + running.cancel() + return card + + +class TestTheCard: + async def test_a_delegate_childs_card_names_the_child(self, tmp_path: Path) -> None: + parent, dispatcher = await _asking_parent(tmp_path) + mock = MockProvider(sequences={"test-worker": [SHELL, DONE]}) + _bind_runtime(parent, dispatcher, mock) + spec = ChildTask(task="Run the test suite\nand report what fails", call_id="call_ab12") + + card = await _run_and_approve(parent, spec) + + assert card.worker is not None + assert card.worker.via == "delegate" and card.worker.kind == "worker" + assert card.worker.id == "call_ab12" and card.worker.parent_call_id == "call_ab12" + assert card.worker.index is None and card.worker.agent is None + assert card.worker.label == "Run the test suite and report what fails" + assert card.worker.tier == "worker" + assert card.worker.worktree_branch is None + # The child's call id still starts with the child's own. + assert card.tool_call_id.startswith("call_ab12/") + # Every surface that only shows the summary says who asked. + assert card.summary.startswith("Worker: ") + assert "make test" in card.summary + + async def test_a_workflow_tasks_card_has_its_number_and_label(self, tmp_path: Path) -> None: + parent, dispatcher = await _asking_parent(tmp_path) + mock = MockProvider(sequences={"test-worker": [SHELL, DONE]}) + _bind_runtime(parent, dispatcher, mock) + spec = ChildTask( + task="Check the API module", + call_id="call_wf.2", + parent_call_id="call_wf", + index=2, + label="API module", + ) + + card = await _run_and_approve(parent, spec) + + assert card.worker is not None + assert card.worker.via == "workflow" + assert card.worker.index == 2 and card.worker.label == "API module" + assert card.worker.parent_call_id == "call_wf" and card.worker.id == "call_wf.2" + assert card.summary.startswith("Worker 2: ") + + async def test_the_sessions_own_card_is_as_it_was(self, tmp_path: Path) -> None: + parent, dispatcher = await _asking_parent(tmp_path) + tool = dispatcher.registry.require("shell") + from tstd.autonomy.classifier import DecisionClass + + asking = asyncio.create_task( + parent.request_approval( + "call_own", tool, {"command": "make test"}, DecisionClass.B, "Run `make test`", "B" + ) + ) + card = await _first_card(parent) + parent.resolve_approval("call_own", approved=True) + await asyncio.wait_for(asking, 5) + assert card.worker is None + assert card.summary == "Run `make test`" + + async def test_a_long_task_is_cut_and_stays_one_line(self, tmp_path: Path) -> None: + parent, dispatcher = await _asking_parent(tmp_path) + mock = MockProvider(sequences={"test-worker": [SHELL, DONE]}) + _bind_runtime(parent, dispatcher, mock) + card = await _run_and_approve(parent, ChildTask(task="word " * 200, call_id="c1")) + assert card.worker is not None + assert len(card.worker.label) <= 60 and "\n" not in card.worker.label + assert len(card.worker.task) <= 300 and card.worker.task.endswith("…") + + async def test_the_event_crosses_the_wire_and_an_older_one_still_parses( + self, tmp_path: Path + ) -> None: + parent, dispatcher = await _asking_parent(tmp_path) + mock = MockProvider(sequences={"test-worker": [SHELL, DONE]}) + _bind_runtime(parent, dispatcher, mock) + card = await _run_and_approve(parent, ChildTask(task="t", call_id="c1")) + back = parse_daemon_event(card.model_dump_json()) + assert isinstance(back, ApprovalRequest) and back.worker == card.worker + old = card.model_dump(mode="json") + del old["worker"] + import json + + again = parse_daemon_event(json.dumps(old)) + assert isinstance(again, ApprovalRequest) and again.worker is None + + +class TestTheSummary: + @pytest.mark.parametrize( + ("worker", "expected"), + [ + ({"kind": "worker"}, "Worker: Run `x`"), + ({"kind": "research"}, "Worker (research): Run `x`"), + ({"kind": "worker", "index": 3}, "Worker 3: Run `x`"), + ({"kind": "research", "index": 3}, "Worker 3 (research): Run `x`"), + ({"kind": "worker", "agent": "db-migrator"}, 'Worker "db-migrator": Run `x`'), + ({"kind": "check", "index": 2}, "Check 2: Run `x`"), + ( + {"kind": "worker", "index": 1, "worktree_branch": "tst/wt/abc"}, + "Worker 1: Run `x` in its worktree", + ), + ], + ) + def test_who_is_asking_comes_first(self, worker: dict[str, Any], expected: str) -> None: + full = ApprovalWorker(id="c", parent_call_id="c", via="workflow", **worker) + assert worker_summary(full, "Run `x`") == expected + + +class TestAlwaysAllow: + async def _proposal(self, tmp_path: Path, worker: ApprovalWorker | None) -> str | None: + parent, dispatcher = await _asking_parent(tmp_path) + + async def writable() -> bool: + return True + + parent._config_writable = writable # type: ignore[method-assign] + tool = dispatcher.registry.require("fs_write") + from tstd.autonomy.classifier import DecisionClass + + asking = asyncio.create_task( + parent.request_approval( + "c1", + tool, + {"path": "notes.md", "content": "x"}, + DecisionClass.B, + "Write notes.md", + "B", + worker=worker, + ) + ) + card = await _first_card(parent) + parent.resolve_approval("c1", approved=False) + await asyncio.wait_for(asking, 5) + return card.proposed_always_allow + + def _worker(self, **over: Any) -> ApprovalWorker: + return ApprovalWorker(id="c", parent_call_id="c", via="delegate", kind="worker", **over) + + async def test_the_sessions_own_card_offers_it(self, tmp_path: Path) -> None: + assert await self._proposal(tmp_path, None) is not None + + async def test_no_childs_card_offers_it(self, tmp_path: Path) -> None: + """TD-5281: the rule is for the whole workspace, so never from a child.""" + workers = ( + self._worker(), + self._worker(agent="db-migrator"), + self._worker(worktree_branch="tst/wt/x"), + ApprovalWorker(id="c", parent_call_id="c", via="workflow", kind="worker", index=2), + ) + for number, worker in enumerate(workers): + home = tmp_path / str(number) + home.mkdir() + assert await self._proposal(home, worker) is None, worker + + +class TestAlwaysAllowVerb: + """A frame that asks anyway (an older window) saves no workspace rule (TD-5281).""" + + async def test_a_childs_call_runs_once_and_saves_nothing(self, tmp_path: Path) -> None: + from tests.test_add_model import _ask, _stop + from tests.test_models_on_phone import _seeded + from tstd.autonomy.classifier import DecisionClass + + daemon, task, ws = await _seeded(tmp_path) + workspace = tmp_path / "ws" + workspace.mkdir() + try: + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sess = daemon.session_registry.get(opened["session_id"]) + assert sess is not None + if sess.state != "running": + await sess.set_state("running") + tool = _parent_dispatcher(workspace).registry.require("fs_write") + worker = ApprovalWorker(id="c", parent_call_id="c", via="delegate", kind="worker") + asking = asyncio.create_task( + sess.request_approval( + "child-1", + tool, + {"path": "notes.md", "content": "x"}, + DecisionClass.B, + "A worker asks: Write notes.md", + "B", + worker=worker, + ) + ) + card = await _first_card(sess) + assert card.proposed_always_allow is None + before = daemon.config_path.read_text(encoding="utf-8") + frame = {"type": "always_allow", "session_id": sess.id, "tool_call_id": "child-1"} + reply = await _ask(ws, frame, "policy_rules") + assert reply["type"] == "error" and reply["code"] == "not_always_allowable" + assert reply["message"].startswith("Approved once.") + # The person said yes to this call, so it runs; nothing else is allowed. + outcome = await asyncio.wait_for(asking, 5) + assert outcome.approved is True + assert not (workspace / ".tst" / "config.yaml").exists() or ( + "notes.md" not in (workspace / ".tst" / "config.yaml").read_text(encoding="utf-8") + ) + assert daemon.config_path.read_text(encoding="utf-8") == before + assert [r for r in sess.policy.rules if r.effect == "auto"] == [] + finally: + await _stop(task, ws) + + async def test_the_sessions_own_call_still_saves_its_rule(self, tmp_path: Path) -> None: + from tests.test_add_model import _ask, _stop + from tests.test_models_on_phone import _seeded + from tstd.autonomy.classifier import DecisionClass + + daemon, task, ws = await _seeded(tmp_path) + workspace = tmp_path / "ws" + workspace.mkdir() + try: + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sess = daemon.session_registry.get(opened["session_id"]) + assert sess is not None + if sess.state != "running": + await sess.set_state("running") + tool = _parent_dispatcher(workspace).registry.require("fs_write") + asking = asyncio.create_task( + sess.request_approval( + "own-1", + tool, + {"path": "notes.md", "content": "x"}, + DecisionClass.B, + "Write notes.md", + "B", + ) + ) + card = await _first_card(sess) + assert card.proposed_always_allow is not None + frame = {"type": "always_allow", "session_id": sess.id, "tool_call_id": "own-1"} + # A saved rule has no reply of its own: the call simply goes ahead. + await ws.send(json.dumps(frame)) + outcome = await asyncio.wait_for(asking, 5) + assert outcome.approved is True + assert [r.tool for r in sess.policy.rules if r.effect == "auto"] == ["fs_write"] + finally: + await _stop(task, ws) diff --git a/core/tests/test_cache_write_price.py b/core/tests/test_cache_write_price.py new file mode 100644 index 00000000..46feb078 --- /dev/null +++ b/core/tests/test_cache_write_price.py @@ -0,0 +1,131 @@ +"""A cache write is charged at its own price (TD-5280). + +Some providers charge more to store prompt tokens in their cache than for +plain input (1.25 times, on Anthropic models). The meter used to charge +them as plain input, so it under-reported. These pin the arithmetic, that +nothing changes for a tier that names no write price, and that a prompt +cache can be asked for through a tier's ``extra_body``. +""" + +from __future__ import annotations + +from typing import Any + +import pytest +from pydantic import ValidationError + +from tests.test_cost import BRAIN +from tests.test_request_extras import _tier +from tstd.config import TierConfig +from tstd.cost import compute_call_cost, compute_call_details, prompt_bill +from tstd.provider import ChatCompletionRequest, Usage + +# $3.00 in, $15.00 out, $0.30 read, $3.75 write: a 1.25 times write. +PRICED = BRAIN.model_copy( + update={ + "input_price": 3.00, + "output_price": 15.00, + "cache_read_price": 0.30, + "cache_write_price": 3.75, + } +) + + +def _usage(**details: Any) -> Usage: + return Usage.from_api_dict( + { + "prompt_tokens": 10_000, + "completion_tokens": 1_000, + "total_tokens": 11_000, + "prompt_tokens_details": details, + } + ) + + +def test_the_write_count_is_read_from_the_usage_block() -> None: + assert _usage(cached_tokens=0, cache_write_tokens=8_000).cache_write_tokens == 8_000 + assert _usage(cached_tokens=6_000).cache_write_tokens is None + assert Usage.from_api_dict({"prompt_tokens": 5}).cache_write_tokens is None + assert Usage.from_api_dict(None).cache_write_tokens is None + + +@pytest.mark.parametrize("bad", [-1, "8000", 8000.5, True, None, [8000]]) +def test_a_write_count_that_is_not_a_plain_count_is_ignored(bad: object) -> None: + assert _usage(cached_tokens=0, cache_write_tokens=bad).cache_write_tokens is None + + +def test_a_first_call_that_writes_the_cache() -> None: + usage = _usage(cached_tokens=0, cache_write_tokens=8_000) + bill = prompt_bill(usage, PRICED) + assert (bill.cached_tokens, bill.uncached_tokens, bill.written_tokens) == (0, 10_000, 8_000) + # 2,000 plain at $3.00 + 8,000 written at $3.75 + 1,000 out at $15.00 + expected = (2_000 * 3.00 + 8_000 * 3.75 + 1_000 * 15.00) / 1_000_000 + assert compute_call_cost(usage, PRICED) == pytest.approx(expected) + assert compute_call_cost(usage, PRICED) == pytest.approx(0.051) + + +def test_a_later_call_that_reads_it() -> None: + usage = _usage(cached_tokens=8_000, cache_write_tokens=500) + # 8,000 read at $0.30 + 500 written at $3.75 + 1,500 plain at $3.00 + output + expected = (8_000 * 0.30 + 500 * 3.75 + 1_500 * 3.00 + 1_000 * 15.00) / 1_000_000 + assert compute_call_cost(usage, PRICED) == pytest.approx(expected) + + +def test_the_breakdown_still_adds_up() -> None: + usage = _usage(cached_tokens=8_000, cache_write_tokens=500) + parts = compute_call_details(usage, PRICED) + assert parts["uncached_prompt_tokens"] == 2_000 + assert parts["prompt_cost"] + parts["cached_cost"] + parts["completion_cost"] == ( + pytest.approx(parts["total_cost"]) + ) + assert parts["total_cost"] == compute_call_cost(usage, PRICED) + + +def test_no_write_price_charges_a_write_as_plain_input() -> None: + """A tier written before the field existed costs what it always did.""" + assert BRAIN.cache_write_price is None + with_writes = _usage(cached_tokens=2_000, cache_write_tokens=6_000) + without = _usage(cached_tokens=2_000) + assert compute_call_cost(with_writes, BRAIN) == compute_call_cost(without, BRAIN) + old = (8_000 * BRAIN.input_price + 2_000 * BRAIN.cache_read_price) / 1_000_000 + ( + 1_000 * BRAIN.output_price / 1_000_000 + ) + assert compute_call_cost(with_writes, BRAIN) == pytest.approx(old) + + +def test_no_reported_writes_changes_nothing_on_a_priced_tier() -> None: + usage = _usage(cached_tokens=2_000) + expected = (8_000 * 3.00 + 2_000 * 0.30 + 1_000 * 15.00) / 1_000_000 + assert compute_call_cost(usage, PRICED) == pytest.approx(expected) + + +def test_a_write_count_larger_than_the_prompt_is_capped() -> None: + """A provider's bad figure cannot bill more tokens than the prompt had.""" + usage = _usage(cached_tokens=4_000, cache_write_tokens=50_000) + bill = prompt_bill(usage, PRICED) + assert bill.written_tokens == 6_000 + expected = (4_000 * 0.30 + 6_000 * 3.75 + 1_000 * 15.00) / 1_000_000 + assert compute_call_cost(usage, PRICED) == pytest.approx(expected) + + +def test_a_free_write_can_be_said() -> None: + free = PRICED.model_copy(update={"cache_write_price": 0.0}) + usage = _usage(cached_tokens=0, cache_write_tokens=10_000) + assert compute_call_cost(usage, free) == pytest.approx(1_000 * 15.00 / 1_000_000) + + +def test_a_negative_write_price_is_refused() -> None: + fields = BRAIN.model_dump() + fields["cache_write_price"] = -1.0 + with pytest.raises(ValidationError): + TierConfig(**fields) + + +def test_a_prompt_cache_can_be_asked_for_through_extra_body() -> None: + """``cache_control`` goes out as a top-level field of the request.""" + tier = _tier("anthropic/some-model", 1_000, {"cache_control": {"type": "ephemeral"}}) + body = ChatCompletionRequest( + model=tier.require_slug(), messages=[], max_tokens=64, extra_body=tier.extra_body + ).to_dict() + assert body["cache_control"] == {"type": "ephemeral"} + assert body["model"] == "anthropic/some-model" and body["max_tokens"] == 64 diff --git a/core/tests/test_delegate.py b/core/tests/test_delegate.py index bb7f10ad..6b4e18b6 100644 --- a/core/tests/test_delegate.py +++ b/core/tests/test_delegate.py @@ -17,7 +17,7 @@ ) from tstd.autonomy import Boundary, DecisionClass, DecisionClassifier from tstd.boundary_config import CapsSection -from tstd.config import SearchConfig +from tstd.config import DelegateConfig, SearchConfig from tstd.context import PromptAssembler from tstd.context.tokens import HeuristicTokenCounter from tstd.cost import CostTracker @@ -475,3 +475,70 @@ async def chat_completion(self, request): # type: ignore[no-untyped-def] await parent.stop_turn() assert await asyncio.wait_for(child, 2) == "Worker stopped: parent stopped the turn." + + +# ── delegate.max_iterations (TD-5265) ─────────────────────────────────── + + +def _never_done() -> MockProvider: + """A worker that asks for one more read, however often it is called.""" + read = Script(kind="tool_call", tool_name="fs_read", tool_arguments='{"path": "notes.md"}') + return MockProvider(default=read) + + +async def test_a_child_stops_after_eight_calls_by_default(tmp_path: Path) -> None: + ws = _workspace(tmp_path) + parent = Session(str(ws)) + mock = _never_done() + runtime = _bind_runtime(parent, _parent_dispatcher(ws), mock) + assert runtime.config.delegate.max_iterations == CHILD_MAX_ITERATIONS == 8 + + result = await run_worker_child(parent=parent, task="read forever") + + assert len(mock.calls) == 8 + assert "all 8 of its model calls (delegate.max_iterations)" in result + + +@pytest.mark.parametrize("limit", [1, 3, 12]) +async def test_a_child_runs_as_many_calls_as_config_allows(tmp_path: Path, limit: int) -> None: + ws = _workspace(tmp_path) + parent = Session(str(ws)) + mock = _never_done() + runtime = _bind_runtime(parent, _parent_dispatcher(ws), mock) + runtime.config = runtime.config.model_copy( + update={"delegate": DelegateConfig(max_iterations=limit)} + ) + + result = await run_worker_child(parent=parent, task="read forever") + + assert len(mock.calls) == limit + assert f"all {limit} of its model calls" in result + + +async def test_a_larger_number_cannot_outrun_the_sessions_own_cap(tmp_path: Path) -> None: + ws = _workspace(tmp_path) + parent = Session(str(ws)) + parent.boundary_config.caps = CapsSection(max_iterations=5) + mock = _never_done() + runtime = _bind_runtime(parent, _parent_dispatcher(ws), mock) + runtime.config = runtime.config.model_copy( + update={"delegate": DelegateConfig(max_iterations=40)} + ) + + result = await run_worker_child(parent=parent, task="read forever") + + assert len(mock.calls) == 5 + assert "iteration cap exceeded: 5 >= 5" in result + + +@pytest.mark.parametrize("bad", [0, -1, 65]) +def test_the_number_is_one_to_sixty_four(bad: int) -> None: + with pytest.raises(ValueError): + DelegateConfig(max_iterations=bad) + + +def test_the_shipped_config_sets_the_default_and_documents_it() -> None: + from tstd.config import default_config_yaml + + shipped = default_config_yaml() + assert "\ndelegate:\n max_iterations: 8\n" in shipped diff --git a/core/tests/test_distill_entry.py b/core/tests/test_distill_entry.py new file mode 100644 index 00000000..6f1a345b --- /dev/null +++ b/core/tests/test_distill_entry.py @@ -0,0 +1,170 @@ +"""Memory distill runs on the chat's own model (TD-5259). + +Distill used to take the daemon's active entry whatever the chat was on, +so a chat kept on a local model had its conversation sent to a hosted one +for distilling, or the other way round. These check which entry is asked, +and which model server. +""" + +from __future__ import annotations + +from pathlib import Path +from typing import Any + +import pytest + +from tests.test_add_model import _ask, _stop +from tests.test_preset_engine import _seeded, _text +from tstd.config import ModelConfig, TierConfig +from tstd.daemon import Daemon +from tstd.session import Session + +Asked = list[tuple[str, str, str]] + + +def _watch(daemon: Daemon, monkeypatch: pytest.MonkeyPatch) -> Asked: + """Each distill as ``(entry, the base_url its client was made for, worker slug)``.""" + seen: Asked = [] + made_for: list[str] = [] + real = daemon._client_for + + async def client_for(tier: TierConfig) -> Any: + made_for.append(tier.base_url) + return await real(tier) + + async def fake_distill(session: Session, provider: Any, config: ModelConfig) -> None: + seen.append((config.active_preset, made_for[-1], config.tier("worker").slug)) + return None + + monkeypatch.setattr(daemon, "_client_for", client_for) + monkeypatch.setattr("tstd.daemon.distill_if_due", fake_distill) + monkeypatch.setattr("tstd.daemon.completed_turn_count", lambda _s: 1) + return seen + + +async def _chat(daemon: Daemon, ws: Any, workspace: Path) -> Session: + opened = await _ask(ws, {"type": "open_workspace", "path": str(workspace)}, "session_state") + sess = daemon.session_registry.get(opened["session_id"]) + assert sess is not None + return sess + + +def _where(daemon: Daemon, entry: str) -> tuple[str, str, str]: + preset = daemon.config.presets[entry] + assert preset.brain is not None and preset.worker is not None + return entry, preset.brain.base_url, preset.worker.slug + + +async def test_a_chat_on_another_entry_is_distilled_by_that_entry( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + seen = _watch(daemon, monkeypatch) + sess = await _chat(daemon, ws, workspace) + assert daemon.config.active_preset == "tst-default" + daemon._bind_session_preset(sess, "local") + await daemon._run_distill(sess) + # The local entry's own server, not the active entry's. + assert seen == [_where(daemon, "local")] + assert _where(daemon, "local")[1] != _where(daemon, "tst-default")[1] + finally: + await _stop(task, ws) + + +async def test_a_chat_on_the_active_entry_is_unchanged( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + seen = _watch(daemon, monkeypatch) + sess = await _chat(daemon, ws, workspace) + await daemon._run_distill(sess) + assert seen == [_where(daemon, "tst-default")] + finally: + await _stop(task, ws) + + +async def test_it_follows_the_entry_the_chat_is_on_now( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + seen = _watch(daemon, monkeypatch) + sess = await _chat(daemon, ws, workspace) + daemon._bind_session_preset(sess, "local") + await daemon._run_distill(sess) + daemon._bind_session_preset(sess, "budget") + await daemon._run_distill(sess) + assert seen == [_where(daemon, "local"), _where(daemon, "budget")] + finally: + await _stop(task, ws) + + +async def test_the_active_entry_changing_does_not_move_another_chats_distill( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + seen = _watch(daemon, monkeypatch) + sess = await _chat(daemon, ws, workspace) + daemon._bind_session_preset(sess, "local") + await _ask(ws, {"type": "set_preset", "name": "budget"}, "setup_state") + assert daemon.config.active_preset == "budget" + await daemon._run_distill(sess) + assert seen == [_where(daemon, "local")] + finally: + await _stop(task, ws) + + +async def test_a_grok_chat_keeps_the_active_entry( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """The Grok engine has no model of its own here: the TD-5239 rule stands.""" + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + seen = _watch(daemon, monkeypatch) + sess = await _chat(daemon, ws, workspace) + # On the Grok entry, and also a Grok chat that names an entry with a model. + daemon._bind_session_preset(sess, "grok") + sess.engine = "grok" + await daemon._run_distill(sess) + daemon._bind_session_preset(sess, "local") + sess.engine = "grok" + await daemon._run_distill(sess) + assert seen == [_where(daemon, "tst-default")] * 2 + finally: + await _stop(task, ws) + + +async def test_a_chat_whose_entry_was_removed_falls_back( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + seen = _watch(daemon, monkeypatch) + sess = await _chat(daemon, ws, workspace) + daemon._bind_session_preset(sess, "vllm") + gone = await _ask(ws, {"type": "delete_preset", "name": "vllm"}, "setup_state") + assert gone["type"] == "setup_state", gone + assert "vllm" not in daemon.config.presets + sess.preset = "vllm" + await daemon._run_distill(sess) + assert seen == [_where(daemon, "tst-default")] + finally: + await _stop(task, ws) + + +async def test_no_model_anywhere_is_still_skipped( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + only_grok = "presets:\n grok:\n engine: grok\nactive_preset: grok\n" + assert "tst-default" in _text() + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch, only_grok) + try: + seen = _watch(daemon, monkeypatch) + sess = await _chat(daemon, ws, workspace) + await daemon._run_distill(sess) + assert seen == [] + finally: + await _stop(task, ws) diff --git a/core/tests/test_endpoint_gate.py b/core/tests/test_endpoint_gate.py new file mode 100644 index 00000000..382747b9 --- /dev/null +++ b/core/tests/test_endpoint_gate.py @@ -0,0 +1,313 @@ +"""How many model calls one server answers at a time (TD-5269). + +One count per server, shared by every client in the daemon. These cases +are which URLs are one server, what config gives each, and that the count +really holds calls back, streams included, and lets go when a call ends, +fails or is cancelled. +""" + +from __future__ import annotations + +import asyncio +from pathlib import Path +from typing import Any + +import httpx +import pytest + +from tests.catalog_helpers import catalog_config_yaml +from tstd.config import ConfigError, load_config +from tstd.endpoint_gate import ( + EndpointGates, + EndpointLimit, + ModelConcurrencyConfig, + endpoint_key, +) +from tstd.provider import ChatCompletionRequest, ChatMessage, ProviderClient, ProviderError + +LOCAL = "http://127.0.0.1:8080/v1" + + +class TestWhichUrlsAreOneServer: + @pytest.mark.parametrize( + "url", + [ + "http://127.0.0.1:8080/v1", + "http://localhost:8080", + "http://127.0.0.1:8080/other/path/", + "http://[::1]:8080/v1", + "http://user:secret@127.0.0.1:8080/v1", + ], + ) + def test_every_spelling_of_this_computer_on_one_port(self, url: str) -> None: + assert endpoint_key(url) == "http://this-computer:8080" + assert "secret" not in (endpoint_key(url) or "") + + def test_another_port_scheme_or_host_is_another_server(self) -> None: + keys = { + endpoint_key("http://127.0.0.1:8080/v1"), + endpoint_key("http://127.0.0.1:8081/v1"), + endpoint_key("https://127.0.0.1:8080/v1"), + endpoint_key("https://api.example.com/v1"), + endpoint_key("https://API.example.com:443/v2"), + endpoint_key("https://other.example.com/v1"), + } + assert len(keys) == 5 # the two api.example.com spellings are one + + @pytest.mark.parametrize("url", ["", "not a url", "ftp://host/x", "http://"]) + def test_what_names_no_server_has_no_gate(self, url: str) -> None: + assert endpoint_key(url) is None + assert EndpointGates(ModelConcurrencyConfig(loopback_default=1)).for_url(url) is None + + +class TestWhatConfigGives: + def test_no_limit_unless_config_sets_one(self) -> None: + gates = EndpointGates() + assert gates.for_url(LOCAL) is None + assert gates.for_url("https://api.example.com/v1") is None + + def test_a_default_for_this_computer_and_one_for_everything_else(self) -> None: + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=2, remote_default=8)) + assert gates.limit_for(LOCAL) == 2 + assert gates.limit_for("https://api.example.com/v1") == 8 + only_local = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + assert only_local.limit_for("https://api.example.com/v1") is None + + def test_a_server_named_by_url_has_its_own_number(self) -> None: + config = ModelConcurrencyConfig( + loopback_default=4, + endpoints=[EndpointLimit(url="http://localhost:8080", max_concurrent=1)], + ) + gates = EndpointGates(config) + assert gates.limit_for(LOCAL) == 1 + assert gates.limit_for("http://127.0.0.1:11434/v1") == 4 + + def test_every_client_for_one_server_shares_one_gate(self) -> None: + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + assert gates.for_url(LOCAL) is gates.for_url("http://localhost:8080/v2") + assert gates.for_url(LOCAL) is not gates.for_url("http://127.0.0.1:11434/v1") + + def test_a_reload_keeps_a_gate_whose_number_did_not_change(self) -> None: + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + first = gates.for_url(LOCAL) + gates.configure(ModelConcurrencyConfig(loopback_default=1, remote_default=3)) + assert gates.for_url(LOCAL) is first + gates.configure(ModelConcurrencyConfig(loopback_default=2)) + assert gates.for_url(LOCAL) is not first + gates.configure(ModelConcurrencyConfig()) + assert gates.for_url(LOCAL) is None + + @pytest.mark.parametrize("block", ["loopback_default: 0", "remote_default: 65", "limit: 1"]) + def test_a_bad_section_is_an_error_at_load(self, tmp_path: Path, block: str) -> None: + path = tmp_path / "config.yaml" + path.write_text(catalog_config_yaml() + f"\nmodel_concurrency:\n {block}\n") + with pytest.raises(ConfigError, match="model_concurrency"): + load_config(path) + + def test_the_shipped_config_limits_only_this_computers_servers(self, tmp_path: Path) -> None: + # TD-5282: two for a model served here; a provider's server is not limited. + path = tmp_path / "config.yaml" + path.write_text(catalog_config_yaml(), encoding="utf-8") + section = load_config(path).model_concurrency + assert section.loopback_default == 2 and section.remote_default is None + assert section.endpoints == [] + + +REPLY = { + "id": "x", + "choices": [ + {"index": 0, "message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"} + ], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, +} +STREAM = ( + b'data: {"id":"x","choices":[{"index":0,"delta":{"content":"ok"}}]}\n\n' + b'data: {"id":"x","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n' + b"data: [DONE]\n\n" +) + + +class _Server: + """A model server that counts how many calls it is answering at once.""" + + def __init__(self, hold: float = 0.05) -> None: + self.now = 0 + self.most = 0 + self.calls = 0 + self.hold = hold + self.fail = False + + async def __call__(self, request: httpx.Request) -> httpx.Response: + self.calls += 1 + self.now += 1 + self.most = max(self.most, self.now) + try: + await asyncio.sleep(self.hold) + if self.fail: + return httpx.Response(400, json={"error": {"message": "no"}}) + if b'"stream": true' in request.content or b'"stream":true' in request.content: + return httpx.Response( + 200, content=STREAM, headers={"content-type": "text/event-stream"} + ) + return httpx.Response(200, json=REPLY) + finally: + self.now -= 1 + + def client(self, base_url: str = LOCAL) -> ProviderClient: + http = httpx.AsyncClient(transport=httpx.MockTransport(self)) + return ProviderClient(base_url=base_url, api_key=None, client=http) + + +def _request(stream: bool = False) -> ChatCompletionRequest: + return ChatCompletionRequest( + model="m", messages=[ChatMessage(role="user", content="hi")], stream=stream + ) + + +async def _drain(client: ProviderClient) -> list[Any]: + return [chunk async for chunk in client.chat_completion_stream(_request(stream=True))] + + +class TestTheCountHolds: + async def test_with_no_gate_calls_run_side_by_side(self) -> None: + server = _Server() + clients = [server.client() for _ in range(4)] + await asyncio.gather(*(c.chat_completion(_request()) for c in clients)) + assert server.most == 4 + + @pytest.mark.parametrize("limit", [1, 2]) + async def test_calls_from_every_client_for_one_server_share_the_limit(self, limit: int) -> None: + server = _Server() + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=limit)) + clients = [server.client() for _ in range(5)] + for client in clients: + client.gate = gates.for_url(client.base_url) + replies = await asyncio.gather(*(c.chat_completion(_request()) for c in clients)) + assert all(not isinstance(r, ProviderError) for r in replies) + assert server.calls == 5 + assert server.most == limit + + async def test_a_stream_holds_its_slot_until_it_ends(self) -> None: + server = _Server() + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + clients = [server.client() for _ in range(3)] + for client in clients: + client.gate = gates.for_url(client.base_url) + streams = await asyncio.gather(*(_drain(c) for c in clients)) + assert all(streams) and server.calls == 3 + assert server.most == 1 + + async def test_another_server_is_not_held_back(self) -> None: + server = _Server() + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + here = server.client(LOCAL) + there = server.client("http://127.0.0.1:11434/v1") + here.gate, there.gate = gates.for_url(here.base_url), gates.for_url(there.base_url) + await asyncio.gather(here.chat_completion(_request()), there.chat_completion(_request())) + assert server.most == 2 + + async def test_a_failed_call_and_a_cancelled_one_give_their_slot_back(self) -> None: + server = _Server(hold=0.2) + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + gate = gates.for_url(LOCAL) + assert gate is not None + first, waiting, later = server.client(), server.client(), server.client() + for client in (first, waiting, later): + client.gate = gate + + running = asyncio.create_task(first.chat_completion(_request())) + queued = asyncio.create_task(waiting.chat_completion(_request())) + await asyncio.sleep(0.05) + # One is being answered, one is waiting for the slot. + assert server.now == 1 and gate.locked() + queued.cancel() + running.cancel() + await asyncio.gather(running, queued, return_exceptions=True) + assert not gate.locked() + + server.fail = True + server.hold = 0.0 + failed = await later.chat_completion(_request()) + assert isinstance(failed, ProviderError) + assert not gate.locked() + + +async def test_the_daemon_gives_clients_for_one_server_one_gate(tmp_path: Path) -> None: + from tstd.daemon import Daemon + + text = catalog_config_yaml().replace( + "\nmodel_concurrency:\n loopback_default: 2\n", + "\nmodel_concurrency:\n loopback_default: 1\n", + 1, + ) + assert "loopback_default: 1" in text + (tmp_path / "config.yaml").write_text(text, encoding="utf-8") + daemon = Daemon(data_dir=tmp_path) + local = daemon.config.presets["local"] + vllm = daemon.config.presets["vllm"] + assert local.brain is not None and local.worker is not None and vllm.brain is not None + brain = await daemon._build_client(local.brain) + worker = await daemon._build_client(local.worker) + other = await daemon._build_client(vllm.brain) + assert brain.gate is not None and brain.gate is worker.gate + # Another port on this computer is another server with a count of its own. + assert other.gate is not None and other.gate is not brain.gate + + +async def test_a_stream_the_reader_abandons_gives_its_slot_back_at_once() -> None: + """The reader stops after one chunk: the exchange is closed there and + then, so the slot is free and the connection hung up, not left to the + garbage collector (found by test_provider_timeouts after TD-5269).""" + server = _Server(hold=0.0) + gates = EndpointGates(ModelConcurrencyConfig(loopback_default=1)) + client = server.client() + client.gate = gates.for_url(client.base_url) + assert client.gate is not None + stream = client.chat_completion_stream(_request(stream=True)) + async for _chunk in stream: + assert client.gate.locked() + break + await stream.aclose() # type: ignore[attr-defined] + assert not client.gate.locked() + # And the next call is not left waiting. + again = await asyncio.wait_for(client.chat_completion(_request()), 2) + assert not isinstance(again, ProviderError) + + +class TestNewInstallDefault: + """TD-5282: a new install limits a local model to two calls; an old one is unchanged.""" + + def test_the_shipped_config_limits_a_local_server_to_two(self) -> None: + import yaml + + from tstd.config import default_config_yaml + + shipped = yaml.safe_load(default_config_yaml())["model_concurrency"] + assert shipped == {"loopback_default": 2, "remote_default": None, "endpoints": []} + limits = ModelConcurrencyConfig.model_validate(shipped) + gates = EndpointGates(limits) + local = gates.for_url("http://127.0.0.1:8080/v1") + assert local is not None and local._value == 2 + # Any spelling of this computer, any port: each server has its own two. + assert gates.for_url("http://localhost:8080/v1") is local + other = gates.for_url("http://127.0.0.1:11434/v1") + assert other is not None and other is not local and other._value == 2 + # A provider's server is not limited. + assert gates.for_url("https://api.example.test/v1") is None + + def test_an_older_config_file_gets_no_limit(self, tmp_path: Path) -> None: + """The section is not one the loader fills in from the shipped file.""" + from tests.catalog_helpers import catalog_config_yaml + from tstd.config import FILLED_FROM_SHIPPED, load_config + + assert "model_concurrency" not in FILLED_FROM_SHIPPED + text = catalog_config_yaml() + start = text.index("\nmodel_concurrency:") + end = text.index("\n\n", start + 1) + older = text[:start] + text[end:] + assert "model_concurrency" not in older.replace("# ", "") + path = tmp_path / "config.yaml" + path.write_text(older, encoding="utf-8") + limits = load_config(path).model_concurrency + assert limits.loopback_default is None and limits.remote_default is None + assert EndpointGates(limits).for_url("http://127.0.0.1:8080/v1") is None diff --git a/core/tests/test_fs_edit_batch.py b/core/tests/test_fs_edit_batch.py new file mode 100644 index 00000000..45e014e9 --- /dev/null +++ b/core/tests/test_fs_edit_batch.py @@ -0,0 +1,201 @@ +"""``fs_edit`` replaces every occurrence, or several things at once (TD-5270). + +A rename used to cost one call per occurrence. The tool stays one file per +call, on the same path the classifier already judges; what changes is how +much one call can do, and that a batch either all lands or none of it does. +""" + +from __future__ import annotations + +from pathlib import Path +from typing import Any + +import pytest + +from tests.test_read_tools import make_dispatcher +from tstd.autonomy import DecisionClass +from tstd.tools import create_registry, fs_edit +from tstd.tools.write import MAX_EDITS, Edit, apply_edits, edits_from_arguments + +TEXT = "alpha = 1\nbeta = alpha + 1\ngamma = alpha * beta\n" + + +def _file(tmp_path: Path, text: str = TEXT) -> Path: + target = tmp_path / "a.py" + target.write_text(text, encoding="utf-8") + return target + + +class TestReplaceAll: + async def test_every_occurrence_goes_and_the_count_is_reported(self, tmp_path: Path) -> None: + target = _file(tmp_path) + out = await fs_edit(None, str(target), "alpha", "first", replace_all=True) + assert target.read_text() == TEXT.replace("alpha", "first") + assert "replaced 3 occurrences" in out + + async def test_none_found_still_fails(self, tmp_path: Path) -> None: + target = _file(tmp_path) + with pytest.raises(ValueError, match="not found"): + await fs_edit(None, str(target), "delta", "x", replace_all=True) + assert target.read_text() == TEXT + + async def test_without_it_two_occurrences_are_still_ambiguous(self, tmp_path: Path) -> None: + target = _file(tmp_path) + with pytest.raises(ValueError, match="ambiguous: 3 occurrences") as caught: + await fs_edit(None, str(target), "alpha", "x") + assert "replace_all" in str(caught.value) + assert target.read_text() == TEXT + + +class TestABatch: + async def test_edits_apply_in_order_each_to_what_the_last_left(self, tmp_path: Path) -> None: + target = _file(tmp_path) + out = await fs_edit( + None, + str(target), + edits=[ + {"old_string": "alpha", "new_string": "a", "replace_all": True}, + {"old_string": "beta = a + 1", "new_string": "beta = a + 2"}, + {"old_string": "gamma", "new_string": "c"}, + ], + ) + assert target.read_text() == "a = 1\nbeta = a + 2\nc = a * beta\n" + assert "3 edits" in out and "replaced 3, 1, 1" in out and "5 occurrences" in out + + @pytest.mark.parametrize( + ("bad", "says"), + [ + ({"old_string": "nowhere", "new_string": "x"}, "edit 2: target string not found"), + ({"old_string": "a", "new_string": "x"}, "edit 2: target string is ambiguous"), + ], + ) + async def test_one_edit_that_cannot_apply_leaves_the_file_as_it_was( + self, tmp_path: Path, bad: dict[str, Any], says: str + ) -> None: + target = _file(tmp_path) + before = target.read_bytes() + with pytest.raises(ValueError, match=says) as caught: + await fs_edit( + None, + str(target), + edits=[ + {"old_string": "alpha = 1", "new_string": "alpha = 9"}, + bad, + {"old_string": "gamma", "new_string": "c"}, + ], + ) + assert "nothing was changed" in str(caught.value) + assert target.read_bytes() == before + + async def test_the_file_is_written_once( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + from tstd.tools import write as write_module + + writes: list[str] = [] + real = write_module._atomic_write + + def counting(path: Path, text: str) -> None: + writes.append(text) + real(path, text) + + monkeypatch.setattr(write_module, "_atomic_write", counting) + target = _file(tmp_path) + await fs_edit( + None, + str(target), + edits=[ + {"old_string": "alpha = 1", "new_string": "alpha = 2"}, + {"old_string": "gamma", "new_string": "c"}, + ], + ) + assert len(writes) == 1 and writes[0] == target.read_text() + + +class TestTheTwoForms: + @pytest.mark.parametrize( + ("kwargs", "says"), + [ + ({}, "give old_string and new_string, or a list of edits"), + ({"old_string": "alpha"}, "give old_string and new_string, or a list of edits"), + ( + { + "old_string": "a", + "new_string": "b", + "edits": [{"old_string": "a", "new_string": "b"}], + }, + "not both", + ), + ({"edits": []}, "edits is empty"), + ({"edits": [{"old_string": "", "new_string": "x"}]}, "edit 1 needs old_string"), + ({"edits": [{"new_string": "x"}]}, "edit 1 needs old_string"), + ( + {"edits": [{"old_string": "a", "new_string": "b"}] * (MAX_EDITS + 1)}, + f"too many edits: {MAX_EDITS + 1}", + ), + ], + ) + async def test_anything_else_changes_nothing( + self, tmp_path: Path, kwargs: dict[str, Any], says: str + ) -> None: + target = _file(tmp_path) + with pytest.raises(ValueError, match=says): + await fs_edit(None, str(target), **kwargs) + assert target.read_text() == TEXT + + def test_the_single_form_is_one_edit(self) -> None: + assert edits_from_arguments("a", "b", False, None) == [Edit("a", "b", False)] + assert edits_from_arguments("a", "", True, None) == [Edit("a", "", True)] + + def test_applying_is_pure(self) -> None: + text, counts = apply_edits("x x y", [Edit("x", "z", True), Edit("y", "x")], "f") + assert text == "z z x" and counts == [2, 1] + + +class TestTheSchemaAndTheGate: + def test_the_tool_is_still_one_file_judged_by_its_path(self) -> None: + tool = create_registry().require("fs_edit") + assert tool.path_fields == ("path",) + assert tool.side_effect_class == "ask" and tool.mutates is True + props = tool.parameters["properties"] + assert tool.parameters["required"] == ["path"] + assert props["edits"]["maxItems"] == MAX_EDITS == 20 + assert props["edits"]["items"]["required"] == ["old_string", "new_string"] + assert props["replace_all"]["type"] == "boolean" + + async def test_a_batch_goes_through_the_dispatcher_as_one_call( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + dispatcher = make_dispatcher(tmp_path) + monkeypatch.chdir(tmp_path) + (tmp_path / "a.txt").write_text("one two one\n", encoding="utf-8") + result = await dispatcher.dispatch( + "c1", + "fs_edit", + { + "path": "a.txt", + "edits": [ + {"old_string": "one", "new_string": "1", "replace_all": True}, + {"old_string": "two", "new_string": "2"}, + ], + }, + ) + assert result.status == "success", result.output + assert result.diff is not None and "-one two one" in result.diff + assert (tmp_path / "a.txt").read_text() == "1 2 1\n" + assert "2 edits" in result.output + + async def test_a_batch_on_a_steering_file_is_still_outside_the_wall( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + dispatcher = make_dispatcher(tmp_path) + monkeypatch.chdir(tmp_path) + (tmp_path / "AGENTS.md").write_text("rules\n", encoding="utf-8") + result = await dispatcher.dispatch( + "c1", + "fs_edit", + {"path": "AGENTS.md", "edits": [{"old_string": "rules", "new_string": "none"}]}, + ) + assert result.status != "success" + assert (tmp_path / "AGENTS.md").read_text() == "rules\n" + assert result.decision_class == DecisionClass.C.value diff --git a/core/tests/test_mcp_config_parity.py b/core/tests/test_mcp_config_parity.py new file mode 100644 index 00000000..92ab45c4 --- /dev/null +++ b/core/tests/test_mcp_config_parity.py @@ -0,0 +1,164 @@ +"""Settings does not lose an MCP server's keys (TD-5277). + +``_mcp_server_lines`` writes a server's block from a list of keys kept by +hand, and the ``set_mcp_server`` handler copies the hand-set keys across +from a second list. Both are complete today. These tests are what makes a +new key on ``McpServerConfig`` fail here until someone has decided, for +each list, what happens to it: nothing was wrong, and nothing would have +said so the day something was. +""" + +from __future__ import annotations + +from pathlib import Path +from typing import Any + +import pytest +import yaml + +from tests.catalog_helpers import catalog_config_yaml +from tests.test_add_model import _ask, _start, _stop +from tstd.config import McpServerConfig, load_config +from tstd.config_write import save_mcp_server +from tstd.mock import MockProvider, Script + +# Every field with a value that is not its default, per transport. A field +# added to the model must be added here, which is the point. +EVERY_FIELD: dict[str, dict[str, Any]] = { + "stdio": { + "transport": "stdio", + # Harmless if it ever ran: these tests start no server. + "command": ["true", "--root", "/srv/data"], + "url": "", + "enabled": False, + "timeout_seconds": 12.5, + "startup_timeout_seconds": 45.0, + "max_call_seconds": 900.0, + "env": {"LOG_LEVEL": "debug", "API_TOKEN": "keychain:token"}, + "headers": {}, + "read_only_tools": ["list_files", "read_file"], + "allow_remote": False, + }, + "http": { + "transport": "http", + "command": [], + "url": "https://mcp.example.com/mcp", + "enabled": False, + "timeout_seconds": 12.5, + "startup_timeout_seconds": 45.0, + "max_call_seconds": 900.0, + "env": {}, + "headers": {"Authorization": "keychain:bearer", "X-Team": "tools"}, + "read_only_tools": ["search"], + "allow_remote": True, + }, +} + +# What the Settings page sends, what belongs to one server's address and is +# dropped when the transport or host changes, and what is carried across a +# Settings save untouched. Every field is in exactly one. +SENT_BY_SETTINGS = {"transport", "command", "url", "enabled"} +BOUND_TO_THE_HOST = {"allow_remote", "env", "headers"} +CARRIED = {"timeout_seconds", "startup_timeout_seconds", "max_call_seconds", "read_only_tools"} + + +def _config(tmp_path: Path) -> Path: + path = tmp_path / "config.yaml" + path.write_text(catalog_config_yaml(), encoding="utf-8") + return path + + +def test_the_table_names_every_field_of_the_model() -> None: + for transport, values in EVERY_FIELD.items(): + assert set(values) == set(McpServerConfig.model_fields), transport + + +def test_every_field_is_sent_bound_or_carried_and_only_one_of_them() -> None: + assert set(McpServerConfig.model_fields) == SENT_BY_SETTINGS | BOUND_TO_THE_HOST | CARRIED + assert not SENT_BY_SETTINGS & BOUND_TO_THE_HOST + assert not SENT_BY_SETTINGS & CARRIED + assert not BOUND_TO_THE_HOST & CARRIED + + +@pytest.mark.parametrize("transport", ["stdio", "http"]) +def test_the_writer_writes_every_field_and_it_reads_back_the_same( + tmp_path: Path, transport: str +) -> None: + path = _config(tmp_path) + spec = McpServerConfig(**EVERY_FIELD[transport]) + save_mcp_server("walk", spec, path) + + block = yaml.safe_load(path.read_text(encoding="utf-8"))["mcp"]["servers"]["walk"] + # The one of command and url the transport does not use is left out, and + # a value at its default may be: everything set here is not. + unused = "url" if transport == "stdio" else "command" + empty = {name for name, value in EVERY_FIELD[transport].items() if value in ({}, [], "")} + expected = set(McpServerConfig.model_fields) - {unused} - empty + if not EVERY_FIELD[transport]["allow_remote"]: + expected -= {"allow_remote"} + assert expected <= set(block), sorted(expected - set(block)) + + assert load_config(path).mcp.servers["walk"] == spec + + +@pytest.mark.parametrize("transport", ["stdio", "http"]) +async def test_a_settings_save_changes_only_what_settings_sent( + tmp_path: Path, transport: str +) -> None: + """The handler's own list: save what Settings sends and nothing else moves. + + The server is stored switched off and stays off, so nothing is started. + """ + data = tmp_path / "data" + data.mkdir() + path = _config(data) + spec = McpServerConfig(**EVERY_FIELD[transport]) + assert spec.enabled is False + save_mcp_server("walk", spec, path) + _daemon, task, ws = await _start(data, MockProvider(default=Script(kind="text"))) + try: + wire = {name: getattr(spec, name) for name in SENT_BY_SETTINGS} + reply = await _ask(ws, {"type": "set_mcp_server", "id": "walk", **wire}, "setup_state") + assert reply["type"] == "setup_state", reply + after = load_config(path).mcp.servers["walk"] + changed = { + name + for name in McpServerConfig.model_fields + if getattr(after, name) != getattr(spec, name) + } + assert changed == set() + finally: + await _stop(task, ws) + + +async def test_a_new_address_drops_what_was_bound_to_the_old_one_and_keeps_the_rest( + tmp_path: Path, +) -> None: + data = tmp_path / "data" + data.mkdir() + path = _config(data) + spec = McpServerConfig(**EVERY_FIELD["stdio"]) + save_mcp_server("walk", spec, path) + _daemon, task, ws = await _start(data, MockProvider(default=Script(kind="text"))) + try: + reply = await _ask( + ws, + { + "type": "set_mcp_server", + "id": "walk", + "transport": "http", + "command": [], + "url": "http://127.0.0.1:9000/mcp", + "enabled": False, + }, + "setup_state", + ) + assert reply["type"] == "setup_state", reply + after = load_config(path).mcp.servers["walk"] + default = McpServerConfig(transport="http", url="http://127.0.0.1:9000/mcp") + for name in BOUND_TO_THE_HOST: + assert getattr(after, name) == getattr(default, name), name + for name in CARRIED: + assert getattr(after, name) == getattr(spec, name), name + finally: + await _stop(task, ws) diff --git a/core/tests/test_mcp_import.py b/core/tests/test_mcp_import.py new file mode 100644 index 00000000..4d2c1837 --- /dev/null +++ b/core/tests/test_mcp_import.py @@ -0,0 +1,420 @@ +"""Importing MCP servers from pasted JSON (TD-5253). + +The parser is pure and the text is hostile. These pin the shapes other +clients use, what is refused or left out, that a token lands in the +keychain and never in ``config.yaml``, a log or the preview, and who may +send the verbs. +""" + +from __future__ import annotations + +import json +import logging +from pathlib import Path +from typing import Any + +import pytest + +from tests.test_add_model import _stop +from tests.test_mcp_peer import _drain, _fresh, _pair +from tests.test_mcp_secrets import MemoryKeychain, memory_keychain # noqa: F401 +from tests.test_models_on_phone import _seeded +from tstd import mcp_import, mcp_import_apply +from tstd.config import load_config +from tstd.mcp_import import McpImportError, parse_import, slug_id, strip_json_comments + +TOKEN = "ghp_IMPORT_TEST_TOKEN_0123456789" +DESKTOP = { + "mcpServers": { + "GitHub Server_1": { + "command": "npx", + "args": ["-y", "@example/server-github"], + "env": {"GITHUB_TOKEN": TOKEN, "PORT": 8080, "DEBUG": "1"}, + } + } +} + + +def _one(data: Any) -> mcp_import.ImportServer: + [server] = parse_import(json.dumps(data)) + return server + + +# ── Reading the text ─────────────────────────────────────────────────── + + +def test_the_claude_desktop_shape() -> None: + server = _one(DESKTOP) + assert (server.source_name, server.id) == ("GitHub Server_1", "github-server-1") + assert (server.transport, server.refused) == ("stdio", "") + assert server.command == ("npx", "-y", "@example/server-github") + assert {v.name: v.disposition for v in server.values} == { + "GITHUB_TOKEN": "keychain", + "PORT": "literal", + "DEBUG": "literal", + } + + +def test_vs_code_servers_cursor_urls_and_a_bare_entry() -> None: + vscode = _one({"servers": {"files": {"type": "stdio", "command": "uvx", "args": ["files"]}}}) + assert (vscode.id, vscode.command) == ("files", ("uvx", "files")) + cursor = _one({"mcpServers": {"docs": {"url": "http://127.0.0.1:9000/mcp"}}}) + assert (cursor.transport, cursor.url, cursor.off_box) == ( + "http", + "http://127.0.0.1:9000/mcp", + False, + ) + assert cursor.notes == ("No type was given; assumed Streamable HTTP.",) + alias = _one( + {"mcpServers": {"d": {"type": "streamable-http", "url": "https://mcp.example.test/x"}}} + ) + assert alias.off_box is True and alias.refused == "" and alias.notes == () + bare = _one({"command": "node", "args": ["server.js"]}) + assert (bare.source_name, bare.id, bare.command) == ("server", "server", ("node", "server.js")) + + +def test_comments_and_trailing_commas_are_allowed() -> None: + text = """ + { + // from the README + "mcpServers": { + "web": { "url": "https://mcp.example.test/a//b", /* inline */ "type": "http", }, + }, + } + """ + [server] = parse_import(text) + assert server.url == "https://mcp.example.test/a//b" + assert ( + strip_json_comments('{"a": "x // y", "b": "/* z */",}') == '{"a": "x // y", "b": "/* z */"}' + ) + + +@pytest.mark.parametrize( + ("entry", "why"), + [ + ({"type": "sse", "url": "https://x.example.test/sse"}, "SSE"), + ({"type": "ws", "url": "wss://x.example.test"}, "WebSocket"), + ({"url": "https://x.example.test", "headersHelper": "sh get.sh"}, "command to run"), + ({"url": "https://x.example.test", "oauth": {"clientId": "a"}}, "OAuth"), + ({"command": "npx", "auth": {"CLIENT_ID": "a"}}, "OAuth"), + ({"url": "http://x.example.test/mcp"}, "https"), + ({"command": "${userHome}/bin/server"}, "placeholder"), + ({"command": "node", "args": ["${workspaceFolder}/s.js"]}, "placeholder"), + ({"url": "https://${HOST}/mcp"}, "placeholder"), + ({"command": ""}, "no command"), + ({"command": "node", "args": "server.js"}, "args"), + ({"command": "node", "args": [True]}, "args"), + ({"type": "carrier-pigeon", "command": "x"}, "type"), + ({"command": "x", "env": {"BAD NAME": "v"}}, "env name"), + ( + {"url": "https://x.example.test", "headers": {"Mcp-Session-Id": "abcdefgh1"}}, + "set by TST Desk", + ), + ("npx server", "not a JSON object"), + ], +) +def test_what_is_refused(entry: Any, why: str) -> None: + server = _one({"mcpServers": {"s": entry}}) + assert server.refused and why in server.refused, server.refused + + +def test_what_is_left_out_is_said() -> None: + server = _one( + { + "mcpServers": { + "s": { + "command": "sh", + "args": ["-c", "x"], + "envFile": ".env", + "cwd": "/tmp", + "timeout": 5, + "sandboxEnabled": True, + "headers": {"X": "abcdefgh1"}, + } + } + } + ) + assert server.refused == "" + assert set(server.not_imported) == {"envFile", "cwd", "timeout", "sandboxEnabled", "headers"} + assert any("sandbox" in note for note in server.notes) + assert any("runs a shell" in note for note in server.notes) + + +def test_values_that_must_be_typed() -> None: + server = _one( + { + "mcpServers": { + "s": { + "command": "x", + "env": { + "API_KEY": "${MY_KEY}", + "OTHER": "${env:OTHER:-d}", + "INPUT": "${input:token}", + "SHORT_TOKEN": "abc", + "MODE": "fast", + "NOTHING": None, + }, + } + } + } + ) + assert {v.name: v.disposition for v in server.values} == { + "API_KEY": "needs_input", + "OTHER": "needs_input", + "INPUT": "needs_input", + "SHORT_TOKEN": "needs_input", + "MODE": "literal", + } + header = _one( + {"mcpServers": {"h": {"url": "https://x.example.test", "headers": {"X-Mode": "a"}}}} + ) + assert [v.disposition for v in header.values] == ["needs_input"] + + +def test_ids_are_slugs_and_never_collide() -> None: + assert slug_id("My Server_1") == "my-server-1" + assert slug_id("123") == "mcp-123" + assert slug_id("***") == "mcp" + assert len(slug_id("x" * 80)) <= 32 + servers = parse_import( + json.dumps({"mcpServers": {"A b": {"command": "x"}, "a-b": {"command": "y"}}}) + ) + assert [s.id for s in servers] == ["a-b", "a-b-2"] + + +@pytest.mark.parametrize( + "text", + ["", "not json", "[1, 2]", "{}", '{"mcpServers": {}}', '{"mcpServers": 3}', "{" * 100_000], +) +def test_text_that_is_not_a_server_list_is_refused_without_quoting_it(text: str) -> None: + with pytest.raises(McpImportError) as caught: + parse_import(text) + assert TOKEN not in str(caught.value) and len(str(caught.value)) < 200 + + +def test_a_parse_error_gives_a_position_and_never_the_text() -> None: + with pytest.raises(McpImportError) as caught: + parse_import('{"mcpServers": {"s": {"env": {"K": "' + TOKEN + '" oops}}}}') + assert "line 1" in str(caught.value) and TOKEN not in str(caught.value) + + +def test_caps() -> None: + many = {"mcpServers": {f"s{i}": {"command": "x"} for i in range(mcp_import.MAX_SERVERS + 1)}} + with pytest.raises(McpImportError, match="more than"): + parse_import(json.dumps(many)) + with pytest.raises(McpImportError, match="KiB"): + parse_import(" " * (mcp_import.MAX_TEXT_BYTES + 1)) + + +def test_a_value_is_never_in_a_repr_or_a_refusal() -> None: + server = _one(DESKTOP) + assert TOKEN not in repr(server) + bad = _one({"mcpServers": {"s": {"command": "x", "env": {"K": [TOKEN]}}}}) + assert bad.refused and TOKEN not in bad.refused + + +def test_the_parser_opens_nothing() -> None: + """Neither module imports anything that reaches a network, a process or a file.""" + import ast + + allowed = { + "__future__", + "json", + "re", + "dataclasses", + "typing", + "collections.abc", + "pathlib", + "mcp_values", + "net", + "", + "keychain_mcp", + "config", + "config_write", + "logging", + "mcp_import", + } + for module in (mcp_import, mcp_import_apply): + tree = ast.parse(Path(module.__file__ or "").read_text(encoding="utf-8")) + for node in ast.walk(tree): + if isinstance(node, ast.Import): + names = [alias.name for alias in node.names] + elif isinstance(node, ast.ImportFrom): + names = [node.module or ""] + else: + continue + for name in names: + assert name in allowed, f"{module.__name__} imports {name}" + assert "open(" not in Path(mcp_import.__file__ or "").read_text(encoding="utf-8") + + +# ── At the socket ────────────────────────────────────────────────────── + + +def _frame(data: Any = DESKTOP, **choice: Any) -> dict[str, Any]: + [server] = parse_import(json.dumps(data)) + return { + "type": "import_mcp_servers", + "text": json.dumps(data), + "servers": [{"source_name": server.source_name, "id": server.id, **choice}], + } + + +async def test_the_preview_carries_no_value_and_saves_nothing( + tmp_path: Path, + memory_keychain: MemoryKeychain, # noqa: F811 +) -> None: + daemon, task, ws = await _seeded(tmp_path) + try: + before = daemon.config_path.read_text(encoding="utf-8") + frame = {"type": "preview_mcp_import", "text": json.dumps(DESKTOP)} + preview = await _fresh(ws, frame, "mcp_import_preview") + assert preview["type"] == "mcp_import_preview" and preview["error"] == "" + [server] = preview["servers"] + assert (server["id"], server["exists"], server["off_box"]) == ( + "github-server-1", + False, + False, + ) + assert server["command"] == ["npx", "-y", "@example/server-github"] + assert {v["name"]: v["disposition"] for v in server["values"]} == { + "GITHUB_TOKEN": "keychain", + "PORT": "literal", + "DEBUG": "literal", + } + assert TOKEN not in json.dumps(preview) + assert all("value" not in v for v in server["values"]) + assert daemon.config_path.read_text(encoding="utf-8") == before + assert memory_keychain._secrets == {} + broken = await _fresh( + ws, {"type": "preview_mcp_import", "text": "{nope"}, "mcp_import_preview" + ) + assert broken["servers"] == [] and "not JSON" in broken["error"] + finally: + await _stop(task, ws) + + +async def test_an_import_puts_the_token_in_the_keychain_and_only_a_reference_in_config( + tmp_path: Path, + memory_keychain: MemoryKeychain, # noqa: F811 + caplog: pytest.LogCaptureFixture, +) -> None: + daemon, task, ws = await _seeded(tmp_path) + caplog.set_level(logging.DEBUG) + try: + state = await _fresh(ws, _frame()) + assert state["type"] == "setup_state", state + [row] = [r for r in state["mcp_servers"] if r["id"] == "github-server-1"] + assert row["command"] == ["npx", "-y", "@example/server-github"] + spec = load_config(daemon.config_path).mcp.servers["github-server-1"] + assert spec.env == {"GITHUB_TOKEN": "keychain:github_token", "PORT": "8080", "DEBUG": "1"} + assert spec.allow_remote is False and spec.enabled is True + text = daemon.config_path.read_text(encoding="utf-8") + assert TOKEN not in text + assert TOKEN in memory_keychain._secrets.values() + assert any( + "tst-mcp.github-server-1.github_token" in key for key in memory_keychain._secrets + ) + assert TOKEN not in json.dumps(state) + assert TOKEN not in caplog.text + finally: + await _stop(task, ws) + + +async def test_what_an_import_refuses_writes_nothing( + tmp_path: Path, + memory_keychain: MemoryKeychain, # noqa: F811 +) -> None: + daemon, task, ws = await _seeded(tmp_path) + try: + before = daemon.config_path.read_text(encoding="utf-8") + remote = { + "mcpServers": { + "web": { + "url": "https://mcp.example.test/x", + "headers": {"Authorization": "Bearer " + TOKEN}, + } + } + } + needs = {"mcpServers": {"s": {"command": "x", "env": {"API_KEY": "${KEY}"}}}} + attempts = [ + (_frame(remote), "another machine"), + (_frame(needs), "type a value for API_KEY"), + (_frame(needs, inputs={"API_KEY": "short"}), "at least 8"), + (_frame(id="Not A Slug"), "lowercase slug"), + ( + _frame({"mcpServers": {"s": {"type": "sse", "url": "https://x.example.test"}}}), + "SSE", + ), + ({**_frame(), "servers": [{"source_name": "nobody", "id": "x"}]}, "no server named"), + ({**_frame(), "text": "{nope"}, "not JSON"), + ] + for frame, why in attempts: + reply = await _fresh(ws, frame) + assert reply["type"] == "error" and reply["code"] == "bad_request", (why, reply) + assert why in reply["message"], reply["message"] + assert TOKEN not in reply["message"] + assert daemon.config_path.read_text(encoding="utf-8") == before + assert memory_keychain._secrets == {} + # With the tick and a typed value they go in. + ok = await _fresh(ws, _frame(remote, allow_remote=True)) + assert ok["type"] == "setup_state", ok + web = load_config(daemon.config_path).mcp.servers["web"] + assert web.allow_remote is True and web.headers == { + "Authorization": "keychain:authorization" + } + ok = await _fresh(ws, _frame(needs, inputs={"API_KEY": "typed-value-0001"})) + assert ok["type"] == "setup_state", ok + assert "typed-value-0001" in memory_keychain._secrets.values() + assert "typed-value-0001" not in daemon.config_path.read_text(encoding="utf-8") + # The same id again is refused rather than replacing what is there. + again = await _fresh(ws, _frame(remote, allow_remote=True)) + assert again["code"] == "bad_request" and "already listed" in again["message"] + finally: + await _stop(task, ws) + + +async def test_a_failed_config_write_leaves_no_keychain_item( + tmp_path: Path, + memory_keychain: MemoryKeychain, # noqa: F811 + monkeypatch: pytest.MonkeyPatch, +) -> None: + _daemon, task, ws = await _seeded(tmp_path) + try: + + def boom(*_args: object, **_kwargs: object) -> None: + raise mcp_import_apply.ConfigError("disk full") + + monkeypatch.setattr(mcp_import_apply, "save_mcp_server", boom) + reply = await _fresh(ws, _frame()) + assert reply["type"] == "error" and "disk full" in reply["message"] + assert memory_keychain._secrets == {} + finally: + await _stop(task, ws) + + +async def test_from_a_phone_it_is_the_mcp_permission_and_the_computer_is_told( + tmp_path: Path, + memory_keychain: MemoryKeychain, # noqa: F811 +) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + try: + preview = {"type": "preview_mcp_import", "text": json.dumps(DESKTOP)} + for frame in (preview, _frame()): + reply = await _fresh(phone, frame, "mcp_import_preview") + assert reply["type"] == "error" and reply["code"] == "phone_permission_off" + assert memory_keychain._secrets == {} + await _fresh( + ws, {"type": "set_phone_permission", "capability": "mcp_servers", "enabled": True} + ) + await _drain(phone) + assert (await _fresh(phone, _frame()))["type"] == "setup_state" + notices = [e for e in await _drain(ws) if e["type"] == "phone_change"] + assert [n["summary"] for n in notices] == [ + "added MCP server 'github-server-1': npx -y @example/server-github" + ] + assert TOKEN not in json.dumps(notices) + finally: + await phone.close() + await _stop(task, ws) diff --git a/core/tests/test_mcp_peer.py b/core/tests/test_mcp_peer.py new file mode 100644 index 00000000..843ee84d --- /dev/null +++ b/core/tests/test_mcp_peer.py @@ -0,0 +1,443 @@ +"""A paired phone and the MCP page, at the socket (TD-5279). + +A stdio MCP server is a command the daemon runs. On main a paired phone +could add one: the verb was open to it and the handler saved and started +whatever it was sent. Now a phone is refused unless the person at the +computer switched "MCP servers" on for phones, and when a phone does use +that, the computer's window is told and the server remembers its origin. +""" + +from __future__ import annotations + +import asyncio +import contextlib +import json +from datetime import UTC, datetime, timedelta +from pathlib import Path +from typing import Any + +import pytest +import yaml +from websockets.asyncio.client import connect + +from tests.test_add_model import _ask, _stop +from tests.test_mcp_secrets import MemoryKeychain, memory_keychain # noqa: F401 +from tests.test_models_on_phone import PHONE, _seeded +from tests.test_models_on_phone import _phone as _legacy_token_client +from tstd import phone_permissions as perms +from tstd.config import load_config +from tstd.protocol import PROTOCOL_VERSION + +SERVER = {"type": "set_mcp_server", "id": "walk", "transport": "stdio", "command": ["true"]} +GRANT = {"type": "set_phone_permission", "capability": "mcp_servers", "enabled": True} +VALUE = { + "type": "set_mcp_secret", + "server_id": "walk", + "target": "env", + "name": "TOKEN", + "value": "a-test-value-0001", +} + + +async def _pair(daemon: Any, name: str = "Pixel 8", kind: str = "phone") -> tuple[Any, str]: + """A connection signed in with a paired device's own key, and the device id.""" + device, key = daemon.remote_devices.add(name, kind=kind) + ws = await connect(f"ws://127.0.0.1:{daemon.ws_server.port}{PHONE}") + await ws.send(json.dumps({"type": "hello", "token": key, "version": PROTOCOL_VERSION})) + ack = json.loads(await ws.recv()) + assert ack["type"] == "hello_ack", ack + return ws, device.id + + +async def _drain(ws: Any) -> list[dict[str, Any]]: + """Everything already on its way to *ws*: broadcasts that are not a reply.""" + seen: list[dict[str, Any]] = [] + with contextlib.suppress(TimeoutError): + while True: + seen.append(json.loads(await asyncio.wait_for(ws.recv(), 0.3))) + return seen + + +async def _fresh(ws: Any, msg: dict[str, Any], want: str = "setup_state") -> dict[str, Any]: + await _drain(ws) + return await _ask(ws, msg, want) + + +def _spy_on_reload(daemon: Any, monkeypatch: pytest.MonkeyPatch) -> list[int]: + calls: list[int] = [] + real = daemon._mcp.reload + + async def reload(config: Any) -> Any: + calls.append(len(config.servers)) + return await real(config) + + monkeypatch.setattr(daemon._mcp, "reload", reload) + return calls + + +async def test_a_phone_cannot_add_an_mcp_server_by_default( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + reloads = _spy_on_reload(daemon, monkeypatch) + try: + text = daemon.config_path.read_text(encoding="utf-8") + # On main this ran: the marker appeared on the computer. + marker = tmp_path / "ran-on-the-computer" + hostile = {**SERVER, "command": ["sh", "-c", f"touch {marker}"]} + reply = await _fresh(phone, hostile) + assert reply["type"] == "error" and reply["code"] == "phone_permission_off" + assert reply["in_reply_to"] == "set_mcp_server" + assert "Settings, Phones and devices on the computer" in reply["message"] + # Nothing saved, nothing started, nothing recorded. + assert daemon.config_path.read_text(encoding="utf-8") == text + assert load_config(daemon.config_path).mcp.servers == {} + assert reloads == [] + assert not perms.origins_path(daemon.data_dir).exists() + await asyncio.sleep(0.3) + assert not marker.exists(), "a phone made this computer run a command" + # And the phone is told the switch is off, so its page can say why. + state = await _fresh(phone, {"type": "get_setup_state"}) + assert state["phone_permissions"]["mcp_servers"] == {"enabled": False, "expires_at": None} + assert not any(p["enabled"] for p in state["phone_permissions"].values()) + finally: + await phone.close() + await _stop(task, ws) + + +async def test_a_phone_cannot_change_remove_or_set_a_value_by_default( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + try: + saved = await _fresh(ws, SERVER) + assert [row["id"] for row in saved["mcp_servers"]] == ["walk"] + text = daemon.config_path.read_text(encoding="utf-8") + reloads = _spy_on_reload(daemon, monkeypatch) + attempts = [ + {**SERVER, "command": ["sh", "-c", "id"]}, + {**SERVER, "enabled": False}, + {"type": "delete_mcp_server", "id": "walk"}, + VALUE, + {"type": "delete_mcp_secret", "server_id": "walk", "target": "env", "name": "TOKEN"}, + ] + for attempt in attempts: + reply = await _fresh(phone, attempt) + assert reply["type"] == "error", attempt["type"] + assert reply["code"] == "phone_permission_off", attempt["type"] + assert daemon.config_path.read_text(encoding="utf-8") == text + assert load_config(daemon.config_path).mcp.servers["walk"].command == ["true"] + assert reloads == [] + finally: + await phone.close() + await _stop(task, ws) + + +async def test_a_phone_cannot_grant_itself(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + token_client = await _legacy_token_client(daemon) + try: + for peer in (phone, token_client): + reply = await _fresh(peer, GRANT) + assert reply["type"] == "error" and reply["code"] == "loopback_only" + assert reply["in_reply_to"] == "set_phone_permission" + assert not perms.permissions_path(daemon.data_dir).exists() + assert daemon.phone_store.live() == frozenset() + # Still refused afterwards. + assert (await _fresh(phone, SERVER))["code"] == "phone_permission_off" + finally: + await token_client.close() + await phone.close() + await _stop(task, ws) + + +async def test_a_phone_can_add_one_after_the_computer_grants( + tmp_path: Path, + memory_keychain: MemoryKeychain, # noqa: F811 +) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, device_id = await _pair(daemon) + try: + granted = await _fresh(ws, GRANT) + assert granted["type"] == "setup_state" + assert granted["phone_permissions"]["mcp_servers"] == {"enabled": True, "expires_at": None} + on_disk = yaml.safe_load( + perms.permissions_path(daemon.data_dir).read_text(encoding="utf-8") + ) + assert on_disk["grants"]["mcp_servers"]["enabled"] is True + # The phone hears the switch without asking. + told = [e for e in await _drain(phone) if e["type"] == "setup_state"] + assert told and told[-1]["phone_permissions"]["mcp_servers"]["enabled"] is True + assert "remote_devices" not in told[-1] + + reply = await _fresh(phone, SERVER) + assert reply["type"] == "setup_state" + assert [row["id"] for row in reply["mcp_servers"]] == ["walk"] + assert reply["mcp_servers"][0]["origin_device"] == "Pixel 8" + assert load_config(daemon.config_path).mcp.servers["walk"].command == ["true"] + + # The computer's window is told which phone did what, and sees the new list. + heard = await _drain(ws) + notices = [e for e in heard if e["type"] == "phone_change"] + assert notices == [ + { + "type": "phone_change", + "seq": 1, + "device_name": "Pixel 8", + "capability": "mcp_servers", + "summary": "added MCP server 'walk': true", + } + ] + states = [e for e in heard if e["type"] == "setup_state"] + assert states and [row["id"] for row in states[-1]["mcp_servers"]] == ["walk"] + device = next(d for d in states[-1]["remote_devices"] if d["id"] == device_id) + assert device["mcp_servers_added"] == ["walk"] + + # The origin is on file, in the data dir and not in config.yaml. + origins = perms.load_origins(daemon.data_dir) + assert origins["walk"].device_id == device_id + assert "origin" not in daemon.config_path.read_text(encoding="utf-8") + + # A value set from the phone is announced by name, never by value. + stored = await _fresh(phone, VALUE) + assert stored["type"] == "setup_state", stored + notices = [e for e in await _drain(ws) if e["type"] == "phone_change"] + assert [n["summary"] for n in notices] == [ + "stored a value for variable 'TOKEN' of MCP server 'walk'." + ] + assert "a-test-value-0001" not in json.dumps(notices) + finally: + await phone.close() + await _stop(task, ws) + + +async def test_a_phone_added_server_never_gets_allow_remote(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + try: + await _fresh(ws, GRANT) + frame = {**SERVER, "allow_remote": True} + reply = await _fresh(phone, frame) + # The verb has no such field; whatever the reply, the opt-in stays off. + if reply["type"] == "setup_state": + assert load_config(daemon.config_path).mcp.servers["walk"].allow_remote is False + else: + assert load_config(daemon.config_path).mcp.servers == {} + finally: + await phone.close() + await _stop(task, ws) + + +async def test_a_grant_is_for_paired_phones_only(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + token_client = await _legacy_token_client(daemon) + machine, _ = await _pair(daemon, "Other computer", kind="machine") + try: + await _fresh(ws, GRANT) + text = daemon.config_path.read_text(encoding="utf-8") + reply = await _fresh(token_client, SERVER) + assert reply["type"] == "error" and reply["code"] == "phone_permission_off" + reply = await _fresh(machine, SERVER) + assert reply["type"] == "error" and reply["code"] == "not_allowed" + assert daemon.config_path.read_text(encoding="utf-8") == text + finally: + await machine.close() + await token_client.close() + await _stop(task, ws) + + +async def test_a_timed_grant_runs_out(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + clock = [datetime(2026, 11, 20, 18, 0, 0, tzinfo=UTC)] + monkeypatch.setattr(perms, "utc_now", lambda: clock[0]) + try: + granted = await _fresh(ws, {**GRANT, "expires_in_seconds": 3600}) + assert granted["phone_permissions"]["mcp_servers"] == { + "enabled": True, + "expires_at": "2026-11-20T19:00:00Z", + } + assert (await _fresh(phone, SERVER))["type"] == "setup_state" + clock[0] += timedelta(hours=1, seconds=1) + reply = await _fresh(phone, {**SERVER, "command": ["false"]}) + assert reply["type"] == "error" and reply["code"] == "phone_permission_off" + assert load_config(daemon.config_path).mcp.servers["walk"].command == ["true"] + state = await _fresh(ws, {"type": "get_setup_state"}) + assert state["phone_permissions"]["mcp_servers"]["enabled"] is False + finally: + await phone.close() + await _stop(task, ws) + + +async def test_the_windows_are_told_when_a_timed_grant_runs_out(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + try: + await _fresh(ws, {**GRANT, "expires_in_seconds": 1}) + await _drain(phone) + for peer in (ws, phone): + for _ in range(40): + event = json.loads(await asyncio.wait_for(peer.recv(), 10)) + if event["type"] == "setup_state": + break + assert event["phone_permissions"]["mcp_servers"]["enabled"] is False + finally: + await phone.close() + await _stop(task, ws) + + +async def test_a_bad_grant_is_refused(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + try: + unknown = await _fresh(ws, {**GRANT, "capability": "everything"}) + assert unknown["type"] == "error" and unknown["code"] == "bad_request" + too_long = await _fresh(ws, {**GRANT, "expires_in_seconds": 8 * 24 * 3600}) + assert too_long["type"] == "error" and too_long["code"] == "bad_request" + assert daemon.phone_store.live() == frozenset() + assert not perms.permissions_path(daemon.data_dir).exists() + finally: + await _stop(task, ws) + + +async def test_turning_the_grant_off_keeps_existing_servers(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + try: + await _fresh(ws, GRANT) + assert (await _fresh(phone, SERVER))["type"] == "setup_state" + off = await _fresh(ws, {**GRANT, "enabled": False}) + assert off["phone_permissions"]["mcp_servers"]["enabled"] is False + # As the disclosure says: it stops new changes, it removes nothing. + assert [row["id"] for row in off["mcp_servers"]] == ["walk"] + assert off["mcp_servers"][0]["origin_device"] == "Pixel 8" + assert load_config(daemon.config_path).mcp.servers["walk"].command == ["true"] + reply = await _fresh(phone, {"type": "delete_mcp_server", "id": "walk"}) + assert reply["type"] == "error" and reply["code"] == "phone_permission_off" + finally: + await phone.close() + await _stop(task, ws) + + +async def test_a_grant_survives_a_restart_of_the_store(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + try: + await _fresh(ws, GRANT) + from tstd.phone_store import PhoneStore + + assert PhoneStore(daemon.data_dir).live() == {"mcp_servers"} + finally: + await _stop(task, ws) + + +async def test_removing_a_device_reports_and_can_remove_what_it_added(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, device_id = await _pair(daemon) + try: + await _fresh(ws, GRANT) + await _fresh(ws, {**SERVER, "id": "mine"}) + assert (await _fresh(phone, SERVER))["type"] == "setup_state" + state = await _fresh(ws, {"type": "get_setup_state"}) + device = next(d for d in state["remote_devices"] if d["id"] == device_id) + assert device["mcp_servers_added"] == ["walk"] + + # Removing the phone alone leaves its server, still marked. + kept = await _fresh(ws, {"type": "remove_remote_device", "device_id": device_id}) + assert sorted(row["id"] for row in kept["mcp_servers"]) == ["mine", "walk"] + walk = next(row for row in kept["mcp_servers"] if row["id"] == "walk") + assert walk["origin_device"] == "Pixel 8" + assert perms.load_origins(daemon.data_dir)["walk"].device_id == device_id + finally: + with contextlib.suppress(Exception): + await phone.close() + await _stop(task, ws) + + +async def test_removing_a_device_with_what_it_added(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, device_id = await _pair(daemon) + try: + await _fresh(ws, GRANT) + await _fresh(ws, {**SERVER, "id": "mine"}) + assert (await _fresh(phone, SERVER))["type"] == "setup_state" + gone = await _fresh( + ws, {"type": "remove_remote_device", "device_id": device_id, "remove_added": True} + ) + assert gone["type"] == "setup_state" + # The phone's server goes; the computer's own stays. + assert [row["id"] for row in gone["mcp_servers"]] == ["mine"] + assert list(load_config(daemon.config_path).mcp.servers) == ["mine"] + assert perms.load_origins(daemon.data_dir) == {} + assert gone["remote_devices"] == [] + finally: + with contextlib.suppress(Exception): + await phone.close() + await _stop(task, ws) + + +async def test_a_phone_forgetting_itself_cannot_remove_servers(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, device_id = await _pair(daemon) + try: + await _fresh(ws, GRANT) + assert (await _fresh(phone, SERVER))["type"] == "setup_state" + await _fresh(ws, {**GRANT, "enabled": False}) + await _drain(phone) + await phone.send( + json.dumps( + {"type": "remove_remote_device", "device_id": device_id, "remove_added": True} + ) + ) + await asyncio.sleep(0.5) + # The grant is off: the flag from a phone removes nothing. + assert list(load_config(daemon.config_path).mcp.servers) == ["walk"] + assert daemon.remote_devices.summaries() == [] + finally: + with contextlib.suppress(Exception): + await phone.close() + await _stop(task, ws) + + +async def test_doctor_warns_while_a_phone_permission_is_on(tmp_path: Path) -> None: + _daemon, task, ws = await _seeded(tmp_path) + try: + report = await _fresh(ws, {"type": "run_diagnostics"}, "diagnostics_report") + assert "phone_permissions" not in [c["name"] for c in report["checks"]] + await _fresh(ws, GRANT) + report = await _fresh(ws, {"type": "run_diagnostics"}, "diagnostics_report") + row = next(c for c in report["checks"] if c["name"] == "phone_permissions") + assert row["status"] == "warn" + assert row["detail"] == "paired phones may change: MCP servers ON (until turned off)" + finally: + await _stop(task, ws) + + +async def test_a_phone_still_sees_the_list(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + try: + await _fresh(ws, SERVER) + state = await _fresh(phone, {"type": "get_setup_state"}) + assert [row["id"] for row in state["mcp_servers"]] == ["walk"] + assert state["mcp_servers"][0]["command"] == ["true"] + finally: + await phone.close() + await _stop(task, ws) + + +async def test_the_computer_can_still_do_all_of_it(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + try: + added = await _fresh(ws, SERVER) + assert added["type"] == "setup_state" + assert added["mcp_servers"][0]["origin_device"] == "" + assert load_config(daemon.config_path).mcp.servers["walk"].command == ["true"] + # A change at the computer is no phone change: nobody is told, nothing recorded. + assert [e for e in await _drain(ws) if e["type"] == "phone_change"] == [] + assert not perms.origins_path(daemon.data_dir).exists() + gone = await _fresh(ws, {"type": "delete_mcp_server", "id": "walk"}) + assert gone["type"] == "setup_state" and gone["mcp_servers"] == [] + finally: + await _stop(task, ws) diff --git a/core/tests/test_mcp_secrets.py b/core/tests/test_mcp_secrets.py index ed1edcfe..5e074f2e 100644 --- a/core/tests/test_mcp_secrets.py +++ b/core/tests/test_mcp_secrets.py @@ -238,11 +238,49 @@ async def test_a_secret_reaches_only_its_own_child( assert await _getenv(dispatcher, "beta", "TD5110_TOKEN") == "" assert await _getenv(dispatcher, "beta", "TD5110_MODE") == "" assert "TD5110_TOKEN" not in os.environ - # The child still has the daemon's environment underneath. + # The child still has the ordinary variables it needs to start. assert await _getenv(dispatcher, "alpha", "PATH") == os.environ["PATH"] finally: await supervisor.aclose() + async def test_a_server_does_not_get_the_daemons_own_secrets( + self, + memory_keychain: MemoryKeychain, + fake_server: list[str], + monkeypatch: pytest.MonkeyPatch, + ) -> None: + """TD-5267: a key in the daemon's environment is not every server's key.""" + monkeypatch.setenv("OPENAI_API_KEY", "sk-not-for-a-server") + monkeypatch.setenv("GITHUB_TOKEN", "ghp-not-for-a-server") + monkeypatch.setenv("MYSQL_PWD", "not-for-a-server") + monkeypatch.setenv("DATABASE_URL", "postgres://user:hunter2@db.internal/app") + monkeypatch.setenv("TD5267_ORDINARY", "fine") + config = McpConfig( + servers={ + # One with values of its own, one with none: both are scrubbed. + "alpha": McpServerConfig( + transport="stdio", command=fake_server, env={"TD5110_MODE": "plain"} + ), + "beta": McpServerConfig(transport="stdio", command=fake_server), + # A server that is given one of those names, by name, still gets it. + "gamma": McpServerConfig( + transport="stdio", command=fake_server, env={"GITHUB_TOKEN": "its-own"} + ), + } + ) + supervisor, _, dispatcher = await _load(config) + try: + assert [row.status for row in supervisor.doctor_rows()] == ["ok", "ok", "ok"] + for server in ("alpha", "beta"): + for name in ("OPENAI_API_KEY", "GITHUB_TOKEN", "MYSQL_PWD", "DATABASE_URL"): + assert await _getenv(dispatcher, server, name) == "", (server, name) + assert await _getenv(dispatcher, server, "TD5267_ORDINARY") == "fine" + assert await _getenv(dispatcher, server, "PATH") == os.environ["PATH"] + assert await _getenv(dispatcher, "gamma", "GITHUB_TOKEN") == "its-own" + assert await _getenv(dispatcher, "gamma", "OPENAI_API_KEY") == "" + finally: + await supervisor.aclose() + async def test_a_missing_item_fails_that_server_and_names_it( self, memory_keychain: MemoryKeychain, fake_server: list[str] ) -> None: diff --git a/core/tests/test_mcp_settings.py b/core/tests/test_mcp_settings.py index 92013bab..b62ac9d8 100644 --- a/core/tests/test_mcp_settings.py +++ b/core/tests/test_mcp_settings.py @@ -199,6 +199,9 @@ async def test_set_disable_delete_appear_on_setup_state( "excluded_tools": [], "env": [], "headers": [], + # TD-5279: added at the computer, so no phone to name. + "origin_device": "", + "origin_at": "", } ] row = _servers(path)["example"] diff --git a/core/tests/test_models_on_phone.py b/core/tests/test_models_on_phone.py new file mode 100644 index 00000000..1aa0baf7 --- /dev/null +++ b/core/tests/test_models_on_phone.py @@ -0,0 +1,179 @@ +"""The model picker on a paired phone (TD-5242). + +The phone runs the same window in a WebView, so the picker is the same +code. What differs is the peer: these cases are which of the model verbs a +phone may send, that it is told when the catalog changes, and that it +never makes this computer ask its own model servers. +""" + +from __future__ import annotations + +import asyncio +import json +from pathlib import Path +from typing import Any + +import pytest +from websockets.asyncio.client import connect + +from tests.test_add_model import _ask, _start, _stop +from tests.test_peer_policy import LOOPBACK_PLAIN, MACHINE, PHONE_VIEWER, PLAIN_REMOTE, _msg +from tests.test_preset_engine import _text as _catalog_with_grok +from tstd.mock import MockProvider, Script +from tstd.peer_policy import check +from tstd.protocol import PROTOCOL_VERSION + +PHONE = "/phone" + +# The verbs E54 added or changed, as a window sends them. +MAY = [ + ("switch_preset", {"name": "budget", "session_id": "s1", "make_default": True}), + ("switch_preset", {"name": "budget", "make_default": True}), + ("new_session", {"session_id": "s1", "preset": "grok"}), + ("check_presets", {}), + ("get_preset_roles", {"name": "budget"}), + ("duplicate_preset", {"source": "local", "label": "Copy"}), + ("set_preset_label", {"name": "local", "label": "Here"}), + ("add_grok_preset", {}), +] + + +class TestWhatAPhoneMaySend: + @pytest.mark.parametrize(("kind", "fields"), MAY) + def test_a_phone_may_use_the_picker_and_the_model_list( + self, kind: str, fields: dict[str, Any] + ) -> None: + for peer in (LOOPBACK_PLAIN, PHONE_VIEWER, PLAIN_REMOTE): + assert check(_msg(kind, **fields), peer, bridged=False) is None, peer + + def test_only_the_computer_removes_a_model(self) -> None: + remove = _msg("delete_preset", name="local") + assert check(remove, LOOPBACK_PLAIN, bridged=False) is None + for peer in (PHONE_VIEWER, PLAIN_REMOTE): + refusal = check(remove, peer, bridged=False) + assert refusal is not None and json.loads(refusal)["code"] == "loopback_only" + + @pytest.mark.parametrize(("kind", "fields"), [*MAY, ("delete_preset", {"name": "local"})]) + def test_another_computers_hub_may_send_none_of_them( + self, kind: str, fields: dict[str, Any] + ) -> None: + if kind == "new_session": + pytest.skip("new_session is a machine-peer verb of its own (TD-5015)") + refusal = check(_msg(kind, **fields), MACHINE, bridged=False) + assert refusal is not None + assert json.loads(refusal)["code"] in ("not_allowed", "loopback_only") + + +async def _seeded(tmp_path: Path) -> tuple[Any, Any, Any]: + data = tmp_path / "data" + data.mkdir() + (data / "config.yaml").write_text(_catalog_with_grok(), encoding="utf-8") + daemon, task, ws = await _start(data, MockProvider(default=Script(kind="text"))) + daemon.ws_server._is_remote_connection = lambda conn: ( + conn.request is not None and conn.request.path == PHONE + ) + # No Tailscale listener in a test: mint the token one would have made, + # so a connection on /phone signs in the way the older token way does. + daemon.ws_server._mint_remote_token() + return daemon, task, ws + + +async def _phone(daemon: Any) -> Any: + ws = await connect(f"ws://127.0.0.1:{daemon.ws_server.port}{PHONE}") + hello = {"type": "hello", "token": daemon.ws_server.remote_token, "version": PROTOCOL_VERSION} + await ws.send(json.dumps(hello)) + ack = json.loads(await ws.recv()) + assert ack["type"] == "hello_ack", ack + return ws + + +class TestOverTheWire: + async def test_a_phone_gets_the_sections_engines_and_slots(self, tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone = await _phone(daemon) + try: + state = await _ask(phone, {"type": "get_setup_state"}, "setup_state") + assert state["preset_groups"]["local"] == "Local" + assert state["preset_engines"] == {"grok": "grok"} + assert state["picker_groups"] == ["Local", "API", "Grok"] + # What a remote peer's setup_state always leaves out. + assert "remote_devices" not in state and "remote_port" not in state + finally: + await phone.close() + await _stop(task, ws) + + async def test_a_phone_is_told_when_the_window_changes_the_catalog( + self, tmp_path: Path + ) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone = await _phone(daemon) + try: + await _ask( + ws, + {"type": "switch_preset", "name": "budget", "make_default": True}, + "setup_state", + ) + told = await _ask(phone, {"type": "ping"}, "setup_state") + assert told["type"] == "setup_state" and told["active_preset"] == "budget" + assert "remote_devices" not in told and "remote_bind" not in told + finally: + await phone.close() + await _stop(task, ws) + + async def test_the_window_is_told_when_a_phone_changes_it(self, tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone = await _phone(daemon) + try: + reply = await _ask( + phone, + {"type": "switch_preset", "name": "budget", "make_default": True}, + "setup_state", + ) + assert reply["type"] == "setup_state" and reply["active_preset"] == "budget" + told = dict(json.loads(await asyncio.wait_for(ws.recv(), 5))) + assert told["type"] == "setup_state" and told["active_preset"] == "budget" + # The computer's own window still gets its full state. + assert "remote_devices" in told + finally: + await phone.close() + await _stop(task, ws) + + async def test_a_phone_never_makes_the_computer_ask_its_model_servers( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + asked: list[list[str]] = [] + + async def answers(urls: list[str]) -> dict[str, bool]: + asked.append(list(urls)) + return {url: False for url in urls} + + monkeypatch.setattr("tstd.daemon.loopback_answers", answers) + monkeypatch.setattr("tstd.daemon._probe_grok", lambda _configured: (None, False)) + daemon, task, ws = await _seeded(tmp_path) + phone = await _phone(daemon) + try: + far = await _ask(phone, {"type": "check_presets"}, "preset_readiness") + rows = {row["preset"]: row["status"] for row in far["rows"]} + assert asked == [] + # Keys and the Grok CLI are still reported; a server is not judged. + assert rows["local"] == "ready" and rows["vllm"] == "ready" + assert rows["grok"] == "no_grok" and rows["tst-default"] == "no_key" + + near = await _ask(ws, {"type": "check_presets"}, "preset_readiness") + assert len(asked) == 1 + assert {r["preset"]: r["status"] for r in near["rows"]}["local"] == "server_down" + finally: + await phone.close() + await _stop(task, ws) + + async def test_a_phone_cannot_remove_a_model(self, tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone = await _phone(daemon) + try: + text = daemon.config_path.read_text(encoding="utf-8") + reply = await _ask(phone, {"type": "delete_preset", "name": "local"}, "setup_state") + assert reply["type"] == "error" and reply["code"] == "loopback_only" + assert daemon.config_path.read_text(encoding="utf-8") == text + finally: + await phone.close() + await _stop(task, ws) diff --git a/core/tests/test_pairing.py b/core/tests/test_pairing.py index 4d6817ec..b65b30b1 100644 --- a/core/tests/test_pairing.py +++ b/core/tests/test_pairing.py @@ -475,6 +475,7 @@ def test_pairing_completed_carries_a_summary_and_no_key() -> None: "last_seen", "connected", "kind", + "mcp_servers_added", } @@ -626,7 +627,15 @@ async def test_pairing_completed_reaches_the_computer_and_not_other_phones(tmp_p assert row["connected"] is True and row["last_seen"] is not None state = await ask(window, {"type": "get_setup_state"}) assert state["remote_devices"] == [row] - assert set(row) == {"id", "name", "paired_at", "last_seen", "connected", "kind"} + assert set(row) == { + "id", + "name", + "paired_at", + "last_seen", + "connected", + "kind", + "mcp_servers_added", + } assert reply["token"] not in json.dumps(state) (row,) = await presence(window) assert row["connected"] is False diff --git a/core/tests/test_peer_policy.py b/core/tests/test_peer_policy.py index 3b6905c3..8edba21e 100644 --- a/core/tests/test_peer_policy.py +++ b/core/tests/test_peer_policy.py @@ -32,6 +32,8 @@ check, classify, ) +from tstd.phone_permissions import CAPABILITIES +from tstd.phone_verbs import HANDSHAKE_FRAMES, PHONE_OPEN, PHONE_OPT_IN from tstd.protocol import parse_client_message # Checked one by one for a way to switch approvals off, widen a rule, or @@ -70,7 +72,8 @@ ) VIEWER = PeerView(remote=False, machine=False, viewer=True) -PHONE_VIEWER = PeerView(remote=True, machine=False, viewer=True) +PHONE_VIEWER = PeerView(remote=True, machine=False, viewer=True, phone=True) +PHONE_PLAIN = PeerView(remote=True, machine=False, viewer=False, phone=True) MACHINE = PeerView(remote=True, machine=True, viewer=False) PLAIN_REMOTE = PeerView(remote=True, machine=False, viewer=False) BRIDGED = "s1:mch_0123456789ab" @@ -90,7 +93,7 @@ def _msg(kind: str, **fields: object) -> protocol.ClientMessage: (False, None, [], LOOPBACK_PLAIN), (True, "phone", ["machines"], PHONE_VIEWER), (True, None, ["machines"], PLAIN_REMOTE), # the legacy token is no device - (True, "phone", [], PLAIN_REMOTE), + (True, "phone", [], PHONE_PLAIN), (True, "machine", ["machines"], MACHINE), (True, "phone", ["machines", "bridge"], MACHINE), (False, None, ["bridge"], PeerView(remote=False, machine=True, viewer=False)), @@ -340,3 +343,162 @@ async def test_a_remote_peer_removing_itself_hears_no_devices_or_address(daemon: for field in REMOTE_SETUP_STATE_DROPS: assert field not in reply, (kind, field) assert [d.name for d in daemon.remote_devices.summaries()] == ["Other phone"] + + +MCP_FRAMES = [ + _msg("set_mcp_server", id="x", transport="stdio", command=["sh", "-c", "id"]), + _msg("delete_mcp_server", id="x"), + _msg("set_mcp_secret", server_id="x", target="env", name="TOKEN", value="v"), + _msg("delete_mcp_secret", server_id="x", target="env", name="TOKEN"), +] +SELF_PAIRED = PeerView(remote=False, machine=True, viewer=False) +GRANTED = frozenset({"mcp_servers"}) + + +def _code(refusal: str | None) -> str | None: + return None if refusal is None else json.loads(refusal)["code"] + + +def test_mcp_verbs_need_the_phone_permission() -> None: + """TD-5279: a server is a command this computer runs; a phone needs the grant.""" + for frame in MCP_FRAMES: + for phone in (PHONE_VIEWER, PHONE_PLAIN): + refusal = check(frame, phone, bridged=False, grants=()) + assert _code(refusal) == "phone_permission_off", frame.type + assert json.loads(refusal or "{}")["in_reply_to"] == frame.type + assert "MCP servers" in json.loads(refusal or "{}")["message"] + assert check(frame, phone, bridged=False, grants=GRANTED) is None, frame.type + + +def test_a_grant_never_applies_to_the_legacy_token_or_a_machine() -> None: + for frame in MCP_FRAMES: + # The legacy remote token is no device: nothing switches this on for it. + assert _code(check(frame, PLAIN_REMOTE, bridged=False, grants=GRANTED)) == ( + "phone_permission_off" + ), frame.type + for machine in (MACHINE, SELF_PAIRED): + assert _code(check(frame, machine, bridged=False, grants=GRANTED)) == ("not_allowed"), ( + frame.type, + machine, + ) + + +def test_the_computer_needs_no_grant() -> None: + for frame in MCP_FRAMES: + assert check(frame, LOOPBACK_PLAIN, bridged=False, grants=()) is None, frame.type + assert check(frame, VIEWER, bridged=False, grants=()) is None, frame.type + + +def test_check_refuses_when_grants_are_not_passed() -> None: + """A caller that forgets the argument fails in the safe direction.""" + for frame in MCP_FRAMES: + assert _code(check(frame, PHONE_VIEWER, bridged=False)) == "phone_permission_off" + + +def test_a_grant_for_something_else_opens_nothing() -> None: + for frame in MCP_FRAMES: + refusal = check(frame, PHONE_VIEWER, bridged=False, grants={"not_this_one"}) + assert _code(refusal) == "phone_permission_off", frame.type + + +def test_set_phone_permission_is_loopback_only() -> None: + """A phone must never grant itself; nor may the legacy token or another computer.""" + frame = _msg("set_phone_permission", capability="mcp_servers", enabled=True) + assert check(frame, LOOPBACK_PLAIN, bridged=False) is None + assert check(frame, VIEWER, bridged=False) is None + for peer in (PHONE_VIEWER, PHONE_PLAIN, PLAIN_REMOTE, MACHINE, SELF_PAIRED): + # Holding every grant changes nothing: it is not a grantable verb. + refusal = check(frame, peer, bridged=False, grants=GRANTED | {"set_phone_permission"}) + assert _code(refusal) == "loopback_only", peer + + +def test_every_opt_in_verb_names_a_known_capability_and_no_other_set() -> None: + assert set(PHONE_OPT_IN) <= protocol._KNOWN_CLIENT_TYPES + assert set(PHONE_OPT_IN.values()) <= set(CAPABILITIES) + # One classification per verb: opt-in is neither computer-only nor a machine verb. + assert LOOPBACK_VERBS.isdisjoint(PHONE_OPT_IN) + assert MACHINE_PEER_VERBS.isdisjoint(PHONE_OPT_IN) + + +def test_verbs_that_make_this_computer_run_a_command_are_never_open_to_a_phone() -> None: + """A later tidy-up must not reopen any of these to a phone.""" + runs_a_command = { + "set_mcp_server", + "delete_mcp_server", + "set_mcp_secret", + "delete_mcp_secret", + "get_config_editor", + "set_config_value", + "reset_config_value", + "trust_workspace_config", + "steering_accept", + "steering_edit", + } + assert runs_a_command <= protocol._KNOWN_CLIENT_TYPES + assert runs_a_command <= LOOPBACK_VERBS | set(PHONE_OPT_IN) + # The switch itself has no setting. + assert "set_phone_permission" in LOOPBACK_VERBS + assert "set_phone_permission" not in PHONE_OPT_IN + + +def test_every_client_type_is_placed_for_a_phone_exactly_once() -> None: + """TD-5284: a new verb starts refused for a phone until someone places it.""" + sets = { + "LOOPBACK_VERBS": set(LOOPBACK_VERBS), + "PHONE_OPT_IN": set(PHONE_OPT_IN), + "PHONE_OPEN": set(PHONE_OPEN), + "MACHINE_VERBS": set(MACHINE_VERBS), + "HANDSHAKE_FRAMES": set(HANDSHAKE_FRAMES), + } + placed: dict[str, str] = {} + for name, verbs in sets.items(): + for verb in verbs: + assert verb not in placed, f"{verb} is in both {placed.get(verb)} and {name}" + placed[verb] = name + known = set(protocol._KNOWN_CLIENT_TYPES) + assert known - set(placed) == set(), "place these for a phone in tstd/phone_verbs.py" + assert set(placed) - known == set(), "these are not client message types any more" + assert set(PHONE_OPT_IN.values()) <= set(CAPABILITIES) + + +def test_a_verb_nobody_placed_is_refused_from_a_phone(monkeypatch: pytest.MonkeyPatch) -> None: + """What the inventory is for: forgetting to list a verb fails closed.""" + from tstd import peer_policy + + frame = _msg("list_sessions") + assert check(frame, PHONE_VIEWER, bridged=False) is None + monkeypatch.setattr(peer_policy, "PHONE_OPEN", PHONE_OPEN - {"list_sessions"}) + for peer in (PHONE_VIEWER, PHONE_PLAIN, PLAIN_REMOTE): + assert _code(check(frame, peer, bridged=False, grants=GRANTED)) == "loopback_only", peer + # The computer itself is not a phone, and another computer keeps its own list. + assert check(frame, LOOPBACK_PLAIN, bridged=False) is None + assert check(frame, VIEWER, bridged=False) is None + assert check(frame, MACHINE, bridged=False) is None + + +def test_the_inventory_changed_nothing_the_day_it_was_written() -> None: + """Every verb a phone could send before is still open, opt-in or a machine verb.""" + for verb in protocol._KNOWN_CLIENT_TYPES - HANDSHAKE_FRAMES - LOOPBACK_VERBS: + assert verb in PHONE_OPEN or verb in PHONE_OPT_IN or verb in MACHINE_VERBS, verb + + +def test_no_verb_that_runs_a_command_or_grants_is_open_to_a_phone() -> None: + never_open = { + "set_phone_permission", + "set_mcp_server", + "delete_mcp_server", + "set_mcp_secret", + "delete_mcp_secret", + "get_config_editor", + "set_config_value", + "reset_config_value", + "trust_workspace_config", + "steering_accept", + "steering_edit", + "drain", + "delete_preset", + "scan_local_servers", + "set_skip_all_approvals", + } + assert never_open <= protocol._KNOWN_CLIENT_TYPES + assert never_open.isdisjoint(PHONE_OPEN) diff --git a/core/tests/test_phone_approval_switch.py b/core/tests/test_phone_approval_switch.py new file mode 100644 index 00000000..45c8f2ce --- /dev/null +++ b/core/tests/test_phone_approval_switch.py @@ -0,0 +1,133 @@ +"""A paired phone turns approvals off only if the computer said so (TD-5283). + +Skip-all persists machine-wide and releases every parked call, Class C +included. A phone could switch it on with nothing shown at the computer. +It is now the phone permission ``approval_switches``. Turning approvals +back on needs nothing: that only tightens. +""" + +from __future__ import annotations + +from pathlib import Path + +from tests.test_add_model import _stop +from tests.test_mcp_peer import _drain, _fresh, _pair +from tests.test_models_on_phone import _phone as _legacy_token_client +from tests.test_models_on_phone import _seeded +from tests.test_peer_policy import ( + LOOPBACK_PLAIN, + MACHINE, + PHONE_PLAIN, + PHONE_VIEWER, + PLAIN_REMOTE, + VIEWER, + _code, + _msg, +) +from tstd.peer_policy import check +from tstd.phone_verbs import required_capability +from tstd.policy import load_skip_all + +ON = {"type": "set_skip_all_approvals", "enabled": True} +OFF = {"type": "set_skip_all_approvals", "enabled": False} +GRANT = {"type": "set_phone_permission", "capability": "approval_switches", "enabled": True} +GRANTED = frozenset({"approval_switches"}) + + +def test_turning_approvals_off_needs_the_permission() -> None: + on = _msg("set_skip_all_approvals", enabled=True) + assert required_capability(on) == "approval_switches" + for phone in (PHONE_VIEWER, PHONE_PLAIN): + assert _code(check(on, phone, bridged=False)) == "phone_permission_off" + assert _code(check(on, phone, bridged=False, grants={"mcp_servers"})) == ( + "phone_permission_off" + ) + assert check(on, phone, bridged=False, grants=GRANTED) is None + # Never the older token, never another computer, always the computer itself. + assert _code(check(on, PLAIN_REMOTE, bridged=False, grants=GRANTED)) == "phone_permission_off" + assert _code(check(on, MACHINE, bridged=False, grants=GRANTED)) == "not_allowed" + for own in (LOOPBACK_PLAIN, VIEWER): + assert check(on, own, bridged=False) is None + + +def test_turning_them_back_on_needs_nothing() -> None: + off = _msg("set_skip_all_approvals", enabled=False) + assert required_capability(off) is None + for peer in (PHONE_VIEWER, PHONE_PLAIN, PLAIN_REMOTE, LOOPBACK_PLAIN): + assert check(off, peer, bridged=False) is None, peer + + +async def test_a_phone_cannot_turn_approvals_off_by_default(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + token_client = await _legacy_token_client(daemon) + try: + for peer in (phone, token_client): + reply = await _fresh(peer, ON) + assert reply["type"] == "error" and reply["code"] == "phone_permission_off" + assert "Turning approvals off" in reply["message"] + assert daemon.skip_all_approvals is False + assert load_skip_all(daemon.data_dir) is False + # The MCP permission does not cover it. + await _fresh(ws, {**GRANT, "capability": "mcp_servers"}) + assert (await _fresh(phone, ON))["code"] == "phone_permission_off" + assert daemon.skip_all_approvals is False + finally: + await token_client.close() + await phone.close() + await _stop(task, ws) + + +async def test_after_the_computer_grants_a_phone_can_and_the_computer_is_told( + tmp_path: Path, +) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + try: + granted = await _fresh(ws, GRANT) + assert granted["phone_permissions"]["approval_switches"]["enabled"] is True + assert granted["phone_permissions"]["mcp_servers"]["enabled"] is False + reply = await _fresh(phone, ON) + assert reply["type"] == "setup_state" and reply["skip_all_approvals"] is True + assert load_skip_all(daemon.data_dir) is True + heard = await _drain(ws) + assert [e for e in heard if e["type"] == "phone_change"] == [ + { + "type": "phone_change", + "seq": 1, + "device_name": "Pixel 8", + "capability": "approval_switches", + "summary": "turned approval prompts off for every session on this computer.", + } + ] + states = [e for e in heard if e["type"] == "setup_state"] + assert states and states[-1]["skip_all_approvals"] is True + finally: + await phone.close() + await _stop(task, ws) + + +async def test_a_phone_can_always_turn_approvals_back_on(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + phone, _ = await _pair(daemon) + try: + assert (await _fresh(ws, ON))["skip_all_approvals"] is True + await _drain(ws) + reply = await _fresh(phone, OFF) + assert reply["type"] == "setup_state" and reply["skip_all_approvals"] is False + assert load_skip_all(daemon.data_dir) is False + # Tightening is no use of a permission: nobody is told. + assert [e for e in await _drain(ws) if e["type"] == "phone_change"] == [] + finally: + await phone.close() + await _stop(task, ws) + + +async def test_the_computer_turning_them_off_is_no_phone_change(tmp_path: Path) -> None: + daemon, task, ws = await _seeded(tmp_path) + try: + assert (await _fresh(ws, ON))["skip_all_approvals"] is True + assert [e for e in await _drain(ws) if e["type"] == "phone_change"] == [] + assert daemon.skip_all_approvals is True + finally: + await _stop(task, ws) diff --git a/core/tests/test_phone_permissions.py b/core/tests/test_phone_permissions.py new file mode 100644 index 00000000..8f0797c1 --- /dev/null +++ b/core/tests/test_phone_permissions.py @@ -0,0 +1,248 @@ +"""The phone-permissions file and the origins sidecar (TD-5279). + +Everything fails closed: whatever is not plainly a live grant reads as off. +""" + +from __future__ import annotations + +from datetime import UTC, datetime, timedelta +from pathlib import Path + +import pytest +import yaml + +from tstd import phone_permissions as perms +from tstd.config import load_config +from tstd.phone_store import PhoneStore, server_saved_summary, value_summary + +NOW = datetime(2026, 11, 20, 18, 0, 0, tzinfo=UTC) + + +def _write(tmp_path: Path, body: object) -> None: + perms.permissions_path(tmp_path).write_text(yaml.safe_dump(body), encoding="utf-8") + + +def test_absent_file_means_everything_off(tmp_path: Path) -> None: + assert perms.load_grants(tmp_path) == {} + store = PhoneStore(tmp_path) + assert store.live() == frozenset() + states = store.states() + assert set(states) == set(perms.CAPABILITIES) + assert not any(state.enabled for state in states.values()) + assert store.doctor_row() is None + + +@pytest.mark.parametrize( + "text", + ["{{{ not yaml", "- a list\n", "just a string", "", "version: 1\ngrants: nope\n"], +) +def test_corrupt_file_means_everything_off(tmp_path: Path, text: str) -> None: + perms.permissions_path(tmp_path).write_text(text, encoding="utf-8") + assert perms.load_grants(tmp_path) == {} + + +def test_bytes_that_are_not_text_mean_everything_off(tmp_path: Path) -> None: + perms.permissions_path(tmp_path).write_bytes(b"\xff\xfe\x00grants") + assert perms.load_grants(tmp_path) == {} + + +@pytest.mark.parametrize( + "entry", + [ + {"enabled": "yes"}, # only a real true + {"enabled": 1}, + {"enabled": False}, + {"enabled": True, "expires_at": "tomorrow"}, # unreadable expiry is not forever + {"enabled": True, "expires_at": 12}, + "true", + None, + ], +) +def test_an_entry_that_is_not_plainly_a_grant_is_off(tmp_path: Path, entry: object) -> None: + _write(tmp_path, {"version": 1, "grants": {"mcp_servers": entry}}) + assert perms.load_grants(tmp_path) == {} + + +def test_another_file_version_is_off(tmp_path: Path) -> None: + _write(tmp_path, {"version": 2, "grants": {"mcp_servers": {"enabled": True}}}) + assert perms.load_grants(tmp_path) == {} + _write(tmp_path, {"grants": {"mcp_servers": {"enabled": True}}}) + assert perms.load_grants(tmp_path) == {} + + +def test_unknown_capability_in_file_is_ignored(tmp_path: Path) -> None: + _write( + tmp_path, + { + "version": 1, + "grants": {"everything": {"enabled": True}, "mcp_servers": {"enabled": True}}, + }, + ) + assert set(perms.load_grants(tmp_path)) == {"mcp_servers"} + + +def test_a_grant_round_trips_and_lists_every_capability(tmp_path: Path) -> None: + grants = perms.changed({}, "mcp_servers", True, None, NOW) + perms.save_grants(tmp_path, grants) + assert perms.load_grants(tmp_path) == grants + on_disk = yaml.safe_load(perms.permissions_path(tmp_path).read_text(encoding="utf-8")) + assert on_disk["version"] == 1 + assert set(on_disk["grants"]) == set(perms.CAPABILITIES) + assert on_disk["grants"]["mcp_servers"] == { + "enabled": True, + "granted_at": "2026-11-20T18:00:00Z", + "expires_at": None, + } + others = [g for name, g in on_disk["grants"].items() if name != "mcp_servers"] + assert all(g == {"enabled": False} for g in others) + perms.save_grants(tmp_path, perms.changed(grants, "mcp_servers", False, None, NOW)) + off = yaml.safe_load(perms.permissions_path(tmp_path).read_text(encoding="utf-8")) + assert all(g == {"enabled": False} for g in off["grants"].values()) + assert perms.load_grants(tmp_path) == {} + + +def test_expired_grant_reads_as_off() -> None: + grants = perms.changed({}, "mcp_servers", True, 3600, NOW) + assert grants["mcp_servers"].expires_at == "2026-11-20T19:00:00Z" + assert perms.live(grants, NOW + timedelta(minutes=59, seconds=59)) == {"mcp_servers"} + assert perms.live(grants, NOW + timedelta(hours=1)) == frozenset() + assert perms.next_expiry(grants, NOW) == NOW + timedelta(hours=1) + assert perms.next_expiry(grants, NOW + timedelta(hours=2)) is None + assert perms.next_expiry(perms.changed({}, "mcp_servers", True, None, NOW), NOW) is None + + +def test_a_bad_ask_is_refused() -> None: + with pytest.raises(ValueError, match="Unknown phone permission"): + perms.changed({}, "everything", True, None, NOW) + with pytest.raises(ValueError, match="seven days"): + perms.changed({}, "mcp_servers", True, perms.MAX_EXPIRY_SECONDS + 1, NOW) + with pytest.raises(ValueError, match="seven days"): + perms.changed({}, "mcp_servers", True, 0, NOW) + + +def test_save_is_atomic(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + """A write that dies part-way leaves the old file, and no stray temp file.""" + perms.save_grants(tmp_path, perms.changed({}, "mcp_servers", True, None, NOW)) + before = perms.permissions_path(tmp_path).read_bytes() + + def boom(*_args: object, **_kwargs: object) -> None: + raise OSError("disk full") + + monkeypatch.setattr(perms.yaml, "safe_dump", boom) + with pytest.raises(OSError, match="disk full"): + perms.save_grants(tmp_path, {}) + assert perms.permissions_path(tmp_path).read_bytes() == before + assert [p.name for p in tmp_path.iterdir()] == ["phone-permissions.yaml"] + + +def test_a_failed_save_leaves_the_store_as_it_was( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + store = PhoneStore(tmp_path) + + def boom(*_args: object, **_kwargs: object) -> None: + raise OSError("read-only") + + monkeypatch.setattr(perms, "save_grants", boom) + with pytest.raises(OSError, match="read-only"): + store.set("mcp_servers", True, None) + assert store.live() == frozenset() + + +def test_the_store_follows_the_clock(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + clock = [NOW] + monkeypatch.setattr(perms, "utc_now", lambda: clock[0]) + store = PhoneStore(tmp_path) + store.set("mcp_servers", True, 3600) + assert store.live() == {"mcp_servers"} + state = store.states()["mcp_servers"] + assert state.enabled and state.expires_at == "2026-11-20T19:00:00Z" + row = store.doctor_row() + assert row is not None and row.status == "warn" + assert row.detail == "paired phones may change: MCP servers ON (until 2026-11-20T19:00:00Z)" + clock[0] = NOW + timedelta(hours=1, seconds=1) + assert store.live() == frozenset() + assert store.states()["mcp_servers"].enabled is False + assert store.doctor_row() is None + # A second store reads the same file and reaches the same answer. + assert PhoneStore(tmp_path).live() == frozenset() + + +def test_the_doctor_row_says_until_turned_off(tmp_path: Path) -> None: + store = PhoneStore(tmp_path) + store.set("mcp_servers", True, None) + row = store.doctor_row() + assert row is not None + assert row.detail == "paired phones may change: MCP servers ON (until turned off)" + assert row.fix is None + + +def test_workspace_config_cannot_grant(tmp_path: Path) -> None: + """The grants are no config key: one written into a config file does nothing.""" + data = tmp_path / "data" + data.mkdir() + workspace = tmp_path / "ws" / ".tst" + workspace.mkdir(parents=True) + text = "phone_permissions:\n mcp_servers:\n enabled: true\n" + (workspace / "config.yaml").write_text(text, encoding="utf-8") + (data / "config.yaml").write_text(text, encoding="utf-8") + for path in (workspace / "config.yaml", data / "config.yaml"): + assert not hasattr(load_config(path), "phone_permissions") + # Only phone-permissions.yaml in the data dir is read, and it is absent. + assert PhoneStore(data).live() == frozenset() + assert PhoneStore(workspace).live() == frozenset() + + +def test_origins_round_trip_and_ignore_what_is_malformed(tmp_path: Path) -> None: + store = PhoneStore(tmp_path) + store.record_origin("probe", "dev_1", "Pixel 8") + store.record_origin("other", "dev_2", "Tablet") + again = PhoneStore(tmp_path) + origin = again.origin_of("probe") + assert origin is not None and (origin.device_id, origin.device_name) == ("dev_1", "Pixel 8") + assert again.servers_from("dev_1") == ["probe"] + again.forget_origin("probe") + assert PhoneStore(tmp_path).servers_from("dev_1") == [] + perms.origins_path(tmp_path).write_text( + "version: 1\nservers:\n bad: nope\n worse: {device_id: 3}\n", encoding="utf-8" + ) + assert perms.load_origins(tmp_path) == {} + perms.origins_path(tmp_path).write_text("{{{", encoding="utf-8") + assert perms.load_origins(tmp_path) == {} + + +def test_a_failed_origin_write_is_not_an_error( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + store = PhoneStore(tmp_path) + + def boom(*_args: object, **_kwargs: object) -> None: + raise OSError("disk full") + + monkeypatch.setattr(perms, "save_origins", boom) + store.record_origin("probe", "dev_1", "Pixel 8") + assert store.origin_of("probe") is None + + +def test_the_notice_names_the_command_and_never_a_value() -> None: + added = server_saved_summary("probe", ["/usr/bin/touch", "/tmp/a b"], "", existed=False) + assert added == "added MCP server 'probe': /usr/bin/touch '/tmp/a b'" + changed = server_saved_summary("web", [], "https://mcp.example.test/x", existed=True) + assert changed == "changed MCP server 'web': https://mcp.example.test/x" + long = server_saved_summary("big", ["x" * 500], "", existed=False) + assert len(long) < 240 and long.endswith("…") + assert value_summary("probe", "env", "TOKEN", stored=True) == ( + "stored a value for variable 'TOKEN' of MCP server 'probe'." + ) + assert value_summary("web", "header", "Authorization", stored=False) == ( + "removed the value of header 'Authorization' of MCP server 'web'." + ) + + +def test_one_grant_does_not_switch_on_another(tmp_path: Path) -> None: + for name in perms.CAPABILITIES: + store = PhoneStore(tmp_path / name) + store.set(name, True, None) + assert store.live() == {name} + assert [n for n, s in store.states().items() if s.enabled] == [name] + assert perms.LABELS[name] in (store.doctor_row() or object()).detail # type: ignore[union-attr] diff --git a/core/tests/test_picker_groups.py b/core/tests/test_picker_groups.py new file mode 100644 index 00000000..38d1c655 --- /dev/null +++ b/core/tests/test_picker_groups.py @@ -0,0 +1,253 @@ +"""The picker's sections and quick-switch slots (TD-5236). + +Local, API and Grok are sections of one list. The daemon says which +section each model is in, so the window never guesses from a URL; an entry +can name its own. A slot, 1 to 9, gives an entry a shortcut. And a change +to the catalog reaches every window of this computer, not only the one +that made it. +""" + +from __future__ import annotations + +import asyncio +import json +from pathlib import Path +from typing import Any + +import pytest +from websockets.asyncio.client import connect + +from tests.test_add_model import _ask, _start, _stop +from tests.test_preset_engine import _text as _catalog_with_grok +from tstd.config import PICKER_GROUPS, ConfigError, ModelConfig, load_config +from tstd.mock import MockProvider, Script +from tstd.preset_write import duplicate_preset +from tstd.protocol import PROTOCOL_VERSION + +LAN = """\ + lan-box: + label: The box next door + group: Local + slot: 2 + brain: &lan + slug: house/model + base_url: http://192.168.1.10:4000/v1 + credential: openrouter + context_window: 65536 + max_output_tokens: 8192 + input_price: 0.0 + output_price: 0.0 + cache_read_price: 0.0 + worker: *lan + validator: *lan + mine: + label: Experiments + group: " Trying out " + brain: *lan + worker: *lan + validator: *lan +""" + + +def _text(extra: str = LAN) -> str: + text = _catalog_with_grok() + assert text.count("\npresets:\n") == 1 + return text.replace("\npresets:\n", "\npresets:\n" + extra, 1) + + +def _with(text: str, entry: str, block: str) -> str: + header = f"\n {entry}:\n" + assert text.count(header) == 1, entry + return text.replace(header, header + block, 1) + + +def _config(tmp_path: Path, text: str | None = None) -> ModelConfig: + path = tmp_path / "config.yaml" + path.write_text(_text() if text is None else text, encoding="utf-8") + return load_config(path) + + +class TestTheSectionOfAnEntry: + def test_the_daemon_reads_it_off_the_entry(self, tmp_path: Path) -> None: + config = _config(tmp_path) + # Every role on this computer. + assert config.preset_group("local") == "Local" + assert config.preset_group("vllm") == "Local" + # A provider's server. + assert config.preset_group("tst-default") == "API" + assert config.preset_group("budget") == "API" + # The Grok engine. + assert config.preset_group("grok") == "Grok" + assert config.preset_group("nope") == "API" + + def test_an_entry_can_say_its_own(self, tmp_path: Path) -> None: + config = _config(tmp_path) + # A server on the local network is not this computer: only the + # person knows it is theirs, and here they said so. + assert config.preset_group("lan-box") == "Local" + assert config.presets["mine"].group == "Trying out" + assert config.preset_group("mine") == "Trying out" + + def test_without_saying_so_a_server_on_the_network_is_api(self, tmp_path: Path) -> None: + config = _config(tmp_path, _text(LAN.replace(" group: Local\n", ""))) + assert config.preset_group("lan-box") == "API" + + def test_a_key_that_points_a_local_role_at_a_provider_makes_it_api( + self, tmp_path: Path + ) -> None: + # TD-1718: a bound key's host is where the request really goes. + text = _text().replace( + " base_url: http://127.0.0.1:11434/v1\n", + " base_url: http://127.0.0.1:11434/v1\n credential: openrouter\n", + ) + config = _config(tmp_path, text) + assert config.credentials["openrouter"].base_url is not None + assert config.preset_group("local") == "API" + + def test_the_built_in_sections_come_in_this_order(self) -> None: + assert PICKER_GROUPS == ("Local", "API", "Grok") + + @pytest.mark.parametrize("group", ['""', "' '", "x" * 21]) + def test_a_blank_group_is_unset_and_a_long_one_is_refused( + self, tmp_path: Path, group: str + ) -> None: + text = _with(_text(""), "budget", f" group: {group}\n") + if len(group) > 20: + with pytest.raises(ConfigError, match=r"presets\.budget\.group"): + _config(tmp_path, text) + return + if group == '""': + with pytest.raises(ConfigError, match=r"presets\.budget\.group"): + _config(tmp_path, text) + return + assert _config(tmp_path, text).presets["budget"].group is None + + +class TestSlots: + def test_a_slot_is_one_to_nine(self, tmp_path: Path) -> None: + assert _config(tmp_path).presets["lan-box"].slot == 2 + for bad in ("0", "10", "-1", "two"): + with pytest.raises(ConfigError, match=r"presets\.budget\.slot"): + _config(tmp_path, _with(_text(""), "budget", f" slot: {bad}\n")) + + def test_no_two_entries_share_one(self, tmp_path: Path) -> None: + text = _with(_text(), "budget", " slot: 2\n") + with pytest.raises(ConfigError) as caught: + _config(tmp_path, text) + message = str(caught.value) + assert "slot 2 is already" in message + assert "lan-box" in message and "budget" in message + + def test_a_copy_does_not_take_its_sources_slot(self, tmp_path: Path) -> None: + path = tmp_path / "config.yaml" + path.write_text(_text(), encoding="utf-8") + duplicate_preset("lan-box", "lan-box-2", "The box, again", path) + config = load_config(path) + assert config.presets["lan-box"].slot == 2 + assert config.presets["lan-box-2"].slot is None + # The section travels with the copy; the shortcut cannot. + assert config.presets["lan-box-2"].group == "Local" + + +def _mock() -> MockProvider: + return MockProvider(default=Script(kind="text")) + + +async def _seeded(tmp_path: Path) -> tuple[Any, Any, Any]: + data = tmp_path / "data" + data.mkdir() + (data / "config.yaml").write_text(_text(), encoding="utf-8") + return await _start(data, _mock()) + + +async def _second_window(daemon: Any) -> Any: + ws = await connect(f"ws://127.0.0.1:{daemon.ws_server.port}") + await ws.send( + json.dumps({"type": "hello", "token": daemon.ws_server.token, "version": PROTOCOL_VERSION}) + ) + assert json.loads(await ws.recv())["type"] == "hello_ack" + return ws + + +async def _next_setup_state(ws: Any) -> dict[str, Any]: + for _ in range(40): + event = dict(json.loads(await asyncio.wait_for(ws.recv(), 5))) + if event["type"] == "setup_state": + return event + raise AssertionError("no setup_state reached the other window") + + +class TestOverTheWire: + async def test_setup_state_names_every_models_section_and_each_slot( + self, tmp_path: Path + ) -> None: + _daemon, task, ws = await _seeded(tmp_path) + try: + state = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + assert state["picker_groups"] == ["Local", "API", "Grok"] + assert set(state["preset_groups"]) == set(state["presets"]) + assert state["preset_groups"]["local"] == "Local" + assert state["preset_groups"]["lan-box"] == "Local" + assert state["preset_groups"]["tst-default"] == "API" + assert state["preset_groups"]["grok"] == "Grok" + assert state["preset_groups"]["mine"] == "Trying out" + assert state["preset_slots"] == {"lan-box": 2} + finally: + await _stop(task, ws) + + @pytest.mark.parametrize( + ("message", "check"), + [ + ( + {"type": "switch_preset", "name": "budget", "make_default": True}, + lambda s: s["active_preset"] == "budget", + ), + ({"type": "set_preset", "name": "budget"}, lambda s: s["active_preset"] == "budget"), + ( + {"type": "duplicate_preset", "source": "local", "label": "Local two"}, + lambda s: "local-two" in s["presets"], + ), + ( + {"type": "set_preset_label", "name": "local", "label": "Here"}, + lambda s: s["preset_labels"]["local"] == "Here", + ), + ({"type": "delete_preset", "name": "mine"}, lambda s: "mine" not in s["presets"]), + ], + ) + async def test_a_change_in_one_window_reaches_the_other( + self, tmp_path: Path, message: dict[str, Any], check: Any + ) -> None: + daemon, task, ws = await _seeded(tmp_path) + other = await _second_window(daemon) + try: + reply = await _ask(ws, message, "setup_state") + assert reply["type"] == "setup_state" and check(reply) + # Unasked: the window that did nothing is told too. + told = await _next_setup_state(other) + assert check(told) + assert told["preset_groups"] == reply["preset_groups"] + finally: + await other.close() + await _stop(task, ws) + + async def test_switching_only_a_chat_tells_no_other_window_about_the_catalog( + self, tmp_path: Path + ) -> None: + workspace = tmp_path / "ws" + workspace.mkdir() + daemon, task, ws = await _seeded(tmp_path) + other = await _second_window(daemon) + try: + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + await _ask( + ws, + {"type": "switch_preset", "name": "budget", "session_id": opened["session_id"]}, + "session_list", + ) + with pytest.raises(asyncio.TimeoutError): + await asyncio.wait_for(_next_setup_state(other), 0.5) + finally: + await other.close() + await _stop(task, ws) diff --git a/core/tests/test_plan_mode.py b/core/tests/test_plan_mode.py index d6a4aa38..6c7f1b13 100644 --- a/core/tests/test_plan_mode.py +++ b/core/tests/test_plan_mode.py @@ -186,6 +186,8 @@ def test_every_builtin_is_decided_and_only_readers_run() -> None: "send_file", "session_search", "shell_job_output", + # TD-5254: changes nothing but the session's own Artifacts pane. + "show_artifact", "task_list", "tool_search", "web_fetch", diff --git a/core/tests/test_preset_engine.py b/core/tests/test_preset_engine.py new file mode 100644 index 00000000..3f8d8676 --- /dev/null +++ b/core/tests/test_preset_engine.py @@ -0,0 +1,432 @@ +"""A catalog entry can name its engine, and a Grok entry needs no model (TD-5239). + +Local, API and Grok are three entries of one catalog. A Grok entry is a +label and ``engine: grok``: the Grok CLI brings its own model, so the three +roles may be left out. These cases cover what load accepts, what the rest +of the code sees for an entry with no roles, which engine a chat gets, and +that a chat keeps the engine it started on. +""" + +from __future__ import annotations + +import asyncio +import json +import re +from pathlib import Path +from typing import Any + +import pytest + +from tests.catalog_helpers import catalog_config_yaml +from tests.test_add_model import _ask, _start, _stop +from tstd.config import ConfigError, ModelConfig, load_config +from tstd.daemon import Daemon, _snapshot_slugs +from tstd.key_hosts import every_tier +from tstd.mock import MockProvider, Script +from tstd.model_cards import targets +from tstd.preset_apply import engine_refusal, no_model_refusal +from tstd.preset_write import delete_preset, duplicate_preset, save_preset_label +from tstd.scheduler.pin import ScheduledPin, pin_failure_summary, pinned_engine +from tstd.session import Session + +GROK = """\ + grok: + label: Grok + description: The Grok CLI, with its own model + engine: grok +""" +PRESETS_HEADER = "\npresets:\n" + + +def _text(extra: str = GROK, *, engine_kind: str | None = None) -> str: + """The test catalog with *extra* entries first and ``tst-default`` active.""" + text = catalog_config_yaml() + assert text.count(PRESETS_HEADER) == 1 + text = text.replace(PRESETS_HEADER, PRESETS_HEADER + extra, 1) + # Named, or the loader would pick the first entry, which is the extra one. + assert not re.search(r"^active_preset:", text, re.MULTILINE) + text += "\nactive_preset: tst-default\n" + if engine_kind is not None: + shipped = "\nengine:\n kind: native\n" + assert text.count(shipped) == 1 + text = text.replace(shipped, f"\nengine:\n kind: {engine_kind}\n", 1) + return text + + +def _config(tmp_path: Path, text: str | None = None) -> ModelConfig: + path = tmp_path / "config.yaml" + path.write_text(_text() if text is None else text, encoding="utf-8") + return load_config(path) + + +def _entry(body: str) -> str: + return " odd:\n" + body + + +TIER = """\ + base_url: http://127.0.0.1:8000/v1 + context_window: 32768 + max_output_tokens: 4096 + input_price: 0.0 + output_price: 0.0 + cache_read_price: 0.0 +""" + + +class TestWhatLoads: + def test_a_grok_entry_needs_no_roles(self, tmp_path: Path) -> None: + config = _config(tmp_path) + grok = config.presets["grok"] + assert grok.engine == "grok" + assert grok.has_tiers is False and grok.tier_map() == {} + assert grok.label == "Grok" + + def test_a_grok_entry_may_still_name_all_three(self, tmp_path: Path) -> None: + body = f" engine: grok\n brain:\n{TIER} worker:\n{TIER} validator:\n{TIER}" + config = _config(tmp_path, _text(_entry(body))) + assert config.presets["odd"].has_tiers is True + assert set(config.presets["odd"].tier_map()) == {"brain", "worker", "validator"} + + def test_a_catalog_from_before_loads_as_it_did(self, tmp_path: Path) -> None: + config = _config(tmp_path, catalog_config_yaml()) + assert all(p.engine is None and p.has_tiers for p in config.presets.values()) + assert {config.preset_engine(name) for name in config.presets} == {"native"} + + @pytest.mark.parametrize( + ("body", "says"), + [ + (" label: No model\n", "Only an entry with `engine: grok`"), + (" label: No model\n engine: native\n", "Only an entry with `engine: grok`"), + ( + f" engine: grok\n brain:\n{TIER} worker:\n{TIER}", + "name all three roles or none of them; missing: validator", + ), + (f" brain:\n{TIER}", "missing: worker, validator"), + ( + " engine: grok\n overrides:\n workflow: {max_parallel: 1}\n", + "`overrides` does nothing on an `engine: grok` entry", + ), + ( + " engine: grok\n fast:\n extra_body: {priority: true}\n", + "`fast` does nothing on an `engine: grok` entry", + ), + (" engine: cursor\n", "presets.odd.engine"), + ], + ) + def test_what_is_refused(self, tmp_path: Path, body: str, says: str) -> None: + with pytest.raises(ConfigError) as caught: + _config(tmp_path, _text(_entry(body))) + assert says in str(caught.value) + assert "odd" in str(caught.value) + + +class TestWhatTheRestSees: + def test_an_active_grok_entry_is_a_model_that_needs_no_key(self, tmp_path: Path) -> None: + config = _config(tmp_path).model_copy(update={"active_preset": "grok"}) + assert config.has_model() is True + assert config.requires_api_key() is False + # Stand-ins, as with an empty catalog: code that reads a window still works. + tiers = config.tiers() + assert set(tiers) == {"brain", "worker", "validator"} + assert all(t.slug is None and t.credential is None for t in tiers.values()) + + def test_every_walk_over_the_catalog_skips_it(self, tmp_path: Path) -> None: + config = _config(tmp_path) + native = [name for name, p in config.presets.items() if p.has_tiers] + assert len(native) == len(config.presets) - 1 + assert targets(config, "grok", None) == [] + assert targets(config, "grok", "brain") == [] + assert len(list(every_tier(config))) >= 3 * len(native) + assert _snapshot_slugs(config)["grok"] == {} + assert set(_snapshot_slugs(config)["local"]) == {"brain", "worker", "validator"} + assert config.fallback_bindings("openrouter") == [] + + def test_the_engine_is_the_entrys_then_the_global_one(self, tmp_path: Path) -> None: + native_global = _config(tmp_path) + assert native_global.preset_engine("grok") == "grok" + assert native_global.preset_engine("local") == "native" + assert native_global.preset_engine("nope") == "native" + grok_global = _config(tmp_path, _text(engine_kind="grok")) + assert grok_global.preset_engine("local") == "grok" + pinned = _text( + _entry( + f" engine: native\n brain:\n{TIER} worker:\n{TIER} validator:\n{TIER}" + ), + engine_kind="grok", + ) + assert _config(tmp_path, pinned).preset_engine("odd") == "native" + + def test_work_no_chat_owns_runs_on_an_entry_that_has_a_model(self, tmp_path: Path) -> None: + config = _config(tmp_path) + assert config.native_default() == config.active_preset + on_grok = config.model_copy(update={"active_preset": "grok"}) + first_native = next(n for n, p in config.presets.items() if p.has_tiers) + assert on_grok.native_default() == first_native + only_grok = config.model_copy( + update={"presets": {"grok": config.presets["grok"]}, "active_preset": "grok"} + ) + assert only_grok.native_default() is None + + +class TestAChatKeepsItsEngine: + def test_which_switches_are_refused(self, tmp_path: Path) -> None: + body = f" engine: native\n brain:\n{TIER} worker:\n{TIER} validator:\n{TIER}" + config = _config(tmp_path, _text(GROK + _entry(body))) + # An entry that names no engine suits a chat on either, as before. + assert engine_refusal(config, "local", "native") is None + assert engine_refusal(config, "local", "grok") is None + assert engine_refusal(config, "grok", "grok") is None + assert engine_refusal(config, "odd", "native") is None + assert engine_refusal(config, "nope", "native") is None + to_grok = engine_refusal(config, "grok", "native") + assert to_grok is not None and "no model of its own" in to_grok + assert "Start a new chat" in to_grok + to_native = engine_refusal(config, "odd", "grok") + assert to_native is not None and "keeps the engine it started on" in to_native + + def test_only_the_native_loop_needs_the_entrys_model(self, tmp_path: Path) -> None: + config = _config(tmp_path) + assert no_model_refusal(config, "grok", "grok") is None + assert no_model_refusal(config, "local", "native") is None + assert no_model_refusal(config, "grok", "native") is not None + + +class TestScheduledPins: + """TD-3812 stores a preset and an engine. The entry's engine fills an empty one.""" + + @pytest.mark.parametrize( + ("pin", "grok_available", "expected_engine", "summary"), + [ + (ScheduledPin(preset="grok"), True, "grok", None), + (ScheduledPin(preset="grok"), False, "grok", "grok engine is unavailable"), + (ScheduledPin(preset="grok", engine="grok"), True, "grok", None), + ( + ScheduledPin(preset="grok", engine="native"), + True, + "native", + "preset 'grok' has no model of its own; it runs only on the grok engine", + ), + (ScheduledPin(preset="local"), False, None, None), + (ScheduledPin(preset="local", engine="grok"), True, "grok", None), + ( + ScheduledPin(preset="local", engine="grok"), + False, + "grok", + "grok engine is unavailable", + ), + (ScheduledPin(), False, None, None), + ( + ScheduledPin(preset="retired", engine="native"), + False, + "native", + "preset 'retired' no longer exists", + ), + ], + ) + def test_the_pin_table( + self, + tmp_path: Path, + pin: ScheduledPin, + grok_available: bool, + expected_engine: str | None, + summary: str | None, + ) -> None: + catalog = _config(tmp_path).presets + assert pinned_engine(pin, catalog) == expected_engine + assert pin_failure_summary(pin, catalog=catalog, grok_available=grok_available) == summary + + +class TestWriters: + """TD-5237's three writers on an entry with no roles.""" + + def test_copy_relabel_and_remove(self, tmp_path: Path) -> None: + path = tmp_path / "config.yaml" + path.write_text(_text(), encoding="utf-8") + duplicate_preset("grok", "grok-2", "Grok two", path) + config = load_config(path) + assert config.presets["grok-2"].engine == "grok" + assert config.presets["grok-2"].has_tiers is False + save_preset_label("grok", "Grok Build", path=path) + assert load_config(path).presets["grok"].label == "Grok Build" + delete_preset("grok", path) + delete_preset("grok-2", path) + # Both gone, and every other line as it was. + assert path.read_text(encoding="utf-8") == _text(extra="") + + +async def _grok_stub(session: Session, **_kwargs: Any) -> None: + """Stands in for the Grok loop: no CLI is started.""" + await asyncio.Event().wait() + + +def _mock() -> MockProvider: + return MockProvider(default=Script(kind="stream", content="done")) + + +async def _seeded( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, text: str | None = None +) -> tuple[Daemon, Any, Any, Path]: + monkeypatch.setattr("tstd.daemon.grok_loop", _grok_stub) + data = tmp_path / "data" + data.mkdir() + (data / "config.yaml").write_text(_text() if text is None else text, encoding="utf-8") + workspace = tmp_path / "ws" + workspace.mkdir() + daemon, task, ws = await _start(data, _mock()) + return daemon, task, ws, workspace + + +class TestOverTheWire: + async def test_setup_state_marks_the_grok_entry_and_shows_no_roles_for_it( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + _daemon, task, ws, _workspace = await _seeded(tmp_path, monkeypatch) + try: + before = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + assert before["preset_engines"] == {"grok": "grok"} + assert before["tier_slugs"] != {} + state = await _ask(ws, {"type": "set_preset", "name": "grok"}, "setup_state") + assert state["active_preset"] == "grok" + assert state["has_model"] is True + assert state["key_required"] is False + assert state["tier_slugs"] == {} + assert state["tier_cards"] == {} + assert state["tier_credentials"] == {} and state["tier_loopback"] == {} + # The Engine setting itself is not changed by picking an entry. + assert state["engine"] == "native" + finally: + await _stop(task, ws) + + async def test_a_chat_opened_on_a_grok_entry_is_a_grok_chat( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + native = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + assert native["engine"] == "native" + await _ask(ws, {"type": "set_preset", "name": "grok"}, "setup_state") + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + assert opened["engine"] == "grok" + sess = daemon.session_registry.get(opened["session_id"]) + assert sess is not None and sess.engine == "grok" and sess.preset == "grok" + record = daemon._session_store.get(opened["session_id"]) + assert record is not None and record.engine == "grok" and record.preset == "grok" + # The first chat is untouched. + first = daemon.session_registry.get(native["session_id"]) + assert first is not None and first.engine == "native" + finally: + await _stop(task, ws) + + async def test_a_switch_across_engines_is_refused_and_changes_nothing( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sid = opened["session_id"] + sess = daemon.session_registry.get(sid) + assert sess is not None + snapshot, was = sess.config, sess.preset + ws_reply = await _ask( + ws, + {"type": "set_session_preset", "session_id": sid, "name": "grok"}, + "session_list", + ) + assert ws_reply["type"] == "error" + assert ws_reply["code"] == "engine_mismatch" + assert "Start a new chat" in ws_reply["message"] + assert sess.preset == was and sess.config is snapshot and sess.engine == "native" + record = daemon._session_store.get(sid) + assert record is not None and record.preset == was + # A switch between two native entries still works. + ok = await _ask( + ws, + {"type": "set_session_preset", "session_id": sid, "name": "local"}, + "session_list", + ) + assert ok["type"] == "session_list" and sess.preset == "local" + finally: + await _stop(task, ws) + + async def test_the_native_loop_is_never_started_on_an_entry_with_no_model( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + before = len(await daemon.session_registry.list_sessions()) + raw = await daemon._start_session(str(workspace), preset="grok", engine="native") + assert raw is not None + reply = json.loads(raw) + assert reply["type"] == "error" and reply["code"] == "engine_mismatch" + assert len(await daemon.session_registry.list_sessions()) == before + # A caller's engine still wins over an entry's when there is a model. + raw = await daemon._start_session(str(workspace), preset="local", engine="grok") + assert raw is not None and json.loads(raw)["engine"] == "grok" + finally: + await _stop(task, ws) + + async def test_the_doctor_skips_the_probe_for_an_active_grok_entry( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, _workspace = await _seeded(tmp_path, monkeypatch) + try: + await _ask(ws, {"type": "set_preset", "name": "grok"}, "setup_state") + rows = {row.name: row for row in await daemon._doctor_key_provider_rows()} + assert rows["api_key"].status == "skip" and rows["provider"].status == "skip" + assert "Grok CLI" in rows["provider"].detail + finally: + await _stop(task, ws) + + +class TestDistill: + """Distill runs on TST Desk's own client whatever engine the chat used.""" + + async def _run( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, text: str + ) -> list[tuple[str, str | None]]: + seen: list[tuple[str, str | None]] = [] + + async def fake_distill(session: Session, provider: Any, config: ModelConfig) -> None: + seen.append((config.active_preset, config.tier("worker").slug)) + return None + + monkeypatch.setattr("tstd.daemon.distill_if_due", fake_distill) + monkeypatch.setattr("tstd.daemon.completed_turn_count", lambda _s: 1) + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch, text) + try: + await _ask(ws, {"type": "set_preset", "name": "grok"}, "setup_state") + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sess = daemon.session_registry.get(opened["session_id"]) + assert sess is not None and sess.engine == "grok" + await daemon._run_distill(sess) + finally: + await _stop(task, ws) + return seen + + async def test_with_grok_active_it_uses_the_first_entry_that_has_a_model( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + seen = await self._run(tmp_path, monkeypatch, _text()) + config = _config(tmp_path) + first = next(n for n, p in config.presets.items() if p.has_tiers) + worker = config.presets[first].worker + assert worker is not None + # Once from this test, and once more when the daemon shuts down. + assert seen and set(seen) == {(first, worker.slug)} + + async def test_with_no_model_anywhere_it_is_skipped( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + from tstd.config import default_config_yaml + + only_grok = default_config_yaml().replace("\npresets: {}\n", "\npresets:\n" + GROK, 1) + assert "engine: grok" in only_grok + assert await self._run(tmp_path, monkeypatch, only_grok) == [] diff --git a/core/tests/test_preset_overrides.py b/core/tests/test_preset_overrides.py new file mode 100644 index 00000000..bd7c9171 --- /dev/null +++ b/core/tests/test_preset_overrides.py @@ -0,0 +1,458 @@ +"""Context settings one model carries with it (TD-5234). + +``effective_config`` is what a chat on an entry runs with: the daemon's +config with that entry's ``overrides`` laid over the global sections. The +cases here are the merge rules, what load refuses, and that a chat really +gets them when it is opened on the entry, switched to it, or revived. +""" + +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any + +import pytest +import yaml +from pydantic import BaseModel + +from tests.catalog_helpers import catalog_config_yaml +from tests.test_add_model import _ask, _start, _stop +from tstd.config import ( + ClearToolResultsConfig, + CompactionConfig, + ComputerUseConfig, + ConfigError, + ConsultConfig, + ModelConfig, + SessionConfig, + WorkflowConfig, + load_config, +) +from tstd.daemon import Daemon +from tstd.mock import MockProvider, Script +from tstd.preset_apply import effective_config +from tstd.preset_overrides import ( + OVERRIDE_SECTIONS, + ClearToolResultsOverride, + CompactionOverride, + ComputerUseOverride, + ConsultOverride, + PresetOverrides, + SessionOverride, + WorkflowOverride, +) +from tstd.preset_write import duplicate_preset +from tstd.protocol import TierState +from tstd.session_store import SessionStore + +PAIRS: list[tuple[type[BaseModel], type[BaseModel]]] = [ + (SessionOverride, SessionConfig), + (ClearToolResultsOverride, ClearToolResultsConfig), + (CompactionOverride, CompactionConfig), + (ConsultOverride, ConsultConfig), + (WorkflowOverride, WorkflowConfig), + (ComputerUseOverride, ComputerUseConfig), +] + +# What the entries below set. Every other key must keep its global value. +SMALL = """\ + overrides: + session: + default_effort: low + compaction: + summary: extractive + background_percent: 50 + clear_tool_results: + keep_recent_tool_results: 1 + exclude_tools: [fs_read] + consult: + tiers: [] + workflow: + max_parallel: 1 + computer_use: + max_prompt_images: 0 +""" +BIG = """\ + overrides: + session: + default_effort: high + default_ultracode: true +""" + + +def _with(text: str, entry: str, block: str) -> str: + header = f"\n {entry}:\n" + assert text.count(header) == 1, entry + return text.replace(header, header + block, 1) + + +def _catalog_text() -> str: + text = _with(catalog_config_yaml(), "local", SMALL) + return _with(text, "budget", BIG) + + +def _config(tmp_path: Path, text: str | None = None) -> ModelConfig: + path = tmp_path / "config.yaml" + path.write_text(_catalog_text() if text is None else text, encoding="utf-8") + return load_config(path) + + +class TestTheWhitelist: + def test_the_six_sections_are_the_whole_list(self) -> None: + assert set(PresetOverrides.model_fields) == set(OVERRIDE_SECTIONS) + assert set(OVERRIDE_SECTIONS) == { + "session", + "clear_tool_results", + "compaction", + "consult", + "workflow", + "computer_use", + } + # Each is a real global section of the same name. + assert set(OVERRIDE_SECTIONS) <= set(ModelConfig.model_fields) + + @pytest.mark.parametrize(("override", "base"), PAIRS) + def test_every_override_key_has_its_global_keys_type_and_limits( + self, override: type[BaseModel], base: type[BaseModel] + ) -> None: + for name, info in override.model_fields.items(): + assert name in base.model_fields, f"{override.__name__}.{name} overrides nothing" + ours, theirs = info, base.model_fields[name] + assert ours.annotation == theirs.annotation | None, name + assert repr(ours.metadata) == repr(theirs.metadata), name + assert ours.default is None and not ours.is_required(), name + + def test_only_what_suits_a_model_is_overridable_in_the_partial_sections(self) -> None: + assert set(SessionOverride.model_fields) == {"default_effort", "default_ultracode"} + assert set(ComputerUseOverride.model_fields) == {"max_prompt_images"} + # Whole sections: a key added to the global one must be decided here too. + for override, base in PAIRS[1:5]: + assert set(override.model_fields) == set(base.model_fields), base.__name__ + + @pytest.mark.parametrize( + "block", + [ + "policy: {skip_all: true}", + "shell: {read_only: {enabled: false}}", + "mcp: {servers: {}}", + "hooks: {after_write: []}", + "remote: {bind: 100.64.0.1}", + "autonomy: {runtime: none}", + "notify: {}", + "credentials: {}", + "engine: {kind: grok}", + "session: {log_max_events: 5}", + "session: {send_file_max_bytes: 1}", + "computer_use: {command: rm}", + "computer_use: {local_worker_preset: vllm}", + "workflow: {max_parralel: 2}", + ], + ) + def test_anything_else_is_an_error_at_load(self, tmp_path: Path, block: str) -> None: + text = _with(catalog_config_yaml(), "local", f" overrides:\n {block}\n") + with pytest.raises(ConfigError, match=r"presets\.local\.overrides"): + _config(tmp_path, text) + + @pytest.mark.parametrize( + "block", + [ + "workflow: {max_parallel: 0}", + "workflow: {max_parallel: 17}", + "compaction: {background_percent: 100}", + "compaction: {summary: clever}", + "session: {default_effort: enormous}", + "consult: {tiers: [manager]}", + "computer_use: {max_prompt_images: -1}", + ], + ) + def test_a_value_outside_the_global_keys_limits_is_an_error_at_load( + self, tmp_path: Path, block: str + ) -> None: + text = _with(catalog_config_yaml(), "local", f" overrides:\n {block}\n") + with pytest.raises(ConfigError, match=r"presets\.local\.overrides"): + _config(tmp_path, text) + + +class TestEffectiveConfig: + def test_no_overrides_is_the_snapshot_a_session_took_before(self, tmp_path: Path) -> None: + config = _config(tmp_path, catalog_config_yaml()) + for name in config.presets: + assert effective_config(config, name) == config.model_copy( + update={"active_preset": name} + ) + + def test_a_set_key_wins_and_every_other_key_keeps_its_global_value( + self, tmp_path: Path + ) -> None: + config = _config(tmp_path) + seen = effective_config(config, "local") + assert seen.active_preset == "local" + assert seen.session.default_effort == "low" + assert seen.compaction.summary == "extractive" + assert seen.compaction.background_percent == 50 + assert seen.clear_tool_results.keep_recent_tool_results == 1 + assert seen.workflow.max_parallel == 1 + assert seen.computer_use.max_prompt_images == 0 + + expected = { + "session": {"default_effort": "low"}, + "compaction": {"summary": "extractive", "background_percent": 50}, + "clear_tool_results": {"keep_recent_tool_results": 1, "exclude_tools": ["fs_read"]}, + "consult": {"tiers": []}, + "workflow": {"max_parallel": 1}, + "computer_use": {"max_prompt_images": 0}, + } + before, after = config.model_dump(), seen.model_dump() + for section, keys in expected.items(): + for key, value in before[section].items(): + assert after[section][key] == keys.get(key, value), f"{section}.{key}" + untouched = set(before) - set(expected) - {"active_preset"} + for section in untouched: + assert after[section] == before[section], section + + def test_a_list_replaces_the_global_list_whole(self, tmp_path: Path) -> None: + text = _catalog_text().replace(" exclude_tools: []", " exclude_tools: [web_fetch]", 1) + config = _config(tmp_path, text) + assert config.clear_tool_results.exclude_tools == ["web_fetch"] + seen = effective_config(config, "local") + assert seen.clear_tool_results.exclude_tools == ["fs_read"] + assert seen.consult.tiers == [] and config.consult.tiers == ["worker"] + + def test_a_null_or_an_empty_section_keeps_the_global_value(self, tmp_path: Path) -> None: + block = " overrides:\n workflow: {max_parallel: null}\n compaction: {}\n" + config = _config(tmp_path, _with(catalog_config_yaml(), "local", block)) + assert effective_config(config, "local") == config.model_copy( + update={"active_preset": "local"} + ) + + def test_the_global_config_and_other_entries_are_not_changed(self, tmp_path: Path) -> None: + config = _config(tmp_path) + before = config.model_dump() + effective_config(config, "local") + effective_config(config, "budget") + assert config.model_dump() == before + other = effective_config(config, "vllm") + assert other.workflow == config.workflow + assert other.session == config.session + + def test_a_later_global_edit_shows_through_the_keys_the_entry_leaves( + self, tmp_path: Path + ) -> None: + config = _config(tmp_path) + edited = config.model_copy( + update={"workflow": WorkflowConfig(max_parallel=5, max_tasks=20)} + ) + seen = effective_config(edited, "local") + assert seen.workflow.max_parallel == 1 # the entry's + assert seen.workflow.max_tasks == 20 # the new global + + def test_an_unknown_entry_has_no_overrides(self, tmp_path: Path) -> None: + config = _config(tmp_path) + assert effective_config(config, "nope") == config.model_copy( + update={"active_preset": "nope"} + ) + + +def test_a_copy_carries_its_sources_overrides(tmp_path: Path) -> None: + path = tmp_path / "config.yaml" + path.write_text(_catalog_text(), encoding="utf-8") + duplicate_preset("local", "local-2", "Local two", path) + config = load_config(path) + assert config.presets["local-2"].overrides == config.presets["local"].overrides + assert effective_config(config, "local-2").workflow.max_parallel == 1 + written = yaml.safe_load(path.read_text(encoding="utf-8"))["presets"]["local-2"] + # Only the keys the source set, so the copy still follows the global rest. + assert written["overrides"]["workflow"] == {"max_parallel": 1} + + +def _mock() -> MockProvider: + return MockProvider(default=Script(kind="stream", content="done")) + + +async def _seeded(tmp_path: Path) -> tuple[Daemon, Any, Any, Path]: + data = tmp_path / "data" + data.mkdir() + (data / "config.yaml").write_text(_catalog_text(), encoding="utf-8") + workspace = tmp_path / "ws" + workspace.mkdir() + daemon, task, ws = await _start(data, _mock()) + return daemon, task, ws, workspace + + +def _last_tier_state(sess: Any) -> TierState: + states = [e for e in sess.event_log.all_events if isinstance(e, TierState)] + assert states + return states[-1] + + +class TestAChatGetsThem: + async def test_a_chat_opened_on_the_entry_starts_with_its_settings( + self, tmp_path: Path + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path) + try: + await _ask(ws, {"type": "set_preset", "name": "local"}, "setup_state") + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sess = daemon.session_registry.get(opened["session_id"]) + assert sess is not None and sess.config is not None and sess.router is not None + assert sess.config.workflow.max_parallel == 1 + assert sess.config.clear_tool_results.keep_recent_tool_results == 1 + assert sess.router.effort == "low" + # The daemon's own config is still the global one. + assert daemon.config.workflow.max_parallel == 3 + assert daemon.config.session.default_effort == "medium" + finally: + await _stop(task, ws) + + async def test_switching_a_chat_switches_its_settings_and_its_effort( + self, tmp_path: Path + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path) + try: + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sid = opened["session_id"] + sess = daemon.session_registry.get(sid) + assert sess is not None and sess.config is not None and sess.router is not None + assert sess.preset == "tst-default" + assert sess.router.effort == "medium" and sess.router.ultracode is False + + await _ask( + ws, + {"type": "set_session_preset", "session_id": sid, "name": "budget"}, + "session_list", + ) + assert sess.router.effort == "high" and sess.router.ultracode is True + state = _last_tier_state(sess) + assert state.effort == "high" and state.ultracode is True + + await _ask( + ws, + {"type": "set_session_preset", "session_id": sid, "name": "local"}, + "session_list", + ) + assert sess.config.workflow.max_parallel == 1 + assert sess.config.compaction.summary == "extractive" + assert sess.router.effort == "low" + # "local" names an effort but no Ultracode default: the chat's switch stays. + assert sess.router.ultracode is True + finally: + await _stop(task, ws) + + async def test_an_entry_that_names_no_effort_leaves_the_chats_own(self, tmp_path: Path) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path) + try: + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sid = opened["session_id"] + sess = daemon.session_registry.get(sid) + assert sess is not None and sess.router is not None + await ws.send(json.dumps({"type": "set_effort", "session_id": sid, "effort": "xhigh"})) + await _ask( + ws, + {"type": "set_session_preset", "session_id": sid, "name": "vllm"}, + "session_list", + ) + assert sess.preset == "vllm" + assert sess.router.effort == "xhigh" + finally: + await _stop(task, ws) + + async def test_the_next_turn_runs_on_the_switched_settings(self, tmp_path: Path) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path) + try: + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sid = opened["session_id"] + sess = daemon.session_registry.get(sid) + assert sess is not None + await ws.send(json.dumps({"type": "attach", "session_id": sid, "from_seq": 1})) + await _ask( + ws, + {"type": "set_session_preset", "session_id": sid, "name": "local"}, + "session_list", + ) + done = await _ask( + ws, + {"type": "user_message", "session_id": sid, "content": "hello"}, + "turn_complete", + ) + assert done["type"] == "turn_complete" + # What the turn's tools were handed: the workflow tool reads its + # limits here, and the loop took the same object for clearing. + runtime = sess.delegate_runtime + assert runtime is not None + assert runtime.config is sess.config + assert runtime.config.workflow.max_parallel == 1 + assert runtime.config.clear_tool_results.keep_recent_tool_results == 1 + assert runtime.config.computer_use.max_prompt_images == 0 + finally: + await _stop(task, ws) + + async def test_a_settings_edit_does_not_rewrite_an_open_chat(self, tmp_path: Path) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path) + try: + await _ask(ws, {"type": "set_preset", "name": "local"}, "setup_state") + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sess = daemon.session_registry.get(opened["session_id"]) + assert sess is not None and sess.config is not None + snapshot = sess.config + await _ask(ws, {"type": "set_preset", "name": "budget"}, "setup_state") + assert sess.config is snapshot + assert sess.config.workflow.max_parallel == 1 + finally: + await _stop(task, ws) + + +class TestRevive: + async def _revived(self, tmp_path: Path, effort: str | None) -> Any: + data = tmp_path / "data" + workspace = tmp_path / "ws" + data.mkdir() + workspace.mkdir() + (data / "config.yaml").write_text(_catalog_text(), encoding="utf-8") + store = SessionStore(data) + await store.upsert("sid-1", str(workspace), "running", preset="local") + if effort is not None: + await store.set_effort("sid-1", effort) + from tstd.session_persist import SessionPersist + + SessionPersist(data).prepare("sid-1") + daemon = Daemon(data_dir=data) + await daemon._restore_sessions() + return daemon + + async def _close(self, daemon: Daemon) -> None: + for live in await daemon.session_registry.list_sessions(): + runner = daemon.session_registry.get_runner(live.id) + if runner is not None: + await runner.cancel() + + async def test_a_revived_chat_has_its_entrys_settings(self, tmp_path: Path) -> None: + daemon = await self._revived(tmp_path, None) + try: + sess = daemon.session_registry.get("sid-1") + assert sess is not None and sess.config is not None and sess.router is not None + assert sess.preset == "local" + assert sess.config.workflow.max_parallel == 1 + # No stored effort: it starts where its model says. + assert sess.router.effort == "low" + finally: + await self._close(daemon) + + async def test_a_revived_chat_keeps_the_effort_it_had(self, tmp_path: Path) -> None: + daemon = await self._revived(tmp_path, "xhigh") + try: + sess = daemon.session_registry.get("sid-1") + assert sess is not None and sess.router is not None + assert sess.router.effort == "xhigh" + finally: + await self._close(daemon) diff --git a/core/tests/test_preset_ready.py b/core/tests/test_preset_ready.py new file mode 100644 index 00000000..189798c3 --- /dev/null +++ b/core/tests/test_preset_ready.py @@ -0,0 +1,195 @@ +"""Whether each model on the picker can run right now (TD-5240). + +Three things can be known without sending a key or asking a provider: the +key is in the keychain, a server on this computer answers, the Grok CLI is +there. The picker shows the first one that is not true. +""" + +from __future__ import annotations + +from collections.abc import Mapping +from pathlib import Path +from typing import Any + +import httpx +import pytest + +from tests.test_add_model import _ask, _start, _stop +from tests.test_preset_engine import _text as _catalog_with_grok +from tstd.config import ModelConfig, load_config +from tstd.local_servers import loopback_answers +from tstd.mock import MockProvider, Script +from tstd.peer_policy import MACHINE_PEER_VERBS +from tstd.preset_ready import check_presets, loopback_urls + +OLLAMA = "http://127.0.0.1:11434/v1" +VLLM = "http://127.0.0.1:8000/v1" + + +def _config(tmp_path: Path) -> ModelConfig: + path = tmp_path / "config.yaml" + path.write_text(_catalog_with_grok(), encoding="utf-8") + return load_config(path) + + +async def _rows( + config: ModelConfig, + *, + stored: set[str] | None = None, + down: set[str] | None = None, + grok: bool = True, +) -> dict[str, tuple[str, str]]: + asked_keys: list[str] = [] + asked_urls: list[str] = [] + + async def key_stored(credential: str) -> bool: + asked_keys.append(credential) + return credential in (stored if stored is not None else {"openrouter"}) + + async def answers(urls: list[str]) -> Mapping[str, bool]: + asked_urls.extend(urls) + return {url: url not in (down or set()) for url in urls} + + rows = await check_presets(config, key_stored=key_stored, answers=answers, grok_found=grok) + assert [row.preset for row in rows] == list(config.presets) + # Only servers on this computer are ever asked. + assert all(url.startswith("http://127.0.0.1") for url in asked_urls) + return {row.preset: (row.status, row.detail) for row in rows} + + +class TestTheChecks: + async def test_everything_in_place_is_ready(self, tmp_path: Path) -> None: + rows = await _rows(_config(tmp_path)) + assert {status for status, _detail in rows.values()} == {"ready"} + assert all(detail == "" for _status, detail in rows.values()) + + async def test_a_key_that_is_not_stored(self, tmp_path: Path) -> None: + rows = await _rows(_config(tmp_path), stored=set()) + assert rows["tst-default"] == ("no_key", "No key is stored for OpenRouter.") + assert rows["budget"][0] == "no_key" + # A model on this computer needs no key, and Grok brings its own. + assert rows["local"][0] == "ready" and rows["grok"][0] == "ready" + + async def test_a_server_on_this_computer_that_does_not_answer(self, tmp_path: Path) -> None: + rows = await _rows(_config(tmp_path), down={OLLAMA}) + assert rows["local"] == ( + "server_down", + "Nothing is answering at 127.0.0.1:11434. Start the model server.", + ) + assert rows["vllm"][0] == "ready" + assert rows["tst-default"][0] == "ready" + + async def test_a_grok_entry_without_the_grok_cli(self, tmp_path: Path) -> None: + rows = await _rows(_config(tmp_path), grok=False) + assert rows["grok"] == ("no_grok", "The Grok CLI was not found on this computer.") + assert rows["local"][0] == "ready" + + async def test_the_first_problem_a_person_would_fix_is_the_one_shown( + self, tmp_path: Path + ) -> None: + # A local role bound to a key that is missing, on a server that is down. + text = ( + _catalog_with_grok() + .replace( + f" base_url: {OLLAMA}\n", + f" base_url: {OLLAMA}\n credential: house\n", + ) + .replace("\ncredentials:\n", "\ncredentials:\n house:\n name: House key\n", 1) + ) + path = tmp_path / "config.yaml" + path.write_text(text, encoding="utf-8") + rows = await _rows(load_config(path), down={OLLAMA}) + assert rows["local"] == ("no_key", "No key is stored for House key.") + + async def test_a_providers_server_is_never_asked(self, tmp_path: Path) -> None: + config = _config(tmp_path) + assert loopback_urls(config) == [OLLAMA, VLLM] + + async def test_with_the_grok_engine_as_the_setting_every_entry_is_a_grok_one( + self, tmp_path: Path + ) -> None: + text = _catalog_with_grok(engine_kind="grok") + path = tmp_path / "config.yaml" + path.write_text(text, encoding="utf-8") + rows = await _rows(load_config(path), stored=set(), down={OLLAMA}, grok=False) + assert {status for status, _detail in rows.values()} == {"no_grok"} + + +class TestTheProbe: + async def _ask(self, handler: Any, urls: list[str]) -> tuple[dict[str, bool], list[str]]: + seen: list[str] = [] + + def record(request: httpx.Request) -> httpx.Response: + seen.append(str(request.url)) + assert "authorization" not in {k.lower() for k in request.headers} + return handler(request) + + async with httpx.AsyncClient(transport=httpx.MockTransport(record)) as client: + return await loopback_answers(urls, client=client), seen + + async def test_any_reply_means_a_server_is_there(self) -> None: + def handler(request: httpx.Request) -> httpx.Response: + return httpx.Response(404 if "11434" in str(request.url) else 200, json={}) + + found, seen = await self._ask(handler, [OLLAMA, VLLM + "/"]) + assert found == {OLLAMA: True, VLLM: True} + assert sorted(seen) == sorted([f"{VLLM}/models", f"{OLLAMA}/models"]) + + async def test_a_refused_connection_or_a_timeout_means_it_is_not(self) -> None: + def handler(request: httpx.Request) -> httpx.Response: + if "11434" in str(request.url): + raise httpx.ConnectError("refused", request=request) + raise httpx.ReadTimeout("slow", request=request) + + found, _seen = await self._ask(handler, [OLLAMA, VLLM]) + assert found == {OLLAMA: False, VLLM: False} + + async def test_a_url_off_this_computer_is_not_asked_and_carries_no_key(self) -> None: + def handler(request: httpx.Request) -> httpx.Response: + return httpx.Response(200, json={}) + + found, seen = await self._ask(handler, ["https://api.example.com/v1", OLLAMA]) + assert seen == [f"{OLLAMA}/models"] + assert found == {"https://api.example.com/v1": True, OLLAMA: True} + + async def test_nothing_local_asks_nothing(self) -> None: + def handler(request: httpx.Request) -> httpx.Response: + raise AssertionError("no request expected") + + found, seen = await self._ask(handler, ["https://api.example.com/v1"]) + assert seen == [] and found == {"https://api.example.com/v1": True} + + +class TestOverTheWire: + async def test_check_presets_answers_with_a_row_per_model( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + asked: list[list[str]] = [] + + async def answers(urls: list[str]) -> dict[str, bool]: + asked.append(list(urls)) + return {url: "8000" in url for url in urls} + + monkeypatch.setattr("tstd.daemon.loopback_answers", answers) + monkeypatch.setattr("tstd.daemon._probe_grok", lambda _configured: (None, False)) + data = tmp_path / "data" + data.mkdir() + (data / "config.yaml").write_text(_catalog_with_grok(), encoding="utf-8") + _daemon, task, ws = await _start(data, MockProvider(default=Script(kind="text"))) + try: + reply = await _ask(ws, {"type": "check_presets"}, "preset_readiness") + assert reply["type"] == "preset_readiness" + rows = {row["preset"]: row for row in reply["rows"]} + assert rows["local"]["status"] == "server_down" + assert "127.0.0.1:11434" in rows["local"]["detail"] + assert rows["vllm"]["status"] == "ready" + assert rows["grok"]["status"] == "no_grok" + # The suite stores no key, so the hosted entries say so. + assert rows["tst-default"]["status"] == "no_key" + assert asked == [[OLLAMA, VLLM]] + finally: + await _stop(task, ws) + + +def test_another_computers_hub_may_not_send_it() -> None: + assert "check_presets" not in MACHINE_PEER_VERBS diff --git a/core/tests/test_preset_roles.py b/core/tests/test_preset_roles.py new file mode 100644 index 00000000..bb43e71a --- /dev/null +++ b/core/tests/test_preset_roles.py @@ -0,0 +1,112 @@ +"""Settings → Model edits the entry you choose, not only the active one (TD-5241). + +``setup_state`` describes the active entry's roles. ``get_preset_roles`` +gives the same four maps for any entry, so the page can show and edit one +without first making it the model new chats use. +""" + +from __future__ import annotations + +from pathlib import Path + +from tests.test_add_model import _ask, _start, _stop +from tests.test_preset_engine import _text as _catalog_with_grok +from tstd.config import load_config +from tstd.mock import MockProvider, Script +from tstd.peer_policy import MACHINE_PEER_VERBS + +ROLE_MAPS = ("tier_slugs", "tier_credentials", "tier_loopback", "tier_cards") + + +async def _seeded(tmp_path: Path) -> tuple: # type: ignore[type-arg] + data = tmp_path / "data" + data.mkdir() + (data / "config.yaml").write_text(_catalog_with_grok(), encoding="utf-8") + return await _start(data, MockProvider(default=Script(kind="text"))) + + +async def test_an_entrys_roles_are_what_setup_state_says_when_it_is_active( + tmp_path: Path, +) -> None: + _daemon, task, ws = await _seeded(tmp_path) + try: + before = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + assert before["active_preset"] == "tst-default" + roles = await _ask(ws, {"type": "get_preset_roles", "name": "budget"}, "preset_roles") + assert roles["type"] == "preset_roles" and roles["preset"] == "budget" + assert set(roles["tier_slugs"]) == {"brain", "worker", "validator"} + # Asking changed nothing: the active entry is still the active one. + again = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + assert again["active_preset"] == "tst-default" + assert {k: again[k] for k in ROLE_MAPS} == {k: before[k] for k in ROLE_MAPS} + + active = await _ask(ws, {"type": "set_preset", "name": "budget"}, "setup_state") + assert {k: roles[k] for k in ROLE_MAPS} == {k: active[k] for k in ROLE_MAPS} + finally: + await _stop(task, ws) + + +async def test_a_role_on_this_computer_and_one_at_a_provider(tmp_path: Path) -> None: + _daemon, task, ws = await _seeded(tmp_path) + try: + local = await _ask(ws, {"type": "get_preset_roles", "name": "local"}, "preset_roles") + assert set(local["tier_loopback"].values()) == {True} + assert set(local["tier_credentials"].values()) == {None} + # Discovered from the server each run: no model is named. + assert set(local["tier_slugs"].values()) == {None} + hosted = await _ask(ws, {"type": "get_preset_roles", "name": "tst-default"}, "preset_roles") + assert set(hosted["tier_loopback"].values()) == {False} + assert hosted["tier_credentials"]["brain"] == "openrouter" + assert hosted["tier_cards"]["brain"]["context_window"] == 1000000 + finally: + await _stop(task, ws) + + +async def test_an_edit_to_a_chosen_entry_shows_in_its_roles_and_not_the_active_ones( + tmp_path: Path, +) -> None: + daemon, task, ws = await _seeded(tmp_path) + try: + before = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + ack = await _ask( + ws, + { + "type": "set_tier_context_window", + "preset": "local", + "tier": "worker", + "context_window": 49152, + }, + "setup_state", + ) + # The ack still describes the active entry, untouched. + assert ack["active_preset"] == "tst-default" + assert ack["tier_cards"] == before["tier_cards"] + roles = await _ask(ws, {"type": "get_preset_roles", "name": "local"}, "preset_roles") + assert roles["tier_cards"]["worker"]["context_window"] == 49152 + worker = load_config(daemon.config_path).presets["local"].worker + assert worker is not None and worker.context_window == 49152 + finally: + await _stop(task, ws) + + +async def test_an_entry_with_no_roles_has_none(tmp_path: Path) -> None: + _daemon, task, ws = await _seeded(tmp_path) + try: + roles = await _ask(ws, {"type": "get_preset_roles", "name": "grok"}, "preset_roles") + assert roles["preset"] == "grok" + assert all(roles[key] == {} for key in ROLE_MAPS) + finally: + await _stop(task, ws) + + +async def test_an_unknown_entry_is_a_typed_error(tmp_path: Path) -> None: + _daemon, task, ws = await _seeded(tmp_path) + try: + reply = await _ask(ws, {"type": "get_preset_roles", "name": "nope"}, "preset_roles") + assert reply["type"] == "error" and reply["code"] == "unknown_preset" + finally: + await _stop(task, ws) + + +def test_another_computers_hub_may_not_send_it() -> None: + assert "get_preset_roles" not in MACHINE_PEER_VERBS diff --git a/core/tests/test_preset_write.py b/core/tests/test_preset_write.py new file mode 100644 index 00000000..3313e530 --- /dev/null +++ b/core/tests/test_preset_write.py @@ -0,0 +1,371 @@ +"""Copy, relabel and remove catalog entries (TD-5237). + +The writers edit a file a person also edits, so every case checks two +things: the catalog the daemon would load afterwards, and that the lines +the edit does not own are still there byte for byte. +""" + +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any + +import pytest + +from tests.catalog_helpers import catalog_config_yaml +from tests.test_add_model import _ask, _start, _stop +from tstd.config import ConfigError, default_config_yaml, load_config +from tstd.mock import MockProvider, Script +from tstd.peer_policy import LOOPBACK_VERBS, MACHINE_PEER_VERBS +from tstd.preset_write import delete_preset, duplicate_preset, save_preset_label + +ANCHORED = """\ +# my models +presets: + shared: + label: Shared + description: One model on every role + brain: &one + slug: vendor/model-a + base_url: http://127.0.0.1:8000/v1 + context_window: 32768 + max_output_tokens: 4096 + input_price: 0.0 + output_price: 0.0 + cache_read_price: 0.0 + worker: *one + validator: *one + borrower: + label: Borrower + brain: *one + worker: *one + validator: *one + +# which one new chats use +active_preset: shared +""" + + +def _write(tmp_path: Path, text: str) -> Path: + path = tmp_path / "config.yaml" + path.write_text(text, encoding="utf-8") + return path + + +def _catalog(tmp_path: Path) -> Path: + return _write(tmp_path, catalog_config_yaml()) + + +class TestDuplicate: + def test_the_copy_is_the_same_model_under_a_new_label(self, tmp_path: Path) -> None: + path = _catalog(tmp_path) + duplicate_preset("local", "local-2", "Local, small window", path) + cfg = load_config(path) + copy, source = cfg.presets["local-2"], cfg.presets["local"] + assert copy.label == "Local, small window" + for tier in ("brain", "worker", "validator"): + assert getattr(copy, tier) == getattr(source, tier) + assert list(cfg.presets)[-1] == "local-2" + + def test_only_the_new_block_is_added(self, tmp_path: Path) -> None: + before = catalog_config_yaml() + path = _write(tmp_path, before) + duplicate_preset("local", "local-2", "Copy", path) + after = path.read_text(encoding="utf-8").split("\n") + start = after.index(" local-2:") + # The block is every following line indented under the entry. + end = next(i for i in range(start + 1, len(after)) if not after[i].startswith(" ")) + assert "\n".join(after[:start] + after[end:]) == before + + def test_a_copy_shares_no_anchor_with_its_source(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + duplicate_preset("shared", "mine", "Mine", path) + text = path.read_text(encoding="utf-8") + block = text[text.index(" mine:") : text.index("# which one")] + assert "&" not in block and "*" not in block + assert block.count("slug: vendor/model-a") == 3 + cfg = load_config(path) + assert cfg.presets["mine"].brain == cfg.presets["shared"].brain + assert cfg.presets["mine"].description == "One model on every role" + # The comment that opens the next section stayed with it. + assert "\n# which one new chats use\nactive_preset: shared\n" in text + + def test_a_label_cannot_inject_yaml(self, tmp_path: Path) -> None: + path = _catalog(tmp_path) + duplicate_preset("local", "odd", 'x"\n picker: more', path) + cfg = load_config(path) + assert cfg.presets["odd"].label == 'x"\n picker: more' + assert cfg.presets["odd"].picker == cfg.presets["local"].picker + + @pytest.mark.parametrize( + ("source", "name", "label", "says"), + [ + ("nope", "copy", "Copy", "Unknown preset"), + ("local", "vllm", "Copy", "already exists"), + ("local", "Bad Name", "Copy", "lowercase slug"), + ("local", "copy", " ", "Give the model a name"), + ("local", "copy", "x" * 41, "at most 40"), + ], + ) + def test_refusals_write_nothing( + self, tmp_path: Path, source: str, name: str, label: str, says: str + ) -> None: + path = _catalog(tmp_path) + with pytest.raises(ConfigError, match=says): + duplicate_preset(source, name, label, path) + assert path.read_text(encoding="utf-8") == catalog_config_yaml() + + +class TestLabel: + def test_only_the_label_line_changes(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + save_preset_label("shared", "House model", path=path) + assert path.read_text(encoding="utf-8") == ANCHORED.replace( + " label: Shared", ' label: "House model"' + ) + cfg = load_config(path) + assert cfg.presets["shared"].label == "House model" + assert cfg.presets["shared"].description == "One model on every role" + assert set(cfg.presets) == {"shared", "borrower"} + + def test_a_missing_label_is_added_under_the_entry(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED.replace(" label: Borrower\n", "")) + save_preset_label("borrower", "Second", "Same model again", path) + text = path.read_text(encoding="utf-8") + assert ' borrower:\n label: "Second"\n description: "Same model again"\n' in text + cfg = load_config(path) + assert cfg.presets["borrower"].label == "Second" + assert cfg.presets["borrower"].description == "Same model again" + + def test_an_empty_description_removes_the_line(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + save_preset_label("shared", "Shared", "", path) + assert "description" not in path.read_text(encoding="utf-8") + assert load_config(path).presets["shared"].description is None + + def test_a_label_cannot_inject_yaml(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + save_preset_label("shared", 'a"\n picker: more', path=path) + cfg = load_config(path) + assert cfg.presets["shared"].label == 'a"\n picker: more' + assert cfg.presets["shared"].picker == "primary" + + @pytest.mark.parametrize( + ("name", "label", "description", "says"), + [ + ("nope", "X", None, "Unknown preset"), + ("shared", " ", None, "Give the model a name"), + ("shared", "x" * 41, None, "at most 40"), + ("shared", "X", "d" * 121, "at most 120"), + ], + ) + def test_refusals_write_nothing( + self, tmp_path: Path, name: str, label: str, description: str | None, says: str + ) -> None: + path = _write(tmp_path, ANCHORED) + with pytest.raises(ConfigError, match=says): + save_preset_label(name, label, description, path) + assert path.read_text(encoding="utf-8") == ANCHORED + + +class TestDelete: + def test_the_entry_goes_and_the_rest_stays(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + delete_preset("borrower", path) + gone = ( + " borrower:\n label: Borrower\n brain: *one\n" + " worker: *one\n validator: *one\n" + ) + assert path.read_text(encoding="utf-8") == ANCHORED.replace(gone, "") + cfg = load_config(path) + assert list(cfg.presets) == ["shared"] + assert cfg.active_preset == "shared" + + def test_removing_the_active_entry_moves_active_to_the_first_left(self, tmp_path: Path) -> None: + path = _catalog(tmp_path) + before = load_config(path) + names = list(before.presets) + victim = names[0] + text = path.read_text(encoding="utf-8") + if "active_preset:" not in text: + path.write_text(text + f"\nactive_preset: {victim}\n", encoding="utf-8") + else: + from tstd.config_write import save_active_preset + + save_active_preset(victim, path) + delete_preset(victim, path) + cfg = load_config(path) + assert victim not in cfg.presets + assert cfg.active_preset == names[1] + assert f"active_preset: {names[1]}" in path.read_text(encoding="utf-8") + + def test_removing_the_last_entry_leaves_an_empty_catalog(self, tmp_path: Path) -> None: + path = _write(tmp_path, default_config_yaml()) + from tests.test_add_model import _add + + _add(path) + from tstd.config_write import save_active_preset + + save_active_preset("my-model", path) + delete_preset("my-model", path) + cfg = load_config(path) + assert cfg.presets == {} + assert cfg.active_preset == "" + assert cfg.has_model() is False + assert "\npresets: {}\n" in path.read_text(encoding="utf-8") + + def test_an_entry_another_one_borrows_from_is_refused(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + with pytest.raises(ConfigError, match="reuses part of it"): + delete_preset("shared", path) + assert path.read_text(encoding="utf-8") == ANCHORED + + def test_the_computer_use_helper_model_is_refused(self, tmp_path: Path) -> None: + text = catalog_config_yaml().replace( + 'local_worker_preset: ""', "local_worker_preset: vllm", 1 + ) + assert "local_worker_preset: vllm" in text + path = _write(tmp_path, text) + with pytest.raises(ConfigError, match="computer-use helper"): + delete_preset("vllm", path) + assert path.read_text(encoding="utf-8") == text + + def test_an_unknown_entry_is_refused(self, tmp_path: Path) -> None: + path = _write(tmp_path, ANCHORED) + with pytest.raises(ConfigError, match="Unknown preset"): + delete_preset("nope", path) + assert path.read_text(encoding="utf-8") == ANCHORED + + +def _mock() -> MockProvider: + return MockProvider(default=Script(kind="text")) + + +class TestOverTheWire: + """The three verbs as Settings sends them, against a running daemon.""" + + async def test_duplicate_adds_an_entry_and_leaves_the_active_one(self, tmp_path: Path) -> None: + daemon, task, ws = await _start(tmp_path, _mock()) + try: + before = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + state = await _ask( + ws, + {"type": "duplicate_preset", "source": "local", "label": "Local, tight"}, + "setup_state", + ) + # setup_state lists the catalog sorted by key. + assert state["presets"] == sorted([*before["presets"], "local-tight"]) + assert state["preset_labels"]["local-tight"] == "Local, tight" + assert state["active_preset"] == before["active_preset"] + on_disk = load_config(daemon.config_path) + assert on_disk.presets["local-tight"].brain == on_disk.presets["local"].brain + finally: + await _stop(task, ws) + + async def test_a_second_copy_with_the_same_label_gets_its_own_key(self, tmp_path: Path) -> None: + _daemon, task, ws = await _start(tmp_path, _mock()) + try: + msg = {"type": "duplicate_preset", "source": "local", "label": "Copy"} + first = await _ask(ws, msg, "setup_state") + second = await _ask(ws, msg, "setup_state") + added = [n for n in second["presets"] if n not in first["presets"]] + assert len(added) == 1 and added[0] != "copy" + assert "copy" in first["presets"] + finally: + await _stop(task, ws) + + async def test_relabel_changes_what_the_picker_shows(self, tmp_path: Path) -> None: + _daemon, task, ws = await _start(tmp_path, _mock()) + try: + state = await _ask( + ws, + { + "type": "set_preset_label", + "name": "local", + "label": "On this computer", + "description": "Nothing leaves the machine", + }, + "setup_state", + ) + assert state["preset_labels"]["local"] == "On this computer" + assert state["preset_descriptions"]["local"] == "Nothing leaves the machine" + assert "local" in state["presets"] + finally: + await _stop(task, ws) + + async def test_delete_moves_active_and_an_open_chat_keeps_its_model( + self, tmp_path: Path + ) -> None: + workspace = tmp_path / "ws" + workspace.mkdir() + daemon, task, ws = await _start(tmp_path / "data", _mock()) + try: + before = await _ask(ws, {"type": "get_setup_state"}, "setup_state") + victim = before["active_preset"] + opened = await _ask( + ws, {"type": "open_workspace", "path": str(workspace)}, "session_state" + ) + sess = daemon.session_registry.get(opened["session_id"]) + assert sess is not None and sess.config is not None + snapshot = sess.config + slug = snapshot.tier("brain").slug + + state = await _ask(ws, {"type": "delete_preset", "name": victim}, "setup_state") + assert victim not in state["presets"] + # The first entry left in the file, the loader's own choice. + left = list(load_config(daemon.config_path).presets) + assert victim not in left + assert state["active_preset"] == left[0] + assert state["has_model"] is True + # TD-1721: the open chat still calls the model it opened with. + assert sess.config is snapshot + assert sess.config.tier("brain").slug == slug + finally: + await _stop(task, ws) + + @pytest.mark.parametrize( + "msg", + [ + {"type": "duplicate_preset", "source": "nope", "label": "Copy"}, + {"type": "set_preset_label", "name": "nope", "label": "X"}, + {"type": "delete_preset", "name": "nope"}, + ], + ) + async def test_an_unknown_entry_is_a_typed_error( + self, tmp_path: Path, msg: dict[str, Any] + ) -> None: + daemon, task, ws = await _start(tmp_path, _mock()) + try: + text = daemon.config_path.read_text(encoding="utf-8") + reply = await _ask(ws, msg, "setup_state") + assert reply["type"] == "error" + assert reply["code"] == "bad_request" + assert "Unknown preset" in reply["message"] + assert daemon.config_path.read_text(encoding="utf-8") == text + finally: + await _stop(task, ws) + + async def test_no_key_is_written_by_a_copy(self, tmp_path: Path) -> None: + daemon, task, ws = await _start(tmp_path, _mock()) + try: + await _ask( + ws, + {"type": "duplicate_preset", "source": "tst-default", "label": "Hosted copy"}, + "setup_state", + ) + copy = load_config(daemon.config_path).presets["hosted-copy"] + dumped = json.dumps(copy.model_dump(mode="json")) + assert "api_key" not in dumped and "sk-" not in dumped + assert ( + copy.brain.credential + == load_config(daemon.config_path).presets["tst-default"].brain.credential + ) + finally: + await _stop(task, ws) + + +def test_another_computer_may_send_none_of_them_and_only_this_one_may_delete() -> None: + verbs = {"duplicate_preset", "set_preset_label", "delete_preset"} + assert not verbs & MACHINE_PEER_VERBS + assert "delete_preset" in LOOPBACK_VERBS diff --git a/core/tests/test_remote_devices.py b/core/tests/test_remote_devices.py index b2771ce8..8cd9c21b 100644 --- a/core/tests/test_remote_devices.py +++ b/core/tests/test_remote_devices.py @@ -371,7 +371,15 @@ def test_summaries_carry_the_connected_flag_and_never_a_hash(tmp_path: Path) -> rows = {s.id: s.model_dump() for s in reg.summaries(connected={a.id})} assert rows[a.id]["connected"] is True and rows[b.id]["connected"] is False for row in rows.values(): - assert set(row) == {"id", "name", "paired_at", "last_seen", "connected", "kind"} + assert set(row) == { + "id", + "name", + "paired_at", + "last_seen", + "connected", + "kind", + "mcp_servers_added", + } @pytest.mark.parametrize( diff --git a/core/tests/test_session_preset.py b/core/tests/test_session_preset.py index 2f115bbc..2cdf283a 100644 --- a/core/tests/test_session_preset.py +++ b/core/tests/test_session_preset.py @@ -12,8 +12,15 @@ import pytest from websockets.asyncio.client import connect +from tstd import model_cards from tstd.daemon import Daemon -from tstd.protocol import PROTOCOL_VERSION, AssistantDelta, TierState, UserTurn +from tstd.protocol import ( + PROTOCOL_VERSION, + AssistantDelta, + EndpointModels, + TierState, + UserTurn, +) from tstd.provider import ChatMessage from tstd.session_persist import SessionPersist from tstd.session_store import SessionStore @@ -214,11 +221,27 @@ async def test_catalog_slug_edit_does_not_rewrite_an_open_session( def _save(preset: str, tier: str, slug: str, path: Path | None = None) -> Path: return Path("/tmp/fake-config.yaml") + # TD-5233: the handler refreshes the new slug's model card before it + # replies. Unstubbed, that reads the host keychain's real + # ``tst-openrouter`` item (about 5 s where Secret Service is slow, + # past _ask's 2 s) and, with a key there, sends it to the live host. + asked: list[tuple[str, str | None]] = [] + + async def _test_key(name: str = "openrouter") -> str: + return "test-key" + + async def _offline(base_url: str, api_key: str | None, **_: Any) -> EndpointModels: + asked.append((base_url, api_key)) + return EndpointModels(base_url=base_url, error="offline in tests") + + monkeypatch.setattr("tstd.daemon.get_api_key", _test_key) + monkeypatch.setattr("tstd.daemon.save_tier_slug", _save) monkeypatch.setattr( "tstd.daemon.Daemon._reload_user_config", lambda self: None, ) + monkeypatch.setattr(model_cards, "list_endpoint_models", _offline) with tempfile.TemporaryDirectory() as tmp: daemon, task = await _start_daemon(Path(tmp)) try: @@ -241,6 +264,8 @@ def _save(preset: str, tier: str, slug: str, path: Path | None = None) -> Path: }, ) assert resp["type"] == "setup_state" + # One host asked, with the stub's key: nothing left the test. + assert [key for _host, key in asked] == ["test-key"] assert sess.config is opened_obj assert sess.config.tier("brain").slug == opened_slug await ws.close() diff --git a/core/tests/test_shell_sandbox.py b/core/tests/test_shell_sandbox.py index ea4905a4..b87235dd 100644 --- a/core/tests/test_shell_sandbox.py +++ b/core/tests/test_shell_sandbox.py @@ -100,6 +100,61 @@ def test_root_read_only_workspace_writable_no_network( assert f"--bind {ws.resolve()} {ws.resolve()}" in joined assert argv[-3:] == ["/bin/sh", "-c", "make"] + def test_a_hidden_dir_around_the_workspace_is_masked_before_the_bind( + self, tmp_path: Path + ) -> None: + """TD-5261: a worktree under the data dir, the default place for one. + + Runs everywhere, bubblewrap or not: it reads the order of the plan. + """ + fake_home = tmp_path / "home" + (fake_home / ".ssh").mkdir(parents=True) + data = tmp_path / "data" + ws = data / "worktrees" / "wt1" + ws.mkdir(parents=True) + (ws / "secrets").mkdir() + section = SandboxSection(enabled=True, hidden_paths=[str(ws / "secrets")]) + argv = build_plan(str(ws), "true", section, home=fake_home, extra_hidden=[data]).argv + + def at(*run: str) -> int: + return next(i for i in range(len(argv)) if tuple(argv[i : i + len(run)]) == run) + + around = at("--perms", "0111", "--tmpfs", str(data)) + bind = at("--bind", str(ws), str(ws)) + inside = at("--perms", "0000", "--tmpfs", str(ws / "secrets")) + elsewhere = at("--perms", "0000", "--tmpfs", str(fake_home / ".ssh")) + closed = at("--remount-ro", str(data)) + # Around the workspace: first, and passable. Inside it or apart: last, and closed. + assert around < bind < closed < inside + assert bind < elsewhere + # Once as the passable mask and once to close it to writes (TD-5278): + # never a second mask over the workspace. + assert argv.count(str(data)) == 2 + closed_masks = [ + argv[i + 3] + for i in range(len(argv) - 3) + if argv[i : i + 3] == ["--perms", "0000", "--tmpfs"] + ] + assert str(data) not in closed_masks + + def test_a_symlinked_hidden_dir_around_the_workspace_is_masked_once( + self, tmp_path: Path + ) -> None: + """TD-5278: the hidden list holds such a directory under both names.""" + fake_home = tmp_path / "home" + fake_home.mkdir() + real_data = (tmp_path / "disk" / "data").resolve() + (real_data / "worktrees" / "wt1").mkdir(parents=True) + link = tmp_path / "data-link" + link.symlink_to(real_data) + # The daemon knows its data dir by the link; the workspace is under it. + workspace = link / "worktrees" / "wt1" + argv = build_plan(str(workspace), "true", ON, home=fake_home, extra_hidden=[link]).argv + masks = [argv[i + 1] for i, arg in enumerate(argv) if arg == "--tmpfs"] + assert masks.count(str(real_data)) == 1 + assert str(link) not in masks + assert argv.index(str(real_data)) < argv.index("--bind") + def test_masks_come_after_the_workspace_bind(self, fake_home: Path) -> None: # The workspace is the home directory itself: the masks must still win. argv = build_plan(str(fake_home), "true", ON, home=fake_home).argv @@ -295,6 +350,133 @@ async def test_writes_fenced_to_workspace_and_temp(self, tmp_path: Path) -> None finally: shutil.rmtree(host_dir, ignore_errors=True) + async def test_a_worktree_inside_the_data_dir_is_still_the_workspace(self) -> None: + """TD-5261: a session's worktree lives under the data dir by default. + + The data dir is one of the hidden paths, and its mask used to land + on top of the workspace bind, so every command failed to enter its + own folder. + """ + from tstd.logging import user_data_dir + + data = user_data_dir() + ws = data / "worktrees" / "wt1" + ws.mkdir(parents=True) + (data / "sessions.json").write_text("DATA-DIR-SECRET", encoding="utf-8") + (data / "worktrees" / "other").mkdir() + (data / "worktrees" / "other" / "theirs.txt").write_text("theirs", encoding="utf-8") + try: + out = await run_shell( + _session(ws, ON), + "echo in > made.txt && cat made.txt; " + f"cat {data}/sessions.json 2>/dev/null || echo NO-DATA; " + f"ls {data} 2>/dev/null || echo NO-LIST; " + f"cat {data}/worktrees/other/theirs.txt 2>/dev/null || echo NO-SIBLING", + ) + assert shell_sandbox.SANDBOX_NOTE in out + assert (ws / "made.txt").read_text() == "in\n" + assert "in\n" in out + # The rest of the data dir stays out of reach: its files, its + # listing, and another session's worktree. + assert "NO-DATA" in out and "DATA-DIR-SECRET" not in out + assert "NO-LIST" in out + assert "NO-SIBLING" in out and "theirs" not in out + finally: + shutil.rmtree(data / "worktrees", ignore_errors=True) + (data / "sessions.json").unlink(missing_ok=True) + + async def test_a_worktree_under_a_symlinked_hidden_dir_still_works(self) -> None: + """TD-5278: the mask is laid once, under the directory's real name.""" + # Not under /tmp: the sandbox gives a command a /tmp of its own, and + # the link has to be there for the command to come in through it. + host_dir = Path(tempfile.mkdtemp(dir="/var/tmp")) + try: + real_data = host_dir / "disk" / "data" + ws = real_data / "worktrees" / "wt1" + ws.mkdir(parents=True) + (real_data / "secret.txt").write_text("DATA-DIR-SECRET", encoding="utf-8") + link = host_dir / "data-link" + link.symlink_to(real_data) + section = SandboxSection(enabled=True, hidden_paths=[str(link)]) + out = await run_shell( + _session(link / "worktrees" / "wt1", section), + "echo in > made.txt && cat made.txt; " + f"cat {link}/secret.txt 2>/dev/null || echo NO-DATA; " + f"touch {real_data}/planted 2>/dev/null || echo NO-PLANT; " + f"chmod 755 {real_data} 2>/dev/null || echo NO-CHMOD", + ) + assert (ws / "made.txt").read_text() == "in\n", out + assert "NO-DATA" in out and "DATA-DIR-SECRET" not in out + # The mask cannot be written into or opened up from inside. + assert "NO-PLANT" in out and "NO-CHMOD" in out + assert not (real_data / "planted").exists() + finally: + shutil.rmtree(host_dir, ignore_errors=True) + + @pytest.mark.skipif(shutil.which("git") is None, reason="needs git") + async def test_a_worktrees_git_metadata_cannot_be_written_from_inside( + self, tmp_path: Path + ) -> None: + """TD-5278: what git reads later, on the host, stays as it was. + + A writable admin dir is enough for a sandboxed command to plant a + ``core.fsmonitor`` or redirect ``commondir``; the person's next + ``git status`` outside the sandbox would then run it. + """ + from tstd.logging import user_data_dir + + def git(*args: str, cwd: Path) -> str: + done = subprocess.run( + ["git", "-c", "user.name=t", "-c", "user.email=t@example.com", *args], + cwd=cwd, + capture_output=True, + text=True, + check=True, + ) + return done.stdout.strip() + + repo = tmp_path / "project" + repo.mkdir() + git("init", "-q", "-b", "main", cwd=repo) + (repo / "a.txt").write_text("one\n", encoding="utf-8") + git("add", "a.txt", cwd=repo) + git("commit", "-q", "-m", "first", cwd=repo) + ws = user_data_dir() / "worktrees" / "wt-git" + ws.parent.mkdir(parents=True, exist_ok=True) + git("worktree", "add", "-q", "-b", "tst/wt/x", str(ws), cwd=repo) + admin = Path(git("rev-parse", "--absolute-git-dir", cwd=ws)) + common = repo / ".git" + assert admin.parent == common / "worktrees" + + def snapshot() -> dict[str, bytes]: + return { + str(path.relative_to(common)): path.read_bytes() + for path in sorted(common.rglob("*")) + if path.is_file() + } + + before = snapshot() + try: + out = await run_shell( + _session(ws, ON), + "git status --short >/dev/null 2>&1 && echo STATUS-OK || echo STATUS-FAILED; " + "echo two > a.txt; " + f"echo '[core]' >> {admin}/config.worktree 2>/dev/null || echo NO-WT-CONFIG; " + f"echo /tmp/elsewhere > {admin}/commondir 2>/dev/null || echo NO-COMMONDIR; " + f"echo x >> {common}/config 2>/dev/null || echo NO-CONFIG; " + f"echo x > {common}/hooks/pre-commit 2>/dev/null || echo NO-HOOK; " + "git add a.txt 2>/dev/null && echo ADDED || echo NO-ADD", + ) + # The work itself lands in the worktree. + assert (ws / "a.txt").read_text() == "two\n" + for refused in ("NO-WT-CONFIG", "NO-COMMONDIR", "NO-CONFIG", "NO-HOOK", "NO-ADD"): + assert refused in out, out + assert snapshot() == before, "git metadata changed from inside the sandbox" + # And the host's git still sees an ordinary, uncommitted change. + assert git("status", "--short", cwd=ws).strip() == "M a.txt" + finally: + shutil.rmtree(user_data_dir() / "worktrees", ignore_errors=True) + async def test_timeout_kills_the_sandboxed_tree(self, tmp_path: Path) -> None: ws = tmp_path / "ws" ws.mkdir() diff --git a/core/tests/test_show_artifact.py b/core/tests/test_show_artifact.py new file mode 100644 index 00000000..3e6890ab --- /dev/null +++ b/core/tests/test_show_artifact.py @@ -0,0 +1,379 @@ +"""TD-5254: the agent shows text in the Artifacts pane with ``show_artifact``. + +Two doors: a text file in the workspace (no copy; the pane shows the file +as it is), or text the model hands over, kept in the session's own data +dir under a name the daemon makes. These pin the contract, the wall, Class +A with no card, the caps, and that a second call replaces the first. +""" + +from __future__ import annotations + +import hashlib +import os +from dataclasses import dataclass, field +from pathlib import Path +from typing import Any + +import pytest + +from tests.platform_helpers import assert_owner_only_mode +from tests.test_classifier import WS, boundary, classify, fired_as, req +from tests.test_dispatch import make_classifier +from tstd.artifacts import ArtifactRecord, ArtifactStore +from tstd.autonomy import Boundary, DecisionClass +from tstd.autonomy.classifier import PLAN_READ_ONLY_BUILTINS +from tstd.config import SessionConfig +from tstd.policy import ApprovalOutcome, PolicyConfig +from tstd.protocol import ArtifactReady +from tstd.sent_files import SentFiles +from tstd.session import Session +from tstd.session_persist import SessionPersist +from tstd.shown_artifacts import ShownArtifacts +from tstd.tools import ToolDispatcher +from tstd.tools.boundary import PathGuard +from tstd.tools.delegate import WORKER_CHILD_TOOLS +from tstd.tools.handlers import register_builtin_handlers +from tstd.tools.registry import create_registry +from tstd.tools.show_artifact import SHOW_ARTIFACT_TOOL + +POSIX = os.name == "posix" + + +@dataclass +class Env: + ws: Path + data: Path + store: ArtifactStore + session: Session + dispatcher: ToolDispatcher + approvals: list[tuple[Any, ...]] = field(default_factory=list) + + def ready(self) -> list[ArtifactReady]: + return [e for e in self.session.event_log.all_events if isinstance(e, ArtifactReady)] + + def rows(self) -> list[ArtifactRecord]: + return self.store.list_records(self.session.id, self.ws) + + def stored(self) -> list[str]: + folder = self.store.bytes_dir(self.session.id) + return sorted(p.name for p in folder.iterdir()) if folder.exists() else [] + + async def show(self, call_id: str = "call_1", **arguments: Any) -> Any: + return await self.dispatcher.dispatch(call_id, "show_artifact", arguments, self.session) + + +def _env(tmp_path: Path, config: SessionConfig | None = None) -> Env: + ws = tmp_path / "ws" + ws.mkdir() + persist = SessionPersist(tmp_path / "data") + store = ArtifactStore(persist) + settings = config or SessionConfig() + service = ShownArtifacts(store, lambda: settings) + session = Session(str(ws)) + persist.prepare(session.id) + session.persist_dir = persist.dir_for(session.id) + dispatcher = ToolDispatcher( + create_registry(), + classifier=make_classifier(str(ws)), + path_guard=PathGuard(Boundary(workspace_root=ws)), + workspace=ws, + ) + dispatcher.policy = PolicyConfig() + service.bind_guard(lambda: dispatcher.path_guard) + env = Env(ws, tmp_path / "data", store, session, dispatcher) + + async def _approve(*args: Any) -> ApprovalOutcome: + env.approvals.append(args) + return ApprovalOutcome(True) + + dispatcher.approval_handler = _approve + register_builtin_handlers( + dispatcher, sent_files=SentFiles(store, settings), shown_artifacts=service + ) + return env + + +def _tree(root: Path) -> list[str]: + return sorted(p.relative_to(root).as_posix() for p in root.rglob("*") if p.is_file()) + + +# ── Registration ─────────────────────────────────────────────────────── + + +def test_the_tool_is_registered_as_a_read_that_runs_in_order() -> None: + tool = create_registry().require("show_artifact") + assert tool is SHOW_ARTIFACT_TOOL + assert tool.path_fields == ("path",) + assert tool.side_effect_class == "auto" + assert tool.parallel_safe is False + assert tool.mutates is False and tool.provenance is None + assert tool.parameters["required"] == ["title"] + assert tool.parameters["additionalProperties"] is False + assert set(tool.parameters["properties"]) == {"title", "path", "content", "name", "caption"} + + +def test_only_the_sessions_own_agent_has_it() -> None: + assert "show_artifact" not in WORKER_CHILD_TOOLS + assert "show_artifact" in PLAN_READ_ONLY_BUILTINS + + +# ── The two doors ────────────────────────────────────────────────────── + + +async def test_a_workspace_file_is_shown_without_a_copy(tmp_path: Path) -> None: + env = _env(tmp_path) + (env.ws / "docs").mkdir() + (env.ws / "docs" / "report.md").write_text("# Report\n", encoding="utf-8") + result = await env.show(title="The report", path="docs/report.md", caption="First draft") + assert result.status == "success", result.output + assert result.decision_class is DecisionClass.A + assert env.approvals == [] + assert result.output == 'Showing "The report" in Artifacts (report.md).' + [row] = env.rows() + assert (row.kind, row.location, row.path) == ("artifact", "workspace", "docs/report.md") + assert (row.title, row.name, row.mime) == ("The report", "report.md", "text/markdown") + assert (row.size, row.sha256) == (None, None) + assert (row.caption, row.tool_call_id) == ("First draft", "call_1") + # A pointer, not a copy: nothing was written but the index. + assert env.stored() == [] + [event] = env.ready() + assert (event.artifact_id, event.kind, event.name) == (row.id, "artifact", "report.md") + + +async def test_given_text_is_kept_in_the_session_under_a_name_the_daemon_makes( + tmp_path: Path, +) -> None: + env = _env(tmp_path) + text = "

Chart

\n" + result = await env.show(title="A chart", content=text, name="chart.html") + assert result.status == "success", result.output + assert result.decision_class is DecisionClass.A and env.approvals == [] + [row] = env.rows() + data = text.encode() + assert (row.kind, row.location, row.path) == ("artifact", "session", f"artifacts/{row.id}") + assert (row.name, row.mime, row.size) == ("chart.html", "text/html", len(data)) + assert row.sha256 == hashlib.sha256(data).hexdigest() + copy = env.store.bytes_dir(env.session.id) / row.id + assert copy.read_bytes() == data + assert_owner_only_mode(copy.stat().st_mode & 0o777) + assert env.stored() == [row.id] + assert result.output == f'Showing "A chart" in Artifacts (chart.html, {len(data)} bytes).' + # The workspace is untouched, and the reply names no data-dir path. + assert _tree(env.ws) == [] + assert str(env.data) not in result.output + assert str(env.data) not in env.ready()[0].model_dump_json() + + +@pytest.mark.parametrize( + ("name", "mime"), + [("notes.md", "text/markdown"), ("tool.py", "text/plain"), ("data.json", "application/json")], +) +async def test_the_name_picks_the_type_and_unknown_text_is_plain( + tmp_path: Path, name: str, mime: str +) -> None: + env = _env(tmp_path) + assert (await env.show(title="x", content="hello", name=name)).status == "success" + assert env.rows()[0].mime == mime + + +# ── What it refuses ──────────────────────────────────────────────────── + + +@pytest.mark.parametrize( + "arguments", + [ + {"title": "x"}, + {"title": "x", "path": "a.md", "content": "hello", "name": "a.md"}, + {"title": "x", "content": "hello"}, + {"title": "x", "content": "hello", "name": " "}, + ], +) +async def test_one_door_at_a_time_and_content_needs_a_name( + tmp_path: Path, arguments: dict[str, Any] +) -> None: + env = _env(tmp_path) + (env.ws / "a.md").write_text("a", encoding="utf-8") + result = await env.show(**arguments) + assert result.status != "success" + assert result.error_code == "bad_request", result.output + assert env.rows() == [] and env.ready() == [] and env.stored() == [] + + +async def test_only_text_and_only_a_regular_file(tmp_path: Path) -> None: + env = _env(tmp_path) + (env.ws / "pic.png").write_bytes(b"\x89PNG\r\n\x1a\n\xff\xfe\x00") + (env.ws / "folder").mkdir() + binary = await env.show(title="x", path="pic.png") + assert binary.error_code == "not_text" and "send_file" in binary.output + assert (await env.show(title="x", path="folder")).error_code == "not_a_file" + assert (await env.show(title="x", path="gone.md")).error_code == "artifact_not_found" + if POSIX: + os.mkfifo(env.ws / "pipe") + assert (await env.show(title="x", path="pipe")).error_code == "not_a_file" + assert env.rows() == [] and env.ready() == [] + + +async def test_a_path_outside_the_workspace_is_class_c_and_shows_nothing(tmp_path: Path) -> None: + env = _env(tmp_path) + outside = tmp_path / "secret.txt" + outside.write_text("no", encoding="utf-8") + + async def deny(*args: Any) -> ApprovalOutcome: + env.approvals.append(args) + return ApprovalOutcome(False, "no") + + env.dispatcher.approval_handler = deny + result = await env.show(title="x", path=str(outside)) + assert result.status != "success" + assert result.decision_class is not DecisionClass.A + assert env.rows() == [] and env.ready() == [] + if POSIX: + (env.ws / "link.txt").symlink_to(outside) + linked = await env.show(title="x", path="link.txt") + assert linked.status != "success" and env.rows() == [] + + +async def test_a_name_is_a_label_never_a_place(tmp_path: Path) -> None: + env = _env(tmp_path) + for hostile in ( + "../../AGENTS.md", + "/etc/passwd", + "CLAUDE.md", + "..\\..\\x.md", + ".tst/rules/a.md", + ): + result = await env.show(title="x", content="text", name=hostile) + assert result.status == "success", (hostile, result.output) + # Whatever the name, the only files written are artifacts/ in the session dir. + ids = {row.id for row in env.rows()} + assert set(env.stored()) == ids + assert _tree(env.ws) == [] + assert not (tmp_path / "AGENTS.md").exists() + for row in env.rows(): + assert row.name is not None and "/" not in row.name and "\\" not in row.name + assert row.path == f"artifacts/{row.id}" + + +async def test_too_much_text_is_refused_either_way(tmp_path: Path) -> None: + env = _env(tmp_path, SessionConfig(artifact_max_bytes=100)) + (env.ws / "big.md").write_text("x" * 101, encoding="utf-8") + assert (await env.show(title="x", path="big.md")).error_code == "artifact_too_large" + over = await env.show(title="x", content="é" * 51, name="a.md") # 102 bytes + assert over.error_code == "artifact_too_large" + assert (await env.show(title="x", content="é" * 50, name="a.md")).status == "success" + assert [row.size for row in env.rows()] == [100] + + +async def test_the_count_and_the_session_quota_are_caps(tmp_path: Path) -> None: + env = _env(tmp_path, SessionConfig(artifact_max_count=2, send_file_session_max_bytes=25)) + assert (await env.show(title="a", content="0123456789", name="a.md")).status == "success" + assert (await env.show(title="b", content="0123456789", name="b.md")).status == "success" + third = await env.show(title="c", content="x", name="c.md") + assert third.error_code == "artifact_limit" + # Replacing one is not a third, and is counted once against the quota. + assert (await env.show(title="a2", content="0123456789abcde", name="a.md")).status == "success" + grown = await env.show(title="a3", content="0123456789abcdef", name="a.md") + assert grown.error_code == "session_quota" + assert sorted(row.size or 0 for row in env.rows()) == [10, 15] + + +# ── Showing again ────────────────────────────────────────────────────── + + +async def test_showing_the_same_thing_again_replaces_it_and_keeps_its_id(tmp_path: Path) -> None: + env = _env(tmp_path) + (env.ws / "plan.md").write_text("one", encoding="utf-8") + await env.show("c1", title="Plan", path="plan.md") + await env.show("c2", title="Plan, revised", path="./plan.md") + await env.show("c3", title="Notes", content="first", name="notes.md") + await env.show("c4", title="Notes v2", content="second, longer", name="notes.md") + await env.show("c5", title="Other", content="other", name="other.md") + rows = env.rows() + assert [row.title for row in rows] == ["Plan, revised", "Notes v2", "Other"] + events = env.ready() + assert events[0].artifact_id == events[1].artifact_id == rows[0].id + assert events[2].artifact_id == events[3].artifact_id == rows[1].id + assert rows[1].tool_call_id == "c4" + # One file per content artifact; the replaced text is gone. + assert set(env.stored()) == {rows[1].id, rows[2].id} + kept = env.store.bytes_dir(env.session.id) / rows[1].id + assert kept.read_bytes() == b"second, longer" + + +async def test_a_sent_file_is_never_replaced_by_a_shown_one(tmp_path: Path) -> None: + env = _env(tmp_path) + (env.ws / "a.md").write_text("sent", encoding="utf-8") + sent = await env.dispatcher.dispatch("s1", "send_file", {"files": ["a.md"]}, env.session) + assert sent.status == "success", sent.output + await env.show(title="shown", content="shown", name="a.md") + assert sorted(row.kind for row in env.rows()) == ["artifact", "sent_file"] + + +# ── The classifier ───────────────────────────────────────────────────── + + +def test_the_rule_is_for_the_builtin_alone() -> None: + inside = (WS / "a.md",) + for fine in (req(tool_name="show_artifact"), req(tool_name="show_artifact", reads=inside)): + decision = classify(boundary(), fine) + assert decision.decision_class is DecisionClass.A + assert fired_as(decision, "show-artifact") + for no_grant in ( + req(tool_name="show_artifact", provenance="mcp:srv"), + req(tool_name="show_artifact", provenance="plugin:dist"), + req(tool_name="show_artifact", hosts=frozenset({"example.com"})), + req(tool_name="show_artifact", writes=inside), + req(tool_name="show_artifact", is_mutation=True), + req(tool_name="show_artifact", reads=(WS / "a.md", WS / "b.md")), + ): + assert not fired_as(classify(boundary(), no_grant), "show-artifact"), no_grant + outside = classify(boundary(), req(tool_name="show_artifact", reads=(Path("/etc/passwd"),))) + assert outside.decision_class is DecisionClass.C + + +# ── A real daemon session ────────────────────────────────────────────── + + +async def test_a_daemon_session_shows_with_no_card_and_lists_it(tmp_path: Path) -> None: + """The daemon wires the service into every session it builds.""" + import json + + from tests.machine_helpers import eventually + from tests.test_artifacts import _open, _shutdown + from tests.test_two_daemons import _Steps + from tstd.daemon import Daemon + from tstd.mock import Script + from tstd.protocol import ApprovalRequest, ToolResult + + call = json.dumps({"title": "Summary", "content": "# Done\n", "name": "summary.md"}) + steps = [ + Script(kind="tool_call", tool_name="show_artifact", tool_arguments=call), + Script(kind="stream", content="Shown."), + ] + daemon = Daemon(data_dir=tmp_path / "data", provider=_Steps(steps)) + ws = tmp_path / "ws" + ws.mkdir() + sid = await _open(daemon, ws) + try: + session = daemon.session_registry.get(sid) + assert session is not None + message = {"type": "user_message", "session_id": sid, "content": "summarise"} + await daemon._handle_message(json.dumps(message), None) + log = session.event_log + await eventually(lambda: any(isinstance(e, ToolResult) for e in log.all_events), within=20) + [result] = [e for e in log.all_events if isinstance(e, ToolResult)] + assert result.status == "success", result.output + assert not [e for e in log.all_events if isinstance(e, ApprovalRequest)] + [ready] = [e for e in log.all_events if isinstance(e, ArtifactReady)] + assert (ready.kind, ready.name, ready.mime) == ("artifact", "summary.md", "text/markdown") + listed = json.loads( + await daemon._handle_message( + json.dumps({"type": "list_artifacts", "session_id": sid}), None + ) + or "{}" + ) + assert [row["artifact_id"] for row in listed["artifacts"]] == [ready.artifact_id] + assert listed["artifacts"][0]["name"] == "summary.md" + assert list(ws.iterdir()) == [] or [p.name for p in ws.iterdir()] == [".tst"] + finally: + await _shutdown(daemon, sid) diff --git a/core/tests/test_switch_preset.py b/core/tests/test_switch_preset.py new file mode 100644 index 00000000..2530d3a0 --- /dev/null +++ b/core/tests/test_switch_preset.py @@ -0,0 +1,446 @@ +"""One switch: this chat, the model new chats use, or both (TD-5235). + +The picker's click. Everything is checked before anything changes; a +switch asked for while a turn runs waits for that turn and no longer; a +chat with no loop just has the name recorded; and ``new_session`` can open +a chat on a named entry, which is how the picker reaches an entry on +another engine. +""" + +from __future__ import annotations + +import asyncio +import contextlib +import json +from pathlib import Path +from typing import Any + +import pytest + +from tests.test_add_model import _ask, _start, _stop +from tests.test_preset_engine import _grok_stub +from tests.test_preset_engine import _text as _catalog_with_grok +from tstd.config import load_config +from tstd.daemon import Daemon +from tstd.mock import MockProvider, Script +from tstd.peer_policy import MACHINE_PEER_VERBS +from tstd.protocol import TierState +from tstd.session_store import SessionStore + +EFFORT = """\ + overrides: + session: + default_effort: high +""" + + +def _text() -> str: + """The test catalog plus a Grok entry; ``budget`` names an effort.""" + text = _catalog_with_grok() + header = "\n budget:\n" + assert text.count(header) == 1 + return text.replace(header, header + EFFORT, 1) + + +async def _seeded( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: MockProvider | None = None +) -> tuple[Daemon, Any, Any, Path]: + monkeypatch.setattr("tstd.daemon.grok_loop", _grok_stub) + data = tmp_path / "data" + data.mkdir() + (data / "config.yaml").write_text(_text(), encoding="utf-8") + workspace = tmp_path / "ws" + workspace.mkdir() + mock = provider or MockProvider(default=Script(kind="stream", content="done")) + daemon, task, ws = await _start(data, mock) + return daemon, task, ws, workspace + + +async def _open(ws: Any, workspace: Path) -> str: + opened = await _ask(ws, {"type": "open_workspace", "path": str(workspace)}, "session_state") + return str(opened["session_id"]) + + +def _tier_states(sess: Any) -> list[TierState]: + return [e for e in sess.event_log.all_events if isinstance(e, TierState)] + + +async def _read_all(ws: Any) -> None: + with contextlib.suppress(Exception): + async for _message in ws: + pass + + +async def _until(condition: Any, what: str) -> None: + for _ in range(400): + if condition(): + return + await asyncio.sleep(0.01) + raise AssertionError(f"never happened: {what}") + + +def _brain(daemon: Daemon, name: str) -> str: + brain = daemon.config.presets[name].brain + assert brain is not None and brain.slug is not None + return brain.slug + + +class TestTheThreeSwitches: + async def test_this_chat_only(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + sid = await _open(ws, workspace) + sess = daemon.session_registry.get(sid) + assert sess is not None and sess.router is not None + reply = await _ask( + ws, {"type": "switch_preset", "name": "budget", "session_id": sid}, "session_list" + ) + assert reply["type"] == "session_list" + assert sess.preset == "budget" + assert daemon.config.active_preset == "tst-default" + assert load_config(daemon.config_path).active_preset == "tst-default" + # TD-5234: the entry names an effort, and the click sets it. + assert sess.router.effort == "high" + assert _tier_states(sess)[-1].preset == "budget" + assert _tier_states(sess)[-1].pending_preset is None + finally: + await _stop(task, ws) + + async def test_the_model_new_chats_use_only( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + sid = await _open(ws, workspace) + sess = daemon.session_registry.get(sid) + assert sess is not None + snapshot = sess.config + reply = await _ask( + ws, + {"type": "switch_preset", "name": "budget", "make_default": True}, + "setup_state", + ) + assert reply["type"] == "setup_state" and reply["active_preset"] == "budget" + assert load_config(daemon.config_path).active_preset == "budget" + # The open chat is not rewritten. + assert sess.preset == "tst-default" and sess.config is snapshot + second = daemon.session_registry.get(await _open(ws, workspace)) + assert second is not None and second.preset == "budget" + finally: + await _stop(task, ws) + + async def test_both_in_one_message( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + sid = await _open(ws, workspace) + other = await _open(ws, workspace) + reply = await _ask( + ws, + { + "type": "switch_preset", + "name": "budget", + "session_id": sid, + "make_default": True, + }, + "setup_state", + ) + assert reply["active_preset"] == "budget" + switched = daemon.session_registry.get(sid) + untouched = daemon.session_registry.get(other) + assert switched is not None and switched.preset == "budget" + assert untouched is not None and untouched.preset == "tst-default" + record = daemon._session_store.get(sid) + assert record is not None and record.preset == "budget" + finally: + await _stop(task, ws) + + async def test_the_same_model_again_changes_nothing( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + sid = await _open(ws, workspace) + sess = daemon.session_registry.get(sid) + assert sess is not None + snapshot, text = sess.config, daemon.config_path.read_text(encoding="utf-8") + states = len(_tier_states(sess)) + await _ask( + ws, + { + "type": "switch_preset", + "name": "tst-default", + "session_id": sid, + "make_default": True, + }, + "setup_state", + ) + assert sess.config is snapshot and len(_tier_states(sess)) == states + assert daemon.config_path.read_text(encoding="utf-8") == text + finally: + await _stop(task, ws) + + +class TestNothingHalfApplies: + @pytest.mark.parametrize( + ("patch", "code"), + [ + ({"name": "nope", "make_default": True}, "unknown_preset"), + ({"name": "budget"}, "bad_request"), + ( + {"name": "budget", "session_id": "no-such-session", "make_default": True}, + "session_not_found", + ), + ({"name": "grok", "session_id": "", "make_default": True}, "engine_mismatch"), + ], + ) + async def test_a_refusal_changes_neither_the_chat_nor_the_default( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, patch: dict[str, Any], code: str + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + sid = await _open(ws, workspace) + sess = daemon.session_registry.get(sid) + assert sess is not None + snapshot, text = sess.config, daemon.config_path.read_text(encoding="utf-8") + msg = {"type": "switch_preset", **patch} + if msg.get("session_id") == "": + msg["session_id"] = sid + reply = await _ask(ws, msg, "setup_state") + assert reply["type"] == "error" and reply["code"] == code + assert sess.preset == "tst-default" and sess.config is snapshot + assert sess.pending_preset is None + assert daemon.config.active_preset == "tst-default" + assert daemon.config_path.read_text(encoding="utf-8") == text + finally: + await _stop(task, ws) + + +class TestWhileATurnRuns: + """The switch waits for the turn, and the turn keeps the model it started on.""" + + async def _turn_running( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> tuple[Daemon, Any, Any, str, Any, MockProvider]: + slow = MockProvider(default=Script(kind="stream", content="a b c d", chunk_delay=0.02)) + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch, slow) + sid = await _open(ws, workspace) + sess = daemon.session_registry.get(sid) + assert sess is not None + await ws.send(json.dumps({"type": "attach", "session_id": sid, "from_seq": 1})) + await ws.send(json.dumps({"type": "user_message", "session_id": sid, "content": "one"})) + await _until(lambda: sess.turn_in_flight and len(slow.calls) > 0, "the turn started") + # These tests watch the daemon, not the socket. Unread, the client's + # queue fills, it stops reading, and closing waits out its timeout. + self._reader = asyncio.create_task(_read_all(ws)) + return daemon, task, ws, sid, sess, slow + + async def _quiet(self) -> None: + """Stop reading, so a test can ask and read a reply itself.""" + self._reader.cancel() + with contextlib.suppress(asyncio.CancelledError): + await self._reader + + async def test_it_is_held_then_applied_when_the_turn_completes( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, sid, sess, slow = await self._turn_running(tmp_path, monkeypatch) + try: + old, new = _brain(daemon, "tst-default"), _brain(daemon, "budget") + assert old != new + snapshot = sess.config + await ws.send( + json.dumps({"type": "switch_preset", "name": "budget", "session_id": sid}) + ) + await _until(lambda: sess.pending_preset == "budget", "the switch is pending") + # Held: the chat is still on its own model, and says what is waiting. + assert sess.preset == "tst-default" and sess.config is snapshot + held = _tier_states(sess)[-1] + assert held.preset == "tst-default" and held.pending_preset == "budget" + + await _until(lambda: not sess.turn_in_flight, "the turn completed") + # The bind comes first and its tier_state a moment later: wait for the event. + await _until( + lambda: _tier_states(sess)[-1].preset == "budget", "the pending switch applied" + ) + assert sess.preset == "budget" and sess.pending_preset is None + applied = _tier_states(sess)[-1] + assert applied.preset == "budget" and applied.pending_preset is None + assert sess.router.effort == "high" + record = daemon._session_store.get(sid) + assert record is not None and record.preset == "budget" + # The turn that was running asked only its own model. + assert {call.model for call in slow.calls} == {old} + + before = len(slow.calls) + await ws.send(json.dumps({"type": "user_message", "session_id": sid, "content": "two"})) + await _until(lambda: len(slow.calls) > before, "the next turn asked a model") + assert slow.calls[before].model == new + await _until(lambda: not sess.turn_in_flight, "the second turn completed") + finally: + await _stop(task, ws) + + async def test_a_message_queued_behind_the_turn_runs_on_the_new_model( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, sid, sess, slow = await self._turn_running(tmp_path, monkeypatch) + try: + old, new = _brain(daemon, "tst-default"), _brain(daemon, "budget") + await ws.send(json.dumps({"type": "user_message", "session_id": sid, "content": "two"})) + await ws.send( + json.dumps({"type": "switch_preset", "name": "budget", "session_id": sid}) + ) + await _until( + lambda: sess.preset == "budget" and not sess.turn_in_flight, "both turns ran" + ) + models = [call.model for call in slow.calls] + assert models[0] == old + assert new in models + assert models.index(new) == len([m for m in models if m == old]) + finally: + await _stop(task, ws) + + async def test_a_later_switch_replaces_it_and_one_back_cancels_it( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + _daemon, task, ws, sid, sess, _slow = await self._turn_running(tmp_path, monkeypatch) + try: + for name in ("budget", "local"): + await ws.send( + json.dumps({"type": "switch_preset", "name": name, "session_id": sid}) + ) + await _until(lambda: sess.pending_preset == "local", "the later switch replaced it") + await ws.send( + json.dumps({"type": "switch_preset", "name": "tst-default", "session_id": sid}) + ) + await _until(lambda: sess.pending_preset is None, "switching back cancelled it") + assert _tier_states(sess)[-1].pending_preset is None + await _until(lambda: not sess.turn_in_flight, "the turn completed") + assert sess.preset == "tst-default" + finally: + await _stop(task, ws) + + async def test_an_entry_removed_meanwhile_is_dropped( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + _daemon, task, ws, sid, sess, _slow = await self._turn_running(tmp_path, monkeypatch) + try: + await ws.send( + json.dumps({"type": "switch_preset", "name": "budget", "session_id": sid}) + ) + await _until(lambda: sess.pending_preset == "budget", "the switch is pending") + await ws.send(json.dumps({"type": "delete_preset", "name": "budget"})) + await _until(lambda: not sess.turn_in_flight, "the turn completed") + await _until(lambda: sess.pending_preset is None, "the pending switch was dropped") + assert sess.preset == "tst-default" + assert _tier_states(sess)[-1].pending_preset is None + finally: + await _stop(task, ws) + + async def test_the_old_verb_still_refuses( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + _daemon, task, ws, sid, sess, _slow = await self._turn_running(tmp_path, monkeypatch) + await self._quiet() + try: + reply = await _ask( + ws, + {"type": "set_session_preset", "session_id": sid, "name": "budget"}, + "session_list", + ) + assert reply["type"] == "error" and reply["code"] == "session_busy" + assert sess.pending_preset is None and sess.preset == "tst-default" + finally: + await _stop(task, ws) + + +async def test_a_chat_with_no_loop_has_the_name_recorded(tmp_path: Path) -> None: + data = tmp_path / "data" + workspace = tmp_path / "ws" + data.mkdir() + workspace.mkdir() + (data / "config.yaml").write_text(_text(), encoding="utf-8") + # No conversation snapshot: restored as a row that never gets a loop. + await SessionStore(data).upsert("sid-1", str(workspace), "complete", preset="tst-default") + daemon, task, ws = await _start(data, MockProvider(default=Script(kind="text"))) + try: + sess = daemon.session_registry.get("sid-1") + assert sess is not None and sess.router is None + reply = await _ask( + ws, {"type": "switch_preset", "name": "budget", "session_id": "sid-1"}, "session_list" + ) + assert reply["type"] == "session_list" + row = next(r for r in reply["sessions"] if r["session_id"] == "sid-1") + assert row["preset"] == "budget" and sess.preset == "budget" + record = daemon._session_store.get("sid-1") + assert record is not None and record.preset == "budget" + # The old verb still says why it cannot. + old = await _ask( + ws, + {"type": "set_session_preset", "session_id": "sid-1", "name": "local"}, + "session_list", + ) + assert old["type"] == "error" and old["code"] == "session_not_live" + finally: + await _stop(task, ws) + + +class TestANewChatOnAnEntry: + async def test_it_opens_on_the_entry_with_the_entrys_engine( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + sid = await _open(ws, workspace) + grok = await _ask( + ws, {"type": "new_session", "session_id": sid, "preset": "grok"}, "session_state" + ) + assert grok["type"] == "session_state" and grok["engine"] == "grok" + made = daemon.session_registry.get(grok["session_id"]) + assert made is not None and made.preset == "grok" and made.engine == "grok" + native = await _ask( + ws, + {"type": "new_session", "session_id": sid, "preset": "budget"}, + "session_state", + ) + other = daemon.session_registry.get(native["session_id"]) + assert other is not None and other.preset == "budget" and other.engine == "native" + # Neither changed the model new chats use, nor the chat it came from. + assert daemon.config.active_preset == "tst-default" + first = daemon.session_registry.get(sid) + assert first is not None and first.preset == "tst-default" + finally: + await _stop(task, ws) + + async def test_without_an_entry_it_is_the_default_as_before( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + sid = await _open(ws, workspace) + plain = await _ask(ws, {"type": "new_session", "session_id": sid}, "session_state") + made = daemon.session_registry.get(plain["session_id"]) + assert made is not None and made.preset == "tst-default" + finally: + await _stop(task, ws) + + async def test_an_unknown_entry_starts_nothing( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + daemon, task, ws, workspace = await _seeded(tmp_path, monkeypatch) + try: + sid = await _open(ws, workspace) + before = len(await daemon.session_registry.list_sessions()) + reply = await _ask( + ws, {"type": "new_session", "session_id": sid, "preset": "nope"}, "session_state" + ) + assert reply["type"] == "error" and reply["code"] == "unknown_preset" + assert len(await daemon.session_registry.list_sessions()) == before + finally: + await _stop(task, ws) + + +def test_another_computers_hub_may_not_send_it() -> None: + assert "switch_preset" not in MACHINE_PEER_VERBS + assert "set_session_preset" in MACHINE_PEER_VERBS diff --git a/core/tstd/artifacts.py b/core/tstd/artifacts.py index d255fe4e..a4c69922 100644 --- a/core/tstd/artifacts.py +++ b/core/tstd/artifacts.py @@ -17,6 +17,7 @@ from __future__ import annotations +import hashlib import json import os import re @@ -133,12 +134,25 @@ def record( *, path: str | None = None, content: bytes | None = None, + name: str | None = None, + caption: str | None = None, + tool_call_id: str | None = None, + upsert: bool = False, + max_count: int | None = None, + max_session_bytes: int | None = None, ) -> ArtifactRecord: """Store an artifact under the workspace wall or the session persist dir. ``content`` writes bytes to ``artifacts/`` in the session dir. ``path`` registers a workspace path (relative) or an absolute path already inside the session persist dir. Pass exactly one. + + ``upsert`` (TD-5254) replaces an earlier ``artifact`` row for the + same thing and keeps its id, so a revised artifact is one row in + the pane: the same workspace path, or the same ``name`` for + content. ``max_count`` caps this session's ``artifact`` rows and + ``max_session_bytes`` everything the session stores, sent files + included; a replacement is counted once. """ session_id = _safe_session_id(session_id) if (path is None) == (content is None): @@ -150,31 +164,72 @@ def record( self._persist.prepare(session_id) persist_dir = self._persist.dir_for(session_id) - artifact_id = uuid.uuid4().hex - - if content is not None: - dest = persist_dir / _BYTES_DIR / artifact_id - self._write_bytes(dest, persist_dir, content) - stored_path = f"{_BYTES_DIR}/{artifact_id}" - location: Literal["workspace", "session"] = "session" - else: - assert path is not None + location: Literal["workspace", "session"] = "session" + stored_path = "" + if path is not None: location, stored_path = self._classify_path(path, workspace, persist_dir) - record = ArtifactRecord( - id=artifact_id, - session_id=session_id, - mime=mime.strip(), - path=stored_path, - title=title.strip(), - location=location, - ) with self._lock: records = self._load(session_id) - records.append(record) + prior = self._prior(records, location, stored_path, name) if upsert else None + shown = [r for r in records if r.kind == "artifact"] + if prior is None and max_count is not None and len(shown) >= max_count: + raise ArtifactError( + "artifact_limit", + f"This session already shows {max_count} artifacts, the most it keeps.", + ) + artifact_id = prior.id if prior is not None else uuid.uuid4().hex + size: int | None = None + digest: str | None = None + if content is not None: + held = sum(r.size or 0 for r in records if r is not prior) + if max_session_bytes is not None and held + len(content) > max_session_bytes: + raise ArtifactError( + "session_quota", + "This session has no room left for stored files and artifacts.", + ) + self._write_bytes(persist_dir / _BYTES_DIR / artifact_id, persist_dir, content) + stored_path = f"{_BYTES_DIR}/{artifact_id}" + size = len(content) + digest = hashlib.sha256(content).hexdigest() + record = ArtifactRecord( + id=artifact_id, + session_id=session_id, + mime=mime.strip(), + path=stored_path, + title=title.strip(), + location=location, + name=name, + size=size, + sha256=digest, + caption=caption, + tool_call_id=tool_call_id, + ) + if prior is None: + records.append(record) + else: + records[records.index(prior)] = record self._save(session_id, records) return record + @staticmethod + def _prior( + records: list[ArtifactRecord], + location: Literal["workspace", "session"], + stored_path: str, + name: str | None, + ) -> ArtifactRecord | None: + """The ``artifact`` row a new one replaces, if any (TD-5254).""" + for row in records: + if row.kind != "artifact" or row.location != location: + continue + # A session row a path points at keeps the path as its key. + if stored_path and row.path == stored_path: + return row + if not stored_path and name is not None and row.name == name: + return row + return None + def bytes_dir(self, session_id: str) -> Path: """``sessions//artifacts``, where ``sent_file`` copies live (TD-5143).""" return self._persist.dir_for(_safe_session_id(session_id)) / _BYTES_DIR diff --git a/core/tstd/autonomy/classifier.py b/core/tstd/autonomy/classifier.py index f0d46457..14fb41ce 100644 --- a/core/tstd/autonomy/classifier.py +++ b/core/tstd/autonomy/classifier.py @@ -754,6 +754,9 @@ class default only when no rule matches. "ask_user", "consult", "send_file", + # TD-5254: shows text in the Artifacts pane; it changes nothing but + # the session's own state, and a draft is worth seeing while planning. + "show_artifact", "exit_plan_mode", # TD-5127: loads deferred MCP definitions; each loaded tool is # still judged on its own, so an unlisted one stays refused. @@ -856,6 +859,28 @@ def _rule_desktop_actuation(req: DecisionRequest, _boundary: Boundary) -> bool: return req.actuates is True +def _rule_show_artifact(req: DecisionRequest, boundary: Boundary) -> bool: + """``show_artifact`` puts text in the session's Artifacts pane (TD-5254). + + It writes only daemon state under the session's own data dir: an index + row, and for given text one capped file whose name the daemon makes. + No workspace file, no host. A call with text and no path names no path + at all, so without this rule it would fall to the worker classifier and + ask every time. Only the builtin matches: an MCP or plugin tool of that + name carries provenance and gets no grant. An outside or unsafe path + never gets here; the two boundary rules come first. + """ + return ( + req.tool_name == "show_artifact" + and req.provenance is None + and not req.is_mutation + and not req.writes + and not req.hosts + and len(req.reads) <= 1 + and all(is_in_workspace(boundary, p) for p in req.reads) + ) + + def _rule_send_file(req: DecisionRequest, boundary: Boundary) -> bool: """``send_file`` copies workspace files into the session's own data dir (TD-5143). @@ -1245,6 +1270,12 @@ def _rule_outside_writable(req: DecisionRequest, boundary: Boundary) -> bool: decision_class=DecisionClass.A, match=_rule_send_file, ), + Rule( + id="show-artifact", + description="show_artifact of an in-workspace text file, or of text it is given", + decision_class=DecisionClass.A, + match=_rule_show_artifact, + ), Rule( id="steering-proposal", description="propose_steering_edit parks a diff for the user; writes nothing", diff --git a/core/tstd/config.py b/core/tstd/config.py index b6f17548..b9f4c20c 100644 --- a/core/tstd/config.py +++ b/core/tstd/config.py @@ -35,11 +35,13 @@ ) from .effort import DEFAULT_EFFORT, LEGACY_ULTRACODE, EffortLevel +from .endpoint_gate import ModelConcurrencyConfig from .hooks_config import HooksConfig from .logging import get_logger, user_data_dir from .lsp_config import LspConfig from .mcp_values import coerce_map from .net import is_loopback_url as is_loopback_url +from .preset_overrides import PresetOverrides, merged_sections from .provider import TimeoutConfig from .shell_read_only import READ_ONLY_ENTRIES @@ -55,6 +57,13 @@ # declared preset when the configured name is missing. DEFAULT_PRESET = "" +# The picker's built-in sections (TD-5236), in the order it lists them. An +# entry's own ``group`` may be one of these or a name of the person's own. +GROUP_LOCAL = "Local" +GROUP_API = "API" +GROUP_GROK = "Grok" +PICKER_GROUPS: tuple[str, ...] = (GROUP_LOCAL, GROUP_API, GROUP_GROK) + # Implicit keychain account for an unbound remote tier (TD-1717). DEFAULT_CREDENTIAL_ID = "openrouter" RESERVED_CREDENTIAL_IDS = frozenset( @@ -256,6 +265,11 @@ class TierConfig(BaseModel): input_price: float = Field(ge=0) output_price: float = Field(ge=0) cache_read_price: float = Field(ge=0) + # What the provider charges to store prompt tokens in its cache + # (TD-5280), when that differs from plain input: 1.25 times input on + # Anthropic models. Unset means "the input price", which is what the + # meter charged before the field existed. + cache_write_price: float | None = Field(default=None, ge=0) context_window: int = Field(gt=0) max_output_tokens: int = Field(gt=0) # When true, PNG/JPEG/GIF/WebP attachments are accepted on user turns @@ -505,11 +519,19 @@ def _does_something(self) -> FastConfig: class Preset(BaseModel): - """A complete set of tier configurations.""" + """One entry of the model catalog: a model per role, or the Grok engine. + + A native entry names all three roles. An entry with ``engine: grok`` + runs chats on the Grok CLI, which brings its own model, so it may + leave the three roles out (TD-5239): all three, or none. + """ - brain: TierConfig - worker: TierConfig - validator: TierConfig + brain: TierConfig | None = None + worker: TierConfig | None = None + validator: TierConfig | None = None + # Which agent loop a chat on this entry uses (TD-5239). None = the + # global ``engine.kind``, what every entry meant before. + engine: Literal["native", "grok"] | None = None # How the composer's model picker shows this preset (TD-5172): the name # on the list and chip, a one-line ``description`` shown under it, and # where it sits: the main list (``primary``), "More models" (``more``), @@ -518,16 +540,59 @@ class Preset(BaseModel): label: str | None = Field(default=None, min_length=1, max_length=40) description: str | None = Field(default=None, max_length=120) picker: Literal["primary", "more", "team"] = "primary" + # The picker's section for this entry (TD-5236). None = the daemon says + # Grok, Local or API from the entry itself (``ModelConfig.preset_group``). + group: str | None = Field(default=None, min_length=1, max_length=20) + # A quick-switch slot, 1 to 9 (TD-5236): ⌘⌥ / Ctrl+Alt+ + # picks this entry. None = no shortcut. No two entries share one. + slot: int | None = Field(default=None, ge=1, le=9) # The Fast toggle (TD-5176): None = this model has no fast mode. fast: FastConfig | None = None + # Context settings for chats on this model (TD-5234), laid over the + # global sections of the same names. None = the global values. + overrides: PresetOverrides | None = None - @field_validator("description") + @field_validator("description", "group") @classmethod def _blank_description_is_unset(cls, value: str | None) -> str | None: if value is None: return None return value.strip() or None + @model_validator(mode="after") + def _roles_and_engine_agree(self) -> Preset: + named = [name for name in TIER_NAMES if getattr(self, name) is not None] + if named and len(named) != len(TIER_NAMES): + missing = ", ".join(name for name in TIER_NAMES if name not in named) + raise ValueError(f"name all three roles or none of them; missing: {missing}") + if not named and self.engine != "grok": + raise ValueError( + "brain, worker and validator are required. Only an entry with " + "`engine: grok` may leave them out: the Grok CLI brings its own model" + ) + if self.engine == "grok": + # Both act on TST Desk's own loop, which a Grok chat does not run. + # Refused, not ignored: a setting that does nothing must not sit + # in the file looking as if it were in force. + for key in ("overrides", "fast"): + if getattr(self, key) is not None: + raise ValueError(f"`{key}` does nothing on an `engine: grok` entry; remove it") + return self + + @property + def has_tiers(self) -> bool: + """Whether this entry names a model per role (a Grok entry may not).""" + return self.brain is not None + + def tier_map(self) -> dict[TierName, TierConfig]: + """The entry's own roles; empty for an entry with none.""" + found: dict[TierName, TierConfig] = {} + for name in TIER_NAMES: + tier: TierConfig | None = getattr(self, name) + if tier is not None: + found[name] = tier + return found + class CredentialConfig(BaseModel): """Display name and optional host for one keychain-backed API key. @@ -737,6 +802,11 @@ class SessionConfig(BaseModel): log_max_events: int = Field(default=DEFAULT_LOG_MAX_EVENTS, ge=1) send_file_max_bytes: int = Field(default=536_870_912, ge=1, le=2_147_483_648) send_file_session_max_bytes: int = Field(default=4_294_967_296, ge=1) + # What ``show_artifact`` may put in the Artifacts pane (TD-5254): the + # most text one artifact holds, and how many a session keeps. Two MiB + # is the most the window's preview reads, so anything shown opens. + artifact_max_bytes: int = Field(default=2_097_152, ge=1, le=2_097_152) + artifact_max_count: int = Field(default=200, ge=1, le=10_000) # Reasoning effort a session starts with until the picker sets one. default_effort: EffortLevel = DEFAULT_EFFORT # Whether a new session starts with Ultracode on (TD-5195). @@ -851,6 +921,26 @@ class CompactionConfig(BaseModel): reinject_tokens: int = Field(default=6_000, ge=0) +# How many model calls one delegated child may make unless config says more +# (TD-5265). The number TD-4602 shipped with, and still the default. +DEFAULT_CHILD_MAX_ITERATIONS = 8 + + +class DelegateConfig(BaseModel): + """Limits for a ``delegate`` child (TD-5265). + + ``max_iterations`` is how many model calls one child may make before it + stops with what it has. Eight suits a lookup; a child that edits several + files and runs a test needs more. The session's own ``caps`` still + bound the total: a child's calls count against the same iteration, + spend and time caps as its parent's. + """ + + model_config = ConfigDict(extra="forbid") + + max_iterations: int = Field(default=DEFAULT_CHILD_MAX_ITERATIONS, ge=1, le=64) + + def _default_consult_tiers() -> list[TierName]: return ["worker"] @@ -1659,7 +1749,10 @@ class ModelConfig(BaseModel): clear_tool_results: ClearToolResultsConfig = Field(default_factory=ClearToolResultsConfig) compaction: CompactionConfig = Field(default_factory=CompactionConfig) consult: ConsultConfig = Field(default_factory=ConsultConfig) + delegate: DelegateConfig = Field(default_factory=DelegateConfig) workflow: WorkflowConfig = Field(default_factory=WorkflowConfig) + # How many calls one model server answers at once, daemon-wide (TD-5269). + model_concurrency: ModelConcurrencyConfig = Field(default_factory=ModelConcurrencyConfig) model_refresh: ModelRefreshConfig = Field(default_factory=ModelRefreshConfig) shell: ShellConfig = Field(default_factory=ShellConfig) provider_retry: ProviderRetryConfig = Field(default_factory=ProviderRetryConfig) @@ -1711,6 +1804,21 @@ def _null_presets_is_empty(cls, data: Any) -> Any: data = {**data, "local_servers": []} return data + @model_validator(mode="after") + def _preset_overrides_fit(self) -> ModelConfig: + """An entry's ``overrides`` must merge with the global sections (TD-5234). + + Each key has its own limits already. This catches what only the + merged section can show, at load, where the message names the entry, + instead of at the moment a chat switches to it. + """ + for name, preset in self.presets.items(): + try: + merged_sections(self, preset.overrides) + except ValueError as e: + raise ValueError(f"presets.{name}.overrides: {e}") from e + return self + @model_validator(mode="after") def _active_preset_falls_back(self) -> ModelConfig: """A missing or unknown ``active_preset`` lands on the first preset. @@ -1727,12 +1835,54 @@ def has_model(self) -> bool: """Whether any model is set up, i.e. the active preset exists.""" return self.active_preset in self.presets + @model_validator(mode="after") + def _slots_are_not_shared(self) -> ModelConfig: + """One shortcut picks one entry (TD-5236).""" + taken: dict[int, str] = {} + for name, preset in self.presets.items(): + if preset.slot is None: + continue + if preset.slot in taken: + raise ValueError( + f"presets.{name}.slot: slot {preset.slot} is already " + f"presets.{taken[preset.slot]}'s; give each entry its own" + ) + taken[preset.slot] = name + return self + + def preset_group(self, name: str) -> str: + """The picker section entry *name* sits in (TD-5236). + + Its own ``group`` when it names one. Otherwise ``Grok`` for an + entry on the Grok engine, ``Local`` when every role is served on + this computer, and ``API`` for the rest. A server on the local + network is not this computer, so it reads ``API`` unless the entry + says ``group: Local``: only the person knows it is theirs. + """ + preset = self.presets.get(name) + if preset is None: + return GROUP_API + if preset.group is not None: + return preset.group + if preset.engine == "grok": + return GROUP_GROK + tiers = preset.tier_map().values() + if tiers and all(is_loopback_url(resolve_base_url(self, tier)) for tier in tiers): + return GROUP_LOCAL + return GROUP_API + + def preset_engine(self, name: str) -> Literal["native", "grok"]: + """The engine a chat on entry *name* uses: its own, else ``engine.kind`` (TD-5239).""" + preset = self.presets.get(name) + if preset is not None and preset.engine is not None: + return preset.engine + return self.engine.kind + @model_validator(mode="after") def _bound_credentials_exist(self) -> ModelConfig: known = set(self.credentials) | {DEFAULT_CREDENTIAL_ID} for preset_name, preset in self.presets.items(): - for tier_name in TIER_NAMES: - tier = getattr(preset, tier_name) + for tier_name, tier in preset.tier_map().items(): bound = [(f"presets.{preset_name}.{tier_name}", tier.credential)] bound += [ (f"presets.{preset_name}.{tier_name}.fallbacks[{i}]", backup.credential) @@ -1752,8 +1902,8 @@ def fallback_bindings(self, credential_id: str) -> list[str]: return [ f"presets.{preset_name}.{tier_name}.fallbacks[{i}]" for preset_name, preset in self.presets.items() - for tier_name in TIER_NAMES - for i, backup in enumerate(getattr(preset, tier_name).fallbacks) + for tier_name, tier in preset.tier_map().items() + for i, backup in enumerate(tier.fallbacks) if backup.credential == credential_id ] @@ -1770,9 +1920,25 @@ def tiers(self) -> dict[TierName, TierConfig]: that only reads windows or prices keeps working on a first run. """ preset = self.presets.get(self.active_preset) - if preset is None: + # An ``engine: grok`` entry may name no model (TD-5239). It gets the + # same stand-ins: a Grok chat never asks TST Desk's router for one. + own = preset.tier_map() if preset is not None else {} + if not own: return {name: _no_model_tier() for name in TIER_NAMES} - return {name: preset.__getattribute__(name) for name in TIER_NAMES} + return own + + def native_default(self) -> str | None: + """The entry whose model does work no chat owns, or None (TD-5239). + + Memory distill and the doctor's probe run on TST Desk's own client + whatever engine a chat used. They take the active entry when it + names a model, else the first entry that does. None means there is + no such model, and that work is skipped. + """ + active = self.presets.get(self.active_preset) + if active is not None and active.has_tiers: + return self.active_preset + return next((name for name, entry in self.presets.items() if entry.has_tiers), None) def requires_api_key(self) -> bool: """Whether the active preset will send at least one key (TD-1801, TD-1717). diff --git a/core/tstd/config.yaml b/core/tstd/config.yaml index a5ee5cf8..c9c4041f 100644 --- a/core/tstd/config.yaml +++ b/core/tstd/config.yaml @@ -15,6 +15,9 @@ # input_price — $ per million input tokens (prompt) # output_price — $ per million output tokens (completion) # cache_read_price — $ per million input tokens read from cache +# cache_write_price — optional: $ per million input tokens the provider +# stores in its cache, when it charges more than +# input_price for them. Unset = input_price. # context_window — max context tokens the model supports # max_output_tokens — max tokens the model may generate; sent as # max_tokens on every turn (TD-5008) @@ -219,6 +222,27 @@ consult: tiers: [worker] max_per_turn: 2 +# A delegate child (TD-5265): how many model calls one may make before it +# stops with what it has. Its calls also count against the session's own +# caps, so a larger number here cannot outrun them. +delegate: + max_iterations: 8 + +# How many calls one model server answers at once, across every chat, +# worker and scheduled run (TD-5269). null is no limit. A model served on +# this computer gets 2 (TD-5282): one GPU answering five requests at once +# answers all of them slowly, and some time out and are sent again. Raise +# it if your server is built for more, or set null for no limit. A config +# file written before this section existed has no limit, as before. +# endpoints gives one server, by URL, a number of its own: +# endpoints: +# - url: http://127.0.0.1:8080 +# max_concurrent: 1 +model_concurrency: + loopback_default: 2 + remote_default: null + endpoints: [] + # The workflow tool (TD-5195), used only while a session's Ultracode switch # is on: it runs several workers at once on independent pieces of a job, and # each result can be checked before it comes back. max_parallel (1-16) is how @@ -382,7 +406,8 @@ autonomy: verify_max_prompt_tokens: null # User-listed MCP servers (TD-4401). Empty is none — no doctor rows, -# no dynamic discovery. HTTP urls must be loopback. A server's env: +# no dynamic discovery. HTTP urls must be loopback, unless that server +# sets allow_remote: true and uses https (TD-5136). A server's env: # (stdio) or headers: (http) holds literals that are not secrets, or # "keychain:" — tokens stay in the keychain (TD-5110); Settings → # MCP servers stores one without showing it. Once the MCP tool schemas diff --git a/core/tstd/config_editor_checks.py b/core/tstd/config_editor_checks.py index 8f26689b..88c19042 100644 --- a/core/tstd/config_editor_checks.py +++ b/core/tstd/config_editor_checks.py @@ -321,7 +321,9 @@ def _tier_at(config: ModelConfig, path: list[PathPart]) -> TierConfig | None: preset = config.presets.get(path[1]) if preset is None: return None - tier: TierConfig = getattr(preset, str(path[2])) + tier: TierConfig | None = getattr(preset, str(path[2])) + if tier is None: + return None if path[3:4] == ["fallbacks"] and len(path) == 5 and isinstance(path[4], int): return tier.fallbacks[path[4]] if path[4] < len(tier.fallbacks) else None return tier if len(path) == 3 else None diff --git a/core/tstd/config_help.py b/core/tstd/config_help.py index e5720c1e..766c6f48 100644 --- a/core/tstd/config_help.py +++ b/core/tstd/config_help.py @@ -10,7 +10,17 @@ from __future__ import annotations +from pydantic import BaseModel + from .config_help_features import FEATURE_HELP +from .preset_overrides import ( + ClearToolResultsOverride, + CompactionOverride, + ComputerUseOverride, + ConsultOverride, + SessionOverride, + WorkflowOverride, +) S = "seconds" TOKENS = "tokens" @@ -30,6 +40,8 @@ "clear_tool_results": "Clear stale tool results", "compaction": "Compaction", "consult": "Consult", + "delegate": "Delegate", + "model_concurrency": "Calls at once per model server", "workflow": "Workflow", "model_refresh": "Model refresh", "shell": "Shell", @@ -57,6 +69,9 @@ "the first one.", "ModelConfig.credentials": "Named API keys: the name Settings shows and the host each key " "talks to. The keys themselves live in the OS keychain.", + "ModelConfig.model_concurrency": "How many calls one model server answers at once, " + "across every chat, worker and scheduled run.", + "ModelConfig.delegate": "Limits for a child the agent delegates part of a task to.", "ModelConfig.search": "Where web_search sends queries, and how web_fetch reads pages.", "ModelConfig.local_servers": "Model servers on this computer that Add a model's search " "asks for their models. Loopback only; no key is sent.", @@ -104,17 +119,36 @@ "Preset.brain": "Plans and talks to you. A session opens on it for its first turns.", "Preset.worker": "Runs the steps: edits, the tool-call classifier, compaction and summaries.", "Preset.validator": "Reviews diffs and tests, including the review after writes.", + "Preset.engine": "Which agent loop a chat on this one uses: TST Desk's own, or the Grok " + "CLI. Empty uses the Engine setting. With grok, the three roles may be left out.", "Preset.label": "The name on the model picker. A model with no label is left off the " "picker unless it is in use.", "Preset.description": "One line under the label on the picker: what this model is for.", "Preset.picker": "Where the picker lists it: the main list, More models, or Teams.", + "Preset.group": "The picker's section for this one. Empty lets TST Desk choose: Grok, " + "Local when every role is served on this computer, else API.", + "Preset.slot": "A quick-switch slot, 1 to 9: ⌘⌥ and the digit (Ctrl+Alt elsewhere) picks " + "this one. Empty is no shortcut. No two share a slot.", "Preset.fast": "The model's Fast mode. With this set, the picker shows a Fast switch.", + "Preset.overrides": "Context settings for chats on this model, in place of the global " + "ones of the same names. Nothing here changes approvals, shell rules or keys.", + "PresetOverrides.session": "What a chat on this model starts with.", + "PresetOverrides.clear_tool_results": "Clear stale tool results, for this model.", + "PresetOverrides.compaction": "Compaction, for this model.", + "PresetOverrides.consult": "The consult tool, for this model.", + "PresetOverrides.workflow": "Workflow limits, for this model's server.", + "PresetOverrides.computer_use": "The image cap, for this model.", "TierConfig.slug": "The model id sent as model. Optional on a loopback endpoint, where " "it is read from /v1/models.", "TierConfig.base_url": "The OpenAI-compatible endpoint. A bound key's own host wins over this.", "TierConfig.input_price": ("Dollars per million prompt tokens not served from cache.", USD), "TierConfig.output_price": ("Dollars per million completion tokens.", USD), "TierConfig.cache_read_price": ("Dollars per million prompt tokens served from cache.", USD), + "TierConfig.cache_write_price": ( + "Dollars per million prompt tokens the provider stores in its cache. " + "Leave unset to charge them as plain input.", + USD, + ), "TierConfig.context_window": ( "The compaction budget. Keep it no larger than the server actually serves.", TOKENS, @@ -183,6 +217,20 @@ for key, value in (_MODEL_HELP | FEATURE_HELP).items() } +#: A model's override of a global key (TD-5234) says what that key says. +OVERRIDE_OF: dict[str, tuple[type[BaseModel], str]] = { + "SessionOverride": (SessionOverride, "SessionConfig"), + "ClearToolResultsOverride": (ClearToolResultsOverride, "ClearToolResultsConfig"), + "CompactionOverride": (CompactionOverride, "CompactionConfig"), + "ConsultOverride": (ConsultOverride, "ConsultConfig"), + "WorkflowOverride": (WorkflowOverride, "WorkflowConfig"), + "ComputerUseOverride": (ComputerUseOverride, "ComputerUseConfig"), +} +for _name, (_model, _base) in OVERRIDE_OF.items(): + for _field in _model.model_fields: + _text, _unit = FIELD_HELP[f"{_base}.{_field}"] + FIELD_HELP[f"{_name}.{_field}"] = (f"{_text} Empty keeps the global value.", _unit) + #: Values the generic editor never shows or writes: names only, with where #: each value lives. Their own page stores a value in the keychain (TD-5110). SECRET_FIELDS: dict[str, str] = { @@ -220,12 +268,15 @@ "their keychain values.", } -#: Keys with no Reset: the picker sets them. -NOT_RESETTABLE: frozenset[str] = frozenset({"ModelConfig.active_preset"}) +#: Keys with no Reset. The picker sets the active model. A model's three +#: roles are all there or all absent (TD-5239), so one is never reset alone. +NOT_RESETTABLE: frozenset[str] = frozenset( + {"ModelConfig.active_preset", "Preset.brain", "Preset.worker", "Preset.validator"} +) #: Long text gets a multi-line box. MULTILINE_FIELDS: frozenset[str] = frozenset( - {"CompactionConfig.summary_prompt", "HookEntry.command"} + {"CompactionConfig.summary_prompt", "CompactionOverride.summary_prompt", "HookEntry.command"} ) #: The fields a new entry's form asks for, when not just its required ones. diff --git a/core/tstd/config_help_features.py b/core/tstd/config_help_features.py index 69e9710c..08906703 100644 --- a/core/tstd/config_help_features.py +++ b/core/tstd/config_help_features.py @@ -53,6 +53,14 @@ "Largest file send_file may copy into the chat (at most 2 GiB).", BYTES, ), + "SessionConfig.artifact_max_bytes": ( + "Most text one artifact shown by show_artifact may hold (at most 2 MiB).", + BYTES, + ), + "SessionConfig.artifact_max_count": ( + "How many artifacts one session keeps in the Artifacts pane.", + "artifacts", + ), "SessionConfig.send_file_session_max_bytes": ( "Total of sent files one session may hold.", BYTES, @@ -85,6 +93,15 @@ "CompactionConfig.reinject_files": "Recent files read again after a compaction. 0 brings " "none back.", "CompactionConfig.reinject_tokens": ("Most those files may take.", TOKENS), + "ModelConcurrencyConfig.loopback_default": "Calls at once for a server on this computer. " + "Empty is no limit. A single GPU usually wants 1 or 2.", + "ModelConcurrencyConfig.remote_default": "Calls at once for any other server. Empty is " + "no limit.", + "ModelConcurrencyConfig.endpoints": "Servers with a number of their own, by URL.", + "EndpointLimit.url": "The server, by URL. Only its scheme, host and port matter.", + "EndpointLimit.max_concurrent": "Calls that server answers at once.", + "DelegateConfig.max_iterations": "Model calls one delegated child may make before it " + "stops with what it has. The session's caps still bound the total.", "ConsultConfig.tiers": "Tiers offered the consult tool. None turns it off.", "ConsultConfig.max_per_turn": "Consults one turn may make, delegate children included.", "WorkflowConfig.max_parallel": "Workers running at once. Size it to what the worker's " diff --git a/core/tstd/cost.py b/core/tstd/cost.py index ccd93d1d..6bb139b4 100644 --- a/core/tstd/cost.py +++ b/core/tstd/cost.py @@ -64,6 +64,49 @@ def billable_cached_tokens(cached: int | None) -> int: return cached if cached is not None else 0 +@dataclass(frozen=True) +class PromptBill: + """One call's prompt side, split the way a provider bills it (TD-5280). + + ``uncached_tokens`` is every prompt token not read from cache, and it + includes ``written_tokens``, the ones the provider stored in its cache + on this call. ``prompt_cost`` covers all of ``uncached_tokens``: the + written ones at the tier's cache-write price, the rest at its input + price. Unrounded, so the parts sum to the total before rounding. + """ + + cached_tokens: int + uncached_tokens: int + written_tokens: int + prompt_cost: float + cached_cost: float + + +def prompt_bill(usage: Usage, tier_cfg: TierConfig) -> PromptBill: + """Split *usage*'s prompt tokens into what each is charged at. + + A cache write is never counted for more tokens than were left after + the cache reads, whatever the provider reported, so a malformed figure + cannot make a call cost more than its whole prompt at the write price. + With no ``cache_write_price`` on the tier a written token costs the + input price, which is what every call cost before the field existed. + """ + cached_tokens = billable_cached_tokens(usage.cached_prompt_tokens) + uncached = max(0, usage.prompt_tokens - cached_tokens) + written = min(uncached, max(0, usage.cache_write_tokens or 0)) + write_price = ( + tier_cfg.input_price if tier_cfg.cache_write_price is None else tier_cfg.cache_write_price + ) + prompt_cost = ((uncached - written) * tier_cfg.input_price + written * write_price) / 1_000_000 + return PromptBill( + cached_tokens=cached_tokens, + uncached_tokens=uncached, + written_tokens=written, + prompt_cost=prompt_cost, + cached_cost=cached_tokens * tier_cfg.cache_read_price / 1_000_000, + ) + + def compute_call_cost(usage: Usage, tier_cfg: TierConfig) -> float: """Compute the dollar cost of a single API call. @@ -74,13 +117,10 @@ def compute_call_cost(usage: Usage, tier_cfg: TierConfig) -> float: Returns: Cost in dollars. """ - cached_tokens = billable_cached_tokens(usage.cached_prompt_tokens) - uncached = max(0, usage.prompt_tokens - cached_tokens) - prompt_cost = uncached * tier_cfg.input_price / 1_000_000 - cached_cost = cached_tokens * tier_cfg.cache_read_price / 1_000_000 + bill = prompt_bill(usage, tier_cfg) completion_cost = usage.completion_tokens * tier_cfg.output_price / 1_000_000 - return round(prompt_cost + cached_cost + completion_cost, 6) + return round(bill.prompt_cost + bill.cached_cost + completion_cost, 6) def compute_call_details(usage: Usage, tier_cfg: TierConfig) -> dict[str, float]: @@ -90,19 +130,13 @@ def compute_call_details(usage: Usage, tier_cfg: TierConfig) -> dict[str, float] Dict with keys: uncached_prompt_tokens, prompt_cost, cached_cost, completion_cost, total_cost. """ - cached_tokens = billable_cached_tokens(usage.cached_prompt_tokens) - uncached = max(0, usage.prompt_tokens - cached_tokens) + bill = prompt_bill(usage, tier_cfg) return { - "uncached_prompt_tokens": uncached, - "prompt_cost": round(uncached * tier_cfg.input_price / 1_000_000, 6), - "cached_cost": round(cached_tokens * tier_cfg.cache_read_price / 1_000_000, 6), + "uncached_prompt_tokens": bill.uncached_tokens, + "prompt_cost": round(bill.prompt_cost, 6), + "cached_cost": round(bill.cached_cost, 6), "completion_cost": round(usage.completion_tokens * tier_cfg.output_price / 1_000_000, 6), - "total_cost": round( - uncached * tier_cfg.input_price / 1_000_000 - + cached_tokens * tier_cfg.cache_read_price / 1_000_000 - + usage.completion_tokens * tier_cfg.output_price / 1_000_000, - 6, - ), + "total_cost": compute_call_cost(usage, tier_cfg), } @@ -262,10 +296,10 @@ def _build_record( """Build a CallRecord for *usage* and compute its dollar cost.""" tier_cfg = cfg or self._config.tier(tier) cost = compute_call_cost(usage, tier_cfg) - cached_tokens = billable_cached_tokens(usage.cached_prompt_tokens) - uncached = max(0, usage.prompt_tokens - cached_tokens) - prompt_cost = round(uncached * tier_cfg.input_price / 1_000_000, 6) - cached_cost = round(cached_tokens * tier_cfg.cache_read_price / 1_000_000, 6) + bill = prompt_bill(usage, tier_cfg) + uncached = bill.uncached_tokens + prompt_cost = round(bill.prompt_cost, 6) + cached_cost = round(bill.cached_cost, 6) completion_cost = round(usage.completion_tokens * tier_cfg.output_price / 1_000_000, 6) record = CallRecord( diff --git a/core/tstd/daemon.py b/core/tstd/daemon.py index 9b802320..5e9b17d3 100644 --- a/core/tstd/daemon.py +++ b/core/tstd/daemon.py @@ -18,6 +18,7 @@ import tempfile import time from collections.abc import Callable, Sequence +from contextvars import ContextVar from dataclasses import dataclass, field from datetime import UTC, datetime, timedelta from pathlib import Path @@ -54,6 +55,7 @@ from .chat_history import restorable_history from .config import ( DEFAULT_CREDENTIAL_ID, + PICKER_GROUPS, ConfigError, CredentialConfig, JudgmentsConfig, @@ -140,6 +142,7 @@ from .desktop.protocol import DesktopError from .discovery import resolve_tier_slugs from .effort import effort_record, read_effort_record +from .endpoint_gate import EndpointGates from .grok_acp import GrokEngineError, find_grok_binary from .grok_home import ( grok_configured_model, @@ -148,7 +151,7 @@ open_in_terminal, ) from .grok_loop import grok_loop -from .hub import NOT_HANDLED, Hub +from .hub import NOT_HANDLED, Hub, scrub_setup_state from .key_hosts import KeyProbe, is_key_id, key_got_past_sign_in, key_probe, key_refusal from .keychain import ( KeychainError, @@ -159,7 +162,7 @@ store_api_key, ) from .line_comments import LineCommentError, build_comments_turn -from .local_servers import scan_local_servers +from .local_servers import loopback_answers, scan_local_servers from .local_worker import ( effective_tier, last_cu_surface, @@ -170,6 +173,8 @@ from .loop import ProviderLike, agent_loop, brain_manifest_config from .machine_pairing import DialPolicy from .mcp.loader import McpSupervisor +from .mcp_import import McpImportError, parse_import +from .mcp_import_apply import Choice, apply_import from .mcp_settings import ( delete_mcp_secret, forget_server_secrets, @@ -208,6 +213,8 @@ from .notify.slack import send as send_slack_notify from .notify.telegram import schedule as schedule_telegram_notify from .peer_policy import outbound_event, service_hosts +from .phone_permissions import APPROVAL_SWITCHES, MCP_SERVERS, utc_now +from .phone_store import PhoneStore, server_saved_summary, value_summary from .plan_review import answer as answer_plan from .plan_review import keep_pinned_plan_visible, restore_pinned_plan, restored_plan_mode from .policy import ( @@ -224,7 +231,21 @@ stricter_rule, ) from .port_file import release_port_file +from .preset_apply import ( + effective_config, + engine_refusal, + no_model_refusal, + preset_overrides, +) +from .preset_ready import check_presets +from .preset_write import ( + add_grok_preset, + delete_preset, + duplicate_preset, + save_preset_label, +) from .protocol import ( + AddGrokPreset, AddModel, AddPin, AlwaysAllow, @@ -242,6 +263,7 @@ CancelPairing, CharterDocument, CheckCuPermissions, + CheckPresets, ClientMessageT, CloseWorktree, CommandEntry, @@ -263,6 +285,7 @@ DeleteJobTemplate, DeleteMcpSecret, DeleteMcpServer, + DeletePreset, DeleteSession, Deny, DenyVerify, @@ -274,6 +297,7 @@ DiagnosticsReport, Drain, DrainResult, + DuplicatePreset, EmailNotifyState, EndpointModels, EndSession, @@ -285,6 +309,7 @@ GetConfigEditor, GetHooks, GetInstructionStack, + GetPresetRoles, GetSessionJobSource, GetSetupState, GetUsage, @@ -302,6 +327,7 @@ GrokSessionEntry, GrokSessionList, HandshakeError, + ImportMcpServers, InstructionFileEntry, InstructionFiles, JobDraftReply, @@ -326,6 +352,9 @@ ListSessions, LogTrimmed, McpExcludedToolSummary, + McpImportPreview, + McpImportServer, + McpImportValue, McpServerSummary, MemoryAccept, MemoryEdit, @@ -344,7 +373,11 @@ OpenWorkspace, PairingCompleted, ParseJob, + PhoneChange, PolicyRules, + PresetReadiness, + PresetRoles, + PreviewMcpImport, PreviewRestoreFiles, ReadArtifactChunk, RefreshModelCards, @@ -391,8 +424,10 @@ SetLoadGlobalMemory, SetMcpSecret, SetMcpServer, + SetPhonePermission, SetPlan, SetPreset, + SetPresetLabel, SetRemoteAttach, SetSessionPreset, SetSessionStar, @@ -414,6 +449,7 @@ SteeringReject, StopTurn, SubmitLineComments, + SwitchPreset, TestEmail, TestModel, TierCard, @@ -473,7 +509,7 @@ pairing_code_event, ) from .rewind_handlers import preview_restore, restore_files -from .router import TIER_NAMES, TierRouter +from .router import TierRouter from .scheduler.calendar_path import CalendarPathError, require_calendar_file from .scheduler.chain import require_chain from .scheduler.chat_source import SessionScheduleSource, session_schedule_source @@ -536,6 +572,7 @@ from .session_search_handler import search_sessions_reply from .session_stars import load_session_stars, save_session_stars from .session_store import SessionStore +from .shown_artifacts import ShownArtifacts from .shutdown_budget import ( ShutdownBudget, install_posix_shutdown_signals, @@ -738,7 +775,7 @@ def _snapshot_slugs(config: ModelConfig) -> dict[str, dict[str, str | None]]: this cannot be derived from ``self.config`` later (TD-1703). """ return { - name: {tier: getattr(preset, tier).slug for tier in TIER_NAMES} + name: {tier: cfg.slug for tier, cfg in preset.tier_map().items()} for name, preset in config.presets.items() } @@ -918,6 +955,13 @@ def _shipped_typesafe_base_url() -> str: return url.strip() if isinstance(url, str) else "" +#: The connection whose message is being handled, or None outside a handler. +_ANSWERING: ContextVar[object | None] = ContextVar("tstd_answering", default=None) + +#: The line under the label of the entry "Add Grok to your models" writes (TD-5258). +GROK_PRESET_DESCRIPTION = "The Grok CLI, with its own model" + + def _probe_grok(configured: str) -> tuple[Path | None, bool]: """PATH / configured binary probe. Never a credential.""" try: @@ -1163,6 +1207,8 @@ def __init__( known_session=lambda sid: self.session_registry.get(sid) is not None, ) self._slug_snapshot = _snapshot_slugs(self.config) + # TD-5269: one count per model server, shared across sessions. + self._gates = EndpointGates(self.config.model_concurrency) # TD-5185: last model-card refresh per (preset, tier), this run only. self._card_results: dict[tuple[str, str], CardResult] = {} # A background refresh and a Settings one never write at once (TD-5197). @@ -1233,8 +1279,16 @@ def __init__( ) # TD-5015: paired computers. Only the bridge's links dial, only to # paired Tailscale hosts (dial_policy is the tests' seam). + # TD-5279: what a paired phone may change. Off unless the person at + # this computer switched it on; the hub asks on every message. + self.phone_store = PhoneStore(self.data_dir) + self._phone_expiry_task: asyncio.Task[None] | None = None self.hub = Hub( - self.data_dir, self.ws_server, self._handle_list_sessions, policy=dial_policy + self.data_dir, + self.ws_server, + self._handle_list_sessions, + policy=dial_policy, + phone_grants=self.phone_store.live, ) self._cu_server: Any = None # TD-4835: keychain presence cache — see _credential_is_stored. @@ -1374,12 +1428,17 @@ async def _build_client(self, tier_cfg: TierConfig) -> ProviderClient: base_url = resolve_base_url(self.config, tier_cfg) timeouts = tier_cfg.timeouts() if cred_id is None: - return ProviderClient( + client = ProviderClient( base_url=base_url, api_key=None, timeout=timeouts, retry_config=retry_cfg ) - return await ProviderClient.from_keychain( - base_url, provider_name=cred_id, timeout_config=timeouts, retry_config=retry_cfg - ) + else: + client = await ProviderClient.from_keychain( + base_url, provider_name=cred_id, timeout_config=timeouts, retry_config=retry_cfg + ) + # TD-5269: every client for one server shares that server's count. + self._gates.configure(self.config.model_concurrency) + client.gate = self._gates.for_url(base_url) + return client async def _brain_client(self) -> ProviderClient: """Build a client for the active brain tier.""" @@ -1450,14 +1509,16 @@ async def rebuild() -> ProviderClient: self._clients[cache_key] = handle return handle - async def _ensure_provider(self) -> ProviderLike: + async def _ensure_provider(self, config: ModelConfig | None = None) -> ProviderLike: """Create the shared provider client on first use. Distill and doctor stay on the active brain. Session turns pass the effective tier into ``get_provider`` so a CU-heavy worker - can land on a different URL (TD-3903). + can land on a different URL (TD-3903). *config* is the catalog + pointed at another entry, for distill when the active one has no + model of its own (TD-5239). """ - return await self._client_for(self.config.tier("brain")) + return await self._client_for((config or self.config).tier("brain")) async def _setup_state_event(self) -> SetupState: """Current onboarding state (TD-1101): key presence + preset choice. @@ -1471,10 +1532,15 @@ async def _setup_state_event(self) -> SetupState: credentials = await self._credential_summaries() has_api_key = any(item.stored for item in credentials) # An empty catalog is a first run, not an error (TD-5172). - tiers = self.config.tiers() if self.config.has_model() else {} + # Nor is an active entry that is the Grok engine with no model of its + # own (TD-5239): Settings shows no roles for it, not the stand-ins. + active = self.config.presets.get(self.config.active_preset) + tiers = self.config.tiers() if active is not None and active.has_tiers else {} grok_path, grok_ok = _probe_grok(self.config.engine.binary) engine_kind = self.config.engine.kind - key_required = self.config.requires_api_key() and engine_kind != "grok" + # What a new chat would run on: the active entry's engine, else the global one. + new_chat_engine = self.config.preset_engine(self.config.active_preset) + key_required = self.config.requires_api_key() and new_chat_engine != "grok" excluded_by_server = {row.server_id: row.excluded for row in self._mcp.doctor_rows()} return SetupState( seq=1, @@ -1497,6 +1563,14 @@ async def _setup_state_event(self) -> SetupState: team_presets=sorted( name for name, p in self.config.presets.items() if p.picker == "team" ), + preset_engines={ + name: p.engine for name, p in sorted(self.config.presets.items()) if p.engine + }, + preset_groups={name: self.config.preset_group(name) for name in self.config.presets}, + picker_groups=list(PICKER_GROUPS), + preset_slots={ + name: p.slot for name, p in sorted(self.config.presets.items()) if p.slot + }, has_model=self.config.has_model(), skip_all_approvals=self.skip_all_approvals, load_global_memory=self.load_global_memory, @@ -1508,7 +1582,11 @@ async def _setup_state_event(self) -> SetupState: remote_attach_enabled=self.remote_attach_enabled, remote_bind=self.ws_server.extra_host, remote_port=self.ws_server.port if self.ws_server.extra_host else None, - remote_devices=self.remote_devices.summaries(self.ws_server.connected_device_ids()), + remote_devices=[ + device.model_copy(update={"mcp_servers_added": self._servers_added_by(device.id)}) + for device in self.remote_devices.summaries(self.ws_server.connected_device_ids()) + ], + phone_permissions=self.phone_store.states(), credentials=credentials, tier_credentials={name: tiers[name].credential for name in tiers}, tier_loopback={name: is_loopback_url(tiers[name].base_url) for name in tiers}, @@ -1545,8 +1623,11 @@ async def _setup_state_event(self) -> SetupState: ], env=await value_summaries(sid, spec.env, self._mcp_secret_is_stored), headers=await value_summaries(sid, spec.headers, self._mcp_secret_is_stored), + origin_device=origin.device_name if origin else "", + origin_at=origin.at if origin else "", ) for sid, spec in sorted(self.config.mcp.servers.items()) + for origin in (self.phone_store.origin_of(sid),) ], speech_enabled=self.config.speech.enabled, speech_ready=self.config.speech.enabled and bool(self.config.speech.base_url), @@ -1817,11 +1898,14 @@ def _set_tier_context_window(self, preset: str, tier: str, window: int) -> str | ) return None - def _tier_cards(self, tiers: dict[str, TierConfig]) -> dict[str, TierCard]: - """The active preset's stored numbers and last refresh, per tier.""" + def _tier_cards( + self, tiers: dict[str, TierConfig], preset: str | None = None + ) -> dict[str, TierCard]: + """An entry's stored numbers and last refresh, per tier. Default: the active one.""" + owner = self.config.active_preset if preset is None else preset cards: dict[str, TierCard] = {} for name, tier in tiers.items(): - last = self._card_results.get((self.config.active_preset, name)) + last = self._card_results.get((owner, name)) cards[name] = TierCard( input_price=tier.input_price, output_price=tier.output_price, @@ -1915,8 +1999,7 @@ async def _delete_named_credential(self, credential_id: str) -> None: ) bound: list[tuple[str, str]] = [] for preset_name, preset in self.config.presets.items(): - for tier_name in ("brain", "worker", "validator"): - tier = getattr(preset, tier_name) + for tier_name, tier in preset.tier_map().items(): if tier.credential == cred_id: bound.append((preset_name, tier_name)) try: @@ -1929,8 +2012,8 @@ async def _delete_named_credential(self, credential_id: str) -> None: raise if cred_id in self.config.credentials: delete_credential_entry(cred_id, self.config_path) - for preset_name, tier_name in bound: - save_tier_credential(preset_name, tier_name, None, self.config_path) + for bound_preset, bound_tier in bound: + save_tier_credential(bound_preset, bound_tier, None, self.config_path) self._reload_user_config() def _cu_permissions_session(self, event: DaemonEvent) -> str | None: @@ -2148,6 +2231,50 @@ async def _handle_add_model(self, msg: AddModel) -> str: self.config = self.config.model_copy(update={"active_preset": name}) self._slug_snapshot = _snapshot_slugs(self.config) log.info("model added", extra={"extra_fields": {"preset": name}}) + await self._tell_other_windows_the_catalog() + return (await self._setup_state_event()).model_dump_json() + + async def _handle_catalog_edit( + self, msg: DuplicatePreset | SetPresetLabel | DeletePreset | AddGrokPreset + ) -> str: + """Copy, relabel, remove or add-Grok for the catalog; ack with setup_state (TD-5237). + + The catalog changes for new chats and for the next switch. A chat + that is open keeps the snapshot it holds, a removed entry's included + (TD-1721), and no key is touched. + """ + try: + if isinstance(msg, DuplicatePreset): + made = preset_name_for(msg.label, set(self.config.presets)) + duplicate_preset(msg.source, made, msg.label, self.config_path) + fields = {"action": "duplicate", "preset": made, "source": msg.source} + elif isinstance(msg, SetPresetLabel): + save_preset_label(msg.name, msg.label, msg.description, self.config_path) + fields = {"action": "label", "preset": msg.name} + elif isinstance(msg, AddGrokPreset): + # TD-5258: an entry for an engine that is not here would only + # ever fail, so it is not offered until the CLI is found. + _path, found = await asyncio.to_thread(_probe_grok, self.config.engine.binary) + if not found: + return build_error( + "grok_unavailable", + "The Grok CLI was not found on this computer. Install Grok Build " + "and run `grok login`, or set engine.binary in config.yaml.", + ) + made = preset_name_for(msg.label, set(self.config.presets)) + add_grok_preset(made, msg.label, GROK_PRESET_DESCRIPTION, self.config_path) + fields = {"action": "add_grok", "preset": made} + else: + delete_preset(msg.name, self.config_path) + fields = {"action": "delete", "preset": msg.name} + self._reload_user_config() + except ConfigError as e: + return build_error("bad_request", str(e)) + if isinstance(msg, DeletePreset): + for key in [k for k in self._card_results if k[0] == msg.name]: + del self._card_results[key] + log.info("model catalog edited", extra={"extra_fields": fields}) + await self._tell_other_windows_the_catalog() return (await self._setup_state_event()).model_dump_json() async def _test_model(self, preset: str | None) -> ModelTested: @@ -2155,7 +2282,16 @@ async def _test_model(self, preset: str | None) -> ModelTested: name = preset or self.config.active_preset if name not in self.config.presets: return ModelTested(preset=name, ok=False, detail="No model is set up yet.") - tier_cfg = apply_credential_host(self.config, self.config.presets[name].brain) + brain = self.config.presets[name].brain + if brain is None: + # TD-5239: a Grok entry with no model of its own. + return ModelTested( + preset=name, + ok=False, + detail="This one runs on the Grok CLI, which brings its own model. " + "There is no model here to test.", + ) + tier_cfg = apply_credential_host(self.config, brain) try: client = await self._client_for(tier_cfg) response = await client.chat_completion( @@ -2196,6 +2332,14 @@ async def _doctor_key_provider_rows(self) -> list[DiagnosticCheck]: transport failure means we never got far enough to judge the key. """ fix_key = "Re-enter a valid key: title-bar gear → Provider API key." + active = self.config.presets.get(self.config.active_preset) + if active is not None and not active.has_tiers: + # TD-5239: the Grok CLI brings its own model and sign-in. + why = "not checked: the active model runs on the Grok CLI" + return [ + DiagnosticCheck(name="api_key", status="skip", detail=why), + DiagnosticCheck(name="provider", status="skip", detail=why), + ] try: err = await self._provider_probe() except KeychainError as e: @@ -2351,6 +2495,11 @@ async def _diagnostics_report(self) -> DiagnosticsReport: ) ) + # TD-5279: a phone permission left on is worth seeing outside Settings. + phone_row = self.phone_store.doctor_row() + if phone_row is not None: + checks.append(phone_row) + return DiagnosticsReport(seq=1, checks=checks) # ── Usage and cost (TD-1706) ─────────────────────────────────────── @@ -2560,6 +2709,7 @@ async def _serve(self) -> None: if await asyncio.to_thread(cu_stopfile.stop_engaged): self.set_computer_use_killed(True, write_file=False) self._tasks.append(asyncio.create_task(self._cu_stop_watch(), name="cu-stop-watch")) + self._arm_phone_permission_expiry() await self._shutdown_event.wait() async def _shutdown(self) -> None: @@ -3055,6 +3205,8 @@ async def _on_session_event(self, event: DaemonEvent, event_log: SessionEventLog # restart re-sorted the rail by revive order (TD-5201). used = event.state != "running" and event.reason != SHUTDOWN_REASON await self._session_store.update_state(event.session_id, event.state, touch=used) + elif isinstance(event, TurnComplete) and isinstance(session_id, str): + await self._apply_pending_preset(session_id) elif isinstance(event, UserTurn): # A turn is use: the rail's newest-first order, quick entry and # `tst run --continue` all read updated_at as "used last", and a @@ -3139,7 +3291,14 @@ async def _handle_message(self, raw: str, _connection: Any) -> str | None: Broadcast and replay carry log events, already scrubbed at insertion, so they do not pass through here. """ - reply = await self._dispatch_message(raw, _connection) + # The connection being answered, for a handler that tells the other + # windows what this one's reply already says (TD-5236). A handler + # runs in its connection's own task, so this is that task's value. + token = _ANSWERING.set(_connection) + try: + reply = await self._dispatch_message(raw, _connection) + finally: + _ANSWERING.reset(token) if reply is None: return None # TD-5015: a viewer's session_list carries every computer's rows; @@ -3226,8 +3385,15 @@ async def _dispatch_message(self, raw: str, _connection: Any) -> str | None: "session_not_found", f"Session {msg.session_id!r} not found", ) - # The folder, not the anchor's worktree (TD-5126). - return await self._start_session(anchor.project_path) + if msg.preset is not None and msg.preset not in self.config.presets: + return build_error( + "unknown_preset", + f"Unknown preset {msg.preset!r}; declared: " + f"{', '.join(sorted(self.config.presets))}", + ) + # The folder, not the anchor's worktree (TD-5126). TD-5235: on + # the entry asked for, with its engine, when one is named. + return await self._start_session(anchor.project_path, preset=msg.preset) if isinstance(msg, UserMessage): # Every refusal names in_reply_to, so the window can tell a @@ -3587,6 +3753,47 @@ async def send(reply: str) -> None: if isinstance(msg, SetSessionPreset): return await self._handle_set_session_preset(msg) + if isinstance(msg, SwitchPreset): + return await self._handle_switch_preset(msg) + if isinstance(msg, GetPresetRoles): + # TD-5241: the roles of the entry Settings → Model is editing, + # in the shape setup_state gives the active one. + entry = self.config.presets.get(msg.name) + if entry is None: + return build_error( + "unknown_preset", + f"Unknown preset {msg.name!r}; declared: " + f"{', '.join(sorted(self.config.presets))}", + ) + own = entry.tier_map() + return PresetRoles( + preset=msg.name, + tier_slugs=dict(self._slug_snapshot.get(msg.name, {})), + tier_credentials={name: tier.credential for name, tier in own.items()}, + tier_loopback={name: is_loopback_url(tier.base_url) for name, tier in own.items()}, + tier_cards=self._tier_cards( + {str(name): tier for name, tier in own.items()}, msg.name + ), + ).model_dump_json() + if isinstance(msg, CheckPresets): + # TD-5240: keys by presence, this computer's servers by a keyless + # ask, the Grok CLI by its path. Nothing leaves the machine. + _grok_path, grok_found = await asyncio.to_thread(_probe_grok, self.config.engine.binary) + # TD-5242: asking this computer's model servers is for the person + # at it (TD-5180's rule for the port scan). A phone still learns + # about keys and the Grok CLI; a server it cannot ask reads ready. + from_afar = self.ws_server.peer_view(_connection).remote + + async def _not_asked(_urls: list[str]) -> dict[str, bool]: + return {} + + readiness = await check_presets( + self.config, + key_stored=self._credential_is_stored, + answers=_not_asked if from_afar else loopback_answers, + grok_found=grok_found, + ) + return PresetReadiness(rows=readiness).model_dump_json() if isinstance(msg, TrustWorkspaceConfig): return await self._handle_trust_workspace_config(msg) @@ -3734,6 +3941,18 @@ async def send(reply: str) -> None: "class_c_not_always_allowable", "External imports can never be always-allowed", ) + # TD-5281: a child's card offers no Always allow, so this is an + # older window or a hand-made frame. The person did say yes to + # this call, so it runs once; no rule is saved for the workspace. + if pending.from_worker: + found.resolve_approval(msg.tool_call_id, True) + return build_error( + "not_always_allowable", + "Approved once. A worker's request can't be always-allowed: the rule " + "would cover the whole workspace. Add one in Settings → Policy if you " + "want it.", + session_id=found.id, + ) # The narrowest rule, never a blanket grant; None when the # call is a class-C decision (TD-803 criterion 4), or a shell # command chained from several commands (TD-5101). @@ -3887,6 +4106,11 @@ async def send(reply: str) -> None: if msg.enabled: for session in await self.session_registry.list_sessions(): session.resolve_skippable_approvals() + # TD-5283: from a phone this is a phone permission in use. + await self._note_phone_change( + APPROVAL_SWITCHES, + "turned approval prompts off for every session on this computer.", + ) return (await self._setup_state_event()).model_dump_json() if isinstance(msg, SetLoadGlobalMemory): @@ -3932,6 +4156,9 @@ async def send(reply: str) -> None: if isinstance(msg, SetRemoteAttach): return await self._handle_set_remote_attach(msg) + + if isinstance(msg, SetPhonePermission): + return await self._handle_set_phone_permission(msg) if isinstance(msg, RotateRemoteToken): return await self._handle_rotate_remote_token() if isinstance(msg, StartPairing | CancelPairing | RemoveRemoteDevice): @@ -4422,8 +4649,12 @@ async def send(reply: str) -> None: "active preset changed", extra={"extra_fields": {"preset": msg.name}}, ) + await self._tell_other_windows_the_catalog() return (await self._setup_state_event()).model_dump_json() + if isinstance(msg, DuplicatePreset | SetPresetLabel | DeletePreset | AddGrokPreset): + return await self._handle_catalog_edit(msg) + if isinstance(msg, SetTierSlug): try: save_tier_slug(msg.preset, msg.tier, msg.slug, self.config_path) @@ -4501,6 +4732,11 @@ async def send(reply: str) -> None: return build_error("bad_request", f"Saved, but the config no longer loads: {e}") await self._mcp.reload(self.config.mcp) log.info("mcp server saved", extra={"extra_fields": {"server_id": msg.id}}) + await self._note_phone_change( + MCP_SERVERS, + server_saved_summary(msg.id, msg.command, msg.url, existed=prior is not None), + server_id=msg.id, + ) return (await self._setup_state_event()).model_dump_json() if isinstance(msg, DeleteMcpServer): @@ -4518,11 +4754,20 @@ async def send(reply: str) -> None: return build_error("bad_request", f"Saved, but the config no longer loads: {e}") await self._mcp.reload(self.config.mcp) log.info("mcp server removed", extra={"extra_fields": {"server_id": msg.id}}) + # Whoever removed it, there is nothing left to attribute. + self.phone_store.forget_origin(msg.id) + await self._note_phone_change(MCP_SERVERS, f"removed MCP server '{msg.id}'.") return (await self._setup_state_event()).model_dump_json() if isinstance(msg, SetMcpSecret | DeleteMcpSecret): return await self._handle_mcp_secret(msg) + if isinstance(msg, PreviewMcpImport): + return self._preview_mcp_import(msg).model_dump_json() + + if isinstance(msg, ImportMcpServers): + return await self._handle_import_mcp_servers(msg) + # ── Diagnostics (TD-1104 doctor) ───────────────────────────── if isinstance(msg, RunDiagnostics): return (await self._diagnostics_report()).model_dump_json() @@ -4657,7 +4902,8 @@ async def _revive_session( level, ultracode = read_effort_record(record.effort if record is not None else None) if sess.router is None: sess.router = TierRouter() - session_cfg = self.config.session + # A row with no stored effort starts where its model says (TD-5234). + session_cfg = _session_model_config(sess, self.config).session sess.router.set_effort(level if level is not None else session_cfg.default_effort) sess.router.set_ultracode(ultracode if level is not None else session_cfg.default_ultracode) sess.loaded_mcp_tools[:] = await asyncio.to_thread( @@ -4864,10 +5110,20 @@ async def _run_distill( """Park a proposal and emit ``memory_proposal`` when memory changed.""" if session.turn_in_flight or completed_turn_count(session) < 1: return + native = self._distill_entry(session) + if native is None: + log.info( + "distill skipped; no model to run it on", + extra={"extra_fields": {"session_id": session.id}}, + ) + return + config = self.config + if native != config.active_preset: + config = config.model_copy(update={"active_preset": native}) try: - provider = await self._ensure_provider() + provider = await self._ensure_provider(config) distill_provider = provider if flight is None else SignalingProvider(provider, flight) - emitted = await distill_if_due(session, distill_provider, self.config) + emitted = await distill_if_due(session, distill_provider, config) except Exception as exc: log.warning( "distill skipped; provider failed", @@ -4879,6 +5135,27 @@ async def _run_distill( self._pending_memory[session.id] = emitted await session.event_log.add(emitted.event) + def _distill_entry(self, session: Session) -> str | None: + """The catalog entry whose model distills *session*, or None. + + A chat on TST Desk's own loop is distilled by the entry it is on now + (TD-5259): its conversation goes to the model server it was already + going to, so a chat kept on a local model is not sent to a hosted + one for this, nor the other way round. "Now" is the entry at the + moment of distilling, not the one most turns ran on: the person's + latest choice of where this chat goes is the one to respect. + + A chat on the Grok engine has no model of its own here, and a chat + whose entry has since been removed has none either. Both keep the + TD-5239 rule: the active entry when it names a model, else the + first that does; with none there is nothing to run it on. + """ + if session.engine != "grok": + entry = self.config.presets.get(session.preset) + if entry is not None and entry.has_tiers: + return session.preset + return self.config.native_default() + async def _distill_live_sessions(self, flight: ProviderFlight | None = None) -> None: """Graceful quit: distill every live idle session that had a turn.""" sessions = await self.session_registry.list_sessions() @@ -4923,8 +5200,10 @@ async def _save_queue(messages: list[QueuedUserMessage]) -> None: router = sess.router if router is None: router = TierRouter() - router.set_effort(self.config.session.default_effort) - router.set_ultracode(self.config.session.default_ultracode) + # The chat's own snapshot: its model may set what a chat starts on (TD-5234). + starts_with = _session_model_config(sess, self.config).session + router.set_effort(starts_with.default_effort) + router.set_ultracode(starts_with.default_ultracode) sess.router = router # TD-5113: every config load below goes through the trust gate. @@ -4972,6 +5251,9 @@ async def get_provider(tier_cfg: TierConfig | None = None) -> ProviderLike: sent_files = SentFiles(self._artifacts, self.config.session) # send_file re-checks with the guard that judged its paths (TD-5143). sent_files.bind_guard(lambda: tool_dispatcher.path_guard) + # TD-5254: show_artifact, with the same wall and the live caps. + shown_artifacts = ShownArtifacts(self._artifacts, lambda: self.config.session) + shown_artifacts.bind_guard(lambda: tool_dispatcher.path_guard) register_builtin_handlers( tool_dispatcher, # Read per call: a live move (TD-1715) reloads boundary_config. @@ -4980,6 +5262,7 @@ async def get_provider(tier_cfg: TierConfig | None = None) -> ProviderLike: browser_driver=self.browser_driver, grounding_client=GroundingClient.from_config(self.config.computer_use.grounding), sent_files=sent_files, + shown_artifacts=shown_artifacts, session_recall=SessionRecall(self._session_index, self._session_store.records), ax_driver=self.ax_driver, ) @@ -5083,6 +5366,15 @@ async def _start_session( the session uses the window's current choice. Neither is written to Settings. """ + # TD-5239: the entry names the engine unless the caller did. Checked + # before the session exists, so a refusal leaves nothing behind. + chosen = self._resolved_preset(self.config.active_preset if preset is None else preset) + chosen_engine = self.config.preset_engine(chosen) if engine is None else engine + # A caller's engine wins over the entry's (a scheduled pin, TD-3812), + # except that TST Desk's own loop cannot run an entry with no model. + refusal = no_model_refusal(self.config, chosen, chosen_engine) + if refusal is not None: + return build_error("engine_mismatch", refusal) # Memory templates (TD-2101): plant .tst/memory/ on every session # start so an already-opened workspace still gets the files. Never # overwrites; config scaffold stays OpenWorkspace-only. @@ -5103,8 +5395,8 @@ async def _start_session( await asyncio.to_thread(scaffold_workspace_memory, sess.workspace_path) # A scheduled pin names the loop for this session only. The window's # active preset and engine stay where Settings left them. - sess.engine = self.config.engine.kind if engine is None else engine - self._bind_session_preset(sess, self.config.active_preset if preset is None else preset) + sess.engine = chosen_engine + self._bind_session_preset(sess, chosen) await self._session_store.upsert( sess.id, workspace_path, @@ -5161,7 +5453,8 @@ def _bind_session_preset(self, sess: Session, name: str) -> str: """ chosen = self._resolved_preset(name) sess.preset = chosen - sess.config = self.config.model_copy(update={"active_preset": chosen}) + # TD-5234: the entry's own context settings ride on the snapshot. + sess.config = effective_config(self.config, chosen) # /compact between turns uses the new preset's worker too. if sess.compaction_summarizer is not None: sess.compaction_summarizer.rebind(sess.config.compaction, sess.config.tier("worker")) @@ -5268,22 +5561,125 @@ async def _handle_set_session_preset(self, msg: SetSessionPreset) -> str: "unknown_preset", f"Unknown preset {msg.name!r}; declared: {', '.join(sorted(self.config.presets))}", ) - self._bind_session_preset(found, msg.name) + # TD-5239: a chat keeps the engine it started on. + refusal = engine_refusal(self.config, msg.name, found.engine) + if refusal is not None: + return build_error("engine_mismatch", refusal, session_id=found.id) + await self._apply_session_preset(found, msg.name) + return await self._handle_list_sessions() + + async def _apply_session_preset(self, found: Session, name: str) -> None: + """Point a live, idle-or-between-turns chat at entry *name* and say so. + + The one path a chat's model changes by: ``set_session_preset``, + ``switch_preset``, and a pending switch when its turn completes. + """ + assert found.router is not None + self._bind_session_preset(found, name) + # TD-5234: one click sets everything the model defines. An effort or + # Ultracode default the entry itself names applies to this chat now; + # one it leaves to the global setting does not touch the chat's own. + chosen = preset_overrides(self.config, found.preset) + starts_with = chosen.session if chosen is not None else None + if starts_with is not None: + if starts_with.default_effort is not None: + found.router.set_effort(starts_with.default_effort) + if starts_with.default_ultracode is not None: + found.router.set_ultracode(starts_with.default_ultracode) await self._session_store.set_preset(found.id, found.preset) await found.event_log.add( tier_state_event(found, _session_model_config(found, self.config)) ) log.info( "session preset set", - extra={ - "extra_fields": { - "session_id": found.id, - "preset": found.preset, - } - }, + extra={"extra_fields": {"session_id": found.id, "preset": found.preset}}, ) + + async def _handle_switch_preset(self, msg: SwitchPreset) -> str: + """This chat, the model new chats use, or both, in one step (TD-5235).""" + if msg.session_id is None and not msg.make_default: + return build_error( + "bad_request", "Nothing to switch: name a session, make_default, or both" + ) + if msg.name not in self.config.presets: + return build_error( + "unknown_preset", + f"Unknown preset {msg.name!r}; declared: {', '.join(sorted(self.config.presets))}", + ) + found: Session | None = None + if msg.session_id is not None: + found = self.session_registry.get(msg.session_id) + if found is None: + return build_error("session_not_found", f"Session {msg.session_id!r} not found") + # A chat with no loop never runs, so no engine can clash with it. + if found.router is not None: + refusal = engine_refusal(self.config, msg.name, found.engine) + if refusal is not None: + return build_error("engine_mismatch", refusal, session_id=found.id) + # Everything is checked. The default goes first: it is the one step + # that can still fail (a config that will not save), and then the + # chat has not been touched either. + if msg.make_default and msg.name != self.config.active_preset: + try: + save_active_preset(msg.name, self.config_path) + except ConfigError as e: + return build_error("bad_request", str(e)) + self.config = self.config.model_copy(update={"active_preset": msg.name}) + self._slug_snapshot = _snapshot_slugs(self.config) + log.info("active preset changed", extra={"extra_fields": {"preset": msg.name}}) + if found is not None: + await self._switch_chat(found, msg.name) + if msg.make_default: + await self._tell_other_windows_the_catalog() + return (await self._setup_state_event()).model_dump_json() return await self._handle_list_sessions() + async def _switch_chat(self, found: Session, name: str) -> None: + """Switch one chat now, hold the switch for its turn's end, or record it.""" + if found.router is None: + # Restored with no conversation: there is no loop to rebind. + found.preset = name + await self._session_store.set_preset(found.id, name) + return + if found.turn_in_flight: + # Back to its own model cancels a switch that was waiting. + found.pending_preset = None if name == found.preset else name + await found.event_log.add( + tier_state_event(found, _session_model_config(found, self.config)) + ) + log.info( + "session preset pending", + extra={"extra_fields": {"session_id": found.id, "preset": name}}, + ) + return + found.pending_preset = None + if name != found.preset: + await self._apply_session_preset(found, name) + + async def _apply_pending_preset(self, session_id: str) -> None: + """A turn completed: bind the switch that was waiting for it (TD-5235). + + The event log awaits its subscribers before the loop takes the next + message, so the next turn, a queued one included, runs on the new + entry. An entry removed meanwhile, or one whose engine no longer + suits, is dropped, and the chip is told nothing is waiting. + """ + found = self.session_registry.get(session_id) + if found is None or found.pending_preset is None or found.router is None: + return + name, found.pending_preset = found.pending_preset, None + gone = name not in self.config.presets + if gone or engine_refusal(self.config, name, found.engine) is not None: + log.info( + "pending session preset dropped", + extra={"extra_fields": {"session_id": found.id, "preset": name, "gone": gone}}, + ) + await found.event_log.add( + tier_state_event(found, _session_model_config(found, self.config)) + ) + return + await self._apply_session_preset(found, name) + async def _handle_set_effort(self, msg: SetEffort) -> str | None: """Pick the session's reasoning effort and ack with ``tier_state``. @@ -5472,6 +5868,10 @@ async def _handle_pairing_verb( if isinstance(msg, StartPairing): return await self._handle_start_pairing() if isinstance(msg, RemoveRemoteDevice): + if msg.remove_added: + refused = await self._remove_servers_added_by(msg.device_id) + if refused is not None: + return refused await self._remove_remote_device(msg.device_id, connection) return None self.pairing.cancel() @@ -5498,6 +5898,137 @@ async def _remove_remote_device(self, device_id: str, connection: Any) -> None: await connection.send(scrub_wire_json(self.hub.finish(reply, connection))) await self.ws_server.close_device(device_id) + # ── phone permissions (TD-5279) ─────────────────────────────────── + + async def _handle_set_phone_permission(self, msg: SetPhonePermission) -> str: + """Switch what a paired phone may change. The peer policy let only loopback in.""" + try: + self.phone_store.set(msg.capability, msg.enabled, msg.expires_in_seconds) + except ValueError as e: + return build_error("bad_request", str(e), in_reply_to=msg.type) + except OSError as e: + return build_error( + "phone_permission_failed", + f"Couldn't save the phone permissions file: {e.strerror or e}", + in_reply_to=msg.type, + ) + log.info( + "phone permission set", + extra={ + "extra_fields": { + "capability": msg.capability, + "enabled": msg.enabled, + "expires_in_seconds": msg.expires_in_seconds, + } + }, + ) + self._arm_phone_permission_expiry() + # A phone's pages follow the switch, and so do this computer's other windows. + await self._tell_other_windows_the_catalog() + return (await self._setup_state_event()).model_dump_json() + + def _arm_phone_permission_expiry(self) -> None: + """Tell every window when the soonest timed grant runs out. + + The policy reads the clock on every message, so a grant stops + working at its expiry with or without this; this only keeps what + the windows show true. + """ + if self._phone_expiry_task is not None: + self._phone_expiry_task.cancel() + self._phone_expiry_task = None + until = self.phone_store.next_expiry() + if until is None: + return + + async def _wait() -> None: + # A task copies the context it was made in: without this the + # window that granted would be left out as "already answered". + _ANSWERING.set(None) + await asyncio.sleep(max(0.0, (until - utc_now()).total_seconds()) + 0.5) + self._phone_expiry_task = None + log.info("phone permission expired") + await self._tell_other_windows_the_catalog() + self._arm_phone_permission_expiry() + + self._phone_expiry_task = asyncio.create_task(_wait(), name="phone-permission-expiry") + self._tasks.append(self._phone_expiry_task) + + def _servers_added_by(self, device_id: str) -> list[str]: + """The MCP servers a device added or changed that are still listed.""" + return [ + sid + for sid in self.phone_store.servers_from(device_id) + if sid in self.config.mcp.servers + ] + + def _phone_actor(self) -> tuple[str, str] | None: + """``(device id, name)`` when the message being answered came from a paired phone.""" + connection: Any = _ANSWERING.get() + if connection is None or not self.ws_server.peer_view(connection).phone: + return None + device_id = self.ws_server.device_of(connection) + if device_id is None: + return None + for device in self.remote_devices.summaries(): + if device.id == device_id: + return device_id, device.name + return device_id, "A paired phone" + + async def _note_phone_change( + self, capability: str, summary: str, *, server_id: str | None = None + ) -> None: + """A phone used a grant: record where it came from and tell the computer. + + Nothing happens for a change made at the computer. The audit + database keys every record to a session and this belongs to none, + so the trail is the daemon log, with the device named. + """ + actor = self._phone_actor() + if actor is None: + return + device_id, device_name = actor + if server_id is not None: + self.phone_store.record_origin(server_id, device_id, device_name) + log.warning( + "phone used a phone permission", + extra={ + "extra_fields": { + "capability": capability, + "device_id": device_id, + "device_name": device_name, + "summary": summary, + } + }, + ) + event = PhoneChange(device_name=device_name, capability=capability, summary=summary) + await self.ws_server.broadcast(scrub_wire_json(event.model_dump_json()), loopback_only=True) + # The computer's open Settings page shows the change without a reopen. + await self._tell_other_windows_the_catalog() + + async def _remove_servers_added_by(self, device_id: str) -> str | None: + """The unpair review's "also remove these". An error frame when a removal fails.""" + for server_id in self._servers_added_by(device_id): + removed = self.config.mcp.servers.get(server_id) + try: + delete_mcp_server_entry(server_id, self.config_path) + except ConfigError as e: + return build_error("bad_request", str(e), in_reply_to="remove_remote_device") + if removed is not None: + await forget_server_secrets(server_id, removed) + self._invalidate_stored_probe() + self.phone_store.forget_origin(server_id) + log.info( + "mcp server removed with its phone", + extra={"extra_fields": {"server_id": server_id, "device_id": device_id}}, + ) + try: + self._reload_user_config() + except ConfigError as e: + return build_error("bad_request", f"Saved, but the config no longer loads: {e}") + await self._mcp.reload(self.config.mcp) + return None + async def _handle_start_pairing(self) -> str: """Mint a code; turn remote attach on first, as the toggle would. @@ -5544,6 +6075,24 @@ async def _on_device_presence(self, _device_id: str) -> None: state = await self._setup_state_event() await self.ws_server.broadcast(scrub_wire_json(state.model_dump_json()), loopback_only=True) + async def _tell_other_windows_the_catalog(self) -> None: + """A model was added, changed, removed or made the default (TD-5236). + + ``setup_state`` is otherwise only the reply to whoever asked, so a + second window kept showing the old list and the old default until + it reopened Settings. A paired phone shows the same picker, so it + is told too (TD-5242), with what a remote peer's ``setup_state`` + always leaves out left out. Not the connection being answered: its + reply already says it. + """ + state = scrub_wire_json((await self._setup_state_event()).model_dump_json()) + await self.ws_server.broadcast( + state, + loopback_only=True, + exclude=_ANSWERING.get(), + remote_payload=scrub_setup_state(state), + ) + def _remember_remote_port(self) -> None: """Save the bound port so the next start reuses it (TD-5005). @@ -6293,6 +6842,83 @@ async def _handle_mcp_secret(self, msg: SetMcpSecret | DeleteMcpSecret) -> str: } }, ) + await self._note_phone_change( + MCP_SERVERS, + value_summary(msg.server_id, msg.target, msg.name, stored=verb == "store"), + server_id=msg.server_id, + ) + return (await self._setup_state_event()).model_dump_json() + + def _preview_mcp_import(self, msg: PreviewMcpImport) -> McpImportPreview: + """What importing the pasted JSON would do (TD-5253). Saves nothing.""" + try: + found = parse_import(msg.text) + except McpImportError as e: + return McpImportPreview(error=str(e)) + return McpImportPreview( + servers=[ + McpImportServer( + source_name=server.source_name, + id=server.id, + transport=server.transport, + command=list(server.command), + url=server.url, + off_box=server.off_box, + exists=server.id in self.config.mcp.servers, + values=[ + McpImportValue( + name=value.name, + target=value.target, + disposition=value.disposition, + reason=value.reason, + ) + for value in server.values + ], + not_imported=list(server.not_imported), + notes=list(server.notes), + refused=server.refused, + ) + for server in found + ] + ) + + async def _handle_import_mcp_servers(self, msg: ImportMcpServers) -> str: + """Save the chosen servers: keychain first, then config, then start them.""" + choices = [ + Choice( + source_name=item.source_name, + id=item.id.strip(), + allow_remote=item.allow_remote, + inputs=item.inputs, + ) + for item in msg.servers + ] + try: + saved = await apply_import(msg.text, choices, self.config.mcp, self.config_path) + except KeychainLockedError as e: + return build_error("keychain_locked", str(e), in_reply_to=msg.type) + except (KeychainError, NotImplementedError) as e: + return build_error( + "key_store_failed", + f"Could not store an MCP server value: {e}", + in_reply_to=msg.type, + ) + except (ConfigError, ValidationError) as e: + return build_error("bad_request", str(e), in_reply_to=msg.type) + finally: + self._invalidate_stored_probe() + try: + self._reload_user_config() + except ConfigError as e: + return build_error("bad_request", f"Saved, but the config no longer loads: {e}") + await self._mcp.reload(self.config.mcp) + for server_id, server in saved: + log.info("mcp server imported", extra={"extra_fields": {"server_id": server_id}}) + await self._note_phone_change( + MCP_SERVERS, + server_saved_summary(server_id, server.command, server.url, existed=False), + server_id=server_id, + ) return (await self._setup_state_event()).model_dump_json() async def _handle_set_email_notify(self, msg: SetEmailNotify) -> str: @@ -6636,7 +7262,7 @@ async def _notify(message: str) -> None: await notify_autonomy_stop(self.config, message) sess.autonomy_notify = _notify - sess.engine = self.config.engine.kind + sess.engine = self.config.preset_engine(self.config.active_preset) self._bind_session_preset(sess, self.config.active_preset) await self._session_store.upsert( sess.id, str(workspace), sess.state, engine=sess.engine, preset=sess.preset diff --git a/core/tstd/endpoint_gate.py b/core/tstd/endpoint_gate.py new file mode 100644 index 00000000..6d0adfb1 --- /dev/null +++ b/core/tstd/endpoint_gate.py @@ -0,0 +1,111 @@ +"""How many model calls one server answers at a time (TD-5269). + +Nothing bounded model calls across the daemon. A ``workflow`` call limits +its own children, and that was all: two chats, a scheduled run and a +workflow could all ask one local server at once, and a single GPU then +serves all of them slowly or fails some. Here each server gets one count, +shared by every caller in the daemon. + +A gate is keyed by the server, not the model or the key: scheme, host and +port, with every spelling of this computer folded to one host. Two paths +on one port are one server, and a GPU does not care which key asked. + +No limit is set unless config sets one, so nothing changes on upgrade. A +slot is held for one HTTP exchange only (see ``ProviderClient``): never +across a retry's wait, and never while a tool runs, so a call that holds a +slot is never waiting on another call for the same server. +""" + +from __future__ import annotations + +import asyncio +from urllib.parse import urlsplit + +from pydantic import BaseModel, ConfigDict, Field + +from .net import is_loopback_url + +_LOOPBACK = "this-computer" +_DEFAULT_PORTS = {"http": 80, "https": 443} + + +class EndpointLimit(BaseModel): + """One server's own limit, in place of the default for its kind.""" + + model_config = ConfigDict(extra="forbid") + + url: str = Field(min_length=1) + max_concurrent: int = Field(ge=1, le=64) + + +class ModelConcurrencyConfig(BaseModel): + """The ``model_concurrency`` section of ``config.yaml`` (TD-5269). + + ``loopback_default`` is how many calls a server on this computer + answers at once; ``remote_default`` the same for any other. ``null`` + is no limit, which is how it was before the key existed. ``endpoints`` + names a server by URL and gives it a number of its own. + """ + + model_config = ConfigDict(extra="forbid") + + loopback_default: int | None = Field(default=None, ge=1, le=64) + remote_default: int | None = Field(default=None, ge=1, le=64) + endpoints: list[EndpointLimit] = Field(default_factory=list) + + +def endpoint_key(url: str) -> str | None: + """The server *url* names: its scheme, host and port. None when it names none. + + Every loopback spelling (``localhost``, ``127.0.0.1``, ``::1``) is one + host. The path and any credential in the URL are left out. + """ + try: + parts = urlsplit(url.strip()) + host = parts.hostname + port = parts.port + except ValueError: + return None + if parts.scheme not in _DEFAULT_PORTS or not host: + return None + name = _LOOPBACK if is_loopback_url(url) else host.lower() + return f"{parts.scheme}://{name}:{port or _DEFAULT_PORTS[parts.scheme]}" + + +class EndpointGates: + """One count per server, shared by every client in the daemon.""" + + def __init__(self, config: ModelConcurrencyConfig | None = None) -> None: + self._config = config or ModelConcurrencyConfig() + # key -> (limit, the semaphore every client for that server shares) + self._gates: dict[str, tuple[int, asyncio.Semaphore]] = {} + + def configure(self, config: ModelConcurrencyConfig) -> None: + """Take a reloaded config. A server whose number did not change keeps + its gate, calls in flight included; one whose number changed gets a + new gate the next time a client is built for it.""" + self._config = config + + def limit_for(self, base_url: str) -> int | None: + """The limit config gives the server at *base_url*, or None for none.""" + key = endpoint_key(base_url) + if key is None: + return None + for entry in self._config.endpoints: + if endpoint_key(entry.url) == key: + return entry.max_concurrent + if is_loopback_url(base_url): + return self._config.loopback_default + return self._config.remote_default + + def for_url(self, base_url: str) -> asyncio.Semaphore | None: + """The gate for the server at *base_url*, or None when it has no limit.""" + limit = self.limit_for(base_url) + key = endpoint_key(base_url) + if limit is None or key is None: + return None + held = self._gates.get(key) + if held is None or held[0] != limit: + held = (limit, asyncio.Semaphore(limit)) + self._gates[key] = held + return held[1] diff --git a/core/tstd/hub.py b/core/tstd/hub.py index ffa72935..43f93247 100644 --- a/core/tstd/hub.py +++ b/core/tstd/hub.py @@ -21,7 +21,7 @@ import contextlib import json import re -from collections.abc import Awaitable, Callable +from collections.abc import Awaitable, Callable, Collection from pathlib import Path from typing import Any @@ -88,8 +88,12 @@ def __init__( *, policy: DialPolicy | None = None, timings: Timings = DEFAULT_TIMINGS, + phone_grants: Callable[[], Collection[str]] | None = None, ) -> None: self._ws = ws_server + # TD-5279: the phone permissions switched on right now. Without a + # source there are none, so every opt-in verb is refused. + self._phone_grants = phone_grants self._own_session_list = own_session_list self.registry = MachineRegistry(data_dir) self.registry.load() @@ -127,7 +131,8 @@ async def intercept(self, msg: ClientMessage, connection: Any) -> object: peer = self._ws.peer_view(connection) sid = message_session_id(msg) bridged = sid is not None and is_bridged(sid) - refusal = check(msg, peer, bridged=bridged) + grants = self._phone_grants() if self._phone_grants is not None else () + refusal = check(msg, peer, bridged=bridged, grants=grants) if refusal is not None: return refusal if msg.type in MACHINE_VERBS: diff --git a/core/tstd/key_hosts.py b/core/tstd/key_hosts.py index d74f396e..ecdb8d1b 100644 --- a/core/tstd/key_hosts.py +++ b/core/tstd/key_hosts.py @@ -22,7 +22,6 @@ from .config import ( CREDENTIAL_ID_RE, RESERVED_CREDENTIAL_IDS, - TIER_NAMES, ModelConfig, TierConfig, apply_credential_host, @@ -66,8 +65,7 @@ def unknown_key(config: ModelConfig, credential_id: str) -> str | None: def every_tier(config: ModelConfig) -> Iterator[TierConfig]: """Every tier of every preset, then each one's backups (TD-5120).""" for preset in config.presets.values(): - for name in TIER_NAMES: - tier: TierConfig = getattr(preset, name) + for tier in preset.tier_map().values(): yield tier yield from tier.fallbacks diff --git a/core/tstd/local_servers.py b/core/tstd/local_servers.py index 0063a749..db9d88a9 100644 --- a/core/tstd/local_servers.py +++ b/core/tstd/local_servers.py @@ -75,3 +75,39 @@ async def scan_local_servers( if reply.error is None and reply.models ] return LocalServers(servers=found, checked=[url for _name, url in wanted]) + + +async def loopback_answers( + urls: Sequence[str], *, client: httpx.AsyncClient | None = None +) -> dict[str, bool]: + """Whether a server answers at each loopback URL (TD-5240). + + The picker's readiness check for a model served on this computer. The + same three rules as the scan above: asked only when the person opens + the picker, loopback only with no key (an off-box URL is not asked and + reads as answering: it is not this function's to judge), and nothing + written. Any HTTP reply counts, a refused connection or a timeout does + not: this says a server is there, not that the model will run. + """ + wanted = list(dict.fromkeys(url.strip().rstrip("/") for url in urls)) + local = [url for url in wanted if is_loopback_url(url)] + found = {url: True for url in wanted} + if not local: + return found + owned = client is None + http = client or httpx.AsyncClient(timeout=SCAN_TIMEOUT, transport=direct_transport(local[0])) + + async def _ask(url: str) -> bool: + try: + await http.get(f"{url}/models", headers={"Accept": "application/json"}) + except httpx.HTTPError: + return False + return True + + try: + answers = await asyncio.gather(*(_ask(url) for url in local)) + finally: + if owned: + await http.aclose() + found.update(dict(zip(local, answers, strict=True))) + return found diff --git a/core/tstd/local_worker.py b/core/tstd/local_worker.py index c46c5d64..e9b45e5e 100644 --- a/core/tstd/local_worker.py +++ b/core/tstd/local_worker.py @@ -157,6 +157,7 @@ def tier_state_event(session: Session, config: ModelConfig) -> TierState: override=session.router.override, model_slugs=slugs, preset=config.active_preset, + pending_preset=session.pending_preset, hosts=hosts, plan=session.router.plan_mode, vision=tier_cfg.vision, diff --git a/core/tstd/mcp/stdio.py b/core/tstd/mcp/stdio.py index 31b8f1e1..277cf3aa 100644 --- a/core/tstd/mcp/stdio.py +++ b/core/tstd/mcp/stdio.py @@ -5,11 +5,11 @@ import asyncio import contextlib import json -import os from collections.abc import Mapping from typing import Any from ..logging import get_logger +from ..tools.shell import sanitized_env from .calls import ( CallTimedOut, ToolsChanged, @@ -93,7 +93,12 @@ async def start(self) -> None: stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, limit=STREAM_LIMIT, - env={**os.environ, **self._env} if self._env else None, + # TD-5267: never the daemon's whole environment. A server + # is somebody else's program; it gets the ordinary + # variables it needs to start and none that look like a + # key, a token, a password or a credential in a URL, plus + # what this server was given by name. + env={**sanitized_env(), **self._env}, ) except OSError as e: raise McpError(f"failed to start stdio MCP: {e}") from e diff --git a/core/tstd/mcp_import.py b/core/tstd/mcp_import.py new file mode 100644 index 00000000..b16ce216 --- /dev/null +++ b/core/tstd/mcp_import.py @@ -0,0 +1,312 @@ +"""Read MCP servers out of pasted JSON (TD-5253). Pure: no I/O, nothing run. + +A server's README usually gives a block of JSON for some other client: +``{"mcpServers": {"name": {"command": "npx", "args": [...], "env": {...}}}}``. +This turns such a block into what Settings would have made by hand: an id, +an argv or a URL, and for each value where it will live. A token goes to +the keychain; ``config.yaml`` gets only a reference. + +The text is hostile input. It is size- and count-capped, parsed as data +and never evaluated, and no error or preview here ever quotes a value +from ``env`` or ``headers``. Anything that would run an extra command or +read a file (``headersHelper``, ``envFile``) or that TST Desk cannot do +(OAuth, SSE, WebSocket) is refused or left out, and said so. +""" + +from __future__ import annotations + +import json +import re +from dataclasses import dataclass, field +from typing import Any, Literal + +from .mcp_values import check_name, check_secret +from .net import is_loopback_url + +MAX_TEXT_BYTES = 256 * 1024 +MAX_SERVERS = 50 +MAX_ARGS = 200 + +Disposition = Literal["keychain", "literal", "needs_input"] + +_ID_RE = re.compile(r"^[a-z][a-z0-9-]{0,31}$") +_PLACEHOLDER = "${" +_SECRET_NAME = re.compile(r"TOKEN|KEY|SECRET|PASSWORD|PASSWD|AUTH|CREDENTIAL|COOKIE", re.IGNORECASE) +# Keys that make a client run something else or sign in; the server is not imported. +_REFUSED_KEYS = { + "headersHelper": "it names a command to run for its headers", + "oauth": "it needs an OAuth sign-in, which TST Desk does not do for MCP servers", + "auth": "it needs an OAuth sign-in, which TST Desk does not do for MCP servers", + "oauthClientId": "it needs an OAuth sign-in, which TST Desk does not do for MCP servers", +} +_KNOWN_KEYS = {"command", "args", "env", "url", "headers", "type", *_REFUSED_KEYS} +_HTTP_TYPES = {"http", "streamable-http", "streamablehttp"} + + +class McpImportError(ValueError): + """The pasted text cannot be read at all. The message never quotes it.""" + + +@dataclass(frozen=True) +class ImportValue: + """One env var or header of a server to import. ``value`` never leaves the daemon.""" + + name: str + target: Literal["env", "header"] + disposition: Disposition + reason: str = "" + value: str = field(default="", repr=False) + + +@dataclass(frozen=True) +class ImportServer: + """One server read from the text.""" + + source_name: str + id: str + transport: Literal["stdio", "http"] = "stdio" + command: tuple[str, ...] = () + url: str = "" + off_box: bool = False + values: tuple[ImportValue, ...] = () + not_imported: tuple[str, ...] = () + notes: tuple[str, ...] = () + #: Why this server cannot be imported; empty when it can. + refused: str = "" + + +def strip_json_comments(text: str) -> str: + """*text* without ``//`` and ``/* */`` comments and trailing commas. + + Editors' config files allow both and READMEs copy them. Strings are + left alone, so a ``//`` inside a URL survives. + """ + out: list[str] = [] + i, n = 0, len(text) + in_string = False + while i < n: + ch = text[i] + if in_string: + out.append(ch) + if ch == "\\" and i + 1 < n: + out.append(text[i + 1]) + i += 2 + continue + if ch == '"': + in_string = False + i += 1 + continue + if ch == '"': + in_string = True + out.append(ch) + i += 1 + elif text.startswith("//", i): + end = text.find("\n", i) + i = n if end == -1 else end + elif text.startswith("/*", i): + end = text.find("*/", i + 2) + i = n if end == -1 else end + 2 + elif ch == ",": + j = i + 1 + while j < n and (text[j] in " \t\r\n"): + j += 1 + if j < n and text[j] in "}]": + i += 1 # a trailing comma: drop it + else: + out.append(ch) + i += 1 + else: + out.append(ch) + i += 1 + return "".join(out) + + +def slug_id(name: str) -> str: + """A server id from whatever the snippet called it: ``My Server_1`` → ``my-server-1``.""" + slug = re.sub(r"[^a-z0-9]+", "-", name.lower()).strip("-") + if not slug or not slug[0].isalpha(): + slug = f"mcp-{slug}".rstrip("-") + return slug[:32].rstrip("-") or "mcp" + + +def valid_id(server_id: str) -> bool: + return bool(_ID_RE.match(server_id)) + + +def parse_import(text: str) -> list[ImportServer]: + """Every server in *text*. McpImportError when it is not readable JSON of a known shape.""" + if len(text.encode("utf-8", errors="replace")) > MAX_TEXT_BYTES: + raise McpImportError(f"That is more than {MAX_TEXT_BYTES // 1024} KiB of text.") + try: + data = json.loads(strip_json_comments(text)) + except json.JSONDecodeError as exc: + # The position, never the text: it may hold a token. + raise McpImportError(f"That is not JSON (line {exc.lineno}, column {exc.colno}).") from None + except RecursionError: + raise McpImportError("That JSON is nested too deeply.") from None + if not isinstance(data, dict): + raise McpImportError("Paste the JSON object that lists the servers.") + entries = _entries(data) + if not entries: + raise McpImportError("No servers found. Expected an mcpServers or servers block.") + if len(entries) > MAX_SERVERS: + raise McpImportError(f"That lists more than {MAX_SERVERS} servers.") + servers: list[ImportServer] = [] + used: set[str] = set() + for name, raw in entries: + server = _server(name, raw) + # Two names that slug alike must not land on one id. + base, n = server.id, 1 + candidate = base + while candidate in used: + n += 1 + suffix = f"-{n}" + candidate = f"{base[: 32 - len(suffix)]}{suffix}" + used.add(candidate) + if candidate != server.id: + server = _replace(server, id=candidate) + servers.append(server) + return servers + + +def _replace(server: ImportServer, **changes: Any) -> ImportServer: + return ImportServer(**{**server.__dict__, **changes}) + + +def _entries(data: dict[str, Any]) -> list[tuple[str, Any]]: + for wrapper in ("mcpServers", "servers"): + block = data.get(wrapper) + if isinstance(block, dict): + return [(str(name), raw) for name, raw in block.items()] + # One bare entry, as `claude mcp add-json` takes it. + if "command" in data or "url" in data: + return [("server", data)] + return [] + + +def _server(name: str, raw: Any) -> ImportServer: + base = ImportServer(source_name=name, id=slug_id(name)) + if not isinstance(raw, dict): + return _replace(base, refused="its entry is not a JSON object") + for key, why in _REFUSED_KEYS.items(): + if key in raw: + return _replace(base, refused=why) + kind = raw.get("type") + if kind is not None and not isinstance(kind, str): + return _replace(base, refused="its type is not a word") + kind = (kind or "").lower() + if kind == "sse": + return _replace( + base, refused="it uses the older SSE transport, which TST Desk does not speak" + ) + if kind in ("ws", "websocket"): + return _replace( + base, refused="it uses a WebSocket transport, which TST Desk does not speak" + ) + not_imported = tuple(sorted(str(k) for k in raw if k not in _KNOWN_KEYS)) + notes: list[str] = [] + if "sandbox" in raw or "sandboxEnabled" in raw: + notes.append("The snippet expects a sandbox. TST Desk will not put this server in one.") + url = raw.get("url") + if url is not None or kind in _HTTP_TYPES: + if not isinstance(url, str) or not url.strip(): + return _replace(base, refused="it has no url") + if kind and kind not in _HTTP_TYPES: + return _replace(base, refused=f"its type {kind!r} is not one TST Desk knows") + if _PLACEHOLDER in url: + return _replace( + base, refused="its url has a ${...} placeholder; put the real value in first" + ) + if not kind: + notes.append("No type was given; assumed Streamable HTTP.") + off_box = not is_loopback_url(url) + if off_box and not url.lower().startswith("https://"): + return _replace(base, refused="a server on another machine must use https") + values, refused = _values("header", raw.get("headers")) + if refused: + return _replace(base, refused=refused) + if raw.get("env"): + not_imported = (*not_imported, "env") + return _replace( + base, + transport="http", + url=url.strip(), + off_box=off_box, + values=values, + not_imported=not_imported, + notes=tuple(notes), + ) + if kind and kind != "stdio": + return _replace(base, refused=f"its type {kind!r} is not one TST Desk knows") + command = raw.get("command") + args = raw.get("args", []) + if not isinstance(command, str) or not command.strip(): + return _replace(base, refused="it has no command") + if not isinstance(args, list) or not all(isinstance(a, str | int | float) for a in args): + return _replace(base, refused="its args are not a list of words") + if any(isinstance(a, bool) for a in args) or len(args) > MAX_ARGS: + return _replace(base, refused="its args are not a list of words") + argv = (command.strip(), *(str(a) for a in args)) + if any(_PLACEHOLDER in token for token in argv): + return _replace( + base, refused="its command has a ${...} placeholder; put the real value in first" + ) + if " " in command.strip() and not args: + notes.append("The command has spaces and no args. It is run as one word, not split.") + if argv[0].rsplit("/", 1)[-1].lower() in { + "sh", + "bash", + "zsh", + "cmd", + "cmd.exe", + "powershell", + "pwsh", + }: + notes.append("This runs a shell. Read the whole command before importing it.") + values, refused = _values("env", raw.get("env")) + if refused: + return _replace(base, refused=refused) + if raw.get("headers"): + not_imported = (*not_imported, "headers") + return _replace( + base, command=argv, values=values, not_imported=not_imported, notes=tuple(notes) + ) + + +def _values(target: Literal["env", "header"], raw: Any) -> tuple[tuple[ImportValue, ...], str]: + """The values, or a reason the server is refused. Never quotes a value.""" + if raw is None: + return (), "" + if not isinstance(raw, dict): + return (), f"its {'env' if target == 'env' else 'headers'} is not a JSON object" + out: list[ImportValue] = [] + for key, value in raw.items(): + name = str(key) + try: + check_name(target, name) + except ValueError as exc: + return (), str(exc) + if value is None: + continue + if isinstance(value, bool) or not isinstance(value, str | int | float): + return (), f"the value of {name} is not text" + out.append(_classify(target, name, str(value))) + return tuple(out), "" + + +def _classify(target: Literal["env", "header"], name: str, value: str) -> ImportValue: + if _PLACEHOLDER in value: + return ImportValue( + name, target, "needs_input", "The snippet reads this from a variable. Type the value." + ) + problem = check_secret(value) + if problem is None: + return ImportValue(name, target, "keychain", value=value) + if target == "header" or _SECRET_NAME.search(name): + return ImportValue( + name, target, "needs_input", f"The value given {problem}. Type the real one." + ) + if any(ch in value for ch in ("\r", "\n", "\x00")): + return ImportValue(name, target, "needs_input", "The value must be one line.") + # Short, and not named like a secret: a port, a flag, a mode. + return ImportValue(name, target, "literal", value=value) diff --git a/core/tstd/mcp_import_apply.py b/core/tstd/mcp_import_apply.py new file mode 100644 index 00000000..febb50b0 --- /dev/null +++ b/core/tstd/mcp_import_apply.py @@ -0,0 +1,154 @@ +"""Save servers read from pasted JSON (TD-5253): keychain first, then config. + +The window's preview is not trusted. The same text is parsed again here +and every choice the person made (an id, a typed value, the off-box tick) +is checked against that parse. A server is saved whole or not at all: its +secrets are stored first, and if the config write then fails they are +deleted again, so no keychain item is left that nothing names. +""" + +from __future__ import annotations + +from collections.abc import Mapping +from dataclasses import dataclass +from pathlib import Path + +from . import keychain_mcp +from .config import ConfigError, McpConfig, McpServerConfig +from .config_write import save_mcp_server +from .logging import get_logger +from .mcp_import import ImportServer, McpImportError, parse_import, valid_id +from .mcp_values import check_secret, entry_for_name, keychain_ref + +log = get_logger("tstd.mcp_import") + + +@dataclass(frozen=True) +class Choice: + """What the person decided for one server in the preview.""" + + source_name: str + id: str + allow_remote: bool = False + inputs: Mapping[str, str] | None = None + + +@dataclass(frozen=True) +class _Ready: + server: ImportServer + id: str + spec: McpServerConfig + secrets: dict[str, str] # keychain entry -> value + + +def _ready(server: ImportServer, choice: Choice, mcp: McpConfig, taken: set[str]) -> _Ready: + where = f"{server.source_name!r}" + if server.refused: + raise ConfigError(f"{where} cannot be imported: {server.refused}.") + if not valid_id(choice.id): + raise ConfigError( + f"{where}: the id must be a lowercase slug, letters, digits and -, at most 32." + ) + if choice.id in mcp.servers or choice.id in taken: + raise ConfigError( + f"{where}: a server with the id {choice.id!r} is already listed. " + "Give this one another id, or remove that one first." + ) + if server.off_box and not choice.allow_remote: + raise ConfigError( + f"{where} is on another machine. Tick that you allow connecting to it to import it." + ) + inputs = dict(choice.inputs or {}) + env: dict[str, str] = {} + headers: dict[str, str] = {} + secrets: dict[str, str] = {} + for value in server.values: + mapping = env if value.target == "env" else headers + if value.disposition == "literal": + mapping[value.name] = value.value + continue + text = value.value + if value.disposition == "needs_input": + text = inputs.get(value.name, "") + if not text: + raise ConfigError(f"{where}: type a value for {value.name}.") + problem = check_secret(text) + if problem is not None: + raise ConfigError(f"{where}: the value for {value.name} {problem}.") + base = entry_for_name(value.name) + entry, n = base, 1 + while entry in secrets: + n += 1 + suffix = f"-{n}" + entry = f"{base[: 64 - len(suffix)]}{suffix}" + secrets[entry] = text + mapping[value.name] = keychain_ref(entry) + try: + spec = McpServerConfig( + transport=server.transport, + command=list(server.command), + url=server.url, + enabled=True, + allow_remote=server.off_box and choice.allow_remote, + env=env, + headers=headers, + ) + except ValueError as exc: + # A pydantic error can carry its input; give the reasons only. + reasons = getattr(exc, "errors", None) + detail = ( + "; ".join(str(e.get("msg", "")) for e in reasons(include_input=False)) + if callable(reasons) + else "its settings are not valid" + ) + raise ConfigError(f"{where}: {detail}") from None + return _Ready(server, choice.id, spec, secrets) + + +async def apply_import( + text: str, choices: list[Choice], mcp: McpConfig, path: Path +) -> list[tuple[str, ImportServer]]: + """Import the chosen servers. Returns ``(id, server)`` for each one saved. + + Every choice is checked before anything is written, so a mistake in + the third server does not leave the first two half-done. + + Raises: + ConfigError: The text, a choice or a value is not acceptable. Never quotes a value. + KeychainError: The keychain refused a value. + """ + try: + parsed = {server.source_name: server for server in parse_import(text)} + except McpImportError as exc: + raise ConfigError(str(exc)) from None + if not choices: + raise ConfigError("Choose at least one server to import.") + ready: list[_Ready] = [] + taken: set[str] = set() + for choice in choices: + server = parsed.get(choice.source_name) + if server is None: + raise ConfigError(f"There is no server named {choice.source_name!r} in that text.") + item = _ready(server, choice, mcp, taken) + taken.add(item.id) + ready.append(item) + saved: list[tuple[str, ImportServer]] = [] + for item in ready: + stored: list[str] = [] + try: + for entry, value in item.secrets.items(): + await keychain_mcp.store_mcp_secret(item.id, entry, value) + stored.append(entry) + save_mcp_server(item.id, item.spec, path) + except Exception: + for entry in stored: + try: + await keychain_mcp.delete_mcp_secret(item.id, entry) + except Exception: + log.exception( + "could not remove an imported value after a failed save", + extra={"extra_fields": {"server_id": item.id, "entry": entry}}, + ) + raise + saved.append((item.id, item.server)) + return saved diff --git a/core/tstd/model_cards.py b/core/tstd/model_cards.py index 327ad5eb..64e1581f 100644 --- a/core/tstd/model_cards.py +++ b/core/tstd/model_cards.py @@ -145,8 +145,12 @@ def targets(config: ModelConfig, preset: str, tier: str | None = None) -> list[C raise ConfigError(f"Unknown tier {tier!r}; tiers are: {', '.join(TIER_NAMES)}") names = [tier] if tier is not None else list(TIER_NAMES) found: list[CardTarget] = [] + # An ``engine: grok`` entry may name no model (TD-5239): nothing to refresh. + own = entry.tier_map() for name in names: - tier_cfg: TierConfig = getattr(entry, name) + if name not in own: + continue + tier_cfg: TierConfig = own[name] found.append(CardTarget(preset, name, None, tier_cfg)) found.extend( CardTarget(preset, name, index, backup) diff --git a/core/tstd/peer_policy.py b/core/tstd/peer_policy.py index 91d306f0..208597fd 100644 --- a/core/tstd/peer_policy.py +++ b/core/tstd/peer_policy.py @@ -27,6 +27,8 @@ from typing import Literal from urllib.parse import urlsplit +from .phone_permissions import LABELS +from .phone_verbs import PHONE_OPEN, PHONE_OPT_IN, required_capability from .protocol import ClientMessage, DaemonEvent, ModelFallback, TierState, build_error DeviceKind = Literal["phone", "machine"] @@ -141,8 +143,16 @@ # where keys and tokens are sent, and the phone listener. The narrower pages # a phone may use stay open to it; this one, reading included, is the # computer's own. +# ``delete_preset`` (TD-5237) removes a model from the catalog. Copying or +# relabelling one from a phone is as narrow as ``add_model``; taking one +# away is done at the computer whose config it is. +# ``set_phone_permission`` (TD-5279) decides what a phone may change, so a +# phone never sends it: it could otherwise grant itself. It is the one +# phone permission with no setting. LOOPBACK_VERBS: frozenset[str] = frozenset( { + "set_phone_permission", + "delete_preset", "drain", "trust_workspace_config", "steering_accept", @@ -163,6 +173,9 @@ class PeerView: machine: bool viewer: bool history: bool = False + # A paired phone: remote, with a device record of ``kind: phone``. Not + # the legacy remote token, which is no device, and never a machine. + phone: bool = False @property def loopback(self) -> bool: @@ -182,7 +195,13 @@ def classify(remote: bool, device_kind: DeviceKind | None, features: Iterable[st machine = device_kind == "machine" or BRIDGE_FEATURE in wanted viewer = not machine and VIEWER_FEATURE in wanted and (not remote or device_kind == "phone") history = HISTORY_FEATURE in wanted - return PeerView(remote=remote, machine=machine, viewer=viewer, history=history) + phone = remote and not machine and device_kind == "phone" + return PeerView(remote=remote, machine=machine, viewer=viewer, history=history, phone=phone) + + +def placed_for_a_phone(kind: str) -> bool: + """Whether a verb has been given a place in the phone inventory.""" + return kind in PHONE_OPEN or kind in PHONE_OPT_IN or kind in MACHINE_VERBS # Loopback names say nothing about where a service lives. @@ -289,17 +308,38 @@ def message_session_id(msg: ClientMessage) -> str | None: return value if isinstance(value, str) else None -def check(msg: ClientMessage, peer: PeerView, *, bridged: bool) -> str | None: +def check( + msg: ClientMessage, peer: PeerView, *, bridged: bool, grants: Collection[str] = () +) -> str | None: """A ready error frame when *peer* may not send *msg*, else None. *bridged* says the message's ``session_id`` is in the bridged grammar. - Forwarding itself is the daemon's call; this only refuses. + Forwarding itself is the daemon's call; this only refuses. *grants* are + the phone permissions switched on right now (TD-5279); a caller that + passes none refuses every opt-in verb from a phone. """ sid = message_session_id(msg) if msg.type in LOOPBACK_VERBS and (peer.remote or peer.machine): return build_error( "loopback_only", "Only the computer itself can do that.", in_reply_to=msg.type ) + # TD-5279: only a paired phone can use a grant. The legacy remote token + # is no device, so nothing switches these on for it; another computer + # is refused below as it always was. + capability = required_capability(msg) + wants_grant = capability is not None and peer.remote and not peer.machine + if wants_grant and not (peer.phone and capability in grants): + return build_error( + "phone_permission_off", + f"Turn on '{LABELS[capability or '']}' under Settings, Phones and devices " + "on the computer to do this from a phone.", + in_reply_to=msg.type, + ) + # TD-5284: a verb nobody has placed for a phone is not open to one. + if peer.remote and not peer.machine and not placed_for_a_phone(msg.type): + return build_error( + "loopback_only", "Only the computer itself can do that.", in_reply_to=msg.type + ) # TD-5118: a turn's own allow rules switch approvals off for it, so they # come only from ``tst run`` on this computer, for its own sessions; a # phone or another computer still approves call by call. diff --git a/core/tstd/phone_permissions.py b/core/tstd/phone_permissions.py new file mode 100644 index 00000000..cb40ad55 --- /dev/null +++ b/core/tstd/phone_permissions.py @@ -0,0 +1,239 @@ +"""What a paired phone may change, as granted at the computer (TD-5279). + +Some things a phone could ask for reach further than the session it is +looking at: an MCP server is a command this computer runs, at once and at +every session start, and it never meets the classifier. Those verbs are +refused from a phone unless the person at the computer switched the +matching capability on, after reading what it lets a phone do. + +The grants live in ``/phone-permissions.yaml``, beside +``remote-attach.yaml`` and for the same reason: it is this computer's own +state. It is not a ``config.yaml`` key and no workspace file is read for +it, so a repository can never switch a grant on. + +Everything here fails closed. An absent, unreadable or malformed file, an +unknown capability, an entry that is not exactly ``enabled: true``, an +expiry that does not parse and an expiry in the past all read as off. + +Also here: ``mcp-origins.yaml``, which remembers the phone a server was +added or changed from. Removing a phone does not undo what it changed, so +the record is what lets the computer show it and offer to remove it. +""" + +from __future__ import annotations + +import contextlib +import os +import tempfile +from collections.abc import Mapping +from dataclasses import dataclass +from datetime import UTC, datetime, timedelta +from pathlib import Path +from typing import Any, Final + +import yaml + +# The closed set. A capability is added here together with its verbs in +# ``peer_policy.PHONE_OPT_IN`` and its own disclosure in Settings, Phones and devices. +MCP_SERVERS: Final = "mcp_servers" +# Turning approval prompts off for every session (TD-5283). Turning them +# back on needs nothing: that only tightens. +APPROVAL_SWITCHES: Final = "approval_switches" +CAPABILITIES: Final[tuple[str, ...]] = (MCP_SERVERS, APPROVAL_SWITCHES) + +# What the refusal and the Doctor row call each one. +LABELS: Final[Mapping[str, str]] = { + MCP_SERVERS: "MCP servers", + APPROVAL_SWITCHES: "Turning approvals off", +} + +# A timed grant is for "while I am out", not a second way to say forever. +MAX_EXPIRY_SECONDS: Final = 7 * 24 * 60 * 60 + +_FILE_VERSION: Final = 1 +_TIME_FORMAT: Final = "%Y-%m-%dT%H:%M:%SZ" + + +def utc_now() -> datetime: + """Now, in UTC. The one clock this module reads; tests replace it.""" + return datetime.now(UTC) + + +def stamp(moment: datetime) -> str: + return moment.astimezone(UTC).strftime(_TIME_FORMAT) + + +def parse_stamp(value: object) -> datetime | None: + if not isinstance(value, str): + return None + try: + return datetime.strptime(value, _TIME_FORMAT).replace(tzinfo=UTC) + except ValueError: + return None + + +@dataclass(frozen=True) +class Grant: + """One capability switched on. Off is the absence of a grant.""" + + granted_at: str + expires_at: str | None = None + + def live(self, now: datetime) -> bool: + if self.expires_at is None: + return True + until = parse_stamp(self.expires_at) + return until is not None and now < until + + +Grants = dict[str, Grant] + + +def permissions_path(data_dir: str | Path) -> Path: + return Path(data_dir) / "phone-permissions.yaml" + + +def _read_mapping(path: Path) -> dict[str, Any]: + try: + data: Any = yaml.safe_load(path.read_text(encoding="utf-8")) + except (OSError, UnicodeDecodeError, yaml.YAMLError): + return {} + return data if isinstance(data, dict) else {} + + +def _write_mapping(path: Path, fields: dict[str, Any]) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=f".{path.stem}.", suffix=".tmp") + try: + with os.fdopen(fd, "w", encoding="utf-8") as f: + yaml.safe_dump(fields, f, sort_keys=False, allow_unicode=True) + os.replace(tmp, path) + except BaseException: + with contextlib.suppress(OSError): + os.unlink(tmp) + raise + + +def load_grants(data_dir: str | Path) -> Grants: + """The grants on file. Anything that is not plainly a grant is left out.""" + data = _read_mapping(permissions_path(data_dir)) + if data.get("version") != _FILE_VERSION: + return {} + raw = data.get("grants") + if not isinstance(raw, dict): + return {} + grants: Grants = {} + for name in CAPABILITIES: + entry = raw.get(name) + if not isinstance(entry, dict) or entry.get("enabled") is not True: + continue + expires = entry.get("expires_at") + if expires is not None and parse_stamp(expires) is None: + continue + granted = entry.get("granted_at") + grants[name] = Grant( + granted_at=granted if isinstance(granted, str) else "", + expires_at=expires, + ) + return grants + + +def save_grants(data_dir: str | Path, grants: Grants) -> None: + """Write *grants* atomically. Every capability is listed, on or off.""" + body: dict[str, Any] = {} + for name in CAPABILITIES: + grant = grants.get(name) + if grant is None: + body[name] = {"enabled": False} + else: + body[name] = { + "enabled": True, + "granted_at": grant.granted_at, + "expires_at": grant.expires_at, + } + _write_mapping(permissions_path(data_dir), {"version": _FILE_VERSION, "grants": body}) + + +def changed( + grants: Grants, + capability: str, + enabled: bool, + expires_in_seconds: int | None, + now: datetime, +) -> Grants: + """*grants* with one capability switched. Raises ValueError on a bad ask.""" + if capability not in CAPABILITIES: + raise ValueError(f"Unknown phone permission: {capability}") + after = dict(grants) + if not enabled: + after.pop(capability, None) + return after + expires_at: str | None = None + if expires_in_seconds is not None: + if not 0 < expires_in_seconds <= MAX_EXPIRY_SECONDS: + raise ValueError("A timed phone permission lasts at most seven days.") + expires_at = stamp(now + timedelta(seconds=expires_in_seconds)) + after[capability] = Grant(granted_at=stamp(now), expires_at=expires_at) + return after + + +def live(grants: Grants, now: datetime) -> frozenset[str]: + """The capabilities a phone may use right now.""" + return frozenset(name for name, grant in grants.items() if grant.live(now)) + + +def next_expiry(grants: Grants, now: datetime) -> datetime | None: + """When the soonest still-live timed grant runs out.""" + ends = [ + until + for grant in grants.values() + if grant.live(now) and (until := parse_stamp(grant.expires_at)) is not None + ] + return min(ends) if ends else None + + +# ── where a server came from ────────────────────────────────────────── + + +@dataclass(frozen=True) +class Origin: + """The phone an MCP server was last added or changed from.""" + + device_id: str + device_name: str + at: str + + +Origins = dict[str, Origin] + + +def origins_path(data_dir: str | Path) -> Path: + return Path(data_dir) / "mcp-origins.yaml" + + +def load_origins(data_dir: str | Path) -> Origins: + data = _read_mapping(origins_path(data_dir)) + raw = data.get("servers") if data.get("version") == _FILE_VERSION else None + if not isinstance(raw, dict): + return {} + found: Origins = {} + for server_id, entry in raw.items(): + if not isinstance(server_id, str) or not isinstance(entry, dict): + continue + device_id, name, at = entry.get("device_id"), entry.get("device_name"), entry.get("at") + if isinstance(device_id, str) and isinstance(name, str) and isinstance(at, str): + found[server_id] = Origin(device_id=device_id, device_name=name, at=at) + return found + + +def save_origins(data_dir: str | Path, origins: Origins) -> None: + servers = { + server_id: {"device_id": o.device_id, "device_name": o.device_name, "at": o.at} + for server_id, o in sorted(origins.items()) + } + _write_mapping(origins_path(data_dir), {"version": _FILE_VERSION, "servers": servers}) + + +def servers_from(origins: Origins, device_id: str) -> list[str]: + """The servers one device added or changed, by id.""" + return sorted(sid for sid, origin in origins.items() if origin.device_id == device_id) diff --git a/core/tstd/phone_store.py b/core/tstd/phone_store.py new file mode 100644 index 00000000..9f65cf25 --- /dev/null +++ b/core/tstd/phone_store.py @@ -0,0 +1,130 @@ +"""The daemon's phone permissions, held in memory and on disk (TD-5279). + +One :class:`PhoneStore` per daemon. ``phone_permissions`` is the file +format and the rules; this is the daemon's copy of both files plus the +shapes it hands to ``setup_state``, Doctor and the peer policy. + +A write that fails leaves memory as it was: a grant that could not be +saved is not in force, and one that could not be cleared is still shown +as on, so what the person sees is what the next start will read. +""" + +from __future__ import annotations + +import shlex +from collections.abc import Sequence +from datetime import datetime +from pathlib import Path + +from . import phone_permissions as perms +from .logging import get_logger +from .protocol import DiagnosticCheck, PhonePermissionState + +log = get_logger("tstd.phone_store") + +# What the notice quotes of a command or address: enough to recognise it. +_QUOTE_LIMIT = 200 + + +def _clip(text: str) -> str: + return text if len(text) <= _QUOTE_LIMIT else text[: _QUOTE_LIMIT - 1] + "…" + + +def server_saved_summary(server_id: str, command: Sequence[str], url: str, *, existed: bool) -> str: + """What a phone did to a server, without the phone's name.""" + what = shlex.join(command) if command else url + verb = "changed" if existed else "added" + return f"{verb} MCP server '{server_id}': {_clip(what)}" + + +def value_summary(server_id: str, target: str, name: str, *, stored: bool) -> str: + """A value stored or removed. The name only; the value is never here.""" + kind = "header" if target == "header" else "variable" + verb = "stored a value for" if stored else "removed the value of" + return f"{verb} {kind} '{name}' of MCP server '{server_id}'." + + +class PhoneStore: + def __init__(self, data_dir: str | Path) -> None: + self._data_dir = Path(data_dir) + self._grants = perms.load_grants(self._data_dir) + self._origins = perms.load_origins(self._data_dir) + + # ── grants ──────────────────────────────────────────────────────── + + def live(self) -> frozenset[str]: + """The capabilities a phone may use right now.""" + return perms.live(self._grants, perms.utc_now()) + + def set(self, capability: str, enabled: bool, expires_in_seconds: int | None) -> None: + """Switch one capability. ValueError on a bad ask, OSError on a failed write.""" + after = perms.changed( + self._grants, capability, enabled, expires_in_seconds, perms.utc_now() + ) + perms.save_grants(self._data_dir, after) + self._grants = after + + def states(self) -> dict[str, PhonePermissionState]: + """Every capability, on or off, as ``setup_state`` shows it.""" + now = perms.utc_now() + out: dict[str, PhonePermissionState] = {} + for name in perms.CAPABILITIES: + grant = self._grants.get(name) + if grant is not None and grant.live(now): + out[name] = PhonePermissionState(enabled=True, expires_at=grant.expires_at) + else: + out[name] = PhonePermissionState() + return out + + def next_expiry(self) -> datetime | None: + return perms.next_expiry(self._grants, perms.utc_now()) + + def doctor_row(self) -> DiagnosticCheck | None: + """A warning while any phone permission is on; no row when all are off.""" + now = perms.utc_now() + on: list[str] = [] + for name in perms.CAPABILITIES: + grant = self._grants.get(name) + if grant is None or not grant.live(now): + continue + until = f"until {grant.expires_at}" if grant.expires_at else "until turned off" + on.append(f"{perms.LABELS[name]} ON ({until})") + if not on: + return None + return DiagnosticCheck( + name="phone_permissions", + status="warn", + detail="paired phones may change: " + "; ".join(on), + ) + + # ── where a server came from ────────────────────────────────────── + + def origin_of(self, server_id: str) -> perms.Origin | None: + return self._origins.get(server_id) + + def servers_from(self, device_id: str) -> list[str]: + return perms.servers_from(self._origins, device_id) + + def record_origin(self, server_id: str, device_id: str, device_name: str) -> None: + after = dict(self._origins) + after[server_id] = perms.Origin( + device_id=device_id, device_name=device_name, at=perms.stamp(perms.utc_now()) + ) + self._save_origins(after) + + def forget_origin(self, server_id: str) -> None: + if server_id not in self._origins: + return + after = dict(self._origins) + del after[server_id] + self._save_origins(after) + + def _save_origins(self, after: perms.Origins) -> None: + # The server is already saved by now; a record that cannot be + # written must not turn that into an error. It is logged instead. + try: + perms.save_origins(self._data_dir, after) + except OSError: + log.exception("could not write the MCP origins file") + return + self._origins = after diff --git a/core/tstd/phone_verbs.py b/core/tstd/phone_verbs.py new file mode 100644 index 00000000..db414871 --- /dev/null +++ b/core/tstd/phone_verbs.py @@ -0,0 +1,210 @@ +"""Every verb, placed for a paired phone (TD-5279, TD-5284). + +A phone acts as the person, call by call: it sends turns and answers +approvals. Some verbs reach further than the session it is looking at, +and those are not open to it by default. This module is the table. + +Pure data and two small rules read off a message's own fields; the +checking is in ``peer_policy.check``. +""" + +from __future__ import annotations + +from .phone_permissions import APPROVAL_SWITCHES, MCP_SERVERS +from .protocol import ClientMessage + +# What a phone may send only after the computer switched the capability on +# (TD-5279), verb to capability. The MCP page: a stdio server is a command +# this computer runs, now and at every session start, and it never meets +# the classifier. A paired phone could add one with no word from the person +# at the computer, and it stayed in config.yaml after the phone was +# removed. A server's values go with it: they are that command's +# environment, or the headers sent to its host. A phone still sees the list +# in ``setup_state`` either way. +PHONE_OPT_IN: dict[str, str] = { + "set_mcp_server": MCP_SERVERS, + "delete_mcp_server": MCP_SERVERS, + "set_mcp_secret": MCP_SERVERS, + "delete_mcp_secret": MCP_SERVERS, + # Pasted JSON (TD-5253) is the same act as the Add form, several at once. + "preview_mcp_import": MCP_SERVERS, + "import_mcp_servers": MCP_SERVERS, + # Skip-all (TD-5283) persists machine-wide and releases every parked + # call, Class C included. A phone can already approve call by call; + # this is the same power with nobody looking at each call. + "set_skip_all_approvals": APPROVAL_SWITCHES, +} + + +# Everything a paired phone may send with no setting (TD-5284), written +# out. Until this list existed a phone could send any verb nobody had +# thought to list against it, so a new verb was open to phones by default. +# Now a verb in none of the sets is refused from a phone, exactly as it +# already was from another computer, and a test fails until it is placed: +# +# - ``LOOPBACK_VERBS``: the computer's own, no setting. +# - ``PHONE_OPT_IN``: refused unless the computer switched it on. +# - ``MACHINE_VERBS``: paired computers, with their own rule in ``check``. +# - here: open to a phone. +# +# This list is what a phone could already send the day it was written; it +# changes nothing by itself. Several entries are waiting to move to +# ``PHONE_OPT_IN``, one capability at a time (the model and key verbs, +# computer-use control, starting an unattended run, the connector +# sign-ins, ``shutdown``, releasing the computer-use stop, changing a saved +# key's host and changing the mail server). Some are refused or narrowed +# for a remote peer by their own handler (``start_pairing``, +# ``remove_remote_device``); being here only means the policy passes them on. +PHONE_OPEN: frozenset[str] = frozenset( + { + "add_grok_preset", + "add_model", + "add_pin", + "always_allow", + "answer_question", + "approve", + "approve_grok_plan", + "archive_session", + "attach", + "cancel", + "cancel_pairing", + "check_cu_permissions", + "check_presets", + "close_worktree", + "compact_context", + "create_rule", + "delete_api_key", + "delete_credential", + "delete_job", + "delete_job_template", + "delete_session", + "deny", + "deny_verify", + "design_hit_test", + "detach", + "duplicate_preset", + "end_session", + "export_usage", + "extract_attachment", + "fork_from", + "get_charter", + "get_hooks", + "get_instruction_stack", + "get_preset_roles", + "get_session_job_source", + "get_setup_state", + "get_usage", + "github_connect", + "github_disconnect", + "google_connect", + "google_disconnect", + "list_artifacts", + "list_commands", + "list_endpoint_models", + "list_grok_extensions", + "list_grok_sessions", + "list_instructions", + "list_job_runs", + "list_job_templates", + "list_jobs", + "list_memory", + "list_pins", + "list_policy_rules", + "list_sessions", + "memory_accept", + "memory_edit", + "memory_reject", + "microsoft_connect", + "microsoft_disconnect", + "move_session", + "new_session", + "open_artifact", + "open_in_terminal", + "open_workspace", + "parse_job", + "preview_restore_files", + "read_artifact_chunk", + "refresh_model_cards", + "remove_pin", + "remove_queued", + "remove_remote_device", + "rename_session", + "reset_cu_permissions", + "restore_files", + "resume", + "review_plan", + "revoke_policy_rule", + "rotate_remote_token", + "run_diagnostics", + "run_grok_command", + "run_job", + "run_verify", + "save_charter", + "save_job", + "save_job_template", + "save_memory", + "search_sessions", + "send_queued", + "set_api_key", + "set_branch", + "set_coworker", + "set_credential", + "set_cu_indicators", + "set_cu_kill", + "set_cu_policy", + "set_effort", + "set_email_notify", + "set_engine", + "set_fast", + "set_grok_mode", + "set_judgments", + "set_load_global_memory", + "set_plan", + "set_preset", + "set_preset_label", + "set_remote_attach", + "set_session_preset", + "set_session_star", + "set_spend_cap", + "set_tier", + "set_tier_context_window", + "set_tier_credential", + "set_tier_slug", + "set_ultracode", + "set_voice", + "set_workspace_pin", + "shutdown", + "start_autonomy", + "start_pairing", + "steering_reject", + "stop_turn", + "submit_line_comments", + "switch_preset", + "test_email", + "test_model", + "transcribe", + "user_message", + "validate_api_key", + } +) + +# The first frames of a connection. They are answered by the handshake and +# never reach ``check``; named so the inventory test can account for them. +HANDSHAKE_FRAMES: frozenset[str] = frozenset({"hello", "pair"}) + + +def _only_tightens(msg: ClientMessage) -> bool: + """A message that can only narrow what runs needs no permission. + + Read off the message's own fields, so the rule stays in this pure + table. Turning skip-all *off* brings the approval prompts back. + """ + return msg.type == "set_skip_all_approvals" and getattr(msg, "enabled", True) is False + + +def required_capability(msg: ClientMessage) -> str | None: + """The phone permission *msg* needs from a remote peer, if any.""" + capability = PHONE_OPT_IN.get(msg.type) + if capability is None or _only_tightens(msg): + return None + return capability diff --git a/core/tstd/preset_apply.py b/core/tstd/preset_apply.py new file mode 100644 index 00000000..bce05e57 --- /dev/null +++ b/core/tstd/preset_apply.py @@ -0,0 +1,68 @@ +"""The config a chat on one catalog entry runs with (TD-5234). + +One function, used wherever a session takes its snapshot of the catalog +(TD-1721): the daemon's config, pointed at the entry, with that entry's +``overrides`` laid over the global sections. Everything a turn reads from +the snapshot (stale-result clearing, compaction, consult, the image cap, +workflow limits, the effort a chat starts on) follows the entry with no +change at the call sites. +""" + +from __future__ import annotations + +from .config import ModelConfig +from .preset_overrides import PresetOverrides, merged_sections + + +def preset_overrides(config: ModelConfig, name: str) -> PresetOverrides | None: + """The ``overrides`` of entry *name*, or None when it sets none.""" + preset = config.presets.get(name) + return preset.overrides if preset is not None else None + + +def effective_config(config: ModelConfig, name: str) -> ModelConfig: + """*config* as a chat on entry *name* sees it. Pure: *config* is not changed. + + With no ``overrides`` this is ``config`` with ``active_preset`` set, + what a session's snapshot was before TD-5234. An unknown *name* has no + overrides either; choosing a real one is the caller's job. + """ + update: dict[str, object] = {"active_preset": name} + update.update(merged_sections(config, preset_overrides(config, name))) + return config.model_copy(update=update) + + +def no_model_refusal(config: ModelConfig, name: str, engine: str) -> str | None: + """Why TST Desk's own loop cannot run on entry *name*, or None (TD-5239). + + The loop needs the entry's models, and an ``engine: grok`` entry may + have none. The Grok engine needs nothing from the entry. + """ + preset = config.presets.get(name) + if preset is None or preset.has_tiers or engine != "native": + return None + return ( + f"{preset.label or name} runs on the Grok CLI and has no model of its own, so a " + "chat on TST Desk's own loop cannot use it. Start a new chat to use it." + ) + + +def engine_refusal(config: ModelConfig, name: str, engine: str) -> str | None: + """Why an open chat on *engine* cannot switch to entry *name*, or None (TD-5239). + + A chat keeps the engine it started on, so an entry that names another + engine is refused, and so is one with no model for TST Desk's own loop. + An entry that names no engine suits either, as before. + """ + missing = no_model_refusal(config, name, engine) + if missing is not None: + return missing + preset = config.presets.get(name) + if preset is None or preset.engine is None or preset.engine == engine: + return None + ours = "the Grok CLI" if engine == "grok" else "TST Desk's own loop" + theirs = "the Grok CLI" if preset.engine == "grok" else "TST Desk's own loop" + return ( + f"This chat runs on {ours}, and {preset.label or name} runs on {theirs}. A chat " + "keeps the engine it started on. Start a new chat to use it." + ) diff --git a/core/tstd/preset_overrides.py b/core/tstd/preset_overrides.py new file mode 100644 index 00000000..2e48f8a8 --- /dev/null +++ b/core/tstd/preset_overrides.py @@ -0,0 +1,148 @@ +"""Context settings one model carries with it (TD-5234). + +A catalog entry already says which model each role uses and how big its +window is. The settings that go with a model's size and speed did not +travel with it: how compaction writes its summary, when stale tool results +are cleared, how many workers run at once, the effort a chat starts on. +They were global, so moving between a small local model and a large hosted +one meant editing them by hand each time. + +``overrides`` on an entry sets any of them for chats on that entry. It is +a whitelist of six sections, each a partial of the global section with the +same limits. Nothing here can loosen what the agent may do: approvals, the +shell lists, MCP servers, hooks, remote attach, notifications, autonomy and +keys are not in it, so picking a model never changes the trust surface. + +This module imports nothing from :mod:`tstd.config`, which imports it. +""" + +from __future__ import annotations + +from typing import Literal + +from pydantic import BaseModel, ConfigDict, Field, field_validator + +from .effort import EffortLevel + +# Spelled out, not imported: tstd.config imports this module. A test pins +# every field here to the annotation and limits of the field it overrides. +_TierName = Literal["brain", "worker", "validator"] + + +class _Partial(BaseModel): + """A section where every key is optional and an unknown key is an error.""" + + model_config = ConfigDict(extra="forbid") + + def changes(self) -> dict[str, object]: + """The keys this entry sets. A key left out, or null, keeps the global value.""" + return { + key: value + for key, value in self.model_dump(exclude_unset=True).items() + if value is not None + } + + +class CompactionOverride(_Partial): + """``compaction`` for chats on this model. See ``CompactionConfig``.""" + + summary: Literal["model", "extractive"] | None = None + summary_prompt: str | None = None + summary_max_tokens: int | None = Field(default=None, ge=64) + summary_timeout_seconds: float | None = Field(default=None, gt=0) + background_percent: int | None = Field(default=None, ge=0, le=99) + reinject_files: int | None = Field(default=None, ge=0) + reinject_tokens: int | None = Field(default=None, ge=0) + + +class ClearToolResultsOverride(_Partial): + """``clear_tool_results`` for chats on this model.""" + + enabled: bool | None = None + keep_recent_tool_results: int | None = Field(default=None, ge=0) + clear_at_least_tokens: int | None = Field(default=None, ge=0) + exclude_tools: list[str] | None = None + + +class WorkflowOverride(_Partial): + """``workflow`` for chats on this model: size it to this model's server.""" + + max_parallel: int | None = Field(default=None, ge=1, le=16) + max_tasks: int | None = Field(default=None, ge=1, le=32) + + +class ConsultOverride(_Partial): + """``consult`` for chats on this model.""" + + tiers: list[_TierName] | None = None + max_per_turn: int | None = Field(default=None, ge=1, le=20) + + @field_validator("tiers") + @classmethod + def _dedupe_tiers(cls, value: list[_TierName] | None) -> list[_TierName] | None: + return None if value is None else list(dict.fromkeys(value)) + + +class SessionOverride(_Partial): + """What a chat on this model starts with. The log and file caps stay global.""" + + default_effort: EffortLevel | None = None + default_ultracode: bool | None = None + + +class ComputerUseOverride(_Partial): + """How many images this model's requests may keep. The rest stays global.""" + + max_prompt_images: int | None = Field(default=None, ge=0) + + +#: The global sections an entry may override, in config.yaml's order. +OVERRIDE_SECTIONS: tuple[str, ...] = ( + "session", + "clear_tool_results", + "compaction", + "consult", + "workflow", + "computer_use", +) + + +class PresetOverrides(_Partial): + """The ``overrides`` block of one catalog entry.""" + + session: SessionOverride | None = None + clear_tool_results: ClearToolResultsOverride | None = None + compaction: CompactionOverride | None = None + consult: ConsultOverride | None = None + workflow: WorkflowOverride | None = None + computer_use: ComputerUseOverride | None = None + + def sections(self) -> dict[str, dict[str, object]]: + """Each section this entry changes, with the keys it sets.""" + found: dict[str, dict[str, object]] = {} + for name in OVERRIDE_SECTIONS: + part: _Partial | None = getattr(self, name) + if part is not None and part.changes(): + found[name] = part.changes() + return found + + +def merged_sections(config: BaseModel, overrides: PresetOverrides | None) -> dict[str, BaseModel]: + """The global sections of *config* with *overrides* laid over them. + + Only sections the entry changes are returned. A key the entry sets + wins; every other key keeps *config*'s value; a list replaces the + global list whole. Each result is built by the section's own model, so + its rules apply to the merged value too. *config* is not changed. + + Raises: + ValueError: A merged section is not valid. pydantic's message + names the key. + """ + if overrides is None: + return {} + merged: dict[str, BaseModel] = {} + for name, changes in overrides.sections().items(): + base: BaseModel = getattr(config, name) + merged[name] = type(base).model_validate({**base.model_dump(), **changes}) + return merged diff --git a/core/tstd/preset_ready.py b/core/tstd/preset_ready.py new file mode 100644 index 00000000..e80c2f15 --- /dev/null +++ b/core/tstd/preset_ready.py @@ -0,0 +1,98 @@ +"""Whether each model on the picker can run right now (TD-5240). + +A switch used to succeed and the next message then fail: the local server +was not started, the key was never stored, the Grok CLI was not installed. +This says so on the picker's row, before the click. + +Three checks, none of which sends a key or leaves the machine: + +* a role bound to a key: is that key in the keychain (presence only); +* a role served on this computer: does anything answer there; +* an entry on the Grok engine: is the Grok CLI found. + +A provider's server is not asked: that would be a request to a third party +the person did not make, and a key's presence is what can be known without +one. The first problem found is the entry's status, in the order a person +would fix them: the engine, then a key, then a server. + +Pure but for the three callables it is handed, so every case is a table row. +""" + +from __future__ import annotations + +from collections.abc import Awaitable, Callable, Mapping +from urllib.parse import urlsplit + +from .config import ModelConfig, is_loopback_url, resolve_base_url, resolve_credential_id +from .protocol import PresetReady + +KeyStored = Callable[[str], Awaitable[bool]] +Answers = Callable[[list[str]], Awaitable[Mapping[str, bool]]] + + +def _where(url: str) -> str: + """``host:port`` of *url*, for a message. Never the path or a password.""" + try: + parts = urlsplit(url) + return f"{parts.hostname}:{parts.port}" if parts.port else str(parts.hostname) + except ValueError: + return "its server" + + +def loopback_urls(config: ModelConfig) -> list[str]: + """Every distinct URL on this computer that some entry's role is served at.""" + found: list[str] = [] + for preset in config.presets.values(): + for tier in preset.tier_map().values(): + url = resolve_base_url(config, tier).strip().rstrip("/") + if is_loopback_url(url) and url not in found: + found.append(url) + return found + + +async def check_presets( + config: ModelConfig, *, key_stored: KeyStored, answers: Answers, grok_found: bool +) -> list[PresetReady]: + """One row per catalog entry, in catalog order.""" + up = await answers(loopback_urls(config)) + rows: list[PresetReady] = [] + for name in config.presets: + rows.append(await _check(config, name, key_stored, up, grok_found)) + return rows + + +async def _check( + config: ModelConfig, + name: str, + key_stored: KeyStored, + up: Mapping[str, bool], + grok_found: bool, +) -> PresetReady: + preset = config.presets[name] + if config.preset_engine(name) == "grok": + if not grok_found: + return PresetReady( + preset=name, + status="no_grok", + detail="The Grok CLI was not found on this computer.", + ) + # The Grok CLI brings its own model and sign-in: nothing else to check. + return PresetReady(preset=name, status="ready") + for tier in preset.tier_map().values(): + credential = resolve_credential_id(tier) + if credential is not None and not await key_stored(credential): + shown = config.credentials[credential].name if credential in config.credentials else "" + return PresetReady( + preset=name, + status="no_key", + detail=f"No key is stored for {shown or credential}.", + ) + for tier in preset.tier_map().values(): + url = resolve_base_url(config, tier).strip().rstrip("/") + if is_loopback_url(url) and not up.get(url, True): + return PresetReady( + preset=name, + status="server_down", + detail=f"Nothing is answering at {_where(url)}. Start the model server.", + ) + return PresetReady(preset=name, status="ready") diff --git a/core/tstd/preset_write.py b/core/tstd/preset_write.py new file mode 100644 index 00000000..0ee5b266 --- /dev/null +++ b/core/tstd/preset_write.py @@ -0,0 +1,320 @@ +"""Copy, relabel, remove and add-Grok for the catalog in the user's ``config.yaml`` (TD-5237). + +The walkthrough could only add a model, so the way to try a variation was +to overwrite the one entry already there. These three writers let Settings +keep several: copy an entry, give one a new label, remove one. + +Like :mod:`tstd.config_write`, every edit is surgical: the lines the entry +owns change and the rest of the file stays byte-identical. Unlike a single +value, an entry can be tied to others by a YAML anchor (``&my_model`` in +one, ``*my_model`` in the next), so each would-be file is loaded exactly as +the daemon loads it *before* it is written. An edit that would leave a file +that does not load is refused, and nothing is written. + +An entry's key is its permanent id: sessions, scheduled jobs and +``computer_use.local_worker_preset`` all store it. So "rename" here changes +the ``label`` a person sees and never the key. +""" + +from __future__ import annotations + +import json +import re +from pathlib import Path +from typing import Any + +import yaml + +from .config import ( + CREDENTIAL_ID_RE, + ConfigError, + ModelConfig, + config_from_data, + ensure_user_config, + load_config, +) +from .config_write import _atomic_write, _block_bounds, _find_key + +_ENTRY_INDENT_DEFAULT = 2 + + +def _is_content(line: str) -> bool: + stripped = line.strip() + return bool(stripped) and not stripped.startswith("#") + + +def _indent(line: str) -> int: + return len(line) - len(line.lstrip()) + + +def _content_end(lines: list[str], start: int, end: int) -> int: + """One past the last content line in ``[start, end)``. + + A block's bounds run on through blank lines and comments, and a comment + there usually introduces the *next* section. It stays with that section. + """ + last = start + for i in range(start, end): + if _is_content(lines[i]): + last = i + return last + 1 + + +def _presets_block(lines: list[str], config_path: Path) -> tuple[int, int, int]: + """``(header, end, entry_indent)`` of the top-level ``presets:`` block.""" + header = _find_key(lines, 0, len(lines), "presets", 0) + if header < 0 or lines[header].split(":", 1)[1].split("#", 1)[0].strip(): + # Missing, or written inline (``presets: {a: ...}``). + raise ConfigError(f"{config_path} has a `presets:` value this writer cannot edit") + end = _block_bounds(lines, header, 0) + for i in range(header + 1, end): + if _is_content(lines[i]): + return header, end, _indent(lines[i]) + return header, end, _ENTRY_INDENT_DEFAULT + + +def _entry_bounds( + lines: list[str], name: str, config_path: Path +) -> tuple[int, int, int, tuple[int, int]]: + """``(start, end, entry_indent, (presets_header, presets_end))`` for *name*.""" + header, end, entry_indent = _presets_block(lines, config_path) + start = _find_key(lines, header + 1, end, name, entry_indent) + if start < 0: + # Declared (the caller checked) but not as a plain ``name:`` line, + # e.g. a quoted key or a merge from elsewhere. + raise ConfigError(f"{config_path} declares {name!r} in a way this writer cannot edit") + block_end = min(_block_bounds(lines, start, entry_indent), end) + return start, _content_end(lines, start, block_end), entry_indent, (header, end) + + +def _checked(text: str, config_path: Path, why: str) -> ModelConfig: + """Load the would-be file as the daemon would, or refuse the edit.""" + try: + data = yaml.safe_load(text) + except yaml.YAMLError as e: + raise ConfigError(f"{why} ({e})") from e + if not isinstance(data, dict): + raise ConfigError(why) + try: + return config_from_data(data, config_path) + except ConfigError as e: + raise ConfigError(f"{why} ({e})") from e + + +def _require(config: ModelConfig, name: str) -> None: + if name not in config.presets: + declared = ", ".join(sorted(config.presets)) or "none" + raise ConfigError(f"Unknown preset {name!r}; declared presets: {declared}") + + +def _clean_label(label: str) -> str: + cleaned = label.strip() + if not cleaned: + raise ConfigError("Give the model a name") + if len(cleaned) > 40: + raise ConfigError("A model's name can be at most 40 characters") + return cleaned + + +def _yaml_block(value: dict[str, Any], indent: int) -> list[str]: + """*value* as block-style YAML lines, each indented by *indent* spaces.""" + dumped = yaml.safe_dump( + value, sort_keys=False, default_flow_style=False, allow_unicode=True, width=10_000 + ) + pad = " " * indent + return [pad + line if line else line for line in dumped.rstrip("\n").split("\n")] + + +def duplicate_preset(source: str, name: str, label: str, path: Path | None = None) -> Path: + """Write a copy of *source* as a new entry *name* labelled *label*. + + The copy is written from the loaded entry, not from its text: every + role is spelled out in full, so it shares no YAML anchor with the + original and an edit to one never changes the other. Only keys the + original set are written; defaults stay defaults. It lands straight + after the last entry. Never writes a secret: a role's ``credential`` is + a catalog id. + + Raises ``ConfigError`` when *source* is not declared, *name* is taken + or not a slug, or the result would not load. + """ + if not CREDENTIAL_ID_RE.match(name): + raise ConfigError(f"preset name {name!r} must be a lowercase slug [a-z][a-z0-9-]{{0,31}}") + cleaned = _clean_label(label) + config_path = ensure_user_config(path) + config = load_config(config_path) + _require(config, source) + if name in config.presets: + raise ConfigError(f"A preset named {name!r} already exists") + + lines = config_path.read_text(encoding="utf-8").split("\n") + header, end, entry_indent = _presets_block(lines, config_path) + body = config.presets[source].model_dump(mode="json", exclude_unset=True) + body.pop("label", None) + # A quick-switch slot picks one entry (TD-5236): the copy starts without one. + body.pop("slot", None) + block = [ + f"{' ' * entry_indent}{name}:", + f"{' ' * (entry_indent * 2)}label: {json.dumps(cleaned)}", + *_yaml_block(body, entry_indent * 2), + ] + at = _content_end(lines, header, end) + lines[at:at] = block + + text = "\n".join(lines) + loaded = _checked(text, config_path, f"Could not copy {source!r}") + if name not in loaded.presets: + raise ConfigError(f"Could not copy {source!r}: the copy did not load as an entry") + _atomic_write(config_path, text) + return config_path + + +def save_preset_label( + name: str, label: str, description: str | None = None, path: Path | None = None +) -> Path: + """Set the ``label`` of entry *name*, and its ``description`` when given. + + *description* None leaves the line as it is; an empty one removes it. + The entry's key does not change. Strings are written JSON-encoded, a + valid YAML double-quoted scalar, so a label cannot inject YAML. + """ + cleaned = _clean_label(label) + if description is not None and len(description.strip()) > 120: + raise ConfigError("A description can be at most 120 characters") + config_path = ensure_user_config(path) + _require(load_config(config_path), name) + + lines = config_path.read_text(encoding="utf-8").split("\n") + start, end, entry_indent, _ = _entry_bounds(lines, name, config_path) + key_indent = entry_indent * 2 + for i in range(start + 1, end): + if _is_content(lines[i]): + key_indent = _indent(lines[i]) + break + pad = " " * key_indent + + wanted: list[tuple[str, str | None]] = [("label", cleaned)] + if description is not None: + wanted.append(("description", description.strip() or None)) + insert_at = start + 1 + for key, value in wanted: + at = _find_key(lines, start + 1, end, key, key_indent) + if at >= 0 and re.search(r":\s*[|>&*]", lines[at]): + raise ConfigError(f"{config_path} writes {name}.{key} in a way this writer cannot edit") + if value is None: + if at >= 0: + del lines[at] + end -= 1 + continue + line = f"{pad}{key}: {json.dumps(value)}" + if at >= 0: + lines[at] = line + insert_at = at + 1 + else: + lines.insert(insert_at, line) + insert_at += 1 + end += 1 + + text = "\n".join(lines) + loaded = _checked(text, config_path, f"Could not rename {name!r}") + if loaded.presets[name].label != cleaned: + raise ConfigError(f"Could not rename {name!r}: the file sets its label somewhere else") + _atomic_write(config_path, text) + return config_path + + +def delete_preset(name: str, path: Path | None = None) -> Path: + """Remove entry *name* from the catalog. + + Refused while ``computer_use.local_worker_preset`` names it, and when + another entry reuses part of it through a YAML anchor: the file would + no longer load. When *name* was ``active_preset``, the first entry left + becomes the active one (none left: the key is cleared), the same choice + the loader would make. Removing the last entry leaves ``presets: {}``. + """ + config_path = ensure_user_config(path) + config = load_config(config_path) + _require(config, name) + if config.computer_use.local_worker_preset.strip() == name: + raise ConfigError( + f"{name!r} is the computer-use helper model (computer_use.local_worker_preset). " + "Point that at another model first" + ) + + lines = config_path.read_text(encoding="utf-8").split("\n") + start, end, _entry_indent, (header, _presets_end) = _entry_bounds(lines, name, config_path) + del lines[start:end] + remaining = [n for n in config.presets if n != name] + if not remaining: + lines[header] = "presets: {}" + + text = "\n".join(lines) + if config.active_preset == name: + successor = remaining[0] if remaining else "" + active_line = f"active_preset: {successor}" if successor else 'active_preset: ""' + active = re.compile(r"^active_preset:.*$", re.MULTILINE) + if active.search(text): + text = active.sub(active_line, text, count=1) + + loaded = _checked( + text, + config_path, + f"Could not remove {name!r}: another model in the file reuses part of it. " + "Edit config.yaml by hand", + ) + if name in loaded.presets: + raise ConfigError(f"Could not remove {name!r}: the file declares it more than once") + _atomic_write(config_path, text) + return config_path + + +def add_grok_preset(name: str, label: str, description: str, path: Path | None = None) -> Path: + """Add an entry that is the Grok engine and nothing else (TD-5258). + + ``label``, ``description`` and ``engine: grok``: the Grok CLI brings + its own model and sign-in, so the entry names no roles and no key + (TD-5239). It lands after the last entry, or opens the catalog when + there is none. ``active_preset`` is not written. + + Raises ``ConfigError`` when *name* is taken or not a slug, or an entry + already names the Grok engine: one is enough for the picker. + """ + if not CREDENTIAL_ID_RE.match(name): + raise ConfigError(f"preset name {name!r} must be a lowercase slug [a-z][a-z0-9-]{{0,31}}") + cleaned = _clean_label(label) + config_path = ensure_user_config(path) + config = load_config(config_path) + if name in config.presets: + raise ConfigError(f"A preset named {name!r} already exists") + existing = next((n for n, p in config.presets.items() if p.engine == "grok"), None) + if existing is not None: + shown = config.presets[existing].label or existing + raise ConfigError(f"Grok is already one of your models ({shown})") + + lines = config_path.read_text(encoding="utf-8").split("\n") + header = _find_key(lines, 0, len(lines), "presets", 0) + block = [f" {name}:", f" label: {json.dumps(cleaned)}"] + if description.strip(): + block.append(f" description: {json.dumps(description.strip())}") + block.append(" engine: grok") + if header < 0: + if lines and lines[-1].strip(): + lines.append("") + lines += ["presets:", *block, ""] + elif lines[header].split(":", 1)[1].split("#", 1)[0].strip() == "{}": + # The shipped empty catalog. + lines[header] = "presets:" + lines[header + 1 : header + 1] = block + else: + _header, end, entry_indent = _presets_block(lines, config_path) + pad = " " * (entry_indent - 2) + at = _content_end(lines, header, end) + lines[at:at] = [pad + line for line in block] if pad else block + + text = "\n".join(lines) + loaded = _checked(text, config_path, "Could not add Grok") + added = loaded.presets.get(name) + if added is None or added.engine != "grok": + raise ConfigError("Could not add Grok: the entry did not load") + _atomic_write(config_path, text) + return config_path diff --git a/core/tstd/protocol.py b/core/tstd/protocol.py index aa6e352f..79724e24 100644 --- a/core/tstd/protocol.py +++ b/core/tstd/protocol.py @@ -539,8 +539,11 @@ class SetSkipAllApprovals(ClientMessage): """Turn skip-all approvals on or off (TD-804). Machine-wide, no session. The daemon answers with a refreshed - ``setup_state`` so the toggle is honest after restart. Class C and - ``never`` rules are unaffected. + ``setup_state`` so the toggle is honest after restart. On releases + every parked call, Class C included; a ``never`` rule still refuses. + From a paired phone, turning it on is the phone permission + ``approval_switches`` (``phone_permission_off`` without it, TD-5283); + turning it off is open to a phone. """ type: Literal["set_skip_all_approvals"] = "set_skip_all_approvals" @@ -698,6 +701,28 @@ class RemoveRemoteDevice(ClientMessage): type: Literal["remove_remote_device"] = "remove_remote_device" device_id: str = Field(min_length=1) + #: Also remove the MCP servers this device added or changed (TD-5279). + #: Honoured from the computer only; a phone forgetting itself leaves them. + remove_added: bool = False + + +class SetPhonePermission(ClientMessage): + """Switch one phone permission on or off (TD-5279). Loopback only. + + Machine-wide, no session. A phone permission lets a paired phone send + verbs that are otherwise refused from one with ``phone_permission_off`` + (``mcp_servers``: add, change and remove MCP servers and their values). + ``expires_in_seconds`` makes the grant run out on its own; absent, it + lasts until switched off. Persists + ``{user_data_dir}/phone-permissions.yaml``; acked with ``setup_state``. + An unknown capability or an expiry over seven days is ``bad_request``. + No phone, legacy-token client or other computer may send it. + """ + + type: Literal["set_phone_permission"] = "set_phone_permission" + capability: str = Field(min_length=1) + enabled: bool + expires_in_seconds: int | None = Field(default=None, ge=1) class AddMachine(ClientMessage): @@ -889,6 +914,57 @@ class SetSessionPreset(ClientMessage): name: str = Field(min_length=1) +class GetPresetRoles(ClientMessage): + """Ask for one catalog entry's roles, to edit it (TD-5241). + + ``setup_state`` describes the active entry's roles. Settings → Model can + edit any entry, so it asks for the one it is showing. Read-only. + Answered with ``preset_roles``. + """ + + type: Literal["get_preset_roles"] = "get_preset_roles" + name: str = Field(min_length=1) + + +class CheckPresets(ClientMessage): + """Ask whether each model on the picker can run right now (TD-5240). + + Sent when the person opens the picker. The daemon looks for each bound + key in the keychain (presence only), asks each server on this computer + whether it is there, and looks for the Grok CLI for a Grok entry. No + key is sent anywhere and no provider is contacted. Answered with + ``preset_readiness``. + """ + + type: Literal["check_presets"] = "check_presets" + + +class SwitchPreset(ClientMessage): + """One switch: this chat, the model new chats use, or both (TD-5235). + + ``session_id`` names the chat to switch; ``make_default`` also (or only) + makes ``name`` the model new chats use. Everything is checked before + anything changes, so "both" never half-applies. For the chat: + + - idle: switched now, with the entry's context settings and any effort + it names (TD-5234); + - a turn running: kept as pending and applied when that turn completes, + so the next turn runs on it. ``tier_state.pending_preset`` says so. + A later switch replaces it; one back to the chat's own model cancels; + - no live loop (restored with no conversation): the name is recorded. + + A chat keeps its engine (TD-5239), so an entry on another engine is + refused with ``engine_mismatch``; ``new_session`` with ``preset`` + starts a chat on it instead. Acked with ``setup_state`` when the + default changed, else ``session_list``. + """ + + type: Literal["switch_preset"] = "switch_preset" + name: str = Field(min_length=1) + session_id: str | None = None + make_default: bool = False + + class GetInstructionStack(ClientMessage): """Request the current instruction stack for a session.""" @@ -1117,6 +1193,9 @@ class NewSession(ClientMessage): type: Literal["new_session"] = "new_session" session_id: str + # TD-5235: the catalog entry the new chat opens on, with that entry's + # engine. None is the model new chats use, as before. + preset: str | None = None class ArchiveSession(ClientMessage): @@ -1328,6 +1407,61 @@ class SetPreset(ClientMessage): name: str = Field(min_length=1) +class DuplicatePreset(ClientMessage): + """Copy one catalog entry as a new one (TD-5237). + + Settings' "Duplicate". The copy gets ``label`` and a key the daemon + derives from it; every role is written out in full, so it shares + nothing with ``source``. The active entry does not change. Narrow: a + name and a label cannot carry a secret. Acked with ``setup_state``. + """ + + type: Literal["duplicate_preset"] = "duplicate_preset" + source: str = Field(min_length=1) + label: str = Field(min_length=1, max_length=40) + + +class SetPresetLabel(ClientMessage): + """Give one catalog entry a new label (TD-5237). + + Settings' "Rename". The entry's key is its permanent id (sessions and + scheduled jobs store it) and never changes. ``description`` omitted + keeps the stored one; empty removes it. Acked with ``setup_state``. + """ + + type: Literal["set_preset_label"] = "set_preset_label" + name: str = Field(min_length=1) + label: str = Field(min_length=1, max_length=40) + description: str | None = Field(default=None, max_length=120) + + +class AddGrokPreset(ClientMessage): + """Put the Grok CLI on the model list (TD-5258). + + Settings → Engine, "Add Grok to your models". Writes one catalog entry + that is ``engine: grok`` and names no model or key (TD-5239). Refused + when the Grok CLI is not found on this computer, or an entry already + names the Grok engine. The model new chats use does not change. Acked + with ``setup_state``. + """ + + type: Literal["add_grok_preset"] = "add_grok_preset" + label: str = Field(default="Grok", min_length=1, max_length=40) + + +class DeletePreset(ClientMessage): + """Remove one catalog entry (TD-5237). + + Refused while ``computer_use.local_worker_preset`` names it or another + entry reuses part of it. Removing the active entry makes the first one + left active. A chat already on it keeps the model it opened with until + it is switched. No key is deleted. Acked with ``setup_state``. + """ + + type: Literal["delete_preset"] = "delete_preset" + name: str = Field(min_length=1) + + class SetEngine(ClientMessage): """Pick the agent engine for new sessions (native TST loop or Grok ACP).""" @@ -1601,6 +1735,47 @@ class DeleteMcpSecret(ClientMessage): name: str = Field(min_length=1) +class PreviewMcpImport(ClientMessage): + """Read MCP servers out of pasted JSON and say what importing would do (TD-5253). + + ``text`` is the block from a server's README (``mcpServers``, VS Code's + ``servers``, or one bare entry; ``//`` comments and trailing commas are + allowed). Nothing is saved or started. Answered with + ``mcp_import_preview``, which never carries a value. From a paired + phone this needs the ``mcp_servers`` phone permission. + """ + + type: Literal["preview_mcp_import"] = "preview_mcp_import" + text: str = Field(min_length=1, max_length=262_144, repr=False) + + +class McpImportChoice(BaseModel): + """What the person decided for one previewed server (TD-5253).""" + + source_name: str = Field(min_length=1) + id: str = Field(min_length=1) + #: Required true for a server on another machine. Off by default. + allow_remote: bool = False + #: Values the preview asked for, by name. Stored in the keychain. + inputs: dict[str, str] = Field(default_factory=dict, repr=False) + + +class ImportMcpServers(ClientMessage): + """Save the chosen servers from the same pasted JSON (TD-5253). + + The daemon parses ``text`` again and checks every choice itself. Each + token goes to the keychain and ``config.yaml`` gets ``keychain:``; + a server is saved whole or not at all. An id already listed, an + off-box server without ``allow_remote``, a missing value or a refused + server is ``bad_request`` and nothing is written. Acked with + ``setup_state``. Same phone permission as ``set_mcp_server``. + """ + + type: Literal["import_mcp_servers"] = "import_mcp_servers" + text: str = Field(min_length=1, max_length=262_144, repr=False) + servers: list[McpImportChoice] = Field(min_length=1, max_length=50) + + class RunDiagnostics(ClientMessage): """Ask the daemon to run the doctor checks (TD-1104 diagnostics). @@ -2486,6 +2661,32 @@ class PolicyRuleSummary(BaseModel): exact: str | None = None +class ApprovalWorker(BaseModel): + """Which child is asking, on a child's approval card (TD-5268). + + A ``delegate`` or ``workflow`` child's call parks on its parent's + session, and the card used to read as if the session's own agent had + asked. ``id`` is the prefix of the child's tool-call ids; + ``parent_call_id`` is the ``delegate`` or ``workflow`` call that started + it, so a window can drop the card when that call returns. ``tier`` is + the tier's name, never a model slug. + """ + + id: str + parent_call_id: str + via: Literal["delegate", "workflow"] + kind: Literal["worker", "research", "check"] + agent: str | None = None + # A workflow task's number, from 1. None for a ``delegate`` child. + index: int | None = None + label: str = "" + task: str = "" + tier: str = "" + # Set when the child works in a worktree of its own: the paths on the + # card are in that copy, not in the session's folder. + worktree_branch: str | None = None + + class ApprovalRequest(DaemonEvent): """A request for user approval of a tool call (TD-802). @@ -2511,6 +2712,9 @@ class ApprovalRequest(DaemonEvent): summary: str reason: str proposed_always_allow: PolicyRuleSummary | None = None + # TD-5268: the child that is asking. None: the session's own agent. + # Additive; an older viewer reads the same thing in ``summary``. + worker: ApprovalWorker | None = None class QuestionOption(BaseModel): @@ -2703,6 +2907,9 @@ class TierState(DaemonEvent): # tier name → configured model slug, "brain"/"worker"/"validator". model_slugs: dict[str, str] = Field(default_factory=dict) preset: str = "" + # TD-5235: a switch asked for while a turn was running. It applies when + # that turn completes. None when nothing is waiting. Additive. + pending_preset: str | None = None # tier name → hostname:port the provider client will call. hosts: dict[str, str] = Field(default_factory=dict) # Plan mode (TD-4603): when true, every completion is brain until @@ -3591,6 +3798,10 @@ class McpServerSummary(BaseModel): #: Names only (TD-5110). Additive; an older client ignores them. env: list[McpValueSummary] = Field(default_factory=list) headers: list[McpValueSummary] = Field(default_factory=list) + #: The phone this server was last added or changed from, and when + #: (TD-5279). Empty for one only the computer has touched. + origin_device: str = "" + origin_at: str = "" class RemoteDeviceSummary(BaseModel): @@ -3608,6 +3819,20 @@ class RemoteDeviceSummary(BaseModel): # TD-5015: "machine" is another computer's hub. A file entry written # before the field reads as a phone. kind: Literal["phone", "machine"] = "phone" + #: MCP servers this device added or changed that are still listed + #: (TD-5279): what removing it would leave behind. + mcp_servers_added: list[str] = Field(default_factory=list) + + +class PhonePermissionState(BaseModel): + """One phone permission as ``setup_state`` shows it (TD-5279). + + ``enabled`` is whether a phone may use it right now: a grant whose + ``expires_at`` has passed reads as off. + """ + + enabled: bool = False + expires_at: str | None = None class EmailNotifyState(BaseModel): @@ -3703,6 +3928,16 @@ class SetupState(DaemonEvent): # wizard opens on "Add a model". Additive; an older client ignores them. preset_descriptions: dict[str, str] = Field(default_factory=dict) team_presets: list[str] = Field(default_factory=list) + # TD-5239: the engine of each preset that names one. A preset left out + # runs on ``engine``. Additive; an older client shows no mark. + preset_engines: dict[str, Literal["native", "grok"]] = Field(default_factory=dict) + # TD-5236: the picker section of every preset (its own ``group``, else + # Grok, Local or API as the daemon reads the entry), the order the + # built-in sections come in, and each preset's quick-switch slot, 1 to + # 9, for those that have one. Additive; an older client shows one list. + preset_groups: dict[str, str] = Field(default_factory=dict) + picker_groups: list[str] = Field(default_factory=list) + preset_slots: dict[str, int] = Field(default_factory=dict) has_model: bool = True # TD-804: skip-all approvals. Additive, default off — an older client # that ignores the field keeps asking, which is the safe read. @@ -3770,6 +4005,10 @@ class SetupState(DaemonEvent): # TD-4403: listed MCP servers. Additive, default empty. Never a secret; # env and headers are names only (TD-5110). An older client ignores it. mcp_servers: list[McpServerSummary] = Field(default_factory=list) + # TD-5279: what a paired phone may change, by capability. Every known + # capability is listed, on or off. A phone reads it too, so its pages + # can say why a control is unavailable. Additive, default empty. + phone_permissions: dict[str, PhonePermissionState] = Field(default_factory=dict) # TD-4701: hold-to-talk. Additive, default off. ``speech_ready`` is # enabled plus a configured base_url — the URL itself never leaves # the daemon. An older client ignores both fields. @@ -3807,6 +4046,50 @@ class PairingCode(DaemonEvent): expires_at: str +class McpImportValue(BaseModel): + """One env var or header in an import preview. Never its value.""" + + name: str + target: Literal["env", "header"] + #: ``keychain``: stored as a secret. ``literal``: written to config.yaml + #: (short, and not named like a secret). ``needs_input``: type it. + disposition: Literal["keychain", "literal", "needs_input"] + reason: str = "" + + +class McpImportServer(BaseModel): + """One server an import preview found (TD-5253).""" + + source_name: str + id: str + transport: Literal["stdio", "http"] = "stdio" + command: list[str] = Field(default_factory=list) + url: str = "" + #: On another machine: importing needs the person's explicit tick. + off_box: bool = False + #: An id already listed; give it another before importing. + exists: bool = False + values: list[McpImportValue] = Field(default_factory=list) + #: Keys of the snippet that are left out, such as ``envFile`` or ``cwd``. + not_imported: list[str] = Field(default_factory=list) + notes: list[str] = Field(default_factory=list) + #: Why this server cannot be imported; empty when it can. + refused: str = "" + + +class McpImportPreview(DaemonEvent): + """Reply to ``preview_mcp_import`` (TD-5253). Connection-scoped, seq 1. + + ``error`` is set, and ``servers`` empty, when the text could not be + read; it gives a position, never the text. + """ + + type: Literal["mcp_import_preview"] = "mcp_import_preview" + seq: int = 1 + servers: list[McpImportServer] = Field(default_factory=list) + error: str = "" + + class PairingCompleted(DaemonEvent): """A phone redeemed the code (TD-5014). Broadcast to loopback clients only.""" @@ -3815,6 +4098,21 @@ class PairingCompleted(DaemonEvent): device: RemoteDeviceSummary +class PhoneChange(DaemonEvent): + """A paired phone used a phone permission (TD-5279). + + Broadcast to loopback clients only, each time: the person at the + computer is told what a phone changed and which phone. ``summary`` is + one sentence without the device's name. Connection-scoped, seq 1. + """ + + type: Literal["phone_change"] = "phone_change" + seq: int = 1 + device_name: str + capability: str + summary: str + + MachineStatus = Literal["connected", "connecting", "unreachable", "refused", "locked", "outdated"] @@ -4039,6 +4337,45 @@ class ModelTested(DaemonEvent): detail: str = "" +class PresetRoles(DaemonEvent): + """Reply to ``get_preset_roles`` (TD-5241): one entry's roles. + + The same four maps ``setup_state`` carries for the active entry, for + entry ``preset``: each role's model, its bound key, whether it is + served on this computer, and its stored numbers. All empty for an + entry with no roles (the Grok engine, TD-5239). Never a key's value. + """ + + type: Literal["preset_roles"] = "preset_roles" + seq: int = 1 + preset: str + tier_slugs: dict[str, str | None] = Field(default_factory=dict) + tier_credentials: dict[str, str | None] = Field(default_factory=dict) + tier_loopback: dict[str, bool] = Field(default_factory=dict) + tier_cards: dict[str, TierCard] = Field(default_factory=dict) + + +class PresetReady(BaseModel): + """Whether one catalog entry can run right now (TD-5240).""" + + preset: str + # ``no_grok``: on the Grok engine and the CLI is not found. ``no_key``: + # a role's key is not in the keychain. ``server_down``: a role is served + # on this computer and nothing answers there. + status: Literal["ready", "no_key", "server_down", "no_grok"] + # One sentence for the picker's row. Empty when ready. Never a key, and + # of a URL only the host and port. + detail: str = "" + + +class PresetReadiness(DaemonEvent): + """Reply to ``check_presets`` (TD-5240): one row per catalog entry.""" + + type: Literal["preset_readiness"] = "preset_readiness" + seq: int = 1 + rows: list[PresetReady] = Field(default_factory=list) + + class ConfigEditor(DaemonEvent): """Settings → Configuration's view of config.yaml (TD-5203). @@ -4092,10 +4429,12 @@ class DiagnosticCheck(BaseModel): ``skip`` means the check did not apply (no key to validate, no workspace open) — it is not a failure and should not alarm. ``fix`` is the concrete remedy; present exactly when status is ``fail``. + ``warn`` (TD-5279) is a setting that works as chosen and is worth + knowing about, such as a phone permission left on. """ name: str - status: Literal["ok", "fail", "skip"] + status: Literal["ok", "fail", "skip", "warn"] detail: str fix: str | None = None @@ -4822,6 +5161,9 @@ class GoogleStatus(DaemonEvent): | SetUltracode | SetPlan | SetSessionPreset + | SwitchPreset + | CheckPresets + | GetPresetRoles | GetInstructionStack | GetHooks | ListInstructions @@ -4861,6 +5203,10 @@ class GoogleStatus(DaemonEvent): | AddModel | TestModel | SetPreset + | DuplicatePreset + | AddGrokPreset + | SetPresetLabel + | DeletePreset | SetEngine | SetGrokMode | RunGrokCommand @@ -4896,6 +5242,9 @@ class GoogleStatus(DaemonEvent): | StartPairing | CancelPairing | RemoveRemoteDevice + | SetPhonePermission + | PreviewMcpImport + | ImportMcpServers | AddMachine | RemoveMachine | ListMachines @@ -4985,12 +5334,16 @@ class GoogleStatus(DaemonEvent): | SetupState | PairingCode | PairingCompleted + | PhoneChange + | McpImportPreview | MachineList | DrainResult | ApiKeyValidated | EndpointModels | LocalServers | ModelTested + | PresetReadiness + | PresetRoles | ConfigEditor | ConfigValueRejected | DiagnosticsReport @@ -5076,6 +5429,9 @@ class GoogleStatus(DaemonEvent): "set_ultracode", "set_plan", "set_session_preset", + "switch_preset", + "check_presets", + "get_preset_roles", "get_instruction_stack", "get_hooks", "list_instructions", @@ -5116,6 +5472,10 @@ class GoogleStatus(DaemonEvent): "test_model", "delete_api_key", "set_preset", + "duplicate_preset", + "add_grok_preset", + "set_preset_label", + "delete_preset", "set_engine", "set_grok_mode", "run_grok_command", @@ -5150,6 +5510,9 @@ class GoogleStatus(DaemonEvent): "start_pairing", "cancel_pairing", "remove_remote_device", + "set_phone_permission", + "preview_mcp_import", + "import_mcp_servers", "add_machine", "remove_machine", "list_machines", @@ -5239,12 +5602,16 @@ class GoogleStatus(DaemonEvent): "setup_state", "pairing_code", "pairing_completed", + "phone_change", + "mcp_import_preview", "machine_list", "drain_result", "api_key_validated", "endpoint_models", "local_servers", "model_tested", + "preset_readiness", + "preset_roles", "config_editor", "config_value_rejected", "diagnostics_report", @@ -5310,6 +5677,8 @@ def _check_known_type(data: dict[str, Any], known_types: frozenset[str]) -> None "delete_mcp_server", "set_mcp_secret", "delete_mcp_secret", + "preview_mcp_import", + "import_mcp_servers", "set_config_value", "reset_config_value", } diff --git a/core/tstd/provider.py b/core/tstd/provider.py index 2f9c5b10..486e67bc 100644 --- a/core/tstd/provider.py +++ b/core/tstd/provider.py @@ -11,10 +11,11 @@ from __future__ import annotations import asyncio +import contextlib import json import random import re -from collections.abc import AsyncIterator, Awaitable, Callable, Sequence +from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, Sequence from dataclasses import dataclass, field, replace from datetime import UTC, datetime from email.utils import parsedate_to_datetime @@ -390,6 +391,10 @@ class Usage: completion_tokens: int = 0 cached_prompt_tokens: int | None = None total_tokens: int = 0 + #: Prompt tokens the provider stored in its cache on this call + #: (TD-5280), part of ``prompt_tokens``. Some providers charge more + #: for these than for plain input. None when the response named none. + cache_write_tokens: int | None = None @classmethod def from_api_dict(cls, data: dict[str, Any] | None) -> Usage: @@ -398,11 +403,15 @@ def from_api_dict(cls, data: dict[str, Any] | None) -> Usage: return cls() details = data.get("prompt_tokens_details") cached = details.get("cached_tokens") if isinstance(details, dict) else None + written = details.get("cache_write_tokens") if isinstance(details, dict) else None + if isinstance(written, bool) or not isinstance(written, int) or written < 0: + written = None return cls( prompt_tokens=data.get("prompt_tokens", 0), completion_tokens=data.get("completion_tokens", 0), cached_prompt_tokens=cached, total_tokens=data.get("total_tokens", 0), + cache_write_tokens=written, ) @@ -1098,6 +1107,9 @@ def __init__( self.timeout = timeout or TimeoutConfig() self.retry_config = retry_config or RetryConfig() + # TD-5269: the count this server's calls share, daemon-wide. None is + # no limit. Set by whoever builds the client (``Daemon._build_client``). + self.gate: asyncio.Semaphore | None = None self._client = client or httpx.AsyncClient( timeout=self.timeout.to_httpx_timeout(), follow_redirects=True, @@ -1138,6 +1150,12 @@ async def chat_completion( body.pop("stream_options", None) async def _attempt() -> ChatCompletionResponse | ProviderError: + # One slot for this one exchange (TD-5269): the wait before a + # retry is spent outside it. + async with self._slot(): + return await _exchange() + + async def _exchange() -> ChatCompletionResponse | ProviderError: try: async with asyncio.timeout(self.timeout.total): response = await self._client.post( @@ -1242,6 +1260,18 @@ async def chat_completion_stream( body["stream_options"] = {"include_usage": True} async def _stream_once() -> AsyncIterator[StreamChunk | ProviderError]: + # Held from the request to the stream's end (TD-5269): the server + # is busy with this call for that long. Waiting for a slot is not + # part of the reply's own time limit. + # ``aclosing``: when the consumer stops early, or a stall is + # returned, the exchange is closed here and now, which is what + # hangs up the connection. Left to the garbage collector, the + # server would go on generating into a socket nobody reads. + async with self._slot(), contextlib.aclosing(_stream_exchange()) as exchange: + async for chunk in exchange: + yield chunk + + async def _stream_exchange() -> AsyncGenerator[StreamChunk | ProviderError, None]: started = asyncio.get_running_loop().time() try: async with aconnect_sse( @@ -1618,6 +1648,16 @@ def _inband_stream_error( resendable=error.retryable, ) + @contextlib.asynccontextmanager + async def _slot(self) -> AsyncIterator[None]: + """Hold one of this server's slots, or nothing when it has no limit (TD-5269).""" + gate = self.gate + if gate is None: + yield + return + async with gate: + yield + def _headers(self) -> dict[str, str]: """Build the request headers. diff --git a/core/tstd/scheduler/pin.py b/core/tstd/scheduler/pin.py index e0af0db9..553aaa2b 100644 --- a/core/tstd/scheduler/pin.py +++ b/core/tstd/scheduler/pin.py @@ -125,11 +125,38 @@ def pin_failure_summary( """ if pin.preset is not None and pin.preset not in catalog: return f"preset '{pin.preset}' no longer exists" - if pin.engine == "grok" and not grok_available: + if pin.engine == "native" and not pinned_entry_has_model(pin, catalog): + # TD-5239: an entry may be the Grok engine with no model of its own. + return NO_MODEL_SUMMARY.format(preset=pin.preset) + if pinned_engine(pin, catalog) == "grok" and not grok_available: return "grok engine is unavailable" return None +#: The receipt for a job pinned to TST Desk's own loop on an entry that has no model. +NO_MODEL_SUMMARY = "preset '{preset}' has no model of its own; it runs only on the grok engine" + + +def pinned_engine(pin: ScheduledPin, catalog: Mapping[str, object]) -> EngineKind | None: + """The engine this pin asks for: its own, else its entry's, else None (TD-5239). + + None means neither names one, and the session takes the window's. + """ + if pin.engine is not None: + return pin.engine + if pin.preset is None: + return None + named = getattr(catalog.get(pin.preset), "engine", None) + return named if named in ("native", "grok") else None + + +def pinned_entry_has_model(pin: ScheduledPin, catalog: Mapping[str, object]) -> bool: + """False only for a pinned entry that names no model per role (TD-5239).""" + if pin.preset is None: + return True + return bool(getattr(catalog.get(pin.preset), "has_tiers", True)) + + def grok_cli_available(binary: str) -> bool: """Whether the pinned Grok engine can start. The path stays out of the receipt.""" from ..grok_acp import GrokEngineError, find_grok_binary @@ -144,11 +171,12 @@ def grok_cli_available(binary: str) -> bool: async def scheduled_run_block(catalog: Mapping[str, object], *, grok_binary: str) -> str | None: """The receipt for a pin that cannot start, or None to open the session. - The Grok probe runs only when the job pinned Grok. A window that is - on Grok while the job pinned native is not probed. + The Grok probe runs only when the job pinned Grok, itself or through + an entry whose engine is Grok (TD-5239). A window that is on Grok while + the job pinned native is not probed. """ pin = current_scheduled_pin() grok_ok = True - if pin.engine == "grok": + if pinned_engine(pin, catalog) == "grok": grok_ok = await asyncio.to_thread(grok_cli_available, grok_binary) return pin_failure_summary(pin, catalog=catalog, grok_available=grok_ok) diff --git a/core/tstd/scheduler/runner.py b/core/tstd/scheduler/runner.py index da56191a..f5a63317 100644 --- a/core/tstd/scheduler/runner.py +++ b/core/tstd/scheduler/runner.py @@ -688,4 +688,6 @@ def _pin_block_code(summary: str) -> str: """Stable code for a pin that cannot start. The summary stays the receipt.""" if summary == "grok engine is unavailable": return "engine_unavailable" + if summary.endswith("it runs only on the grok engine"): + return "engine_mismatch" return "preset_missing" diff --git a/core/tstd/session.py b/core/tstd/session.py index 1f720fac..434b52e2 100644 --- a/core/tstd/session.py +++ b/core/tstd/session.py @@ -34,6 +34,7 @@ stricter_rule, ) from .protocol import ( + ApprovalWorker, ConversationReset, DaemonEvent, LineCommentAnchor, @@ -149,6 +150,10 @@ class PendingApproval: tool: Tool | None arguments: dict[str, Any] decision_class: DecisionClass + #: A delegate or workflow child is asking (TD-5281). Always allow saves + #: a rule for the whole workspace, so it is never offered from, nor + #: saved for, a child's call. + from_worker: bool = False def _same_messages(a: list[ChatMessage], b: list[ChatMessage]) -> bool: @@ -525,6 +530,9 @@ def __init__(self, workspace_path: str) -> None: # entry, not a fork written back to Settings. self.preset: str = "" self.config: ModelConfig | None = None + # TD-5235: an entry asked for while a turn was running. The daemon + # binds it when that turn completes. Not kept across a restart. + self.pending_preset: str | None = None # TD-2603: machine-wide opt-in. The daemon stamps this on open # and when the Settings toggle flips. self.load_global_memory = False @@ -877,9 +885,13 @@ async def request_approval( decision_class: DecisionClass, summary: str, reason: str, + *, + worker: ApprovalWorker | None = None, ) -> ApprovalOutcome: """Park a tool call awaiting user approval. + *worker* is the child that is asking (TD-5268); the card names it. + Logs the ``approval_request`` event (so reattaching clients replay it), parks the session in ``awaiting_approval``, and awaits a bare future — no spin, no poll. With ``approval_timeout_seconds`` set, @@ -894,7 +906,11 @@ async def request_approval( return denial fut: asyncio.Future[tuple[bool, str | None]] = asyncio.get_running_loop().create_future() self._pending_approvals[tool_call_id] = PendingApproval( - future=fut, tool=tool, arguments=arguments, decision_class=decision_class + future=fut, + tool=tool, + arguments=arguments, + decision_class=decision_class, + from_worker=worker is not None, ) # The rule "always allow" would write (TD-803), surfaced on the # card so the user sees it before committing to save it. None for @@ -908,6 +924,13 @@ async def request_approval( proposed = None if proposed is not None and not await self._config_writable(): proposed = None + # Nor on a child's card (TD-5281). The rule is for the whole + # workspace: saved from one worker's request it would cover the main + # agent and every other worker. A named worker may also be asking + # because of its own rule, which a workspace rule cannot lift, and a + # path in a child's worktree is in a folder that goes when it ends. + if worker is not None: + proposed = None await self.event_log.add( ApprovalRequestEvent( session_id=self.id, @@ -918,6 +941,7 @@ async def request_approval( summary=summary, reason=reason, proposed_always_allow=rule_summary(proposed) if proposed is not None else None, + worker=worker, seq=1, ) ) diff --git a/core/tstd/shell_sandbox.py b/core/tstd/shell_sandbox.py index 3edd0b12..df3b9c7a 100644 --- a/core/tstd/shell_sandbox.py +++ b/core/tstd/shell_sandbox.py @@ -397,6 +397,11 @@ def add(p: Path) -> None: return out +# The mode of a mask over a directory the workspace is inside (TD-5261): +# search only, so the path to the workspace resolves and nothing is listed. +_PASS_THROUGH = "0111" + + def hidden_paths( section: SandboxSection, home: Path | None = None, extra: Sequence[Path] = () ) -> list[Path]: @@ -435,10 +440,29 @@ def build_plan( Order matters: bwrap applies mounts in sequence, so the workspace bind comes after the read-only root and the masks come last, which keeps ``~/.ssh`` covered even when the workspace is the home directory. + + A hidden directory that *contains* the workspace is the other way round + (TD-5261): a session's worktree lives under TST Desk's data dir by + default, and a mask laid over the workspace bind left every command + unable to enter its own folder. Such a directory is masked first, with + a tmpfs that can be passed through but not listed, and the workspace is + then bound into it. Everything else that was in it is still gone. *seccomp* defaults to ``seccomp_file()``. """ real_ws = Path(workspace).resolve() filter_path = seccomp if seccomp is not None else seccomp_file() + hidden = hidden_paths(section, home, extra_hidden) + # Judged on resolved paths, each directory once (TD-5278): the hidden + # list holds a symlinked directory under both its names, and the + # unresolved one, left for last, would cover the workspace again. + around: list[Path] = [] + for path in hidden: + real = path.resolve() + contains = real.is_dir() and real != real_ws and real_ws.is_relative_to(real) + if contains and real not in around: + around.append(real) + # Outermost first, so an inner one is mounted inside the outer. + around.sort(key=lambda p: len(p.parts)) argv = [ bwrap, "--die-with-parent", @@ -455,17 +479,31 @@ def build_plan( "/proc", "--tmpfs", "/tmp", - "--bind", - str(real_ws), - str(real_ws), ] + for path in around: + argv += ["--perms", _PASS_THROUGH, "--tmpfs", str(path)] + argv += ["--bind", str(real_ws), str(real_ws)] for pin in _pins(real_ws): argv += ["--ro-bind", str(pin), str(pin)] - for path in hidden_paths(section, home, extra_hidden): + # Once the workspace is bound into it, the mask itself is closed to + # writes: nothing can be put beside the workspace or re-moded (TD-5278). + for path in around: + argv += ["--remount-ro", str(path)] + for path in hidden: + if path.resolve() in around: + continue if path.is_dir(): argv += ["--perms", "0000", "--tmpfs", str(path)] else: argv += ["--ro-bind", "/dev/null", str(path)] + # TODO(TD-5264): pass --disable-userns once it has been tried on Fedora + # or Arch. A command that can make a user namespace of its own inside + # the sandbox can undo a read-only bind there. Ubuntu's kernel policy + # already refuses that (checked on this project's Linux machine, kernel + # 7.0, bubblewrap 0.11.1); distributions that allow unprivileged user + # namespaces everywhere do not. The flag needs bubblewrap 0.8 or newer + # and a version probe, and nobody has run it where it matters, so it + # is left out rather than shipped untested. for name in _UNSET_ENV: argv += ["--unsetenv", name] argv += ["--chdir", workspace, "--", "/bin/sh", "-c", command] diff --git a/core/tstd/shown_artifacts.py b/core/tstd/shown_artifacts.py new file mode 100644 index 00000000..09aebc7c --- /dev/null +++ b/core/tstd/shown_artifacts.py @@ -0,0 +1,144 @@ +"""What ``show_artifact`` puts in the Artifacts pane (TD-5254). + +Two doors, the two the store has always had. ``path`` points at a text +file in the workspace: no copy is made, so the pane shows the file as it +is now. ``content`` is text the model hands over, kept in the session's +own data dir under a name the daemon makes; the model's ``name`` is a +label that picks the preview, never a place on disk. + +Text only. A file that is not UTF-8, too big, a folder or not a regular +file is refused, and the refusal points at ``send_file``, which is the +tool for handing over a file as it is. +""" + +from __future__ import annotations + +import stat +from collections.abc import Callable +from pathlib import Path +from typing import TYPE_CHECKING + +from .artifacts import ArtifactError, ArtifactRecord, ArtifactStore +from .autonomy.classifier import Boundary +from .sent_files import mime_for, safe_file_name +from .tools.boundary import PathGuard, RefusalError + +if TYPE_CHECKING: + from .config import SessionConfig + +_OCTET = "application/octet-stream" +_NOT_TEXT = "show_artifact shows text. To hand over a file as it is, use send_file." + + +class ShownArtifacts: + """Validates and records one shown artifact.""" + + def __init__(self, store: ArtifactStore, config: Callable[[], SessionConfig]) -> None: + self._store = store + self._config = config + self._guard: Callable[[], PathGuard | None] | None = None + + def bind_guard(self, guard: Callable[[], PathGuard | None]) -> None: + """Re-check a path with the dispatcher's own guard, read at each call.""" + self._guard = guard + + def show( + self, + session_id: str, + workspace: Path, + *, + title: str, + path: str | None = None, + content: str | None = None, + name: str | None = None, + caption: str | None = None, + tool_call_id: str = "", + ) -> ArtifactRecord: + """Record the artifact. ArtifactError names what is wrong with the ask.""" + config = self._config() + limit = config.artifact_max_bytes + if (path is None) == (content is None): + raise ArtifactError("bad_request", "Give path or content, not both and not neither.") + if content is not None: + if not name or not name.strip(): + raise ArtifactError( + "bad_request", "Give name with content, such as summary.md or chart.html." + ) + data = content.encode("utf-8", errors="strict") + if len(data) > limit: + raise ArtifactError("artifact_too_large", _too_large(len(data), limit)) + shown_name = safe_file_name(name) + return self._store.record( + session_id, + title, + _text_mime(shown_name), + workspace, + content=data, + name=shown_name, + caption=caption or None, + tool_call_id=tool_call_id or None, + upsert=True, + max_count=config.artifact_max_count, + max_session_bytes=config.send_file_session_max_bytes, + ) + assert path is not None + checked = self._checked(path, workspace) + self._require_text(checked, limit) + return self._store.record( + session_id, + title, + _text_mime(checked.name), + workspace, + path=str(checked), + name=safe_file_name(checked.name), + caption=caption or None, + tool_call_id=tool_call_id or None, + upsert=True, + max_count=config.artifact_max_count, + ) + + def _checked(self, raw: str, workspace: Path) -> Path: + guard = self._guard() if self._guard is not None else None + if guard is None: + guard = PathGuard(Boundary(workspace_root=workspace)) + try: + return guard.check_read(raw) + except RefusalError as exc: + raise ArtifactError(exc.code, exc.reason) from exc + + @staticmethod + def _require_text(path: Path, limit: int) -> None: + try: + info = path.lstat() + except OSError: + raise ArtifactError("artifact_not_found", f"No such file: {path.name}") from None + if stat.S_ISDIR(info.st_mode): + raise ArtifactError("not_a_file", f"{path.name} is a folder. {_NOT_TEXT}") + if not stat.S_ISREG(info.st_mode): + raise ArtifactError("not_a_file", f"{path.name} is not a regular file. {_NOT_TEXT}") + if info.st_size > limit: + raise ArtifactError("artifact_too_large", _too_large(info.st_size, limit)) + try: + with path.open("rb") as handle: + data = handle.read(limit + 1) + except OSError as exc: + raise ArtifactError("artifact_not_found", f"Could not read {path.name}: {exc}") from exc + if len(data) > limit: + raise ArtifactError("artifact_too_large", _too_large(len(data), limit)) + try: + data.decode("utf-8", errors="strict") + except UnicodeDecodeError: + raise ArtifactError("not_text", f"{path.name} is not text. {_NOT_TEXT}") from None + + +def _text_mime(name: str) -> str: + """The table's type for *name*; text that the table does not know is plain text.""" + mime = mime_for(name) + return "text/plain" if mime == _OCTET else mime + + +def _too_large(size: int, limit: int) -> str: + return ( + f"That is {size} bytes; an artifact holds at most {limit}. " + "Show a shorter piece, or use send_file for the whole file." + ) diff --git a/core/tstd/tools/delegate.py b/core/tstd/tools/delegate.py index e925071c..e4ba41a8 100644 --- a/core/tstd/tools/delegate.py +++ b/core/tstd/tools/delegate.py @@ -34,7 +34,8 @@ from dataclasses import dataclass, replace from typing import TYPE_CHECKING, Any -from ..config import ModelConfig, TierConfig, TierName +from ..autonomy import DecisionClass +from ..config import DEFAULT_CHILD_MAX_ITERATIONS, ModelConfig, TierConfig, TierName from ..context import PromptAssembler from ..context.tokens import make_token_counter from ..effort import with_effort @@ -46,8 +47,9 @@ split_cut_calls, stub_call, ) +from ..policy import ApprovalOutcome from ..prompt_images import cap_prompt_images -from ..protocol import WorkerStep +from ..protocol import ApprovalWorker, WorkerStep from ..provider import ChatCompletionRequest, ChatMessage, ProviderError, ToolCall from ..session import Session from ..worktree import SessionWorktree @@ -111,7 +113,8 @@ log = get_logger("tstd.delegate") -CHILD_MAX_ITERATIONS = 8 +# The default; ``delegate.max_iterations`` in config.yaml is what a child runs with (TD-5265). +CHILD_MAX_ITERATIONS = DEFAULT_CHILD_MAX_ITERATIONS @dataclass @@ -159,6 +162,11 @@ class ChildTask: tier: TierName | None = None source: str = "worker" transcript_budget: int = TRANSCRIPT_CHAR_BUDGET + # TD-5268: what an approval card says about this child. A workflow sets + # all three; a ``delegate`` child is its call, with no number. + parent_call_id: str = "" + index: int | None = None + label: str = "" @dataclass(frozen=True) @@ -353,6 +361,9 @@ async def run_child_task(parent: Session, spec: ChildTask) -> ChildDone: tier=spec.tier, source=spec.source, transcript_budget=spec.transcript_budget, + parent_call_id=spec.parent_call_id, + index=spec.index, + label=spec.label, ) except HandlerRefusal as e: await close_child_tree(own_tree, spec.task, steps) @@ -426,6 +437,9 @@ async def _child_loop( tier: TierName | None = None, source: str = "worker", transcript_budget: int = TRANSCRIPT_CHAR_BUDGET, + parent_call_id: str = "", + index: int | None = None, + label: str = "", ) -> _ChildEnd: """:func:`run_worker_child`'s loop, also saying why it stopped early.""" parent, runtime = bound_runtime(parent, "delegate") @@ -452,6 +466,23 @@ async def _child_loop( names=allow_list, ) child_dispatcher = _child_dispatcher(parent, parent_dispatcher, child_registry, named) + _name_the_child( + parent, + parent_dispatcher, + child_dispatcher, + ApprovalWorker( + id=call_id, + parent_call_id=parent_call_id or call_id, + via="workflow" if index is not None or source == "check" else "delegate", + kind="check" if source == "check" else kind, + agent=named.name if named is not None else None, + index=index, + label=_one_line(label or task, 60), + task=_one_line(task, 300), + tier=tier or (named.tier if named is not None else "worker"), + worktree_branch=worktree.branch if worktree is not None else None, + ), + ) # TD-5126: a child in its own worktree is told that root, not the parent's. assembler = runtime.assembler @@ -559,7 +590,8 @@ def stop(why: str) -> _ChildEnd: # call id in front keeps their approvals apart. prefix = call_id or uuid.uuid4().hex[:12] - for _ in range(CHILD_MAX_ITERATIONS): + child_max = runtime.config.delegate.max_iterations + for _ in range(child_max): if parent.cancel_requested or parent.stop_turn_requested: return stop( "parent cancelled" if parent.cancel_requested else "parent stopped the turn" @@ -644,7 +676,7 @@ def stop(why: str) -> _ChildEnd: content = _tool_message_content(r) if r.images else r.output messages.append(ChatMessage(role="tool", content=content, tool_call_id=tc.id)) - return stop("iteration cap") + return stop(f"the child used all {child_max} of its model calls (delegate.max_iterations)") def _skip_all(dispatcher: ToolDispatcher) -> bool: @@ -678,6 +710,67 @@ def _child_cap_violation( return _cap_violation(parent, tracker, session_start, used, skip_all=False) +def _one_line(text: str, limit: int) -> str: + flat = " ".join(text.split()) + return flat if len(flat) <= limit else flat[: limit - 1].rstrip() + "…" + + +def worker_summary(worker: ApprovalWorker, summary: str) -> str: + """*summary* with who is asking in front (TD-5268). + + The card's title, the OS banner, the timeline row and every notify + channel show ``summary``, so a viewer that does not know the ``worker`` + field (an older phone build) still reads which child it is. + """ + if worker.kind == "check": + who = f"Check {worker.index}" if worker.index is not None else "Check" + elif worker.agent is not None: + who = f'Worker "{worker.agent}"' + elif worker.index is not None: + who = f"Worker {worker.index}" + else: + who = "Worker" + if worker.kind == "research" and worker.agent is None: + who += " (research)" + where = " in its worktree" if worker.worktree_branch is not None else "" + return f"{who}: {summary}{where}" + + +def _name_the_child( + parent: Session, + parent_dispatcher: ToolDispatcher, + child_dispatcher: ToolDispatcher, + worker: ApprovalWorker, +) -> None: + """Make the child's approval cards say which child is asking (TD-5268). + + Only when the handler is the parent session's own: a handler a test or + another host supplied keeps the six arguments it was written for. + """ + if parent_dispatcher.approval_handler != parent.request_approval: + return + + async def ask( + tool_call_id: str, + tool: Tool, + arguments: dict[str, Any], + decision_class: DecisionClass, + summary: str, + reason: str, + ) -> ApprovalOutcome: + return await parent.request_approval( + tool_call_id, + tool, + arguments, + decision_class, + worker_summary(worker, summary), + reason, + worker=worker, + ) + + child_dispatcher.approval_handler = ask + + def _child_dispatcher( parent: Session, parent_dispatcher: ToolDispatcher, diff --git a/core/tstd/tools/delegate_named.py b/core/tstd/tools/delegate_named.py index 897f158a..3865d8ee 100644 --- a/core/tstd/tools/delegate_named.py +++ b/core/tstd/tools/delegate_named.py @@ -73,7 +73,14 @@ def worker_tier(config: ModelConfig, worker: WorkerDefinition) -> tuple[TierName f"Error: worker {worker.name!r} names preset {worker.preset!r}, which is not " f"configured. Presets: {', '.join(sorted(config.presets))}.", ) - tier_cfg: TierConfig = getattr(preset, worker.tier) + tier_cfg: TierConfig | None = getattr(preset, worker.tier) + if tier_cfg is None: + # TD-5239: an ``engine: grok`` entry with no model of its own. + raise HandlerRefusal( + "delegate_agent_preset", + f"Error: worker {worker.name!r} names preset {worker.preset!r}, which runs on " + "the Grok CLI and has no model for a worker. Name a preset that has one.", + ) return worker.tier, tier_cfg diff --git a/core/tstd/tools/handlers.py b/core/tstd/tools/handlers.py index 918af9e1..60940bbf 100644 --- a/core/tstd/tools/handlers.py +++ b/core/tstd/tools/handlers.py @@ -48,6 +48,7 @@ from .session_search import SessionRecall, register_session_search_handler from .shell import OnTimeout, ShellPolicy, run_shell from .shell_job_tools import shell_job_output, shell_job_stop +from .show_artifact import show_artifact from .steering_edit import propose_steering_edit from .task_list import task_list from .tool_search import register_tool_search_handler @@ -59,6 +60,7 @@ # sent_files imports artifacts, which imports this package: type-only here. from ..sent_files import SentFiles from ..session import Session + from ..shown_artifacts import ShownArtifacts # Handler-level cap on formatted lines. Dispatch additionally caps the # returned string's byte length with its own truncation marker. @@ -554,6 +556,7 @@ def register_builtin_handlers( browser_driver: BrowserDriver | None = None, grounding_client: GroundingLocator | None = None, sent_files: SentFiles | None = None, + shown_artifacts: ShownArtifacts | None = None, shell_allowlist: Callable[[], tuple[str, ...] | None] | None = None, session_recall: SessionRecall | None = None, ax_driver: AccessibilityDriver | None = None, @@ -613,6 +616,7 @@ def register_builtin_handlers( register_workflow_handler(dispatcher) dispatcher.register_handler("consult", handle_consult) dispatcher.register_handler("send_file", partial(send_file, service=sent_files)) + dispatcher.register_handler("show_artifact", partial(show_artifact, service=shown_artifacts)) dispatcher.register_handler("propose_steering_edit", propose_steering_edit) dispatcher.register_handler("task_list", task_list) dispatcher.register_handler("ask_user", ask_user) diff --git a/core/tstd/tools/registry.py b/core/tstd/tools/registry.py index b0a92edc..bb8dbb6a 100644 --- a/core/tstd/tools/registry.py +++ b/core/tstd/tools/registry.py @@ -19,6 +19,7 @@ from ..config import SearchConfig from ..provider import FunctionDefinition, ToolDefinition +from .write import MAX_EDITS # ── Side-effect classification ────────────────────────────────────────── @@ -577,7 +578,9 @@ def _search_hosts() -> tuple[str, ...]: "Use it for every change inside an existing file, instead of " "sed -i or other shell edits. " "Fails if the target string is absent or appears more than once — " - "include enough surrounding context to match exactly one place. " + "include enough surrounding context to match exactly one place, " + "or set replace_all. Several changes to one file go in edits, " + "in one call. " "For creating or rewriting whole files, use fs_write.", parameters={ "type": "object", @@ -595,8 +598,29 @@ def _search_hosts() -> tuple[str, ...]: "type": "string", "description": "Replacement text (may be empty to delete)", }, + "replace_all": { + "type": "boolean", + "description": "Replace every occurrence instead of exactly one", + }, + "edits": { + "type": "array", + "minItems": 1, + "maxItems": MAX_EDITS, + "description": "Several replacements in this file, applied in " + "order; nothing is written if one fails. Use instead of " + "old_string and new_string.", + "items": { + "type": "object", + "properties": { + "old_string": {"type": "string", "minLength": 1}, + "new_string": {"type": "string"}, + "replace_all": {"type": "boolean"}, + }, + "required": ["old_string", "new_string"], + }, + }, }, - "required": ["path", "old_string", "new_string"], + "required": ["path"], }, side_effect_class="ask", parallel_safe=False, @@ -678,6 +702,7 @@ def create_registry( from .send_file import register_send_file_tool from .session_search import register_session_search_tool from .shell_job_tools import register_shell_job_tools + from .show_artifact import register_show_artifact_tool from .steering_edit import register_steering_edit_tool from .task_list import register_task_list_tool from .tool_search import register_tool_search_tool @@ -697,6 +722,7 @@ def create_registry( register_workflow_tool(registry) register_consult_tool(registry) register_send_file_tool(registry) + register_show_artifact_tool(registry) register_steering_edit_tool(registry) register_task_list_tool(registry) register_ask_user_tool(registry) diff --git a/core/tstd/tools/show_artifact.py b/core/tstd/tools/show_artifact.py new file mode 100644 index 00000000..b6c81b11 --- /dev/null +++ b/core/tstd/tools/show_artifact.py @@ -0,0 +1,149 @@ +"""The ``show_artifact`` tool: the agent puts text in the Artifacts pane (TD-5254). + +``send_file`` hands over a file as it is, as a card to save. This shows +something to read: a markdown document, a small HTML page, highlighted +code. The dispatcher guards ``path`` before the handler runs, and the +classifier puts the call in Class A by a rule of its own, because all it +writes is the session's own state. +""" + +from __future__ import annotations + +import asyncio +from pathlib import Path +from typing import TYPE_CHECKING + +from ..logging import get_logger +from .registry import Tool, ToolRegistry +from .results import HandlerRefusal + +if TYPE_CHECKING: + from ..artifacts import ArtifactRecord + from ..shown_artifacts import ShownArtifacts + +log = get_logger("tstd.tools.show_artifact") + +SHOW_ARTIFACT_DESCRIPTION = ( + "Show the user something to read in the Artifacts pane: a report, a plan, a table, a " + "diagram, a small HTML page, a piece of code. Give path for a text file in the " + "workspace; the pane shows the file as it is now, so later edits show too. Or give " + "content and name for text that should not be a workspace file; name's extension " + "picks the preview (.md, .html, or highlighted code). Calling again with the same " + "path or name replaces what was shown. An HTML page is shown as a static picture of " + "itself: no scripts, no links. Text only: to hand over a file as it is, use send_file." +) + +SHOW_ARTIFACT_TOOL = Tool( + name="show_artifact", + description=SHOW_ARTIFACT_DESCRIPTION, + parameters={ + "type": "object", + "properties": { + "title": { + "type": "string", + "minLength": 1, + "maxLength": 120, + "description": "Short name shown in the Artifacts pane.", + }, + "path": { + "type": "string", + "minLength": 1, + "description": "A text file inside the workspace to show.", + }, + "content": { + "type": "string", + "minLength": 1, + "description": "The text to show, when it should not be a workspace file.", + }, + "name": { + "type": "string", + "minLength": 1, + "maxLength": 120, + "description": ( + "File name with extension for content, such as summary.md or chart.html." + ), + }, + "caption": { + "type": "string", + "maxLength": 500, + "description": "Optional short note shown with the artifact.", + }, + }, + "required": ["title"], + "additionalProperties": False, + }, + side_effect_class="auto", + # A second call for the same thing replaces the first: order matters. + parallel_safe=False, + path_fields=("path",), + mutates=False, +) + +_UNAVAILABLE = "Showing artifacts is not available in this session." + + +def register_show_artifact_tool(registry: ToolRegistry) -> None: + """Register the builtin ``show_artifact`` tool. Call from ``create_registry``.""" + registry.register(SHOW_ARTIFACT_TOOL) + + +async def show_artifact( + *, + session: object, + tool_call_id: str = "", + title: str, + path: str | None = None, + content: str | None = None, + name: str | None = None, + caption: str | None = None, + service: ShownArtifacts | None, +) -> str: + """Record the artifact and announce it with ``artifact_ready``. + + Recording and announcing run as one shielded step, so a Stop that + lands between them cannot leave a row the window was never told of. + """ + from ..artifacts import ArtifactError + + session_id = getattr(session, "id", None) + workspace = getattr(session, "workspace_path", None) + add = getattr(getattr(session, "event_log", None), "add", None) + if service is None or not isinstance(session_id, str) or add is None or not workspace: + raise HandlerRefusal("show_unavailable", _UNAVAILABLE) + + async def record_and_announce() -> ArtifactRecord: + from ..artifacts import artifact_ready_for + + record = await asyncio.to_thread( + service.show, + session_id, + Path(workspace), + title=title, + path=path, + content=content, + name=name, + caption=caption, + tool_call_id=tool_call_id, + ) + await add(artifact_ready_for(record)) + log.info( + "artifact shown", + extra={"extra_fields": {"artifact_id": record.id, "mime": record.mime}}, + ) + return record + + task = asyncio.ensure_future(record_and_announce()) + task.add_done_callback(_retrieve) + try: + record = await asyncio.shield(task) + except ArtifactError as exc: + raise HandlerRefusal(exc.code, exc.message) from None + what = record.name or record.title + size = f", {record.size} bytes" if record.size is not None else "" + return f'Showing "{record.title}" in Artifacts ({what}{size}).' + + +def _retrieve(task: asyncio.Future[ArtifactRecord]) -> None: + """Mark the outcome seen: after a cancel, no one else awaits it.""" + if not task.cancelled(): + task.exception() diff --git a/core/tstd/tools/workflow.py b/core/tstd/tools/workflow.py index 333a1b16..912dcc01 100644 --- a/core/tstd/tools/workflow.py +++ b/core/tstd/tools/workflow.py @@ -208,6 +208,9 @@ async def one(plan: _Plan) -> _Outcome: isolate_writer=len(plans) >= 2, call_id=f"{base}.{plan.number}", transcript_budget=budget, + parent_call_id=base, + index=plan.number, + label=plan.label, ) outcome = await _run(parent, spec) if not check or outcome.failed is not None: @@ -294,6 +297,9 @@ async def _check( tier=tier, source="check", transcript_budget=budget, + parent_call_id=call_id.split(".", 1)[0], + index=plan.number, + label=plan.label, ) checked = await _run(parent, spec) if checked.failed is not None: diff --git a/core/tstd/tools/write.py b/core/tstd/tools/write.py index 19e31962..42f6c777 100644 --- a/core/tstd/tools/write.py +++ b/core/tstd/tools/write.py @@ -18,7 +18,9 @@ import contextlib import os import tempfile +from dataclasses import dataclass from pathlib import Path +from typing import Any def _atomic_write(target: Path, content: str) -> None: @@ -69,24 +71,92 @@ def _fs_write(path: Path, content: str, append: bool, session: object | None) -> return f"wrote {len(content.encode('utf-8'))} bytes; {verb} {path}" -def _fs_edit(path: Path, old_string: str, new_string: str, session: object | None) -> str: +#: Most replacements one ``fs_edit`` call may carry (TD-5270). +MAX_EDITS = 20 + + +@dataclass(frozen=True) +class Edit: + """One replacement: an exact string, its replacement, and whether every + occurrence goes or exactly one must exist.""" + + old_string: str + new_string: str + replace_all: bool = False + + +def edits_from_arguments( + old_string: str | None, + new_string: str | None, + replace_all: bool, + edits: list[dict[str, Any]] | None, +) -> list[Edit]: + """The call's replacements, from whichever of its two forms it used. + + One pair at the top level, or a list under ``edits``: exactly one of the + two. Anything else is the caller's mistake and changes nothing. + """ + single = old_string is not None or new_string is not None + if edits is not None and single: + raise ValueError("give old_string and new_string, or edits, not both") + if edits is None: + if old_string is None or new_string is None: + raise ValueError("give old_string and new_string, or a list of edits") + return [Edit(old_string, new_string, replace_all)] + if not edits: + raise ValueError("edits is empty — nothing to change") + if len(edits) > MAX_EDITS: + raise ValueError(f"too many edits: {len(edits)} (at most {MAX_EDITS} in one call)") + out: list[Edit] = [] + for index, item in enumerate(edits, start=1): + old, new = item.get("old_string"), item.get("new_string") + if not isinstance(old, str) or not old or not isinstance(new, str): + raise ValueError(f"edit {index} needs old_string (not empty) and new_string") + out.append(Edit(old, new, bool(item.get("replace_all", False)))) + return out + + +def apply_edits(content: str, edits: list[Edit], where: str) -> tuple[str, list[int]]: + """*content* after *edits*, in order, and how many places each changed. + + Each edit sees the text the one before it left. The first that cannot + apply stops everything, and names its place in the list when there is + more than one, so the caller writes nothing. + """ + counts: list[int] = [] + for index, edit in enumerate(edits, start=1): + which = f"edit {index}: " if len(edits) > 1 else "" + found = content.count(edit.old_string) + if found == 0: + raise ValueError(f"{which}target string not found in '{where}' — nothing was changed") + if found > 1 and not edit.replace_all: + raise ValueError( + f"{which}target string is ambiguous: {found} occurrences in '{where}' — " + "include more surrounding context so exactly one matches, or set " + "replace_all — nothing was changed" + ) + content = content.replace(edit.old_string, edit.new_string) + counts.append(found) + return content, counts + + +def _fs_edit(path: Path, edits: list[Edit], session: object | None) -> str: if not path.exists(): raise FileNotFoundError(f"'{path}' does not exist — use fs_write to create it") if path.is_dir(): raise ValueError(f"'{path}' is a directory, not a file") - content = path.read_text(encoding="utf-8") - count = content.count(old_string) - if count == 0: - raise ValueError(f"target string not found in '{path}' — nothing was changed") - if count > 1: - raise ValueError( - f"target string is ambiguous: {count} occurrences in '{path}' — " - "include more surrounding context so exactly one matches" - ) - proposed = content.replace(old_string, new_string, 1) + proposed, counts = apply_edits(path.read_text(encoding="utf-8"), edits, str(path)) _enforce_memory_cap(path, proposed, session) + # One write for the whole call: a batch is one change to the file. _atomic_write(path, proposed) - return f"edited {path}: replaced 1 occurrence" + if len(counts) == 1: + return f"edited {path}: replaced {_occurrences(counts[0])}" + each = ", ".join(str(n) for n in counts) + return f"edited {path}: {len(counts)} edits, replaced {each} ({_occurrences(sum(counts))})" + + +def _occurrences(count: int) -> str: + return "1 occurrence" if count == 1 else f"{count} occurrences" async def fs_write( @@ -97,7 +167,15 @@ async def fs_write( async def fs_edit( - session: object, path: str, old_string: str, new_string: str, tool_call_id: str = "" + session: object, + path: str, + old_string: str | None = None, + new_string: str | None = None, + replace_all: bool = False, + edits: list[dict[str, Any]] | None = None, + tool_call_id: str = "", ) -> str: - """Exact single-occurrence string replacement in a file (TD-604).""" - return await asyncio.to_thread(_fs_edit, Path(path), old_string, new_string, session) + """Exact string replacement in one file (TD-604): one, every occurrence + of one, or several in order with nothing written if any fails (TD-5270).""" + wanted = edits_from_arguments(old_string, new_string, replace_all, edits) + return await asyncio.to_thread(_fs_edit, Path(path), wanted, session) diff --git a/core/tstd/ws.py b/core/tstd/ws.py index 32235d44..172e5461 100644 --- a/core/tstd/ws.py +++ b/core/tstd/ws.py @@ -414,7 +414,13 @@ async def _ping_loop(self) -> None: await conn.send(frame) async def broadcast( - self, payload: str, *, loopback_only: bool = False, audience: Audience = "all" + self, + payload: str, + *, + loopback_only: bool = False, + audience: Audience = "all", + exclude: object | None = None, + remote_payload: str | None = None, ) -> int: """Send one frame to every handshaken client. Returns how many got it. @@ -424,7 +430,11 @@ async def broadcast( rather than to a session, such as the scheduled-job list. *loopback_only* skips remote peers (TD-5014's ``pairing_completed``); it is ``audience="loopback"``. ``"viewers"`` reaches viewers only. - Machine peers never get a broadcast (TD-5015). + Machine peers never get a broadcast (TD-5015). *exclude* is one + connection to leave out: the one whose own reply already says it + (TD-5236). *remote_payload*, with ``audience="loopback"``, is what a + remote peer gets in place of being skipped: the same state as a + remote peer is allowed to see it (TD-5242). A client that closed between the snapshot and the send is skipped, not an error: it will re-read the state when it reconnects. @@ -433,13 +443,18 @@ async def broadcast( if loopback_only: audience = "loopback" for conn in list(self._handshaken): + if conn is exclude: + continue peer = self.peer_view(conn) if peer.machine or (audience == "viewers" and not peer.viewer): continue + frame = payload if audience == "loopback" and self.peer_is_remote(conn): - continue + if remote_payload is None: + continue + frame = remote_payload try: - await conn.send(payload) + await conn.send(frame) except (websockets.exceptions.ConnectionClosed, OSError, RuntimeError): continue sent += 1 diff --git a/docs/architecture.md b/docs/architecture.md index 316041af..25ef92a3 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -488,6 +488,19 @@ exactly what it saw before. | Machine peer | A device whose record says `kind: machine`, or any hello with `"bridge"` (the hub always sends it) | Deny by default: only `MACHINE_PEER_VERBS`, own sessions only, no broadcasts. A bridged id is `bridged_session`; any other verb is `not_allowed`. Never a viewer, so it never forwards. | | Everyone else | The CLI, the legacy `remote-token`, an old client | Own rows only, no `machine_list`: unchanged. | +Every client verb is placed for a phone in `core/tstd/phone_verbs.py` (TD-5284): `PHONE_OPEN` is +what a phone may send with no setting, and a verb in none of the sets is refused from a phone +or the legacy token with `loopback_only`, so a new verb starts closed to phones until someone +places it (a test fails until then). Two lists stand against a phone. `LOOPBACK_VERBS` are the computer's own +and have no setting. `PHONE_OPT_IN` (TD-5279) maps a verb to a *phone permission*: the verb +is refused with `phone_permission_off` unless the person at the computer switched that +capability on with `set_phone_permission`, which is itself in `LOOPBACK_VERBS` so a phone +can never grant itself. The capabilities are `mcp_servers` (the four MCP server verbs and the two import verbs) and `approval_switches` +(`set_skip_all_approvals` with `enabled: true`; a message that only tightens needs none). A grant applies to paired phones only, never to the legacy `remote-token` and +never to a machine peer; it may carry an expiry; and it lives in +`{user_data_dir}/phone-permissions.yaml`, which no config file sets. `Hub.intercept` passes +the live grants to `peer_policy.check`, whose default is none. + `MACHINE_PEER_VERBS` is the session verbs a hub needs (list, attach, detach, send, stop, the approval answers, verify, new and fork, branch, tier, plan, effort, Fast and Ultracode (`set_effort`, `set_fast`, `set_ultracode`), preset, rename, star, archive, @@ -570,14 +583,14 @@ Every message in `ClientMessageT`. "Session" says whether the message carries a | `restore_files` | yes | Restore what a preview showed (TD-5125). `plan_id` is the preview's: a changed file or branch since is `restore_stale` and nothing is written. Hand-edited files are left alone unless named in `include_hand_edited`. Writes the files from the session branch without filter drivers, commits the restore on `tst/session/`, and logs `files_restored`. Never moves HEAD, the index or another ref. Refused like the preview. Forwarded for bridged ids. | | `approve` | yes | Approve a parked tool call. | | `deny` | yes | Deny a parked tool call, with an optional reason. | -| `always_allow` | yes | Approve and save the narrowest policy rule that would have allowed it, with glob characters made literal. Refused for class-C calls (`class_c_not_always_allowable`), and with `not_always_allowable` for a chained shell command or one policy cannot split with confidence (TD-5101). While the workspace config is not trusted (TD-5113) it approves the call once, saves nothing and replies `workspace_config_untrusted`. When an `ask` or `never` rule with the same tool and args is saved, it saves nothing, approves the call once (`ask`) or denies it (`never`), and replies `policy_rule_stricter` (TD-5149). | +| `always_allow` | yes | Approve and save the narrowest policy rule that would have allowed it, with glob characters made literal. Refused for class-C calls (`class_c_not_always_allowable`), and with `not_always_allowable` for a chained shell command or one policy cannot split with confidence (TD-5101). A delegate or workflow child's call is approved once and answered `not_always_allowable` too, with no rule saved: its card offers no Always allow (`proposed_always_allow` is null), because the rule is for the whole workspace (TD-5281). While the workspace config is not trusted (TD-5113) it approves the call once, saves nothing and replies `workspace_config_untrusted`. When an `ask` or `never` rule with the same tool and args is saved, it saves nothing, approves the call once (`ask`) or denies it (`never`), and replies `policy_rule_stricter` (TD-5149). | | `answer_question` | yes | Answer the questions an `ask_user` call parked (TD-5121): one answer per question, in order, each the option labels picked (one unless the question is `multi_select`) and/or the person's own `text`; or `declined: true` to skip them all. A bad answer is refused with `bad_answer` and the questions stay parked; none waiting is `no_pending_question`. Like `approve`, any attached client may answer, a phone included; it is forwarded for bridged ids and, like an approval, never queued while a lane connects. Skip-all never answers one. | | `review_plan` | yes | Review the plan an `exit_plan_mode` call parked (TD-5123): `decision` `approve` ends plan mode and pins the plan, with an optional `plan` (the person's edit, up to 12,000 characters) that replaces the model's; `reject` keeps plan mode on. Optional `feedback` goes to the model either way. An approval with an empty plan is `bad_plan`; none waiting is `no_pending_plan`. Like `approve`, any attached client may review, a phone included; it is forwarded for bridged ids and never queued while a lane connects. Skip-all never reviews one. | | `list_policy_rules` | yes | List the workspace's saved policy rules. | | `revoke_policy_rule` | yes | Remove one saved rule, identified by `(tool, args)`. | | `set_spend_cap` | yes | Set or clear the workspace's session spend budget (`caps.spend_usd` in `.tst/config.yaml`). Null removes it and the built-in $25 pause applies; a number replaces that pause. Acked with `boundary_update`. | | `trust_workspace_config` | yes | Trust or decline the workspace's `.tst/config.yaml` when it loosens anything, naming the `sha256` the card showed (TD-5113). A file that changed since is refused with `workspace_config_changed` and re-announced. The decision is stored per workspace in the data dir and applies to every live session there; acked with their `boundary_update`. It also covers the same bytes in the session's project folder and its worktrees, and a trust runs a `worktree.setup` it was holding in that session (TD-5126). Only this computer's own window may send it (`loopback_only` otherwise). | -| `set_skip_all_approvals` | — | Turn skip-all (dangerously skip permissions) on or off. Machine-wide; every ask including shell and Class C runs as auto; caps do not pause; `never` still refuses. Acked with `setup_state`. | +| `set_skip_all_approvals` | — | Turn skip-all (dangerously skip permissions) on or off. Machine-wide; every ask including shell and Class C runs as auto; caps do not pause; `never` still refuses. Acked with `setup_state`. Turning it **on** from a paired phone is the phone permission `approval_switches`: refused with `phone_permission_off` unless the computer switched it on, and when a phone does, the computer's windows get `phone_change`. Turning it off is open to a phone (TD-5283). | | `set_load_global_memory` | — | Turn global memory on or off. Machine-wide; off never reads `~/.tstdesk/memory/`. Acked with `setup_state` (TD-2603). | | `set_coworker` | — | Turn coworker mode on or off. Machine-wide; persists `{user_data_dir}/coworker.yaml`. Acked with `setup_state` (TD-2905). | | `set_voice` | — | Turn hold-to-talk dictation on or off. Machine-wide; persists `{user_data_dir}/voice.yaml`. Default off. Acked with `setup_state` (TD-4701). | @@ -604,7 +617,10 @@ Every message in `ClientMessageT`. "Session" says whether the message carries a | `set_fast` | yes | Turn the model's Fast mode on or off (TD-5176). Only a preset with a `fast` block offers it. A local model's `fast.hook_url` (loopback) is called with `{model, on, by}` and may refuse (409 with `error`); an API model's `fast.extra_body` rides on requests while it is on. Acked with `tier_state` (`fast`, `fast_available`, `fast_note`); not kept across a restart. | | `set_ultracode` | yes | Turn the session's Ultracode switch on or off (TD-5195). Works with any effort. While it is on, the model may call the `workflow` tool and each turn's user message carries a note saying so. Allowed mid-turn: the note follows from the next turn, while the `workflow` tool follows the switch at once (turned off, the next call is refused). Acked with `tier_state` (`ultracode`). Kept across a restart in the session row's `effort` string (`max+ultracode`), not a key of its own. Default `session.default_ultracode` (`false`). | | `set_plan` | yes | Turn plan mode on or off. Refused `set_tier` to worker/validator while on (TD-4603). Plan mode is read-only (TD-5123): the classifier refuses, with `error_code` `plan_mode`, every call that could change something (`fs_write`, `fs_edit`, `shell` outside the read-only list, MCP tools not in their server's `read_only_tools`, plugin tools, actuation, GitHub writes, `propose_steering_edit`, `delegate` with `kind: worker`, and any other builtin not on `PLAN_READ_ONLY_BUILTINS`, except a `shell` command on the read-only list, the screenshot tools and `delegate` without `kind: worker`, which `plan_mode_allows` lets through first; TD-5161), ahead of policy rules and skip-all, and again after an approval a call waited on; `delegate` with no `kind` runs the research worker (TD-5117). A `workflow` call with any `kind: worker` task is refused whole the same way; a call of only research tasks runs (TD-5195). The model ends it with `exit_plan_mode`. A restart keeps it: the session row's `plan_mode` is read back on revive. | -| `set_session_preset` | yes | Retarget this session at a catalog preset. Refused mid-turn. Does not write Settings. Acked with `session_list` (TD-1721). | +| `set_session_preset` | yes | Retarget this session at a catalog preset. Refused mid-turn. Refused with `engine_mismatch` when the preset names another engine than the session's, or has no tiers and the session runs TST Desk's own loop: a session keeps the engine it started on (TD-5239). Does not write Settings. Acked with `session_list` (TD-1721). | +| `switch_preset` | yes | The picker's click (TD-5235): `session_id` switches that chat to preset `name`, `make_default` also (or only) makes it the preset new chats use. Everything is checked before anything changes. An idle chat switches now; a chat with a turn running keeps the switch as pending and takes it when that turn completes (`tier_state.pending_preset` names it; a later switch replaces it, one back to the chat's own preset cancels it; it is not kept across a daemon restart); a chat with no live loop has the name recorded. Refused with `engine_mismatch` like `set_session_preset`. Not a machine-peer verb. Acked with `setup_state` when the default changed, else `session_list`. | +| `check_presets` | — | Sent when the picker opens (TD-5240): ask whether each catalog preset can run right now. The daemon looks for each bound key in the keychain (presence only), asks each server on this computer whether anything answers at its `/models` (no key, loopback only), and looks for the Grok CLI for a preset on the Grok engine. No provider is contacted. From a remote peer (a paired phone) the servers on this computer are not asked either, and read as ready: asking them is for the person at the computer (TD-5242). Not a machine-peer verb. Answered with `preset_readiness`. | +| `get_preset_roles` | — | Settings → Model (TD-5241): ask for one catalog preset's roles, `name`, so the page can edit a preset that is not the active one. Read-only; it switches nothing. Not a machine-peer verb. Answered with `preset_roles`. | | `get_instruction_stack` | yes | Ask for the resolved steering stack, token counts, and skills (TD-4502). | | `get_hooks` | yes | Ask for the session's active hooks (user config, and the workspace's once trusted) with each one's last run (TD-5134). Answered with `hooks_state`. | | `list_instructions` | — | List a workspace's Instructions files (`AGENTS.md` / `CLAUDE.md` fallback, then `.tst/rules/*`). Human path (TD-2802). | @@ -629,7 +645,7 @@ Every message in `ClientMessageT`. "Session" says whether the message carries a | `drain` | — | Before a restart: wait up to `timeout_s` for open turns, then refuse every verb that starts a turn (`user_message`, `send_queued`, `submit_line_comments`, `fork_from`, `run_grok_command`, `approve_grok_plan`, `start_autonomy`, `run_job`) with `daemon_draining` (and `in_reply_to`, the refused verb) for `hold_s` more. Due jobs wait too; approve, deny, stop and resume still pass. `0`/`0` releases, and a release, a later drain or shutdown ends a drain's wait early. Loopback only (`loopback_only` for a phone or a paired computer). Answered with `drain_result`. Sent by `tst drain` (TD-5023). | | `list_sessions` | — | Ask for the current session list. A viewer's list also carries every paired computer's cached rows, answered from the cache without dialing (TD-5015). | | `search_sessions` | — | Search what this computer's sessions said: `query` (up to 500 characters) and `limit` (1–200, default 50). Answered with `session_search_results` from the local full-text index, `session_index.db` in the data dir (TD-5129). Nothing leaves the computer. | -| `new_session` | yes | Create a fresh session in an existing session's workspace: the folder it was opened on, never its worktree, so a git folder gets another worktree (TD-5126). | +| `new_session` | yes | Create a fresh session in an existing session's workspace: the folder it was opened on, never its worktree, so a git folder gets another worktree (TD-5126). An optional `preset` opens the new session on that catalog preset, with its engine, instead of the active one (TD-5235); it changes neither the active preset nor the anchor session. | | `archive_session` | yes | File a session away, or restore it. Never a kill. | | `set_session_star` | yes | Star or unstar a session. Machine-wide. Acked with `session_list` (TD-3003). | | `delete_session` | yes | Destroy a session and its event log. Refused mid-turn. Its worktree goes too when unchanged; one with changes stays on disk on its branch (TD-5126). | @@ -648,6 +664,10 @@ Every message in `ClientMessageT`. "Session" says whether the message carries a | `reset_config_value` | — | Put one key back: a key at a fixed place to what the packaged `config.yaml` has (else its built-in default), a key inside a list item or map entry to its built-in default by removing its line. A path that ends at a list item or map entry removes it. Acked and refused like `set_config_value`. Loopback only. | | `delete_api_key` | — | Remove an API key from the OS keychain. Only an API key's id, never another keychain item (TD-5182). | | `set_preset` | — | Choose the active model preset. | +| `duplicate_preset` | — | Settings → Model, "Duplicate": copy the `source` entry under a new `label`. The daemon derives the new key and writes every role out in full, so the copy shares no YAML anchor with the original. The active entry does not change. Acked with `setup_state` (TD-5237). | +| `set_preset_label` | — | Settings → Model, "Rename": a new `label` (and `description`, when sent) for one entry. The entry's key never changes. Acked with `setup_state` (TD-5237). | +| `delete_preset` | — | Settings → Model, "Remove": take one entry out of the catalog. This computer's window only. Refused while `computer_use.local_worker_preset` names it or another entry reuses part of it. Removing the active entry makes the first one left active; an open chat keeps the model it opened with. No key is deleted. Acked with `setup_state` (TD-5237). | +| `add_grok_preset` | — | Settings → Engine, "Add Grok to your models": write one catalog entry that is `engine: grok` and names no model or key (TD-5239), labelled `label` (default `Grok`). Refused with `grok_unavailable` when the Grok CLI is not found on this computer, and when an entry already names the Grok engine. The active entry does not change. Acked with `setup_state` (TD-5258). | | `set_engine` | — | Choose the agent engine for new sessions (`native` or `grok`). Acked with `setup_state`. | | `set_grok_mode` | yes | Switch the live Grok ACP session mode. | | `run_grok_command` | yes | Enqueue `/{name} {argument}` as the next Grok prompt. Refused with `session_busy` while the session restores files or closes its worktree (TD-5161). | @@ -661,7 +681,7 @@ Every message in `ClientMessageT`. "Session" says whether the message carries a | `set_credential` | — | Create or rename a named API key without touching the secret (TD-1717). Optional `base_url` sets or clears the host that key talks to (TD-1722). | | `delete_credential` | — | Remove a named key, its secret, and tier bindings (TD-1717). | | `set_tier_credential` | — | Bind a named API key to one tier of one preset (TD-1717). That key's `base_url` is the host the tier calls when set (TD-1718). | -| `set_mcp_server` | — | Create or replace one listed MCP server (id, transport, command argv and/or url, enabled). No `env` or `headers` field; the server's existing ones are kept, unless the transport changes, which drops them and deletes their keychain items (TD-5110). A hand-set `allow_remote` is kept the same way, and dropped with a transport change (TD-5136). A url on another host or port drops the headers (deleting their items) and `allow_remote` too (TD-5161). Acked with `setup_state` (TD-4403). | +| `set_mcp_server` | — | Create or replace one listed MCP server (id, transport, command argv and/or url, enabled). No `env` or `headers` field; the server's existing ones are kept, unless the transport changes, which drops them and deletes their keychain items (TD-5110). A hand-set `allow_remote` is kept the same way, and dropped with a transport change (TD-5136). A url on another host or port drops the headers (deleting their items) and `allow_remote` too (TD-5161). Acked with `setup_state` (TD-4403). A stdio server is a command the computer runs from then on, so this and the three verbs below are a phone permission (TD-5279): a paired phone is refused with `phone_permission_off` unless the computer switched `mcp_servers` on with `set_phone_permission`, the legacy `remote-token` is always refused that way, and another computer gets `not_allowed`. A phone still sees the list in `setup_state`. When a phone does change a server, the computer's windows get `phone_change` and the server's `origin_device` / `origin_at` say which phone and when. | | `delete_mcp_server` | — | Remove one listed MCP server by id, and delete the keychain items its `env` / `headers` named (TD-5110). Unknown id is a typed error. Acked with `setup_state` (TD-4403). | | `set_mcp_secret` | — | Store one env var (stdio) or header (http) of a listed MCP server (`server_id`, `target`, `name`, `value`) in the keychain item `tst-mcp..`; `config.yaml` gets only `keychain:`. The value never appears in any event, log or audit record. Acked with `setup_state` (TD-5110). | | `delete_mcp_secret` | — | Remove one env var or header from a listed MCP server, and the keychain item it named. Acked with `setup_state` (TD-5110). | @@ -681,7 +701,10 @@ Every message in `ClientMessageT`. "Session" says whether the message carries a | `rotate_remote_token` | — | Replace `remote-token` and drop the remote clients that used it. Paired devices keep their own keys. Machine-wide; a no-op while remote attach is off. The token never crosses the wire. Acked with `setup_state` (TD-5005). | | `start_pairing` | — | Show a one-time pairing code for a new phone. Turns remote attach on first when it is off. Loopback only. Acked with `pairing_code`, or the error `pairing_unavailable` when no Tailscale address can be bound (TD-5014). | | `cancel_pairing` | — | Drop the active pairing code. Loopback only. Acked with `setup_state` (TD-5014). | -| `remove_remote_device` | — | Forget one paired device and close only its connections. Loopback, or a phone removing its own entry; any other id from a remote peer gets `loopback_only`. An unknown id changes nothing; a file that cannot be written gets `remove_failed`. Acked with `setup_state` (TD-5014). A hub removing itself from another computer uses the same self-removal (TD-5015). | +| `remove_remote_device` | — | Forget one paired device and close only its connections. Loopback, or a phone removing its own entry; any other id from a remote peer gets `loopback_only`. An unknown id changes nothing; a file that cannot be written gets `remove_failed`. Acked with `setup_state` (TD-5014). A hub removing itself from another computer uses the same self-removal (TD-5015). With `remove_added: true` from the computer, the MCP servers that device added or changed (its `mcp_servers_added`) are removed first; from a phone the flag does nothing (TD-5279). | +| `preview_mcp_import` | — | Read MCP servers out of pasted JSON (`text`: an `mcpServers` or `servers` block or one bare entry; `//` comments and trailing commas allowed) and say what importing would do. Saves and starts nothing. Answered with `mcp_import_preview`. A paired phone needs the `mcp_servers` phone permission (TD-5253). | +| `import_mcp_servers` | — | Save the chosen servers from the same `text`: `servers` is a list of `{source_name, id, allow_remote, inputs}`. The daemon parses the text again and checks every choice; each token goes to the keychain and `config.yaml` gets `keychain:`; a server is saved whole or not at all. An id already listed, an off-box server without `allow_remote`, a value still to be typed, or a refused server (`sse`, `ws`, OAuth, `headersHelper`, a `${...}` placeholder in the command or url) is `bad_request` and nothing is written. No error quotes a value. Acked with `setup_state`. Same phone permission (TD-5253). | +| `set_phone_permission` | — | Switch one phone permission on or off (`capability`, `enabled`, optional `expires_in_seconds` up to seven days). `mcp_servers` lets a paired phone send the four MCP server verbs. Persists `{user_data_dir}/phone-permissions.yaml`, which no config file or workspace can set; an absent or unreadable file means everything off. Loopback only: a phone, the legacy `remote-token` or another computer gets `loopback_only`, so a phone can never grant itself. An unknown capability or a longer expiry is `bad_request`; a file that cannot be written is `phone_permission_failed`. Acked with `setup_state`, which every other window and paired phone is sent too (TD-5279). | | `add_machine` | — | Pair another computer so its sessions show here: a `tstdesk://pair` `link`, or `host`, `port` and `code`. The host must be a Tailscale address or a `*.ts.net` name. Loopback only. Runs as a task; the answer is a pushed `machine_list`, or `pair_failed`, `pair_locked`, `machine_unreachable`, `not_tailscale`, `machine_limit`, `machine_busy` or `keychain_locked`. The code is never logged or echoed (TD-5015). | | `remove_machine` | — | Forget a paired computer: first, best effort, remove this hub from that computer's paired devices, then drop its key and its `computers.yaml` entry. Loopback only. Acked with `machine_list`; an unknown id is `machine_not_found` (TD-5015). | | `list_machines` | — | The paired computers. Loopback or a viewer. Acked with `machine_list` (TD-5015). | @@ -764,13 +787,17 @@ are stamped by a session's event log, `connection` events fix it at 1, and `ping | `machine_list` | connection | The paired computers: id, name, host, port, `status` (`connected`, `connecting`, `unreachable`, `refused`, `locked`, `outdated`), last seen, paired at, `same_computer` for a self-pair, and a `detail` line. Never a key. The reply to the four machine verbs, and pushed to viewers when one changes; never sent to a machine peer (TD-5015). | | `drain_result` | connection | Reply to `drain`, to that connection only: `drained` (no turn open at all), the session ids still `running`, `paused` for the person (the progress guard or a cap, TD-5155) or in `awaiting_approval` (ids only), and `hold_until` (null once released) (TD-5023). Only `running` turns can finish by themselves; `tst drain` exits 0 when none is left unless `--strict`. | | `policy_rules` | connection | The workspace's saved policy rules. An `auto` rule with no wildcard carries `exact`, the one text its `args` matches (`app/[[]id]/page.tsx` → `app/[id]/page.tsx`; for `shell`, the command segment once wrappers such as `timeout 60` are stripped). `exact` is null for a glob, and for every `ask` and `never` rule, which also match in another case (TD-5149, TD-5158). | -| `setup_state` | connection | Onboarding state, and the ack for `set_api_key` / `set_preset` / `set_engine` / `set_tier_slug` / `refresh_model_cards` / `set_skip_all_approvals` / `set_load_global_memory` / `set_coworker` / `set_voice` / `set_cu_indicators` / `set_workspace_pin` / `set_remote_attach` / `rotate_remote_token` / `cancel_pairing` / `remove_remote_device` / `set_mcp_server` / `delete_mcp_server` / `set_mcp_secret` / `delete_mcp_secret` / `set_judgments` / `set_email_notify`. `remote_bind` is the bound Tailscale address, never a token. `remote_devices` lists paired devices (id, name, paired_at, last_seen, connected), never a key or its hash (TD-5014). `tier_cards` is the active preset's stored prices and limits per tier with the last card refresh's outcome (TD-5185). Carries `engine`, `grok_available`, `grok_binary`, `voice_enabled`, and `voice_has_endpoint`. `mcp_servers` is the listed MCP servers (id, transport, command, url, enabled, and `env` / `headers` as name, source, entry and stored presence, TD-5110) — never a secret or a literal value. `speech_enabled` / `speech_ready` are hold-to-talk flags (TD-4701); the speech URL never appears. `judgments_*` echoes the judgment-seam toggles (TD-708 dev). A remote peer (a phone or another computer) gets it without `remote_devices`, `remote_bind` and `remote_port` (TD-5015). `email` is the mail form (enabled, method, host, port, security, username, from_address, password_stored) for TD-3820, TD-4853, and TD-4854; the password never appears. `github` is the connector snapshot (enabled, connected, phase, user code, verification URL, login, message) for TD-4852; tokens and the device code never appear. `microsoft` is the sign-in snapshot (enabled, connected, phase, account, message) for TD-4853; tokens and the authorization code never appear. `google` is the sign-in snapshot (enabled, connected, phase, account, message, client_secret_stored) for TD-4854; the secret, the tokens, and the authorization code never appear. | +| `setup_state` | connection | Onboarding state, and the ack for `set_api_key` / `set_preset` / `set_engine` / `set_tier_slug` / `refresh_model_cards` / `set_skip_all_approvals` / `set_load_global_memory` / `set_coworker` / `set_voice` / `set_cu_indicators` / `set_workspace_pin` / `set_remote_attach` / `rotate_remote_token` / `cancel_pairing` / `remove_remote_device` / `set_mcp_server` / `delete_mcp_server` / `set_mcp_secret` / `delete_mcp_secret` / `set_phone_permission` / `set_judgments` / `set_email_notify`. `remote_bind` is the bound Tailscale address, never a token. `remote_devices` lists paired devices (id, name, paired_at, last_seen, connected), never a key or its hash (TD-5014). `tier_cards` is the active preset's stored prices and limits per tier with the last card refresh's outcome (TD-5185). Carries `engine`, `grok_available`, `grok_binary`, `voice_enabled`, and `voice_has_endpoint`. `mcp_servers` is the listed MCP servers (id, transport, command, url, enabled, and `env` / `headers` as name, source, entry and stored presence, TD-5110) — never a secret or a literal value. `speech_enabled` / `speech_ready` are hold-to-talk flags (TD-4701); the speech URL never appears. `judgments_*` echoes the judgment-seam toggles (TD-708 dev). A remote peer (a phone or another computer) gets it without `remote_devices`, `remote_bind` and `remote_port` (TD-5015). `phone_permissions` lists every phone permission as `{enabled, expires_at}`, with an expired grant reading as off; a phone gets it too, so its pages can say why a control is unavailable (TD-5279). `email` is the mail form (enabled, method, host, port, security, username, from_address, password_stored) for TD-3820, TD-4853, and TD-4854; the password never appears. `github` is the connector snapshot (enabled, connected, phase, user code, verification URL, login, message) for TD-4852; tokens and the device code never appear. `microsoft` is the sign-in snapshot (enabled, connected, phase, account, message) for TD-4853; tokens and the authorization code never appear. `google` is the sign-in snapshot (enabled, connected, phase, account, message, client_secret_stored) for TD-4854; the secret, the tokens, and the authorization code never appear. | | `pairing_code` | connection | Reply to `start_pairing`: the code (and `ABCD-2345` display form), host, port, desktop name, the `tstdesk://pair` link, a QR of it as standalone SVG, and when it expires (TD-5014). | | `pairing_completed` | connection | A phone redeemed the code. Carries that device's summary (never its key). Sent to the computer's own loopback windows only (TD-5014). | +| `mcp_import_preview` | connection | Reply to `preview_mcp_import`: per server its proposed `id`, `transport`, `command` or `url`, `off_box`, `exists`, `refused` (why it cannot be imported), `not_imported` keys, `notes`, and per value its `name`, `target` and `disposition` (`keychain`, `literal`, `needs_input`). Never a value. `error` is set when the text could not be read, with a position and not the text (TD-5253). | +| `phone_change` | connection | A paired phone used a phone permission: `device_name`, `capability` and a one-sentence `summary` (for an MCP server the command or address; for a value its name, never the value). Sent to the computer's own loopback windows only, each time (TD-5279). | | `api_key_validated` | connection | The result of a key probe. `credential` echoes the request's, so Settings → API keys shows the verdict on that key's row; null when the request named none (TD-5200). Sent within 20 s (`KEY_TEST_LIMIT_S`): a server that has not answered by then is reported as no answer. Replies come in request order, and the window ties each verdict to its request by that order, which also places an older daemon's unnamed verdict. Never carries the key. | | `endpoint_models` | connection | A server's model list for "Add a model", or why it could not be read. Never carries the key. | | `local_servers` | connection | The servers on this computer that answered `scan_local_servers` with models, in config order, and every URL asked. | | `model_tested` | connection | The result of `test_model`: the reply, or what went wrong. | +| `preset_readiness` | connection | Reply to `check_presets` (TD-5240): one row per catalog preset with `status` (`ready`, `no_key`, `server_down` or `no_grok`) and one sentence of `detail` for the picker's row. The detail names a key by its display name and a server by host and port only. | +| `preset_roles` | connection | Reply to `get_preset_roles` (TD-5241): for preset `preset`, the four maps `setup_state` carries for the active one: `tier_slugs`, `tier_credentials`, `tier_loopback` and `tier_cards`. All empty for a preset with no tiers (TD-5239). Never a key's value. | | `config_editor` | connection | Reply to `get_config_editor`, and the ack of each config edit (TD-5203). `defs` (every config model and its fields: kind, bounds, choices, default, description, unit) and `defaults` (reset values by JSON-encoded path) come on the reply to `get_config_editor` only; an edit's ack leaves both empty. `values` is the effective config with each `secrets` map's values replaced by where they live (`keychain` or `config`), never the value, and a password in a URL masked. `set_paths` lists every key path the file sets, and `locked_paths` each one it writes through a YAML anchor, alias or merge (those, and the keys under them, are edited in the file by hand); `version` identifies its bytes; `edit` names the edit acknowledged. | | `config_value_rejected` | connection | A config edit that was not written: `code` (`invalid`, `not_editable`, `secret`, `stale`, `not_found`, `write_failed`) and a message for the field at `path`, never echoing the value. The file is untouched (TD-5203). | | `diagnostics_report` | connection | Doctor results: one row per check, with a fix when it failed. The `skills` row names each skill's ignored frontmatter keys (TD-5107). | diff --git a/docs/configuration.md b/docs/configuration.md index 64b385da..fab91171 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -116,6 +116,8 @@ with the reason under the field. Line endings stay as the file has them. | `clear_tool_results` | mapping | see below | Clear stale tool results before whole turns are compacted (TD-5115). Omitted in an older user copy is filled from the shipped file at load, which turns it on. | | `compaction` | mapping | see below | How compaction writes its summary and what it brings back (TD-5128): a worker-written structured summary, written ahead in the background, and the recent files read again. Omitted in an older user copy is filled from the shipped file at load, which turns the worker summary on. A config with the block but no `summary` key is `extractive`. | | `consult` | mapping | see below | Which tiers get the `consult` tool, which asks the brain or the validator one question mid-task, and how many consults one turn may make (TD-5119). Omitted in an older user copy is filled from the shipped file at load: the worker only, two a turn. | +| `delegate` | mapping | see below | How many model calls one `delegate` child may make (TD-5265). Omitted in an older user copy defaults to 8, what it was before the key existed. | +| `model_concurrency` | mapping | no limits | How many calls one model server answers at once, across the whole daemon (TD-5269). Omitted in an older user copy means no limit, as before. A new install's `config.yaml` sets `loopback_default: 2` (TD-5282). | | `workflow` | mapping | see below | Limits for the `workflow` tool, which runs workers side by side while a session's Ultracode switch is on (TD-5195). Omitted means the defaults: 3 at once, 8 a call. | | `model_refresh` | mapping | see below | Refresh every model's prices, windows and slug spelling in the background (TD-5197). Omitted means on, every 24 hours. | | `shell` | mapping | see below | The read-only shell list (TD-5103): commands that run without asking. Omitted in an older user copy means the whole built-in list, on. Config can shrink it or switch it off, never add to it. | @@ -667,7 +669,9 @@ drop a different prefix than `from_seq`. Zero is a load error, not |---|---|---|---| | `log_max_events` | int ≥ 1 | `10000` | Maximum events kept in `events.jsonl`. Oldest drop first: past the cap the oldest tenth goes at once, so the window holds 90-100% of it. Attach from a rotated seq gets `log_trimmed` and replays from the earliest kept seq; a window gets the chat's earlier messages from the saved conversation instead (`conversation_history`, TD-5201). A restart adds two events to an idle chat (its loop's start and stop) and logs its wall and tier only when they changed. | | `send_file_max_bytes` | int 1..2147483648 | `536870912` | Largest file `send_file` copies into the chat (512 MiB). Bigger is refused with `file_too_large`; nothing is copied. | -| `send_file_session_max_bytes` | int ≥ 1 | `4294967296` | Total of sent files one session may hold (4 GiB). Past it, `session_quota`. | +| `send_file_session_max_bytes` | int ≥ 1 | `4294967296` | Total of sent files one session may hold (4 GiB). Past it, `session_quota`. Text given to `show_artifact` counts toward it too (TD-5254). | +| `artifact_max_bytes` | int 1..2097152 | `2097152` | Most text one artifact shown by `show_artifact` may hold (2 MiB, which is the most the window's preview reads). Bigger is refused with `artifact_too_large` (TD-5254). | +| `artifact_max_count` | int 1..10000 | `200` | How many artifacts one session keeps in the Artifacts pane. Showing the same file or name again replaces the earlier one and is not counted twice. Past it, `artifact_limit`. | | `default_effort` | `low` \| `medium` \| `high` \| `xhigh` \| `max` | `medium` | Reasoning effort a session starts with until the composer's model picker sets one. Sent as `reasoning_effort` to any tier with `reasoning_effort: true`. An older config's `ultracode` (a sixth level before TD-5195) still loads, as `max` with `default_ultracode: true` unless that key is set. | | `default_ultracode` | bool | `false` | Whether a new session starts with Ultracode on (TD-5195): the picker's switch, beside Fast, that lets the agent run several workers at once with the `workflow` tool (§2 `workflow`). Works with any effort. Once the picker sets it, a session keeps its own, a restart included. | @@ -893,6 +897,51 @@ consult: max_per_turn: 3 ``` +### `delegate` + +A `delegate` child is a worker the agent hands part of a task to; it +returns a summary (TD-4602). + +| Key | Type | Default | Effect | +|---|---|---|---| +| `max_iterations` | int, 1 to 64 | `8` | Model calls one child may make. A child that reaches it stops and returns what it has, saying it used them all. Eight suits a lookup; raise it for children that edit several files and run a test. A child's calls count against the session's own `caps` (iterations, spend, time) together with its parent's and its siblings', so a larger number here cannot outrun them. | + + +```yaml +delegate: + max_iterations: 8 +``` + +### `model_concurrency` + +One count per model server, shared by every chat, delegated worker, +workflow task and scheduled run in the daemon (TD-5269). Without it the +only limit was inside a single `workflow` call, so two chats and a +scheduled run could all ask one local server at once. + +| Key | Type | Default | Effect | +|---|---|---|---| +| `loopback_default` | int 1 to 64, or null | `null` | Calls at once for a server on this computer. `null` is no limit. A single GPU usually wants 1 or 2. The `config.yaml` a new install starts with says `2`; a file from before this section existed has no such line and so no limit, and nothing adds one to it (TD-5282). | +| `remote_default` | int 1 to 64, or null | `null` | The same for any other server. | +| `endpoints` | list of `url` + `max_concurrent` | `[]` | A server named by URL gets its own number in place of the default for its kind. | + +A server is its scheme, host and port: every spelling of this computer +(`localhost`, `127.0.0.1`, `::1`) on one port is one server, whatever the +path, model or key. A call waits for a free slot and holds it for one +request only, a streamed reply until the stream ends; the wait between +retries is spent without one. `workflow.max_parallel` still caps one +workflow call; this caps everything together. + + +```yaml +model_concurrency: + loopback_default: 1 + remote_default: null + endpoints: + - url: http://127.0.0.1:11434 + max_concurrent: 2 +``` + ### `workflow` The `workflow` tool (TD-5195) runs several workers at once on independent @@ -1731,7 +1780,7 @@ loaded five tools. | `startup_timeout_seconds` | number > 0 | `8` | Time allowed for each step of the handshake (`server/discover` or `initialize`, then `tools/list`), whatever `timeout_seconds` says. Raise it for a server that loads a model before it answers. | | `max_call_seconds` | number > 0 | `600` | Hard cap on one tool call, stdio or http, progress or not. Never below `timeout_seconds`. | | `read_only_tools` | list of strings | `[]` | The server's tools, by the name the server gives them (not the `__` name), that only read (TD-5123). Read-only plan mode lets these run and refuses every other tool of the server. Only this list counts: a server's own `readOnlyHint` is not trusted. Names are matched exactly, no globs. It changes nothing outside plan mode: a listed tool still asks like any MCP tool. Settings keeps it when it toggles or edits the server. | -| `env` | mapping of name → string | `{}` | stdio only. Extra environment for this server's child, on top of the daemon's. Names are `[A-Za-z_][A-Za-z0-9_]*`. A value is a literal or `keychain:` (entry: lowercase letters, digits, `_` and `-`, at most 64). Numbers become strings; a YAML boolean is refused, so quote it. A value may not hold a line break or NUL. | +| `env` | mapping of name → string | `{}` | stdio only. Extra environment for this server's child, on top of the daemon's. Names are `[A-Za-z_][A-Za-z0-9_]*`. A value is a literal or `keychain:` (entry: lowercase letters, digits, `_` and `-`, at most 64). Numbers become strings; a YAML boolean is refused, so quote it. A value may not hold a line break or NUL. A stdio server does not get the rest of the daemon's environment as it is (TD-5267): variables whose name looks like a key, token, password or auth setting, and any whose value carries a credential in a URL, are left out, the same scrub the shell tool uses. `PATH`, `HOME`, proxy and locale variables pass through. A name listed here is passed whatever it looks like. | | `headers` | mapping of name → string | `{}` | http only. Extra headers on every request to this server, same value rules as `env`. Names are HTTP field names, one spelling each (case is ignored). `Accept`, `Connection`, `Content-Length`, `Content-Type`, `Host`, `Transfer-Encoding` and MCP's own `MCP-Protocol-Version`, `Mcp-Session-Id`, `Mcp-Method`, `Mcp-Name` and `Mcp-Param-*` are TST Desk's own and are refused. Another `Mcp-` name, such as a server's `Mcp-Api-Key`, is allowed (TD-5161). | @@ -1969,17 +2018,23 @@ Hold-to-talk dictation (TD-4701). The Settings toggle is off by default and live ### A preset -Every preset declares all three tiers. Omitting one is a load error naming it. +Every preset declares all three tiers. Omitting one is a load error naming it. The one +exception is a preset with `engine: grok` (TD-5239): the Grok CLI brings its own model, so +it may leave all three out. It is all three or none. | Key | Type | Effect | |---|---|---| | `brain` | tier | Planning turns. The router opens every session here for the first two turns, and escalates back to it after three consecutive worker failures. | | `worker` | tier | Token-heavy execution — edits, and the tool-call classifier. Where turns land once the lead turns are spent. | | `validator` | tier | Reviewing diffs and tests. Interactive mode may call it once after a write turn (`autonomy.verify`, TD-4204). Autonomy supervision is a later story. | +| `engine` | `native` \| `grok` | Which agent loop a chat on this preset uses (TD-5239): TST Desk's own three-tier loop, or the installed Grok CLI. Default: none, which means the `engine.kind` setting, as before. A chat takes its engine when it is created and keeps it: switching an open chat to a preset that names the other engine is refused with `engine_mismatch`, and so is a chat on TST Desk's own loop switching to a preset with no tiers. A scheduled job's own `engine` pin wins over the preset's; a job that pins none takes the preset's. With `grok`, `overrides` and `fast` are a load error: both act on TST Desk's own loop. While the active preset has no tiers, memory distill runs on the first preset that has them, and is skipped when none does. | | `label` | string | The name the composer's model picker, its chip and the account row show for this preset. A preset with no label is left out of the picker (unless it is the current one). Default: none. | | `description` | string, up to 120 characters | One line under the label in the picker: what this model is for. Default: none. | | `picker` | `primary` \| `more` \| `team` | `primary` puts the preset on the picker's main list; `more` puts it under "More models"; `team` (a preset whose tiers use different models) puts it under "Teams", a submenu shown only when one exists. Default `primary`. | +| `group` | string, up to 20 characters | The section of the picker's main list this preset sits in (TD-5236). Default: none, and TST Desk then chooses: `Grok` for a preset on the Grok engine, `Local` when every tier is served on this computer (after a bound key's host is taken into account), else `API`. A server elsewhere on your network reads `API` until you write `group: Local`. Any other name makes a section of its own, listed after those three. With one section in all, the picker shows no headings. | +| `slot` | 1 to 9 | A quick-switch slot (TD-5236): ⌘⌥ and that digit on macOS, Ctrl+Alt and that digit elsewhere, does what a click on this preset's row in the picker does. Default: none. Two presets with the same slot are a load error naming both. A copy made with Duplicate does not take its source's slot. | | `fast` | map | The model's Fast mode: the picker shows a Fast switch under Effort only for a preset with this block. `hook_url` is a loopback switch for a local model that gets faster by borrowing hardware: the daemon POSTs `{"model": hook_model, "on": true\|false, "by": "tst-desk"}` and reads the reply (409 with `error` = it will not switch now, and the switch stays off); at the start of each turn it GETs the same URL and turns the toggle off if the switch already ended Fast. `hook_model` names this model at that switch. `extra_body` is merged into every request while Fast is on, for an API model with a priority or fast tier. Either or both. `note` is one line the switch shows while it turns on, e.g. "Borrows all four GPUs; about 2 min to switch". `hook_url` must be loopback. Default: none. The Ultracode switch after Fast is not a preset key: every model has it (§2 `session` `default_ultracode`, §2 `workflow`). | +| `overrides` | map | Context settings for chats on this model, in place of the global ones of the same names (TD-5234). Six sections may appear, each with any of the keys listed: `session` (`default_effort`, `default_ultracode`), `clear_tool_results` (`enabled`, `keep_recent_tool_results`, `clear_at_least_tokens`, `exclude_tools`), `compaction` (`summary`, `summary_prompt`, `summary_max_tokens`, `summary_timeout_seconds`, `background_percent`, `reinject_files`, `reinject_tokens`), `consult` (`tiers`, `max_per_turn`), `workflow` (`max_parallel`, `max_tasks`) and `computer_use` (`max_prompt_images`). Each key means what it means in its §2 section and has the same limits. A key the model sets wins; a key left out, or `null`, keeps the global value; a list replaces the global list whole. A chat takes these when it is opened on the model or switched to it, like the model itself, and keeps them until it is switched again. Switching a chat to a model that sets `session.default_effort` or `session.default_ultracode` sets that chat's effort or Ultracode switch to it; a model that leaves them out leaves the chat's own choice alone. Any other section or key is an error at load: approvals, shell rules, MCP servers, hooks, remote attach, notifications, autonomy and keys cannot be set per model. Default: none. | Routing is turn-count and failure driven; it does not read anything from this file beyond the three tier definitions. The composer has no tier chip (TD-5172); a client can still pin a tier @@ -1995,6 +2050,7 @@ for a session with the `set_tier` message. | `input_price` | float ≥ 0 | *required* | Dollars per **million** prompt tokens that were not served from cache. | | `output_price` | float ≥ 0 | *required* | Dollars per **million** completion tokens. | | `cache_read_price` | float ≥ 0 | *required* | Dollars per **million** prompt tokens served from cache. | +| `cache_write_price` | float ≥ 0 | unset | Dollars per **million** prompt tokens the provider stores in its cache on a call, when it charges more for those than `input_price` (1.25 times input on Anthropic models). Counted from the response's `prompt_tokens_details.cache_write_tokens`. Unset charges them at `input_price`, which is what the meter did before this key existed (TD-5280). The model-card refresh does not fill it in; set it by hand from the provider's price list. | | `context_window` | int > 0 | *required* | The compaction budget — see below. | | `max_output_tokens` | int > 0 | *required* | Sent as `max_tokens` on the tier's requests, and the answer reservation subtracted from `context_window` — see below. | | `vision` | bool | `false` | When `true`, PNG/JPEG/GIF/WebP attachments are accepted on user turns routed to this tier (TD-4705), and `fs_read` returns an image file, or PDF page images on request, as an image the model sees (TD-5130). Capability is config — never inferred from the slug in code. | @@ -3115,7 +3171,7 @@ card or Skip all approvals, so none of them can run a refused call, and it tells planning. A call that was already waiting on an approval card when plan mode came on is checked again once it is allowed, so a late Allow or Skip all approvals refuses it too (TD-5161). Reads (`fs_read`, `fs_list`, `fs_grep`, the GitHub reads, screenshots), web search and -fetch, `load_skill`, `send_file`, `task_list`, `ask_user`, `consult`, `tool_search` (TD-5127: it +fetch, `load_skill`, `send_file`, `show_artifact`, `task_list`, `ask_user`, `consult`, `tool_search` (TD-5127: it only loads definitions; a loaded MCP tool still needs `read_only_tools`) and `delegate` for research still run under the usual rules. The agent ends plan mode with `exit_plan_mode`: its plan shows on a card on the window and the phone, where you approve it, edit it and then approve your diff --git a/docs/models.md b/docs/models.md index 18b44c90..f8d4c94f 100644 --- a/docs/models.md +++ b/docs/models.md @@ -13,7 +13,7 @@ Click it to open the picker: | Item | What it does | |---|---| -| A model (label, with a line under it) | Switches this chat to that model. The check mark is on the current one. The line under the name is the model's description. | +| A model (label, with a line under it) | Switches this chat to that model, and makes it the model new chats use, so the next new chat does not go back to the old one. The check mark is on the current one. The line under the name is the model's description. **Only this chat**, at the end of the row, switches the chat and leaves the model new chats use as it is. While a reply is being written the switch waits for it: the chip shows `→ after this turn`, and the next message runs on the new model. A model marked **Grok**, or any model on the other engine than this chat's, reads **Opens a new chat**: a chat keeps the engine it started on, so the click starts a new chat on that model in the same folder. Other chats that are open are never changed. | | **Effort** | How hard the model thinks before it answers: Low, Medium (recommended), High, Extra, Max. Higher effort is more thorough and slower. It is only sent to a model whose entry sets `reasoning_effort: true`. | | **Fast** | On or off. Runs this model faster: a local model may borrow more hardware, an API model may use its provider's priority tier. Shown only for a model whose entry has a `fast` block ([configuration.md](configuration.md), "A preset"). A line under it says what it is doing. A restart turns it off. | | **Ultracode** | On or off for this chat, with any effort. When it is on, the agent may split a big job into independent pieces, run several workers on them at once, have a separate reviewer check each result, and answer from the checked results. Small questions and one-line fixes are still answered directly. It makes more model calls, so it costs more and asks the worker model's server for several answers at once; the limits are `workflow` in [configuration.md](configuration.md). Workers use TST Desk's own file and shell tools, so the worker model must support tool calling: a hosted API model or a local server with tool calls works; a text-only route, or a gateway that runs its own tools instead of the ones TST Desk sends, does not. The chat keeps it, a restart included. | @@ -24,6 +24,40 @@ Click it to open the picker: Before any model is set up, the chip itself reads **Add a model**. +With models of more than one kind, the main list is in sections: **Local** +(served on this computer), **API** (a provider's server) and **Grok**. +TST Desk reads the section off each entry; `group:` in the entry sets it by +hand, which is how a server elsewhere on your own network gets under +Local, or how you make a section of your own. + +Give an entry `slot: 1` (up to 9) and ⌘⌥1 on macOS, or Ctrl+Alt+1 +elsewhere, picks it from anywhere in the window, exactly as a click on its +row would. The digit shows beside the model in the picker. + + +```yaml +presets: + house: + label: House server + description: The GPU box on my network + group: Local + slot: 1 + brain: &house + slug: house/model + base_url: http://192.168.1.20:8000/v1 + credential: my-provider + context_window: 65536 + max_output_tokens: 8192 + input_price: 0.0 + output_price: 0.0 + cache_read_price: 0.0 + worker: *house + validator: *house +credentials: + my-provider: + name: My provider +``` + Next to the chip, **Plan** turns on plan mode: the agent looks around without changing anything, then shows you a plan. Nothing runs until you approve it. @@ -110,6 +144,73 @@ An entry with no `label` is left out of the picker, unless a chat is already using it. To move an entry under **More models**, add `picker: more`. +## Keep more than one + +Settings → Model lists every model you have added, each with three +buttons: + +- **Duplicate** writes a copy with its own entry in `config.yaml`. Change + the copy (its models, keys or context size under **Build a team**, or by + hand) and the original stays as it was. A copy spells out all three roles + in full, so it never shares a YAML anchor with the entry it came from. +- **Rename** changes the label you see in the picker. The entry's key in + `config.yaml` stays the same, because chats and scheduled jobs remember a + model by that key. +- **Remove** takes the entry out of `config.yaml`. It deletes no key, and a + chat that is already using the model keeps it until you switch that chat. + If it was the model new chats use, the first one left takes over. Remove + is refused when another entry reuses part of this one (an anchor), or when + `computer_use.local_worker_preset` names it; the message says which. + +A chat's memory notes are written by the chat's own model. When a chat +ends, TST Desk asks a model to pick out what is worth remembering, and that +means sending it the conversation. It asks the model the chat is on at that +moment, so a chat you kept on a model on your own computer is not sent to a +provider's server for this. A Grok chat has no model of its own in TST +Desk, so its notes are written by the model new chats use (or, if that is +Grok too, the first model in your list). + +## Settings that travel with a model + +Some settings suit a model's size and speed rather than your taste: how +compaction writes its summary, when old tool results are cleared, how many +workers a server can serve at once, the effort a chat starts on. Put them +under `overrides` in the model's entry and they apply to every chat on +that model, in place of the global values. Switching a chat to the model +switches them too. + + +```yaml +presets: + small-local: + label: Small local + description: A small model on this computer + brain: &small + base_url: http://127.0.0.1:8000/v1 + context_window: 32768 + max_output_tokens: 4096 + input_price: 0.0 + output_price: 0.0 + cache_read_price: 0.0 + worker: *small + validator: *small + overrides: + session: + default_effort: low + compaction: + summary: extractive + clear_tool_results: + keep_recent_tool_results: 2 + workflow: + max_parallel: 1 +``` + +Only the keys you write change; the rest keep their global values. The +sections and keys that may appear are listed under `overrides` in +[configuration.md](configuration.md#a-preset). Approvals, shell rules, +MCP servers, hooks and keys are not among them: choosing a model never +changes what the agent is allowed to do. + ## A model on your own computer Any server that speaks the OpenAI chat API works, such as llama.cpp's @@ -138,6 +239,78 @@ presets: validator: *local_model ``` +## Prompt caching for Claude models through OpenRouter + +A long chat sends the same opening (the tools, the instructions, the +earlier messages) on every turn. A provider that caches it charges about a +tenth of the input price for the part it has seen before. OpenAI models +through OpenRouter cache on their own. Claude models cache only when the +request asks, and TST Desk asks only if the entry says so. One line under +each role does it: + + +```yaml +presets: + claude-via-openrouter: + label: Claude (OpenRouter) + description: Hosted, with prompt caching on + brain: &claude + slug: anthropic/model-name + base_url: https://openrouter.ai/api/v1 + credential: openrouter + context_window: 200000 + max_output_tokens: 8192 + input_price: 3.0 + output_price: 15.0 + cache_read_price: 0.3 + cache_write_price: 3.75 + extra_body: + cache_control: + type: ephemeral + worker: *claude + validator: *claude +``` + +`extra_body` is added to every request the role makes, so `cache_control` +goes out as a top-level field, which is where OpenRouter reads it for +Claude models. Storing a prompt in the cache costs more than plain input +(1.25 times on Claude models), and `cache_write_price` is how the cost +meter knows: without it a cache write is counted at `input_price` and the +meter reads a little low. Take both cache prices from the provider's own +price list for the model you use. + +To see that it is working, send two messages in one chat on that entry. +The second turn's cost line should show cached tokens. If it never does, +the model or the route does not support it and the line can come out. + +A Claude key used directly (not through OpenRouter) on its +OpenAI-compatible address gets no prompt caching from TST Desk today. + +## Grok as one of your models + +If you use the Grok CLI, it can sit on the picker beside your models. Its +entry needs only a label and the engine, because the Grok CLI brings its +own model and its own sign-in: + + +```yaml +presets: + grok: + label: Grok + description: The Grok CLI, with its own model + engine: grok +``` + +Settings → Engine → **Add Grok to your models** writes this entry for you +once the Grok CLI is installed; by hand it is the four lines above. + +A new chat started while this entry is the active one runs on the Grok +CLI. A chat keeps the engine it started on, so the picker holds this entry +for a chat that is already running on a model of yours, and holds your +models for a chat that is running on Grok: start a new chat to move +between them. Any other entry may name `engine: native` or `engine: grok` +too; one that names neither follows Settings → Engine, as before. + ## Teams Every turn has three roles: @@ -182,11 +355,26 @@ credentials: name: My provider ``` -Settings → Models → **Build a team** edits the active entry's model and -key per role. +Settings → Model → **Build a team** edits a model's roles: the model, key +and context size of each. **Editing**, above it, chooses which of your +models that is. It starts on the one new chats use, and choosing another +there switches no chat and changes no default. ## When something goes wrong +Each time the picker opens, TST Desk checks what it can without sending a +key or contacting a provider, and says so under a model's name: **No key +is stored for …**, **Nothing is answering at 127.0.0.1:…** for a model +served on this computer, or **The Grok CLI was not found**. It is a +warning, not a lock: the model can still be picked. + +On a paired phone the picker is the same one: the sections, **Only this +chat**, a switch that waits for the reply, and **Opens a new chat** all +work there, and the phone's list follows a change made at the computer. +Two things stay with the computer: removing a model, and asking the model +servers on the computer whether they are running, so on a phone a local +model's row only warns about a missing key. + | You see | What to do | |---|---| | "No model is set up yet" | Add one: picker → **Add a model**, or Settings → Models. | diff --git a/docs/tst-desk-backlog.md b/docs/tst-desk-backlog.md index 47ae14f9..943a2d4e 100644 --- a/docs/tst-desk-backlog.md +++ b/docs/tst-desk-backlog.md @@ -13319,3 +13319,1188 @@ check on a real Mac, including the Accessibility permission. - [ ] Windows: the CLI ships beside `tstd.exe` in the MSI, checked the same way. + +--- + +## Epic E54 — Builds: one-click model presets + +Scope from the 2026-11-20 discovery pass; the go-ahead to start was relayed +to the building session the same day. A build is a `presets:` entry that +carries everything a switch needs: the model per role, the engine, and its +context settings, so Local, API and Grok are one click each. It extends the +catalog (TD-1721: "Settings remains the catalog"); it is not a second +settings document. Unversioned. Stories are added here as they start. + +--- + +### TD-5233 — The session-preset slug test reads no host keychain and no network + +**Size:** 1 · **Depends on:** TD-1721, TD-5185 · **Status:** Done + +`test_catalog_slug_edit_does_not_rewrite_an_open_session` sends +`set_tier_slug`, whose handler refreshes the new slug's model card before +it replies (TD-5185). The test stubbed the config write but not the +refresh, so it read the developer's real `tst-openrouter` keychain item +and, with a key there, would have asked the live host with it. Where the +Secret Service answers slowly the reply took about 5 s against the test's +2 s wait, and the test failed; CI has no `secret-tool`, so it passed there. + +- [x] The test stubs the key read and the model list, and asserts the one + list request carried the stub's key + (`test_session_preset.py::TestSessionPreset::test_catalog_slug_edit_does_not_rewrite_an_open_session`). +- [x] The file passes with every non-loopback connect and DNS lookup + refused, and spawns no `secret-tool`. + +--- + +### TD-5237 — Duplicate, rename and remove a model in Settings + +**Size:** 3 · **Depends on:** TD-5172 · **Status:** Done + +The walkthrough could add a model and nothing could copy, relabel or +remove one, so trying a variation (another context size, another key, a +different model per role) meant overwriting the entry already there in +Settings → Model, and going back meant typing the old values in again. + +- [x] `duplicate_preset` writes a copy of an entry under a new label and a + key derived from it, every role spelled out, sharing no YAML anchor + with its source; the active entry does not change + (`test_preset_write.py::TestDuplicate`, `TestOverTheWire`). +- [x] `set_preset_label` changes an entry's label, and its description + when sent; the key never changes and only those lines change + (`TestLabel`). +- [x] `delete_preset` removes an entry; the active one moving to the first + left; the last one leaving `presets: {}`; an open chat keeps the model + it opened with (`TestDelete`, + `test_delete_moves_active_and_an_open_chat_keeps_its_model`). +- [x] A removal is refused, and nothing written, when another entry reuses + part of the entry or `computer_use.local_worker_preset` names it + (`test_an_entry_another_one_borrows_from_is_refused`, + `test_the_computer_use_helper_model_is_refused`). +- [x] Every would-be file is loaded as the daemon loads it before it is + written; a refusal leaves the file byte-identical + (`test_refusals_write_nothing`). +- [x] No key is written or deleted; another computer's hub may send none + of the three, and only this computer's window may remove + (`test_no_key_is_written_by_a_copy`, + `test_another_computer_may_send_none_of_them_and_only_this_one_may_delete`). +- [x] Settings → Model lists every model with Duplicate, Rename and Remove; + Remove asks first; a refusal shows the daemon's reason + (`model-catalog.test.ts`; the UI walk copies, renames and removes one). + +**Notes:** "Rename" is the label only. The key is the id that +`sessions.json`, scheduled-job pins (TD-3812) and +`computer_use.local_worker_preset` store. Class B entry in DECISIONS.md. + +--- + +### TD-5234 — A model carries its own context settings + +**Size:** 5 · **Depends on:** TD-1721, TD-5237 · **Status:** Done +**Approach signed off:** 2026-11-20 (owner, relayed by the coordinating session). + +A catalog entry said which model each role uses and how big its window is. +The settings that go with a model's size and speed were global: how +compaction writes its summary, when stale tool results are cleared, how +many workers run at once, the effort a chat starts on. Moving between a +small local model and a large hosted one meant editing them by hand. + +- [x] An entry may carry `overrides` with six sections (`session`, + `clear_tool_results`, `compaction`, `consult`, `workflow`, + `computer_use`); every key has its global key's type and limits + (`test_preset_overrides.py::TestTheWhitelist`). +- [x] Any other section or key, or a value outside the limits, is an error + at load that names the entry; approvals, shell rules, MCP servers, + hooks, remote attach, notifications, autonomy, keys and the engine + cannot be set per model (`test_anything_else_is_an_error_at_load`, + `test_a_value_outside_the_global_keys_limits_is_an_error_at_load`). +- [x] `effective_config` is pure: a key the entry sets wins, every other + key keeps its global value, a list replaces whole, and an entry with + no `overrides` gives the snapshot a session took before + (`TestEffectiveConfig`). +- [x] A chat opened on an entry, switched to it, or revived on it runs with + its settings; a Settings change does not rewrite an open chat + (`TestAChatGetsThem`, `TestRevive`). +- [x] The turn after a switch hands its tools the switched settings + (`test_the_next_turn_runs_on_the_switched_settings`). +- [x] Switching a chat to an entry that names `default_effort` or + `default_ultracode` sets the chat's to it; an entry that names + neither leaves the chat's own + (`test_switching_a_chat_switches_its_settings_and_its_effort`, + `test_an_entry_that_names_no_effort_leaves_the_chats_own`). +- [x] A copy (TD-5237) carries its source's `overrides` + (`test_a_copy_carries_its_sources_overrides`). +- [x] Settings → Configuration shows the new keys with words + (`test_config_schema.py`), and both guides document them + (`test_docs_config_reference.py`, `test_docs_models_guide.py`). + +**Notes:** No protocol change. `project_context` and `embeddings` are left +global: the first is read per workspace, not per chat. The compaction +trigger itself is a constant, not a key; a role's `context_window` is what +moves it, and an entry already carries that. Class B entry in DECISIONS.md. +TD-5239 adds the engine to the same resolver, and refuses `overrides` on an +entry with no model of its own. + +--- + +### TD-5239 — A model can name its engine, and a Grok one needs no model of its own + +**Size:** 5 · **Depends on:** TD-5234, TD-3812 · **Status:** Done +**Approach signed off:** 2026-11-20 (owner, relayed by the coordinating session). + +Local, API and Grok are meant to be three entries on one picker. The engine +was a separate, global switch in Settings → Engine, and a catalog entry had +to name a model for each of three roles, which the Grok CLI does not use. + +- [x] An entry may name `engine: native` or `engine: grok`; one that names + neither follows `engine.kind`, and a catalog from before loads as it did + (`test_preset_engine.py::TestWhatLoads`, + `test_the_engine_is_the_entrys_then_the_global_one`). +- [x] The three roles are all there or all absent, and absent only with + `engine: grok`; `overrides` and `fast` on a Grok entry are a load + error (`test_what_is_refused`). +- [x] Everything that walks the catalog's roles skips an entry with none: + key checks, backups, model cards, the slug snapshot; an active one + gives the stand-in tiers and needs no key + (`TestWhatTheRestSees`). +- [x] A chat takes its engine when it is created: the caller's, else the + entry's, else the global one; TST Desk's own loop is never started on + an entry with no model (`test_a_chat_opened_on_a_grok_entry_is_a_grok_chat`, + `test_the_native_loop_is_never_started_on_an_entry_with_no_model`). +- [x] Switching an open chat across engines is refused with + `engine_mismatch` and changes nothing + (`test_a_switch_across_engines_is_refused_and_changes_nothing`, + `TestAChatKeepsItsEngine`). +- [x] A scheduled job's own engine pin wins; an empty one takes the pinned + entry's; a job pinned to the native loop on an entry with no model + gets a receipt (`TestScheduledPins`). +- [x] Memory distill runs on the active entry when it has a model, else the + first entry that has one, and is skipped when none does; the doctor + skips its probe for an active entry with none (`TestDistill`, + `test_the_doctor_skips_the_probe_for_an_active_grok_entry`). +- [x] `setup_state.preset_engines` names the engine of each entry that has + one; with an active entry that has no roles, no roles are sent + (`test_setup_state_marks_the_grok_entry_and_shows_no_roles_for_it`). +- [x] The picker marks a Grok entry and holds an entry on another engine + than the chat's, with the reason; Settings → Model shows no roles for + a Grok entry that has none (`model-engine.test.ts`). +- [x] The copy, relabel and remove writers (TD-5237) work on an entry with + no roles (`TestWriters`). + +**Notes:** No new client message. Starting a new chat on an entry from the +picker, in one click, is TD-5235. Making a Grok entry without editing +`config.yaml` is TD-5258. Class B entry in DECISIONS.md. + +--- + +### TD-5259 — Memory distill follows the chat's own model + +**Size:** 2 · **Depends on:** TD-5239 · **Status:** Done + +Distill at session end and at shutdown ran on the daemon's active entry +(`_run_distill` handed it the daemon's config, and `_ensure_provider` its +brain), not on the entry the chat itself used. A chat on a local model +could so have its conversation sent to a hosted one for distilling, or the +other way round. TD-5239 kept this as it was and only made it work when +the active entry has no model. The owner asked for it to be built. + +**Acceptance criteria:** +- [x] Distill for a chat on TST Desk's own loop runs on that chat's entry, + and the client is the one made for that entry's server + (`test_distill_entry.py::test_a_chat_on_another_entry_is_distilled_by_that_entry`) +- [x] Changing the active entry does not move another chat's distill + (`test_the_active_entry_changing_does_not_move_another_chats_distill`) +- [x] A chat on the Grok engine, which has no model of its own here, keeps + the TD-5239 rule (the active entry, else the first with a model) + (`test_a_grok_chat_keeps_the_active_entry`) +- [x] A chat whose entry has since been removed falls back the same way + (`test_a_chat_whose_entry_was_removed_falls_back`) +- [x] With no model anywhere it is still skipped + (`test_no_model_anywhere_is_still_skipped`) +- [x] Tests for each case with the mock provider, asserting the entry and + the server address the client was made for + +**Notes:** "The chat's entry" is the one it is on when distill runs, not +the one most of its turns ran on: the latest choice of where the chat goes +is the one respected, and it needs no per-turn bookkeeping. The entry's +own context overrides (TD-5234) are not applied to distill; it reads the +catalog pointed at that entry, as it did for the active one. + +--- + +### TD-5258 — Add Grok to your models from Settings → Engine + +**Size:** 2 · **Depends on:** TD-5239 · **Status:** Done + +After TD-5239 a Grok entry could sit on the picker, but only someone who +edited `config.yaml` could put it there. + +- [x] `add_grok_preset` writes one entry that is a label, a description and + `engine: grok`, after the last entry or into the empty shipped + catalog; the rest of the file and `active_preset` are not changed + (`test_add_grok_preset.py::TestTheWriter`). +- [x] It is refused when the Grok CLI is not found on this computer, and + when an entry already names the Grok engine; a refusal writes nothing + (`test_it_is_refused_where_the_grok_cli_is_not_found`, + `test_a_second_ask_is_refused`, `test_once_is_enough`). +- [x] Acked with `setup_state`, which lists the entry and its engine + (`test_it_adds_the_entry_and_leaves_the_active_model`). +- [x] Settings → Engine has "Add Grok to your models": one message on a + click, held with the reason where the CLI is missing, replaced by a + line once Grok is on the list, and the daemon's reason shown on a + refusal (`grok-on-picker.test.ts`). +- [x] The UI walk lists the button and does not press it + (`e2e/census.json`, `GROK_ADD` in `app-walk.spec.ts`). + +**Notes:** The button is always rendered, so the walk's census is the same +on a machine with the Grok CLI and one without. Class B entry in +DECISIONS.md. + +--- + +### TD-5235 — One click switches this chat and the model new chats use + +**Size:** 5 · **Depends on:** TD-5234, TD-5239, TD-5258 · **Status:** Done +**Approach signed off:** 2026-11-20 (owner, relayed by the coordinating session). + +The picker changed only the open chat, so the next new chat went back to +the old model; nothing in the window could change that default. A switch +was refused while a reply was being written, and for a model on the other +engine there was nothing to click at all. + +- [x] `switch_preset {name, session_id?, make_default}` switches a chat, + the model new chats use, or both; an open chat that was not named is + never rewritten (`test_switch_preset.py::TestTheThreeSwitches`). +- [x] Everything is checked first: an unknown entry, an unknown session, an + engine clash or nothing asked for changes neither the chat nor the + default (`TestNothingHalfApplies`). +- [x] Asked for while a turn runs, the switch is held, `tier_state` names + it as `pending_preset`, and it applies when that turn completes; the + running turn asks only its own model and the next one the new model + (`test_it_is_held_then_applied_when_the_turn_completes`). +- [x] A message queued behind the running turn runs on the new model + (`test_a_message_queued_behind_the_turn_runs_on_the_new_model`). +- [x] A later switch replaces a pending one; one back to the chat's own + model cancels it; an entry removed meanwhile is dropped + (`test_a_later_switch_replaces_it_and_one_back_cancels_it`, + `test_an_entry_removed_meanwhile_is_dropped`). +- [x] A chat with no live loop has the name recorded instead of a refusal + (`test_a_chat_with_no_loop_has_the_name_recorded`). +- [x] `new_session` with `preset` opens a chat on that entry with its + engine, and without it behaves as before (`TestANewChatOnAnEntry`). +- [x] `set_session_preset` is unchanged: it still refuses mid-turn and for + a chat with no loop (`test_the_old_verb_still_refuses`). +- [x] Not a machine-peer verb + (`test_another_computers_hub_may_not_send_it`). +- [x] The picker: a click moves the chat and the default; "Only this chat" + moves the chat alone; a pick mid-turn is allowed and the chip shows + what is waiting; a model on the other engine opens a new chat on it; + a chat on another computer keeps the older verb + (`model-switch.test.ts`). + +**Notes:** A pending switch is not kept across a daemon restart (owner, +2026-11-20): the chip then shows the old model, which is the truth. The +event log awaits its subscribers before the loop takes its next message, +which is what makes binding at `turn_complete` safe. Class B entry in +DECISIONS.md. + +--- + +### TD-5236 — Local, API and Grok sections in the picker, and a shortcut per model + +**Size:** 3 · **Depends on:** TD-5235 · **Status:** Done + +The picker was one flat list, so a local model, a provider's and Grok read +as the same kind of thing, and switching always took the mouse. A second +open window also kept showing the old list and the old default after the +first one changed them. + +- [x] `setup_state.preset_groups` gives every entry a section: its own + `group`, else Grok for the Grok engine, Local when every role is served + on this computer (after a bound key's host), else API + (`test_picker_groups.py::TestTheSectionOfAnEntry`). +- [x] A server on the local network reads API until the entry says + `group: Local` (`test_without_saying_so_a_server_on_the_network_is_api`, + `test_an_entry_can_say_its_own`). +- [x] An entry may have `slot` 1 to 9; two entries with one slot are a load + error naming both; a copy does not take its source's slot + (`TestSlots`). +- [x] The picker lists Local, API, Grok, then other sections by name, with + no headings when there is one section; each slot shows beside its + model (`model-sections.test.ts`). +- [x] ⌘⌥1…9 / Ctrl+Alt+1…9 does what a click on that model's row does, + read off the physical key, not over a modal, and without taking ⌘1…9 + from the session slots (`model-sections.test.ts`). +- [x] A model added, relabelled, removed or made the default in one window + reaches this computer's other windows unasked; switching only a chat + does not (`test_a_change_in_one_window_reaches_the_other`, + `test_switching_only_a_chat_tells_no_other_window_about_the_catalog`). +- [x] `ModelPicker.svelte` is under 400 lines; the row is `ModelRow.svelte` + and what a pick does is `model-pick.ts`. + +**Notes:** The window never infers a section from a URL: the daemon sends +it. Other Settings writes to the active entry (a role's model, key or +window) still reach only the window that made them. Class B entry in +DECISIONS.md. + +--- + +### TD-5240 — The picker says when a model cannot run yet + +**Size:** 3 · **Depends on:** TD-5236, TD-5239 · **Status:** Done + +A switch succeeded and the next message then failed: the local server was +not started, the key was never stored, the Grok CLI was not installed. The +first sign was a failed turn. + +- [x] `check_presets` is answered with `preset_readiness`, one row per + entry: `ready`, `no_key`, `server_down` or `no_grok`, and one sentence + (`test_preset_ready.py::TestOverTheWire`). +- [x] A key is checked by presence in the keychain only; a server is asked + only when it is on this computer, with no key, and any reply counts + as there; an entry on the Grok engine is checked for the CLI and + nothing else (`TestTheChecks`, `TestTheProbe`). +- [x] A provider's server is never contacted + (`test_a_providers_server_is_never_asked`, + `test_a_url_off_this_computer_is_not_asked_and_carries_no_key`). +- [x] The first problem a person would fix is the one shown: the engine, + then a key, then a server + (`test_the_first_problem_a_person_would_fix_is_the_one_shown`). +- [x] The picker asks each time it opens and shows a model's problem under + its name; the model can still be picked (`model-ready.test.ts`). +- [x] Not a machine-peer verb (`test_another_computers_hub_may_not_send_it`). + +**Notes:** A warning, not a gate: a server may be starting, and the turn's +own error is still the authority. The loopback probe lives in +`local_servers.py` beside the "Find models on this computer" scan and +follows its rules. Class B entry in DECISIONS.md. + +--- + +### TD-5241 — Settings → Model edits the model you choose + +**Size:** 2 · **Depends on:** TD-5237, TD-5239 · **Status:** Done + +"Build a team" edited the model new chats use and no other. To change a +second model's context size or key, a person first had to make it the +default, which also moved every new chat onto it. + +- [x] `get_preset_roles {name}` is answered with `preset_roles`: that + entry's `tier_slugs`, `tier_credentials`, `tier_loopback` and + `tier_cards`, equal to what `setup_state` carries once it is active; + empty for an entry with no roles; it changes nothing + (`test_preset_roles.py`). +- [x] Settings → Model has an "Editing" chooser of every model, starting on + the active one; choosing another switches no chat and no default + (`model-edit.test.ts::Settings → Model`). +- [x] Every save (a role's model, key, context size, and Refresh) names the + chosen model, and a context size is compared with the chosen model's + (`model-edit.test.ts::saving a role of the chosen model`). +- [x] The chosen model's roles are shown only as the daemon sent them: none + until its answer arrives; read again after each write; let go when + the model is removed or becomes the active one. +- [x] The UI walk lists the chooser (`e2e/census.json`). + +**Notes:** `setup_state` and its meaning are unchanged, so Settings → API +keys and every older client still read the active model's roles from it. +Class B entry in DECISIONS.md. + +--- + +### TD-5242 — The model picker on a paired phone + +**Size:** 2 · **Depends on:** TD-5236, TD-5240, TD-5241 · **Status:** Done + +The phone runs the same window in a WebView, so E54's picker is already +its picker. What was not known: which of the new verbs a phone may send, +whether its list follows a change made at the computer, and whether a row +with "Only this chat" fits a phone's width. + +- [x] A phone may send `switch_preset`, `new_session` with `preset`, + `check_presets`, `get_preset_roles`, `duplicate_preset`, + `set_preset_label` and `add_grok_preset`; only the computer may send + `delete_preset`; another computer's hub none of the model-list verbs + (`test_models_on_phone.py::TestWhatAPhoneMaySend`). +- [x] A phone's `setup_state` carries the sections, engines and slots, and + still leaves out what a remote peer's always does + (`test_a_phone_gets_the_sections_engines_and_slots`). +- [x] A catalog change made in a window reaches a paired phone, and one + made on a phone reaches the window, each with its own view of + `setup_state` (`test_a_phone_is_told_when_the_window_changes_the_catalog`, + `test_the_window_is_told_when_a_phone_changes_it`). +- [x] `check_presets` from a phone reports keys and the Grok CLI and does + not make the computer ask its model servers + (`test_a_phone_never_makes_the_computer_ask_its_model_servers`). +- [x] At 360 px the picker's menu, and each row's "Only this chat", stay + inside the screen (the UI walk, `app-walk.spec.ts`). + +**Notes:** No Kotlin changed: the next APK build carries the new window. +The ⌘⌥ digit chord has no meaning on a phone; a slot's digit still shows +beside its model. Class B entry in DECISIONS.md. + +--- + +## Epic E58 — After the Harness comparison + +Scope from the 2026-11-20 comparison with DeepSeek Harness +(`deepseek-harness-comparison.md`, a research note outside this repo) and +the go-ahead relayed to the building session the same day. Two aims: close +the gaps that matter where Harness leads (agent-made deliverables, an OS +sandbox that is on, agent-callable scheduling, one-click MCP bundles, +longer-lived children), and keep what TST Desk leads on true and visible +(privacy, guardrails, the scheduler). Unversioned. Stories are added here as +they start; size 5 and above need approach sign-off first. + +--- + +### TD-5260 — The README and the shipped comments say what the code does + +**Size:** 1 · **Depends on:** TD-1410, TD-5136, TD-5185 · **Status:** Done + +Three statements had fallen behind the code. The README said the daemon's +only outbound HTTP is to the configured `base_url`; it also reads search +pages, refreshes model cards daily, and reaches whatever the user switches +on. The shipped `config.yaml` said MCP HTTP urls must be loopback; a server +may set `allow_remote` (TD-5136). `mcp/README.md` said the app would gain +MCP loading in a later milestone; it has it. A privacy claim that is +narrower than the truth is worse than none. + +- [x] The README's "No telemetry" entry names every kind of host the daemon + contacts and the setting that governs each, and says the update check + is the packaged app's. +- [x] The shipped `config.yaml` comment on `mcp` matches TD-5136. +- [x] `mcp/README.md` describes how the app loads a listed server today. +- [x] No behaviour changed: the only tracked code file touched is the shipped + `config.yaml`, and there only a comment; the docs, config and + outbound-host tests pass. + +--- + +### TD-5265 — A delegated child's call limit is a setting + +**Size:** 2 · **Depends on:** TD-4602, TD-5195 · **Status:** Done + +A `delegate` child stopped after eight model calls, a constant in the +code. That suits a lookup and cuts off a child asked to edit a few files +and run a test, which then returns half a job. Harness documents children +with their own sessions and a configurable depth; the first step here is +that the one limit TST Desk has is the person's to set. + +- [x] `delegate.max_iterations` (1 to 64, default 8) is how many model + calls one child may make; the shipped config sets and explains it + (`test_delegate.py::test_a_child_runs_as_many_calls_as_config_allows`, + `test_the_shipped_config_sets_the_default_and_documents_it`). +- [x] With the key absent a child behaves as before: eight calls + (`test_a_child_stops_after_eight_calls_by_default`). +- [x] A child that reaches the limit says so and names the key. +- [x] The session's own caps still bound the total, whatever the number + (`test_a_larger_number_cannot_outrun_the_sessions_own_cap`). +- [x] Settings → Configuration shows the key with words, and the reference + documents it (`test_config_schema.py`, `test_docs_config_reference.py`). + +**Notes:** Not in a model's `overrides` (TD-5234) yet: nothing has shown a +need for it per model. Children that run alongside the parent are a later, +larger story. + +--- + +### TD-5261 — The shell sandbox works in a session's own worktree + +**Size:** 2 · **Depends on:** TD-5138, TD-5126 · **Status:** Done + +A session in a git repository works in a worktree, and by default the +worktree is under TST Desk's data dir (TD-5126). The shell sandbox hides +the data dir (TD-5138) and laid that mask over the workspace bind, so with +`sandbox: enabled` every command in such a session failed before it ran: +it could not enter its own folder. The two features are both shipped and +had not been tried together. It also stands in the way of ever turning +the sandbox on by default. + +- [x] A sandboxed command in a worktree under the data dir runs in it and + writes there + (`test_shell_sandbox.py::TestRealSandbox::test_a_worktree_inside_the_data_dir_is_still_the_workspace`). +- [x] The rest of the data dir stays out of reach of that command: its + files, its listing, and another session's worktree (same test). +- [x] A hidden directory that contains the workspace is masked before the + workspace bind and can be passed through but not listed; one inside + the workspace, or apart from it, is masked last and closed as before + (`test_a_hidden_dir_around_the_workspace_is_masked_before_the_bind`, + which needs no bubblewrap). +- [x] Every existing sandbox test passes unchanged. + +**Notes:** Found while checking a design note written after the Harness +comparison; reproduced through `run_shell` before the fix. A workspace +that *is* a hidden directory is unchanged: still masked. + +--- + +### TD-5276 — Only the computer changes its MCP servers + +**Size:** 2 · **Depends on:** TD-4403, TD-5014, TD-5110 · **Status:** Done + +A stdio MCP server is a command the daemon runs, now and at every session +start, and it never meets the classifier. `set_mcp_server` was open to a +paired phone, and the handler saved and started whatever it was sent. So a +paired phone, or anyone holding its key or the older remote token, could +make the computer run a command with nothing shown at the computer, and +the entry stayed in `config.yaml` after the phone was removed. The config +editor was already loopback-only for this stated reason; the MCP page had +been missed. + +- [x] `set_mcp_server`, `delete_mcp_server`, `set_mcp_secret` and + `delete_mcp_secret` are refused with `loopback_only` for a phone, the + older remote token, and another computer, and allowed for this + computer's own window (`test_peer_policy.py::test_the_mcp_page_is_loopback_only`). +- [x] At the socket, a phone's `set_mcp_server` saves nothing, starts + nothing, and the command it carried does not run + (`test_mcp_peer.py::test_a_phone_cannot_add_a_server`). +- [x] A phone cannot change a server's command, switch one on or off, + remove one, or set or remove a value + (`test_a_phone_cannot_change_remove_or_set_a_value_on_one`). +- [x] A phone still sees the list, and the computer's window still manages + it (`test_a_phone_still_sees_the_list`, + `test_the_computers_own_window_still_manages_them`). +- [x] The verbs that make this computer run a command are pinned as + loopback-only so a later tidy-up cannot reopen one + (`test_verbs_that_make_this_computer_run_a_command_stay_loopback_only`). +- [x] Off the computer, Settings → MCP servers is the list and one line + saying where to make changes, with nothing to press + (`mcp-read-only.test.ts`). + +**Superseded in part by TD-5279:** the owner chose to keep the phone's +ability as an opt-in. The four verbs are now refused from a phone with +`phone_permission_off` unless the computer switched "MCP servers" on for +phones; the default (refused) is what this story shipped. + +**Notes:** With the verb reopened, the socket test's command ran on the +test machine: the fault was real, not theoretical. Other verbs a phone may +send that deserve a decision (turning approvals off, `shutdown`, changing +the host a stored key is sent to, starting an unattended run) are listed +in the research note and are not changed here. Class B entry in +DECISIONS.md. + +--- + +### TD-5266 — Every frame in the window is sandboxed + +**Size:** 1 · **Depends on:** TD-4807 · **Status:** Done + +The Grok preview pane showed an HTML file, a PDF or a page the agent chose +in an `', ["', ["x', ["ping=", "evil.example"]], + ["a video poster", '', ["poster", "evil.example"]], + ["a javascript address", 'x', ["javascript:", "href"]], + ["a shadow root", '', [" { + const clean = sanitizeArtifactHtml(hostile); + for (const fragment of gone) expect(clean).not.toContain(fragment); + }); + + it("keeps the words a link or a form showed", () => { + expect(sanitizeArtifactHtml('Read more')).toContain( + "Read more", + ); + }); + + it("keeps an inline image, headings, tables and inline styles", () => { + const clean = sanitizeArtifactHtml( + `

Report

1
chart`, + ); + expect(clean).toContain(""); + expect(clean).toContain(`src="${PIXEL}"`); + }); + + it("keeps a whole document's styles, from the head and the body", () => { + const clean = sanitizeArtifactHtml( + "t" + + "

Hi

", + ); + expect(clean).toContain("h1{color:blue}"); + expect(clean).toContain("p{margin:0}"); + expect(clean).toContain("

Hi

"); + expect(clean).not.toContain(" { + expect(sanitizeArtifactHtml("just words")).toBe("just words"); + expect(sanitizeArtifactHtml("")).toBe(""); + }); +}); + +describe("artifactPreviewSrcdoc", () => { + it("still puts the policy first, and only once, around the cleaned markup", () => { + const srcdoc = artifactPreviewSrcdoc( + 'x', + ); + expect(srcdoc.match(/Content-Security-Policy/g)).toHaveLength(1); + expect(srcdoc).toContain(HTML_PREVIEW_CSP); + expect(srcdoc).not.toContain("default-src *"); + expect(srcdoc).not.toContain("evil.example"); + expect(srcdoc.indexOf("Content-Security-Policy")).toBeLessThan(srcdoc.indexOf("")); + }); + + it("does not loosen the frame", () => { + expect(HTML_PREVIEW_SANDBOX).toBe(""); + expect(STATIC_PREVIEW_NOTE).toBe("Static preview. Links and scripts are off."); + }); +}); diff --git a/ui/src/lib/artifact-html.ts b/ui/src/lib/artifact-html.ts new file mode 100644 index 00000000..dc98f747 --- /dev/null +++ b/ui/src/lib/artifact-html.ts @@ -0,0 +1,86 @@ +// TD-5262: the HTML an artifact preview shows is written by a model, and a +// model can be talked into writing anything. The frame already runs no +// script (an empty sandbox) and loads nothing (a `default-src 'none'` +// policy, first in the document). Two things were left: a link could send +// the frame to any address when clicked, carrying whatever the model put +// in it, and tags such as `` or `` are +// not all governed by the policy in every webview. So the markup is +// cleaned first: nothing that navigates, loads, posts or refreshes. +// +// This is a second layer. The sandbox and the policy stay exactly as they +// were; `htmlPreviewSrcdoc` still wraps the result. + +import DOMPurify from "dompurify"; + +import { htmlPreviewSrcdoc } from "./artifacts"; + +/** Shown above an HTML preview so a dead link is not a surprise. */ +export const STATIC_PREVIEW_NOTE = "Static preview. Links and scripts are off."; + +const FORBID_TAGS = [ + "script", + "iframe", + "frame", + "frameset", + "object", + "embed", + "form", + "base", + "meta", + "link", + "template", + "noscript", +]; + +// Everything that names an address the frame could go to or fetch, and the +// form attributes that would post. Event handlers go by DOMPurify's default. +const FORBID_ATTR = [ + "href", + "xlink:href", + "ping", + "action", + "formaction", + "srcset", + "background", + "poster", + "target", +]; + +// Its own instance: the hook below must not reach markdown or mermaid. +let purifier: ReturnType | null = null; + +function instance(): ReturnType { + if (purifier !== null) return purifier; + purifier = DOMPurify(window); + // `src` survives only as an inline `data:` image. Anything else is an + // address, and the policy's own `img-src data:` says the same. + purifier.addHook("uponSanitizeAttribute", (_node, data) => { + if (data.attrName === "src" && !/^data:image\//i.test(data.attrValue.trim())) { + data.keepAttr = false; + } + }); + return purifier; +} + +/** `html` with everything that could navigate, load, post or refresh taken + * out. Styles stay: the frame's policy already confines what they reach. */ +export function sanitizeArtifactHtml(html: string): string { + const root = instance().sanitize(html, { + WHOLE_DOCUMENT: true, + RETURN_DOM: true, + ADD_TAGS: ["style"], + FORBID_TAGS, + FORBID_ATTR, + ALLOW_DATA_ATTR: false, + ALLOW_UNKNOWN_PROTOCOLS: false, + }) as unknown as Element; + // A whole document keeps its styles in the head; the preview wraps a body. + const styles = [...root.querySelectorAll("head style")].map((el) => el.outerHTML).join(""); + return styles + (root.querySelector("body")?.innerHTML ?? ""); +} + +/** The `srcdoc` an HTML artifact is shown with: cleaned, then wrapped with + * the first-wins policy. */ +export function artifactPreviewSrcdoc(html: string): string { + return htmlPreviewSrcdoc(sanitizeArtifactHtml(html)); +} diff --git a/ui/src/lib/artifacts.svelte.ts b/ui/src/lib/artifacts.svelte.ts index 3f01b6d4..6ddef5cc 100644 --- a/ui/src/lib/artifacts.svelte.ts +++ b/ui/src/lib/artifacts.svelte.ts @@ -19,6 +19,11 @@ export interface ArtifactPreview { path: string; kind: ArtifactPreviewKind; source: string | null; + /** The artifact's file name when it has one (TD-5254). Text the agent + * handed over lives at `artifacts/`, which has no extension, so the + * name is what picks the preview and the highlighting. */ + name: string | null; + caption: string | null; } export const artifacts = $state({ @@ -145,6 +150,11 @@ function reduce(event: DaemonEventUnion): void { caption: event.caption, tool_call_id: event.tool_call_id, }); + // TD-5254: the agent showed the open artifact again. Read it again, + // so the pane is never a stale copy of what the row now names. + if (event.artifact_id === artifacts.selectedId && !isSentFile(event)) { + selectArtifact(event.artifact_id); + } return; } if (event.type === "artifact") { @@ -173,7 +183,8 @@ function upsert(entry: ArtifactEntry): void { } function beginPreview(event: Artifact): void { - const kind = artifactPreviewKind(event.mime, event.path); + const name = event.name ?? null; + const kind = artifactPreviewKind(event.mime, name ?? event.path); artifacts.preview = { artifactId: event.artifact_id, title: event.title, @@ -181,6 +192,8 @@ function beginPreview(event: Artifact): void { path: event.path, kind, source: null, + name, + caption: event.caption ?? null, }; const token = ++previewToken; const sessionId = artifacts.sessionId; diff --git a/ui/src/lib/artifacts.test.ts b/ui/src/lib/artifacts.test.ts index d020c325..9e40d04c 100644 --- a/ui/src/lib/artifacts.test.ts +++ b/ui/src/lib/artifacts.test.ts @@ -316,6 +316,82 @@ describe("open preview", () => { }); }); +describe("shown artifacts (TD-5254)", () => { + const shown = { + artifact_id: "a1", + title: "Tool", + mime: "text/plain", + path: "artifacts/a1", + kind: "artifact" as const, + name: "tool.py", + size: 12, + caption: "A first pass", + tool_call_id: "c1", + }; + + beforeEach(() => { + setArtifactSourceReader(async ({ path }) => `source:${path}`); + bindArtifacts("s1", "/ws"); + emit({ type: "artifact_list", seq: 1, session_id: "s1", artifacts: [entry(shown)] }); + mocks.sent.length = 0; + }); + + it("the name picks the preview when the path has no extension", async () => { + selectArtifact("a1"); + emit({ type: "artifact", seq: 1, session_id: "s1", ...shown }); + await flush(); + expect(artifacts.preview?.kind).toBe("code"); + expect(artifacts.preview?.name).toBe("tool.py"); + expect(artifacts.preview?.caption).toBe("A first pass"); + expect(asFencedMarkdown("x = 1", artifacts.preview?.name ?? "")).toContain("```python"); + + emit({ type: "artifact", seq: 1, session_id: "s1", ...shown, name: "page.html" }); + await flush(); + expect(artifacts.preview?.kind).toBe("html"); + emit({ type: "artifact", seq: 1, session_id: "s1", ...shown, name: "notes.md" }); + await flush(); + expect(artifacts.preview?.kind).toBe("markdown"); + }); + + it("a row from before names existed still previews by its path", async () => { + selectArtifact("a1"); + emit({ + type: "artifact", + seq: 1, + session_id: "s1", + artifact_id: "a1", + title: "Old", + mime: "text/markdown", + path: "notes.md", + }); + await flush(); + expect(artifacts.preview?.kind).toBe("markdown"); + expect(artifacts.preview?.name).toBeNull(); + expect(artifacts.preview?.caption).toBeNull(); + }); + + it("showing the open artifact again reads it again, and replaces its row", async () => { + selectArtifact("a1"); + emit({ type: "artifact", seq: 1, session_id: "s1", ...shown }); + await flush(); + mocks.sent.length = 0; + emit({ type: "artifact_ready", seq: 9, session_id: "s1", ...shown, title: "Tool, revised" }); + expect(artifacts.items.map((row) => row.title)).toEqual(["Tool, revised"]); + expect(mocks.sent).toEqual([{ type: "open_artifact", session_id: "s1", artifact_id: "a1" }]); + }); + + it("showing some other artifact again does not disturb the open one", async () => { + selectArtifact("a1"); + emit({ type: "artifact", seq: 1, session_id: "s1", ...shown }); + await flush(); + mocks.sent.length = 0; + emit({ type: "artifact_ready", seq: 9, session_id: "s1", ...shown, artifact_id: "a2" }); + expect(mocks.sent).toEqual([]); + expect(artifacts.preview?.artifactId).toBe("a1"); + expect(artifacts.items.map((row) => row.artifact_id)).toEqual(["a1", "a2"]); + }); +}); + describe("sent files (TD-5144)", () => { const LIST = fixtures.artifact_list as Extract; const [plain, sent] = LIST.artifacts; diff --git a/ui/src/lib/client.ts b/ui/src/lib/client.ts index 0be92a7a..5fd0aa45 100644 --- a/ui/src/lib/client.ts +++ b/ui/src/lib/client.ts @@ -101,11 +101,15 @@ const KNOWN_EVENT_TYPES = new Set([ "setup_state", // TD-1101 first-run wizard "pairing_code", // TD-5014 "pairing_completed", // TD-5014 + "phone_change", // TD-5279 + "mcp_import_preview", // TD-5253 "machine_list", // TD-5015 "api_key_validated", // TD-1101 "endpoint_models", // TD-5172 "local_servers", // TD-5180 "model_tested", // TD-5172 + "preset_readiness", // TD-5240 + "preset_roles", // TD-5241 "config_editor", // TD-5203 "config_value_rejected", // TD-5203 "diagnostics_report", // TD-1104 doctor diff --git a/ui/src/lib/components/AppShell.svelte b/ui/src/lib/components/AppShell.svelte index cf14578f..84e629b2 100644 --- a/ui/src/lib/components/AppShell.svelte +++ b/ui/src/lib/components/AppShell.svelte @@ -91,7 +91,8 @@ import { startShellJobs } from '../shell-jobs-store.svelte.js'; import { startGoals } from '../goal-store.svelte.js'; import { startVoice } from '../voice.svelte.js'; - import { resolveShortcut, sessionSlot } from '../shortcuts'; + import { modelSlot, resolveShortcut, sessionSlot } from '../shortcuts'; + import { pickSlot } from '../model-pick'; import { sessionIdAtSlot, stepSessionId } from '../rail'; import { sessionOrderAcross } from '../machines'; import { machines } from '../machines.svelte.js'; @@ -140,6 +141,7 @@ ctrlKey: event.ctrlKey, shiftKey: event.shiftKey, altKey: event.altKey, + code: event.code, }, { workspaceMenuOpen: workspaces.menuOpen, @@ -177,6 +179,17 @@ }); const id = slot === null ? null : sessionIdAtSlot(sessionOrder(), slot); if (id !== null) selectRow(id); + } else if (action === 'pick-model') { + // TD-5236: the same as a click on that model's row in the picker. + const slot = modelSlot({ + key: event.key, + code: event.code, + metaKey: event.metaKey, + ctrlKey: event.ctrlKey, + shiftKey: event.shiftKey, + altKey: event.altKey, + }); + if (slot !== null) pickSlot(slot); } else openSettings(); } diff --git a/ui/src/lib/components/ApprovalCard.svelte b/ui/src/lib/components/ApprovalCard.svelte index a7198678..2a12cb36 100644 --- a/ui/src/lib/components/ApprovalCard.svelte +++ b/ui/src/lib/components/ApprovalCard.svelte @@ -6,7 +6,7 @@ // decision they must make: ⏎ approves, Esc denies, from the card itself. import { onMount } from 'svelte'; import Icon from './Icon.svelte'; - import { alwaysAllowLabel, isAlwaysAllowable, isDangerous } from '../approval'; + import { alwaysAllowLabel, isAlwaysAllowable, isDangerous, workerLine } from '../approval'; import { alwaysAllow, approve, canDecide, deny } from '../approval-store.svelte.js'; import { approvalWaitCopy } from '../machines'; import { machineNameFor } from '../machines.svelte.js'; @@ -94,6 +94,9 @@
{approval.summary} + {#if workerLine(approval)} + {workerLine(approval)} + {/if} {approval.toolName} @@ -130,6 +133,7 @@ bind:checked={remember} aria-label={`Always allow in this workspace: ${ruleLabel}`} /> + Always allow this in this workspace {/if} @@ -205,6 +209,12 @@ min-width: 0; } + .who { + font-size: var(--text-sm); + color: var(--color-ink-secondary); + overflow-wrap: anywhere; + } + .title { font-size: var(--text-base); font-weight: var(--weight-medium); diff --git a/ui/src/lib/components/ArtifactPane.svelte b/ui/src/lib/components/ArtifactPane.svelte index 61180d9d..d70f1cdf 100644 --- a/ui/src/lib/components/ArtifactPane.svelte +++ b/ui/src/lib/components/ArtifactPane.svelte @@ -15,11 +15,11 @@ asFencedMarkdown, artifactsEmptyCopy, copyArtifactSource, - htmlPreviewSrcdoc, HTML_PREVIEW_SANDBOX, openArtifactInEditor, workspaceOpenPath, } from '../artifacts'; + import { artifactPreviewSrcdoc, STATIC_PREVIEW_NOTE } from '../artifact-html'; import { artifacts, selectArtifact } from '../artifacts.svelte.js'; import { machineNameFor } from '../machines.svelte.js'; import { staysOnCopy } from '../machines'; @@ -113,22 +113,26 @@ {/if}
+ {#if preview.caption} +

{preview.caption}

+ {/if} {#if artifacts.error !== null}

{artifacts.error}

{:else if preview.source === null} {:else if preview.kind === 'html'} +

{STATIC_PREVIEW_NOTE}

{:else if preview.kind === 'markdown'}
{:else} -
+
{/if} {/if} @@ -230,6 +234,12 @@ gap: var(--space-4); } + .caption { + margin: 0 0 var(--space-2); + font-size: var(--text-sm); + color: var(--color-ink-secondary); + } + .preview-title { margin: 0; font-family: var(--font-display); @@ -270,6 +280,13 @@ min-height: 0; } + .static-note { + margin: 0; + padding: var(--space-1) var(--space-3); + font-size: var(--text-xs); + color: var(--color-ink-muted); + } + .html-frame { flex: 1; width: 100%; diff --git a/ui/src/lib/components/DoctorPane.svelte b/ui/src/lib/components/DoctorPane.svelte index 8d14b325..dfb3ba84 100644 --- a/ui/src/lib/components/DoctorPane.svelte +++ b/ui/src/lib/components/DoctorPane.svelte @@ -9,10 +9,11 @@ /** Row status → glyph. The copied text report keeps its own ASCII marks (STATUS_MARK in the store); these are the pane's visual analog. */ - const STATUS_ICON: Record = { + const STATUS_ICON: Record = { ok: 'check', fail: 'x', skip: 'minus', + warn: 'alert', }; let copied = $state(false); @@ -170,6 +171,10 @@ color: var(--color-ink-secondary); } + .mark--warn { + color: var(--color-warn); + } + .row-body { display: flex; flex-direction: column; diff --git a/ui/src/lib/components/GrokOnPicker.svelte b/ui/src/lib/components/GrokOnPicker.svelte new file mode 100644 index 00000000..a1ffa274 --- /dev/null +++ b/ui/src/lib/components/GrokOnPicker.svelte @@ -0,0 +1,104 @@ + + +

Grok on the model list

+{#if onList !== null} +

+ {modelLabel(onList, settings.presetLabels)} is on the model list. A new chat started on it + runs on the Grok CLI, whatever the engine above is set to. +

+{:else} +

+ Puts Grok beside your models in the picker. It uses the Grok CLI's own model and sign-in, + so there is no key or model to set here. +

+ + {#if !settings.grokAvailable} +

+ The Grok CLI was not found on this computer. Install Grok Build and run + grok login first. +

+ {/if} + {#if problem} + + {/if} +{/if} + + diff --git a/ui/src/lib/components/ModelCatalog.svelte b/ui/src/lib/components/ModelCatalog.svelte new file mode 100644 index 00000000..f504c4f0 --- /dev/null +++ b/ui/src/lib/components/ModelCatalog.svelte @@ -0,0 +1,265 @@ + + +{#if settings.presets.length > 0} +

What you have added

+

+ Duplicate one to try a variation without changing the original. Removing one deletes no key, + and a chat already using it keeps it until you switch that chat. +

+
    + {#each settings.presets as name (name)} +
  • + {#if renaming === name} + { + if (e.key === "Enter") commitRename(name); + if (e.key === "Escape") { + e.stopPropagation(); + renaming = null; + } + }} + /> +
    + + +
    + {:else} +
    + {label(name)} + {#if name === settings.activePreset} + New chats use this + {/if} + {#if modelDescription(name, settings.presetDescriptions)} + {modelDescription(name, settings.presetDescriptions)} + {/if} +
    + {#if confirming === name} +
    + Remove {label(name)}? + + +
    + {:else} +
    + + + +
    + {/if} + {/if} +
  • + {/each} +
+ {#if problem} + + {/if} +{/if} + + diff --git a/ui/src/lib/components/PairedDevices.svelte b/ui/src/lib/components/PairedDevices.svelte index 3bdeb04b..f170be83 100644 --- a/ui/src/lib/components/PairedDevices.svelte +++ b/ui/src/lib/components/PairedDevices.svelte @@ -13,6 +13,9 @@ let { canManage }: { canManage: boolean } = $props(); let confirming = $state(null); + // TD-5279: removing a phone does not undo what it changed. The review + // lists the MCP servers it added and offers to take them with it. + let alsoRemove = $state(false); let now = $state(Date.now()); // The daemon pushes presence from here on; this is the first read. @@ -30,8 +33,17 @@ return `Last seen ${relativeWords(device.last_seen, now)}`; } + function added(device: RemoteDeviceSummary): string[] { + return device.mcp_servers_added ?? []; + } + + function ask(device: RemoteDeviceSummary): void { + alsoRemove = false; + confirming = device.id; + } + function remove(device: RemoteDeviceSummary): void { - removeRemoteDevice(device.id); + removeRemoteDevice(device.id, alsoRemove && added(device).length > 0); confirming = null; } @@ -61,6 +73,16 @@ {#if confirming === device.id} It will need to pair again. + {#if added(device).length > 0} + + This phone added or changed MCP servers that stay after it is removed: + {added(device).join(', ')}. + + + {/if} @@ -71,7 +93,7 @@ class="choice" type="button" aria-label={`Remove ${device.name}`} - onclick={() => (confirming = device.id)}>Remove ask(device)}>Remove {/if} {/if} @@ -142,10 +164,22 @@ .confirm { display: inline-flex; + flex-wrap: wrap; align-items: center; + justify-content: flex-end; gap: var(--space-2); } + .review, + .also { + flex: 1 1 100%; + text-align: right; + } + + .review { + color: var(--color-warn); + } + .confirm-text { font-size: var(--text-xs); color: var(--color-ink-secondary); diff --git a/ui/src/lib/components/PhonePermissions.svelte b/ui/src/lib/components/PhonePermissions.svelte new file mode 100644 index 00000000..e06645e8 --- /dev/null +++ b/ui/src/lib/components/PhonePermissions.svelte @@ -0,0 +1,254 @@ + + +
+

What paired phones may change

+

+ Each of these is off until you turn it on here. A phone cannot turn one on. +

+
    + {#each PHONE_PERMISSIONS as permission (permission.capability)} + {@const on = isGranted(settings.phonePermissions, permission.capability, now)} +
  • + + {permission.title} + {stateWords(settings.phonePermissions, permission.capability, now)} + + {#if on} + + {:else} + + {/if} +
  • + {/each} +
+
+ +{#if asking !== null} + +{/if} + + diff --git a/ui/src/lib/components/PolicyRuleList.svelte b/ui/src/lib/components/PolicyRuleList.svelte index f438fb19..c94bd5a9 100644 --- a/ui/src/lib/components/PolicyRuleList.svelte +++ b/ui/src/lib/components/PolicyRuleList.svelte @@ -13,11 +13,17 @@ import { machineNameFor } from '../machines.svelte.js'; import { settings, revokeRule, setLoadGlobalMemory, setSkipAllApprovals } from '../settings.svelte.js'; + import { permissionCopy } from '../phone-permissions'; + interface Props { /** The attached session, or null when no workspace is open. */ sessionId: string | null; + /** False on a phone the computer has not let turn approvals off + * (TD-5283). Turning them back on is always allowed. */ + canSkipAll?: boolean; } - let { sessionId }: Props = $props(); + let { sessionId, canSkipAll = true }: Props = $props(); + const skipAllOffNote = permissionCopy('approval_switches')?.offOnPhone ?? ''; // TD-5015: another computer's workspace keeps its own rules. let remoteName = $derived(machineNameFor(sessionId)); @@ -38,10 +44,15 @@ type="button" role="switch" aria-checked={settings.skipAllApprovals} + disabled={!settings.skipAllApprovals && !canSkipAll} onclick={() => setSkipAllApprovals(!settings.skipAllApprovals)} >{settings.skipAllApprovals ? 'On' : 'Off'} +{#if !settings.skipAllApprovals && !canSkipAll} + +

{skipAllOffNote}

+{/if}
diff --git a/ui/src/lib/components/PreviewPane.svelte b/ui/src/lib/components/PreviewPane.svelte index 22cb21fb..7c3a11f2 100644 --- a/ui/src/lib/components/PreviewPane.svelte +++ b/ui/src/lib/components/PreviewPane.svelte @@ -58,7 +58,11 @@ {:else if (grok.preview.kind === 'html' || grok.preview.kind === 'pdf' || grok.preview.kind === 'url') && src} - + + {:else} {grok.preview.path ?? grok.preview.url} diff --git a/ui/src/lib/components/SettingsEngine.svelte b/ui/src/lib/components/SettingsEngine.svelte index 857fc4ca..6923ff5a 100644 --- a/ui/src/lib/components/SettingsEngine.svelte +++ b/ui/src/lib/components/SettingsEngine.svelte @@ -1,6 +1,7 @@ + +
+ + {#if mcpImport.done} +

{mcpImport.done}

+ {/if} + {#if open} +

+ Paste the block from a server's README (an mcpServers list). You will see what each + server would run before anything is saved. Tokens go to the keychain, not to config.yaml. +

+ +
+ +
+ {#if mcpImport.error} + + {/if} + {#if stale} +

The text changed. Preview it again before importing.

+ {/if} + {#each mcpImport.rows as row (row.server.source_name)} + {@const problem = rowProblem(row, takenIds(row, mcpImport.rows, listed))} +
+ + {#if row.server.refused} +

Cannot be imported: {row.server.refused}.

+ {:else} + +

+ {#if row.server.transport === 'stdio'} + Runs: {row.server.command.join(' ')} + {:else} + Connects to: {row.server.url} + {/if} +

+ {#each row.server.notes as note (note)} +

{note}

+ {/each} + {#if row.server.off_box} + + {/if} + {#each row.server.values as value (value.name)} + {#if value.disposition === 'needs_input'} + + {:else} +

+ {value.name} · + {value.disposition === 'keychain' ? 'goes to the keychain' : 'written to config.yaml'} +

+ {/if} + {/each} + {#if row.server.not_imported.length > 0} +

Not imported: {row.server.not_imported.join(', ')}.

+ {/if} + {#if row.chosen && problem} +

{problem}

+ {/if} + {/if} +
+ {/each} + {#if mcpImport.rows.length > 0} +
+ +
+ {/if} + {/if} +
+ + diff --git a/ui/src/lib/components/SettingsModels.svelte b/ui/src/lib/components/SettingsModels.svelte index 77a62de6..62c56c26 100644 --- a/ui/src/lib/components/SettingsModels.svelte +++ b/ui/src/lib/components/SettingsModels.svelte @@ -9,6 +9,9 @@ closeSettings, refreshModelCards, saveContextWindow, + chooseEditPreset, + editTarget, + roleView, } from "../settings.svelte.js"; import { cardLine, cardNote } from "../model-card"; import { modelLabel } from "../effort"; @@ -16,6 +19,7 @@ import { openAddModel } from "../onboarding.svelte.js"; import { onMount } from "svelte"; import { sendToDaemon } from "../connection-status.svelte.js"; + import ModelCatalog from "./ModelCatalog.svelte"; // TD-5197: the daemon refreshes model cards in the background, so the // numbers may have changed since the window last asked. Read them fresh @@ -27,6 +31,26 @@ // team": most people run one model in all three roles and never open it. const TIERS = ["brain", "worker", "validator"] as const; + // TD-5239: a model that is the Grok engine may have no roles. The daemon + // then sends none, and there is nothing for "Build a team" to edit. + // TD-5241: the page edits the model chosen here, not only the one new + // chats use. Its roles come from the daemon either way. + const target = $derived(editTarget()); + const roles = $derived(roleView()); + const noRoles = $derived( + settings.hasModel && + target !== null && + settings.presetEngines[target] === "grok" && + Object.keys(roles.slugs).length === 0, + ); + + // What was typed for one model's role is not a draft for another's. + function pickEdit(name: string): void { + drafts = {}; + windowDrafts = {}; + chooseEditPreset(name); + } + // Both are modal: close Settings so the walkthrough is on top. function addModel(): void { closeSettings(); @@ -42,12 +66,12 @@ let drafts = $state>({}); function slugValue(tier: string): string { - return drafts[tier] ?? settings.tierSlugs[tier] ?? ""; + return drafts[tier] ?? roles.slugs[tier] ?? ""; } function commitSlug(tier: string): void { const next = slugValue(tier).trim(); - if (next === "" || next === settings.tierSlugs[tier]) return; + if (next === "" || next === roles.slugs[tier]) return; saveSlug(tier, next); } @@ -55,7 +79,7 @@ let windowDrafts = $state>({}); function windowValue(tier: string): string { - return windowDrafts[tier] ?? String(settings.tierCards[tier]?.context_window ?? ""); + return windowDrafts[tier] ?? String(roles.cards[tier]?.context_window ?? ""); } function commitWindow(tier: string): void { @@ -86,13 +110,38 @@
-{#if settings.hasModel && settings.activePreset} + + +{#if settings.hasModel && target !== null && settings.presets.length > 1} + +

+ Choosing a model here does not switch any chat or the model new chats use. +

+{/if} +{#if noRoles} +

+ {modelLabel(target ?? "", settings.presetLabels)} runs on the Grok CLI, which brings its own + model, so there are no roles to set here. +

+{:else if settings.hasModel && target !== null}

Build a team

Every turn has three roles. One model can fill all of them, and that is the normal setup. A team gives each role its own model: worth it when a strong, costly model should plan while a - cheaper or local one does the bulk of the work. Edits here change the active model's entry in - your config.yaml and apply to new sessions. + cheaper or local one does the bulk of the work. Edits here change the entry of the model + chosen above in your config.yaml and apply to new sessions.

{#each TIERS as tier (tier)}
+ {/if} {#if !onComputer}

Pair or remove phones in Settings on the computer.

@@ -255,9 +264,12 @@ {:else if settings.section === 'budget'} {:else if settings.section === 'policy'} - + {:else if settings.section === 'mcp'} - + {:else if settings.section === 'email'} {:else if settings.section === 'github'} diff --git a/ui/src/lib/components/chat/ModelControls.svelte b/ui/src/lib/components/chat/ModelControls.svelte index 3d9bcc51..7753d215 100644 --- a/ui/src/lib/components/chat/ModelControls.svelte +++ b/ui/src/lib/components/chat/ModelControls.svelte @@ -7,9 +7,14 @@ import { grok, setGrokMode } from "../../grok.svelte.js"; import { session, setPlan } from "../../session-status.svelte.js"; import { settings } from "../../settings.svelte.js"; + import { newChatEngine } from "../../model-engine"; import ModelPicker from "./ModelPicker.svelte"; - const grokEngine = $derived((session.engine ?? settings.engine) === "grok"); + // Before a chat reports its engine: the one a new chat would get (TD-5239). + const grokEngine = $derived( + (session.engine ?? + newChatEngine(settings.activePreset, settings.presetEngines, settings.engine)) === "grok", + ); const PLAN_HINT = "Plan mode: the agent looks around without changing anything, then shows you a plan. Nothing runs until you approve it."; diff --git a/ui/src/lib/components/chat/ModelPicker.svelte b/ui/src/lib/components/chat/ModelPicker.svelte index f875f6b8..872a8f1f 100644 --- a/ui/src/lib/components/chat/ModelPicker.svelte +++ b/ui/src/lib/components/chat/ModelPicker.svelte @@ -18,18 +18,15 @@ EFFORT_OPTIONS, effortLabel, groupModels, - modelDescription, modelLabel, } from "../../effort"; import { MODELS_DOC_URL, openDoc } from "../../docs-links"; import { openAddModel } from "../../onboarding.svelte.js"; - import { - session, - setEffort, - setFast, - setSessionPreset, - setUltracode, - } from "../../session-status.svelte.js"; + import ModelRow from "./ModelRow.svelte"; + import { onMount } from "svelte"; + import { askReadiness, watchReadiness } from "../../model-ready.svelte.js"; + import { sectionModels } from "../../model-sections"; + import { session, setEffort, setFast, setUltracode } from "../../session-status.svelte.js"; import { settings } from "../../settings.svelte.js"; type View = "main" | "effort" | "more" | "teams"; @@ -46,20 +43,24 @@ current, }), ); - const locked = $derived(session.turnActive); + // TD-5236: the main list in sections; the daemon says which each model is in. + const sections = $derived(sectionModels(groups.main, settings.presetGroups, settings.pickerGroups)); function close(): void { open = false; view = "main"; } + // TD-5240: opening the picker asks the daemon which models can run now. + onMount(watchReadiness); + function toggle(): void { - if (!settings.hasModel) { - openAddModel(); - return; + if (!settings.hasModel) openAddModel(); + else if (open) close(); + else { + open = true; + askReadiness(); } - if (open) close(); - else open = true; } function addModel(): void { @@ -72,11 +73,6 @@ void openDoc(MODELS_DOC_URL); } - function pickModel(name: string): void { - if (name !== current) setSessionPreset(name); - close(); - } - // Close on a click outside or Escape (Escape steps back from a submenu first). $effect(() => { if (!open) return; @@ -109,31 +105,20 @@ aria-expanded={open} onclick={toggle} > - {settings.hasModel ? chipLabel(current, session.effort, settings.presetLabels) : "Add a model"}{#if settings.hasModel && session.fast}{" · Fast"}{/if}{#if settings.hasModel && session.ultracode}{" · Ultracode"}{/if} + {settings.hasModel ? chipLabel(current, session.effort, settings.presetLabels) : "Add a model"}{#if settings.hasModel && session.fast}{" · Fast"}{/if}{#if settings.hasModel && session.ultracode}{" · Ultracode"}{/if}{#if settings.hasModel && session.pendingPreset}{` → ${modelLabel(session.pendingPreset, settings.presetLabels)} after this turn`}{/if} {#if open} @@ -341,6 +311,16 @@ outline-offset: -1px; } + .section { + padding: var(--space-2) var(--space-2) var(--space-1); + font-size: var(--text-xs); + color: var(--color-ink-muted); + } + + .pending-tag { + color: var(--color-ink-muted); + } + .item:disabled { opacity: 0.45; cursor: not-allowed; diff --git a/ui/src/lib/components/chat/ModelRow.svelte b/ui/src/lib/components/chat/ModelRow.svelte new file mode 100644 index 00000000..ccde49b8 --- /dev/null +++ b/ui/src/lib/components/chat/ModelRow.svelte @@ -0,0 +1,180 @@ + + +
+ + {#if offersOnlyHere(name)} + + {/if} +
+ + diff --git a/ui/src/lib/doctor.svelte.ts b/ui/src/lib/doctor.svelte.ts index c24e1fe3..a4ee30ab 100644 --- a/ui/src/lib/doctor.svelte.ts +++ b/ui/src/lib/doctor.svelte.ts @@ -74,6 +74,7 @@ export const STATUS_MARK: Record = { ok: "✓", fail: "✗", skip: "–", + warn: "!", }; /** The pasteable report: one line per check, fix lines indented under fails. */ diff --git a/ui/src/lib/error-copy.ts b/ui/src/lib/error-copy.ts index d20cfe71..1780546d 100644 --- a/ui/src/lib/error-copy.ts +++ b/ui/src/lib/error-copy.ts @@ -298,6 +298,11 @@ export function daemonErrorCopy(code: string, message: string): NoticeSpec { body: `${message} The key you typed is still in the wizard — click Store key again once it's unlocked.`, }; } + if (code === "phone_permission_off") { + // TD-5279: a phone asked for something the computer has not switched + // on for phones. The daemon's message names the switch and where it is. + return { severity: "toast", title: "Turned off for phones", body: message }; + } if (code === "session_not_running") { // TD-1711: the send targeted a dead session (tombstone or terminal). // The daemon's message already names the fix (start a new session, diff --git a/ui/src/lib/grok-on-picker.test.ts b/ui/src/lib/grok-on-picker.test.ts new file mode 100644 index 00000000..47b3a397 --- /dev/null +++ b/ui/src/lib/grok-on-picker.test.ts @@ -0,0 +1,122 @@ +// @vitest-environment jsdom +// +// TD-5258: Settings → Engine can put the Grok CLI on the model list. The +// button is always there; it is held, with the reason, where the CLI is +// missing, and gives way to a line once Grok is on the list. + +import { afterEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, unmount } from "svelte"; +import type { ClientMessageUnion, DaemonEventUnion } from "./protocol"; + +const mocks = vi.hoisted(() => ({ + sent: [] as ClientMessageUnion[], + sendOk: true, + subs: new Set<(event: DaemonEventUnion) => void>(), +})); + +vi.mock("./connection-status.svelte.js", () => { + const onDaemonEvent = (handler: (event: DaemonEventUnion) => void) => { + mocks.subs.add(handler); + return () => mocks.subs.delete(handler); + }; + return { + onEvent: onDaemonEvent, + onDaemonEvent, + sendToDaemon: (msg: ClientMessageUnion) => { + if (mocks.sendOk) mocks.sent.push(msg); + return mocks.sendOk; + }, + }; +}); + +import GrokOnPicker from "./components/GrokOnPicker.svelte"; +import { settings } from "./settings.svelte.js"; + +let component: ReturnType | null = null; +const BUTTON = "Add Grok to your models"; + +function open(grokAvailable: boolean): HTMLElement { + settings.grokAvailable = grokAvailable; + settings.presets = ["local"]; + settings.presetLabels = { local: "Local" }; + const target = document.createElement("div"); + document.body.appendChild(target); + component = mount(GrokOnPicker, { target }); + flushSync(); + return target; +} + +function button(root: HTMLElement): HTMLButtonElement | null { + return ( + [...root.querySelectorAll("button")].find( + (b) => b.textContent?.trim() === BUTTON, + ) ?? null + ); +} + +function emit(event: Partial & { type: string }): void { + for (const sub of [...mocks.subs]) sub(event as DaemonEventUnion); + flushSync(); +} + +afterEach(() => { + if (component) unmount(component); + component = null; + document.body.innerHTML = ""; + mocks.sent = []; + mocks.sendOk = true; + mocks.subs.clear(); + settings.grokAvailable = false; + settings.presets = []; + settings.presetLabels = {}; + settings.presetEngines = {}; +}); + +describe("Settings → Engine → Grok on the model list", () => { + it("sends one message when the Grok CLI is here", () => { + const root = open(true); + expect(button(root)?.disabled).toBe(false); + button(root)!.click(); + flushSync(); + expect(mocks.sent).toEqual([{ type: "add_grok_preset", label: "Grok" }]); + // Held until the daemon answers, so a second click cannot add a second. + expect(button(root)?.disabled).toBe(true); + }); + + it("is held, with the reason, where the Grok CLI is not found", () => { + const root = open(false); + expect(button(root)).not.toBeNull(); + expect(button(root)?.disabled).toBe(true); + expect(button(root)?.title).toContain("not found"); + expect(root.textContent).toContain("Install Grok Build"); + button(root)!.click(); + expect(mocks.sent).toEqual([]); + }); + + it("gives way to a line once Grok is on the list", () => { + const root = open(true); + button(root)!.click(); + settings.presets = ["grok", "local"]; + settings.presetLabels = { grok: "Grok", local: "Local" }; + settings.presetEngines = { grok: "grok" }; + emit({ type: "setup_state" }); + expect(button(root)).toBeNull(); + expect(root.textContent).toContain("Grok is on the model list"); + }); + + it("shows why the daemon refused, and lets the person try again", () => { + const root = open(true); + button(root)!.click(); + emit({ type: "error", code: "grok_unavailable", message: "The Grok CLI was not found." }); + expect(root.querySelector('[role="alert"]')?.textContent).toContain("not found"); + expect(button(root)?.disabled).toBe(false); + }); + + it("does not wait on a message that was never sent", () => { + const root = open(true); + mocks.sendOk = false; + button(root)!.click(); + flushSync(); + expect(button(root)?.disabled).toBe(false); + }); +}); diff --git a/ui/src/lib/mcp-import.svelte.ts b/ui/src/lib/mcp-import.svelte.ts new file mode 100644 index 00000000..2451c355 --- /dev/null +++ b/ui/src/lib/mcp-import.svelte.ts @@ -0,0 +1,145 @@ +// Paste a server's JSON, see what importing would do, then import (TD-5253). +// +// The daemon reads the text; this only holds it, shows the preview and +// collects the choices. The text can hold a token, so it is kept in memory +// only and cleared the moment an import succeeds. The preview never holds +// a value: the daemon does not send one. + +import { onEvent, sendToDaemon } from "./connection-status.svelte.js"; +import type { DaemonEventUnion, McpImportChoice, McpImportServer } from "./protocol"; + +export interface ImportRow { + server: McpImportServer; + /** Ticked to import. A refused server can never be ticked. */ + chosen: boolean; + id: string; + allowRemote: boolean; + /** Values the preview asked for, by name. */ + inputs: Record; +} + +export const mcpImport = $state({ + text: "", + /** The text the preview was made from; a later edit makes the preview stale. */ + previewedText: null as string | null, + rows: [] as ImportRow[], + error: null as string | null, + busy: false, + done: null as string | null, +}); + +const ID_RE = /^[a-z][a-z0-9-]{0,31}$/; +let started = false; +let importing = false; + +export function startMcpImport(): () => void { + if (started) return () => {}; + started = true; + const off = onEvent(reduce); + return () => { + started = false; + off(); + }; +} + +export function resetMcpImport(): void { + mcpImport.text = ""; + mcpImport.previewedText = null; + mcpImport.rows = []; + mcpImport.error = null; + mcpImport.busy = false; + mcpImport.done = null; + importing = false; +} + +/** Why this row cannot be imported as it stands, or "" when it can. */ +export function rowProblem(row: ImportRow, taken: readonly string[]): string { + if (row.server.refused) return `Cannot be imported: ${row.server.refused}.`; + if (!ID_RE.test(row.id)) return "The id must be lowercase letters, digits and -, starting with a letter."; + if (taken.includes(row.id)) return "A server with this id is already listed. Give it another id."; + if (row.server.off_box && !row.allowRemote) return "Tick the box to allow connecting to this server."; + const missing = row.server.values.find( + (value) => value.disposition === "needs_input" && !(row.inputs[value.name] ?? "").trim(), + ); + if (missing) return `Type a value for ${missing.name}.`; + return ""; +} + +/** Ids this import may not use: the ones listed already, and the other ticked rows'. */ +export function takenIds(row: ImportRow, rows: readonly ImportRow[], listed: readonly string[]): string[] { + return [...listed, ...rows.filter((other) => other !== row && other.chosen).map((other) => other.id)]; +} + +export function canImport(rows: readonly ImportRow[], listed: readonly string[], stale: boolean): boolean { + const chosen = rows.filter((row) => row.chosen); + if (stale || chosen.length === 0) return false; + return chosen.every((row) => rowProblem(row, takenIds(row, rows, listed)) === ""); +} + +export function isStale(): boolean { + return mcpImport.previewedText !== null && mcpImport.previewedText !== mcpImport.text; +} + +export function previewImport(): void { + const text = mcpImport.text; + if (text.trim() === "") return; + mcpImport.error = null; + mcpImport.done = null; + mcpImport.busy = true; + importing = false; + mcpImport.previewedText = text; + if (!sendToDaemon({ type: "preview_mcp_import", text })) { + mcpImport.busy = false; + mcpImport.error = "Not connected."; + } +} + +export function runImport(listed: readonly string[]): void { + if (mcpImport.previewedText === null || !canImport(mcpImport.rows, listed, isStale())) return; + const servers: McpImportChoice[] = mcpImport.rows + .filter((row) => row.chosen) + .map((row) => ({ + source_name: row.server.source_name, + id: row.id, + ...(row.allowRemote ? { allow_remote: true } : {}), + ...(Object.keys(row.inputs).length > 0 ? { inputs: { ...row.inputs } } : {}), + })); + mcpImport.error = null; + mcpImport.busy = true; + importing = true; + if (!sendToDaemon({ type: "import_mcp_servers", text: mcpImport.previewedText, servers })) { + mcpImport.busy = false; + importing = false; + mcpImport.error = "Not connected."; + } +} + +function reduce(event: DaemonEventUnion): void { + if (event.type === "mcp_import_preview") { + mcpImport.busy = false; + mcpImport.error = event.error || null; + mcpImport.rows = event.servers.map((server) => ({ + server, + chosen: !server.refused, + id: server.id, + allowRemote: false, + inputs: {}, + })); + return; + } + if (event.type === "setup_state" && importing) { + // The ack. The text may hold a token: drop it, and the typed values. + const count = mcpImport.rows.filter((row) => row.chosen).length; + resetMcpImport(); + mcpImport.done = count === 1 ? "Imported 1 server." : `Imported ${count} servers.`; + return; + } + if ( + event.type === "error" && + (event.in_reply_to === "import_mcp_servers" || event.in_reply_to === "preview_mcp_import") + ) { + mcpImport.busy = false; + importing = false; + mcpImport.error = event.message; + } +} diff --git a/ui/src/lib/mcp-import.test.ts b/ui/src/lib/mcp-import.test.ts new file mode 100644 index 00000000..579a9662 --- /dev/null +++ b/ui/src/lib/mcp-import.test.ts @@ -0,0 +1,220 @@ +// @vitest-environment jsdom +// +// TD-5253: paste a server's JSON, see what would run, then import. The +// daemon reads the text and decides; these pin what the window sends, when +// Import is allowed, and that the pasted text is dropped once it is used. + +import { afterEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, unmount } from "svelte"; +import type { ClientMessageUnion, DaemonEventUnion, McpImportServer } from "./protocol"; + +const mocks = vi.hoisted(() => ({ + sent: [] as ClientMessageUnion[], + listeners: new Set<(event: DaemonEventUnion) => void>(), +})); + +vi.mock("./connection-status.svelte.js", () => ({ + onEvent: (fn: (event: DaemonEventUnion) => void) => { + mocks.listeners.add(fn); + return () => mocks.listeners.delete(fn); + }, + sendToDaemon: (msg: ClientMessageUnion) => (mocks.sent.push(msg), true), +})); + +import SettingsMcpImport from "./components/SettingsMcpImport.svelte"; +import { settings, type McpServerRow } from "./settings.svelte.js"; +import { + canImport, + mcpImport, + resetMcpImport, + rowProblem, + type ImportRow, +} from "./mcp-import.svelte.js"; + +let component: ReturnType | null = null; + +function emit(event: unknown): void { + for (const fn of [...mocks.listeners]) fn(event as DaemonEventUnion); + flushSync(); +} + +function server(over: Partial = {}): McpImportServer { + return { + source_name: "GitHub", + id: "github", + transport: "stdio", + command: ["npx", "-y", "server-github"], + url: "", + off_box: false, + exists: false, + values: [{ name: "GITHUB_TOKEN", target: "env", disposition: "keychain" }], + not_imported: [], + notes: [], + refused: "", + ...over, + }; +} + +function row(over: Partial = {}, more: Partial = {}): ImportRow { + const s = server(over); + return { server: s, chosen: true, id: s.id, allowRemote: false, inputs: {}, ...more }; +} + +function show(): HTMLElement { + const target = document.createElement("div"); + document.body.appendChild(target); + component = mount(SettingsMcpImport, { target }); + flushSync(); + return target; +} + +function press(root: ParentNode, label: string): void { + const button = [...root.querySelectorAll("button")].find((b) => b.textContent?.trim() === label); + if (!button) throw new Error(`no button "${label}"`); + button.click(); + flushSync(); +} + +function type(root: ParentNode, label: string, value: string): void { + const field = root.querySelector(`[aria-label="${label}"]`); + if (!field) throw new Error(`no field "${label}"`); + field.value = value; + field.dispatchEvent(new Event("input", { bubbles: true })); + flushSync(); +} + +afterEach(() => { + if (component) unmount(component); + component = null; + document.body.innerHTML = ""; + mocks.sent = []; + settings.mcpServers = []; + resetMcpImport(); +}); + +const TEXT = '{"mcpServers": {"GitHub": {"command": "npx"}}}'; + +describe("when Import is allowed", () => { + it("needs a valid id that is not taken", () => { + expect(rowProblem(row(), [])).toBe(""); + expect(rowProblem(row({}, { id: "Not A Slug" }), [])).toContain("lowercase"); + expect(rowProblem(row(), ["github"])).toContain("already listed"); + }); + + it("needs the tick for a server on another machine", () => { + const remote = { transport: "http" as const, url: "https://mcp.example.test/x", off_box: true }; + expect(rowProblem(row(remote), [])).toContain("Tick the box"); + expect(rowProblem(row(remote, { allowRemote: true }), [])).toBe(""); + }); + + it("needs every asked-for value typed", () => { + const asks = { + values: [{ name: "API_KEY", target: "env" as const, disposition: "needs_input" as const }], + }; + expect(rowProblem(row(asks), [])).toBe("Type a value for API_KEY."); + expect(rowProblem(row(asks, { inputs: { API_KEY: " " } }), [])).toBe("Type a value for API_KEY."); + expect(rowProblem(row(asks, { inputs: { API_KEY: "typed-value" } }), [])).toBe(""); + }); + + it("never for a refused server, a stale preview, or nothing ticked", () => { + expect(rowProblem(row({ refused: "it uses SSE" }), [])).toContain("Cannot be imported"); + expect(canImport([row()], [], false)).toBe(true); + expect(canImport([row()], [], true)).toBe(false); + expect(canImport([row({}, { chosen: false })], [], false)).toBe(false); + // One bad ticked row holds back the lot; an unticked bad row does not. + expect(canImport([row(), row({ source_name: "B", id: "github" })], [], false)).toBe(false); + expect(canImport([row(), row({ refused: "no" }, { chosen: false })], [], false)).toBe(true); + }); +}); + +describe("Settings → MCP servers, Paste JSON", () => { + it("previews, shows what would run, and sends nothing else until Import", () => { + const root = show(); + press(root, "Paste JSON"); + type(root, "MCP server JSON", TEXT); + press(root, "Preview"); + expect(mocks.sent).toEqual([{ type: "preview_mcp_import", text: TEXT }]); + emit({ type: "mcp_import_preview", seq: 1, servers: [server()], error: "" }); + expect(root.textContent).toContain("npx -y server-github"); + expect((root.textContent ?? "").replace(/\s+/g, " ")).toContain( + "GITHUB_TOKEN · goes to the keychain", + ); + expect(mocks.sent).toHaveLength(1); + press(root, "Import"); + expect(mocks.sent[1]).toEqual({ + type: "import_mcp_servers", + text: TEXT, + servers: [{ source_name: "GitHub", id: "github" }], + }); + }); + + it("drops the pasted text and the typed values once the import is acknowledged", () => { + const root = show(); + press(root, "Paste JSON"); + type(root, "MCP server JSON", TEXT); + press(root, "Preview"); + emit({ + type: "mcp_import_preview", + seq: 1, + error: "", + servers: [ + server({ values: [{ name: "API_KEY", target: "env", disposition: "needs_input", reason: "Type it." }] }), + ], + }); + const importButton = [...root.querySelectorAll("button")].find((b) => b.textContent?.trim() === "Import"); + expect(importButton?.disabled).toBe(true); + type(root, "Value for API_KEY", "typed-value-0001"); + press(root, "Import"); + expect(mocks.sent[1]).toMatchObject({ servers: [{ inputs: { API_KEY: "typed-value-0001" } }] }); + emit({ type: "setup_state", seq: 1, has_api_key: false, active_preset: "" }); + expect(mcpImport.text).toBe(""); + expect(mcpImport.rows).toEqual([]); + expect(mcpImport.previewedText).toBeNull(); + expect(root.textContent).toContain("Imported 1 server."); + expect(root.innerHTML).not.toContain("typed-value-0001"); + }); + + it("a setup_state that is not the ack leaves the text alone", () => { + const root = show(); + press(root, "Paste JSON"); + type(root, "MCP server JSON", TEXT); + emit({ type: "setup_state", seq: 1, has_api_key: false, active_preset: "" }); + expect(mcpImport.text).toBe(TEXT); + }); + + it("says why a server cannot be imported, and it cannot be ticked", () => { + const root = show(); + press(root, "Paste JSON"); + type(root, "MCP server JSON", TEXT); + press(root, "Preview"); + emit({ + type: "mcp_import_preview", + seq: 1, + error: "", + servers: [server({ refused: "it needs an OAuth sign-in" })], + }); + expect(root.textContent).toContain("Cannot be imported: it needs an OAuth sign-in."); + expect(root.querySelector('[aria-label="Import GitHub"]')?.disabled).toBe(true); + const importButton = [...root.querySelectorAll("button")].find((b) => b.textContent?.trim() === "Import"); + expect(importButton?.disabled).toBe(true); + }); + + it("makes a changed text be previewed again, and shows the daemon's reason on a refusal", () => { + settings.mcpServers = [{ id: "github" } as McpServerRow]; + const root = show(); + press(root, "Paste JSON"); + type(root, "MCP server JSON", TEXT); + press(root, "Preview"); + emit({ type: "mcp_import_preview", seq: 1, servers: [server()], error: "" }); + expect(root.textContent).toContain("already listed"); + type(root, "Id for GitHub", "github-2"); + type(root, "MCP server JSON", TEXT + " "); + expect(root.textContent).toContain("Preview it again"); + const importButton = [...root.querySelectorAll("button")].find((b) => b.textContent?.trim() === "Import"); + expect(importButton?.disabled).toBe(true); + emit({ type: "mcp_import_preview", seq: 1, servers: [], error: "That is not JSON (line 1, column 2)." }); + expect(root.textContent).toContain("That is not JSON (line 1, column 2)."); + emit({ type: "error", code: "bad_request", message: "'GitHub': type a value for X.", in_reply_to: "import_mcp_servers" }); + expect(root.textContent).toContain("'GitHub': type a value for X."); + }); +}); diff --git a/ui/src/lib/mcp-read-only.test.ts b/ui/src/lib/mcp-read-only.test.ts new file mode 100644 index 00000000..583bf7a1 --- /dev/null +++ b/ui/src/lib/mcp-read-only.test.ts @@ -0,0 +1,106 @@ +// @vitest-environment jsdom +// +// TD-5276, TD-5279: an MCP stdio server is a command the computer runs. Off +// the computer the page is the list and nothing else, and it sends nothing, +// unless the computer switched "MCP servers" on for phones. + +import { afterEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, unmount } from "svelte"; +import type { ClientMessageUnion } from "./protocol"; + +const mocks = vi.hoisted(() => ({ sent: [] as ClientMessageUnion[] })); + +vi.mock("./connection-status.svelte.js", () => ({ + onEvent: () => () => undefined, + sendToDaemon: (msg: ClientMessageUnion) => (mocks.sent.push(msg), true), +})); + +import SettingsMcp from "./components/SettingsMcp.svelte"; +import { settings, type McpServerRow } from "./settings.svelte.js"; + +let component: ReturnType | null = null; + +const SERVERS = [ + { + id: "files", + transport: "stdio", + command: ["npx", "files-server"], + url: "", + enabled: true, + excludedTools: [], + values: [{ name: "FILES_TOKEN", stored: true, literal: false }], + }, + { + id: "docs", + transport: "http", + command: [], + url: "http://127.0.0.1:9000/mcp", + enabled: false, + excludedTools: [], + values: [], + }, +] as unknown as McpServerRow[]; + +function open(canManage: boolean): HTMLElement { + settings.mcpServers = SERVERS; + const target = document.createElement("div"); + document.body.appendChild(target); + component = mount(SettingsMcp, { target, props: { canManage } }); + flushSync(); + return target; +} + +afterEach(() => { + if (component) unmount(component); + component = null; + document.body.innerHTML = ""; + mocks.sent = []; + settings.mcpServers = []; +}); + +describe("Settings → MCP servers, off the computer", () => { + it("shows each server, what it runs and whether it is on", () => { + const root = open(false); + const text = root.textContent ?? ""; + expect(text).toContain("files"); + expect(text).toContain("npx files-server"); + expect(text).toContain("FILES_TOKEN"); + expect(text).toContain("http://127.0.0.1:9000/mcp"); + const states = [...root.querySelectorAll(".row .state")].map((el) => el.textContent?.trim()); + expect(states).toEqual(["On", "Off"]); + }); + + it("has nothing to press or type into, and says where to make changes", () => { + const root = open(false); + expect(root.querySelectorAll("button")).toHaveLength(0); + expect(root.querySelectorAll("input")).toHaveLength(0); + expect(root.textContent).toContain( + "Adding or changing MCP servers from a phone is turned off. You can turn it on under Settings, Phones and devices on the computer.", + ); + expect(mocks.sent).toEqual([]); + }); +}); + +describe("Settings → MCP servers, on the computer", () => { + it("still has the switch, the value field, Remove and Add server", () => { + const root = open(true); + const names = [...root.querySelectorAll("button")].map( + (b) => b.getAttribute("aria-label") ?? b.textContent?.trim() ?? "", + ); + expect(names).toContain("Add server"); + expect(names).toContain("Remove FILES_TOKEN from files"); + // One per server; the Add form's argument row has a Remove of its own. + expect(root.querySelectorAll(".row .actions .btn--danger")).toHaveLength(2); + expect(root.querySelectorAll('[role="switch"]')).toHaveLength(2); + expect(root.textContent).not.toContain("on the computer."); + }); + + it("is what the page does when it is told nothing", () => { + settings.mcpServers = SERVERS; + const target = document.createElement("div"); + document.body.appendChild(target); + component = mount(SettingsMcp, { target }); + flushSync(); + expect(target.querySelectorAll('[role="switch"]')).toHaveLength(2); + }); +}); diff --git a/ui/src/lib/model-catalog.test.ts b/ui/src/lib/model-catalog.test.ts new file mode 100644 index 00000000..431fbf1e --- /dev/null +++ b/ui/src/lib/model-catalog.test.ts @@ -0,0 +1,206 @@ +// @vitest-environment jsdom +// +// TD-5237: Settings → Model lists the catalog with Duplicate, Rename and +// Remove. Each button sends one message; the daemon's reply is the truth. + +import { afterEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, unmount } from "svelte"; +import type { ClientMessageUnion, DaemonEventUnion } from "./protocol"; + +const mocks = vi.hoisted(() => ({ + sent: [] as ClientMessageUnion[], + sendOk: true, + subs: new Set<(event: DaemonEventUnion) => void>(), +})); + +vi.mock("./connection-status.svelte.js", () => { + const onDaemonEvent = (handler: (event: DaemonEventUnion) => void) => { + mocks.subs.add(handler); + return () => mocks.subs.delete(handler); + }; + return { + onEvent: onDaemonEvent, + onDaemonEvent, + sendToDaemon: (msg: ClientMessageUnion) => { + if (mocks.sendOk) mocks.sent.push(msg); + return mocks.sendOk; + }, + }; +}); + +import ModelCatalog from "./components/ModelCatalog.svelte"; +import { copyLabel, LABEL_MAX, renameTarget } from "./model-catalog"; +import { settings } from "./settings.svelte.js"; + +let component: ReturnType | null = null; + +function emit(event: Partial & { type: string }): void { + for (const sub of [...mocks.subs]) sub(event as DaemonEventUnion); + flushSync(); +} + +function open(): HTMLElement { + settings.presets = ["local", "hosted"]; + settings.presetLabels = { local: "Local", hosted: "Hosted" }; + settings.presetDescriptions = { hosted: "A provider's model" }; + settings.activePreset = "hosted"; + const target = document.createElement("div"); + document.body.appendChild(target); + component = mount(ModelCatalog, { target }); + flushSync(); + return target; +} + +function click(root: HTMLElement, name: string): void { + const button = root.querySelector(`button[aria-label="${name}"]`); + if (button === null) throw new Error(`no button named ${name}`); + button.click(); + flushSync(); +} + +function names(root: HTMLElement): string[] { + return [...root.querySelectorAll("button")].map((b) => b.getAttribute("aria-label") ?? ""); +} + +afterEach(() => { + if (component) unmount(component); + component = null; + document.body.innerHTML = ""; + mocks.sent = []; + mocks.sendOk = true; + mocks.subs.clear(); + settings.presets = []; + settings.presetLabels = {}; + settings.presetDescriptions = {}; + settings.activePreset = null; +}); + +describe("copyLabel", () => { + it("adds copy, then a number when that is taken", () => { + expect(copyLabel("Local", ["Local"])).toBe("Local copy"); + expect(copyLabel("Local", ["Local", "local COPY"])).toBe("Local copy 2"); + expect(copyLabel("Local", ["Local copy", "Local copy 2"])).toBe("Local copy 3"); + }); + + it("cuts a long label so the copy still fits", () => { + const made = copyLabel("x".repeat(LABEL_MAX), []); + expect(made).toHaveLength(LABEL_MAX); + expect(made.endsWith(" copy")).toBe(true); + }); +}); + +describe("renameTarget", () => { + it("is the trimmed label, or null when there is nothing to save", () => { + expect(renameTarget(" House ", "Local")).toBe("House"); + expect(renameTarget("Local", "Local")).toBeNull(); + expect(renameTarget(" ", "Local")).toBeNull(); + expect(renameTarget("x".repeat(LABEL_MAX + 1), "Local")).toBeNull(); + }); +}); + +describe("Settings → Model → What you have added", () => { + it("lists every model and marks the one new chats use", () => { + const root = open(); + const rows = [...root.querySelectorAll("li")].map((li) => li.textContent ?? ""); + expect(rows).toHaveLength(2); + expect(rows[0]).toContain("Local"); + expect(rows[0]).not.toContain("New chats use this"); + expect(rows[1]).toContain("Hosted"); + expect(rows[1]).toContain("New chats use this"); + expect(rows[1]).toContain("A provider's model"); + }); + + it("shows nothing before a model is set up", () => { + const target = document.createElement("div"); + document.body.appendChild(target); + component = mount(ModelCatalog, { target }); + flushSync(); + expect(target.textContent?.trim()).toBe(""); + }); + + it("Duplicate sends the source and a label no model has", () => { + const root = open(); + click(root, "Duplicate Local"); + expect(mocks.sent).toEqual([ + { type: "duplicate_preset", source: "local", label: "Local copy" }, + ]); + }); + + it("holds the other buttons until the daemon answers", () => { + const root = open(); + click(root, "Duplicate Local"); + const hosted = root.querySelector('button[aria-label="Remove Hosted"]'); + expect(hosted?.disabled).toBe(true); + emit({ type: "setup_state" }); + expect( + root.querySelector('button[aria-label="Remove Hosted"]')?.disabled, + ).toBe(false); + }); + + it("Rename sends the new label and keeps the key", () => { + const root = open(); + click(root, "Rename Local"); + const input = root.querySelector('input[aria-label="New name for Local"]'); + expect(input?.value).toBe("Local"); + input!.value = " On this computer "; + input!.dispatchEvent(new Event("input", { bubbles: true })); + flushSync(); + click(root, "Save name for Local"); + expect(mocks.sent).toEqual([ + { type: "set_preset_label", name: "local", label: "On this computer" }, + ]); + }); + + it("Rename with the same label, or cancelled, sends nothing", () => { + const root = open(); + click(root, "Rename Local"); + expect( + root.querySelector('button[aria-label="Save name for Local"]')?.disabled, + ).toBe(true); + click(root, "Cancel renaming Local"); + expect(mocks.sent).toEqual([]); + expect(names(root)).toContain("Rename Local"); + }); + + it("Remove asks first, and Keep sends nothing", () => { + const root = open(); + click(root, "Remove Hosted"); + expect(mocks.sent).toEqual([]); + expect(root.textContent).toContain("Remove Hosted?"); + click(root, "Keep Hosted"); + expect(mocks.sent).toEqual([]); + click(root, "Remove Hosted"); + click(root, "Yes, remove Hosted"); + expect(mocks.sent).toEqual([{ type: "delete_preset", name: "hosted" }]); + }); + + it("shows why the daemon refused, and lets the person try again", () => { + const root = open(); + click(root, "Remove Local"); + click(root, "Yes, remove Local"); + emit({ + type: "error", + code: "bad_request", + message: "Could not remove 'local': another model in the file reuses part of it.", + }); + expect(root.querySelector('[role="alert"]')?.textContent).toContain("reuses part of it"); + expect( + root.querySelector('button[aria-label="Remove Local"]')?.disabled, + ).toBe(false); + }); + + it("does not wait on a message that was never sent", () => { + const root = open(); + mocks.sendOk = false; + click(root, "Duplicate Local"); + expect( + root.querySelector('button[aria-label="Duplicate Hosted"]')?.disabled, + ).toBe(false); + }); + + it("ignores an error that belongs to something else", () => { + const root = open(); + emit({ type: "error", code: "bad_request", message: "not ours" }); + expect(root.querySelector('[role="alert"]')).toBeNull(); + }); +}); diff --git a/ui/src/lib/model-catalog.ts b/ui/src/lib/model-catalog.ts new file mode 100644 index 00000000..279b4010 --- /dev/null +++ b/ui/src/lib/model-catalog.ts @@ -0,0 +1,25 @@ +// TD-5237: Settings → Model lists every model in the catalog with Duplicate, +// Rename and Remove. The daemon does the work; this is the one thing the +// window decides by itself, the label a copy starts with. + +/** Longest label the daemon accepts (`Preset.label`). */ +export const LABEL_MAX = 40; + +/** A label for a copy of `label` that no other model has: "X copy", then + * "X copy 2", and so on. The base is cut so the result fits LABEL_MAX. */ +export function copyLabel(label: string, taken: readonly string[]): string { + const used = new Set(taken.map((name) => name.trim().toLowerCase())); + for (let n = 1; ; n += 1) { + const suffix = n === 1 ? " copy" : ` copy ${n}`; + const candidate = label.trim().slice(0, LABEL_MAX - suffix.length).trimEnd() + suffix; + if (!used.has(candidate.toLowerCase())) return candidate; + } +} + +/** What a typed label would be saved as, or null when it cannot be saved: + * empty, too long, or the label the model already has. */ +export function renameTarget(draft: string, current: string): string | null { + const next = draft.trim(); + if (next === "" || next.length > LABEL_MAX || next === current) return null; + return next; +} diff --git a/ui/src/lib/model-edit.test.ts b/ui/src/lib/model-edit.test.ts new file mode 100644 index 00000000..e1790010 --- /dev/null +++ b/ui/src/lib/model-edit.test.ts @@ -0,0 +1,243 @@ +// @vitest-environment jsdom +// +// TD-5241: Settings → Model edits the model you choose, not only the one +// new chats use. The chosen model's roles come from the daemon +// (`preset_roles`); every save names that model; and choosing one switches +// nothing. + +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, unmount } from "svelte"; +import type { ClientMessageUnion, DaemonEventUnion, SetupState } from "./protocol"; + +const mocks = vi.hoisted(() => ({ + handlers: new Set<(e: DaemonEventUnion) => void>(), + sent: [] as ClientMessageUnion[], +})); + +// Every subscriber, not the latest: the page's own parts listen too. +vi.mock("./connection-status.svelte.js", () => { + const onEvent = (handler: (e: DaemonEventUnion) => void) => { + mocks.handlers.add(handler); + return () => mocks.handlers.delete(handler); + }; + return { + onEvent, + onDaemonEvent: onEvent, + sendToDaemon: (msg: ClientMessageUnion) => (mocks.sent.push(msg), true), + }; +}); + +import SettingsModels from "./components/SettingsModels.svelte"; +import { + chooseEditPreset, + editTarget, + isDiscovered, + resetSettings, + roleView, + saveContextWindow, + saveSlug, + saveTierCredential, + selectedCredential, + settings, + startSettings, +} from "./settings.svelte.js"; + +const CARD = { + input_price: 0, + output_price: 0, + cache_read_price: 0, + context_window: 32768, + max_output_tokens: 4096, +}; + +function setupState(over: Partial = {}): SetupState { + return { + type: "setup_state", + seq: 1, + has_api_key: true, + key_required: true, + has_model: true, + presets: ["api", "grok", "local"], + active_preset: "api", + preset_labels: { api: "Hosted", grok: "Grok", local: "Laptop" }, + preset_engines: { grok: "grok" }, + tier_slugs: { brain: "vendor/big", worker: "vendor/small", validator: "vendor/small" }, + tier_credentials: { brain: "openrouter", worker: "openrouter", validator: "openrouter" }, + tier_loopback: { brain: false, worker: false, validator: false }, + tier_cards: { brain: { ...CARD, context_window: 200000 } }, + ...over, + } as SetupState; +} + +const LOCAL_ROLES = { + type: "preset_roles", + seq: 1, + preset: "local", + tier_slugs: { brain: null, worker: null, validator: null }, + tier_credentials: { brain: null, worker: null, validator: null }, + tier_loopback: { brain: true, worker: true, validator: true }, + tier_cards: { brain: CARD, worker: CARD, validator: CARD }, +} as unknown as DaemonEventUnion; + +function emit(event: DaemonEventUnion): void { + for (const handler of [...mocks.handlers]) handler(event); + flushSync(); +} + +let stop: (() => void) | null = null; +let component: ReturnType | null = null; + +beforeEach(() => { + mocks.sent = []; + resetSettings(); + stop = startSettings(); + emit(setupState()); + mocks.sent = []; +}); + +afterEach(() => { + if (component) unmount(component); + component = null; + document.body.innerHTML = ""; + stop?.(); + resetSettings(); +}); + +describe("choosing the model to edit", () => { + it("edits the model new chats use until another is chosen", () => { + expect(editTarget()).toBe("api"); + expect(roleView().slugs.brain).toBe("vendor/big"); + expect(mocks.sent).toEqual([]); + }); + + it("asks the daemon for the chosen model's roles and shows nothing until they come", () => { + chooseEditPreset("local"); + expect(mocks.sent).toEqual([{ type: "get_preset_roles", name: "local" }]); + expect(editTarget()).toBe("local"); + // Not the active model's roles under another model's name. + expect(roleView()).toEqual({ slugs: {}, credentials: {}, loopback: {}, cards: {} }); + emit(LOCAL_ROLES); + expect(roleView().loopback.brain).toBe(true); + expect(isDiscovered("brain")).toBe(true); + expect(selectedCredential("brain")).toBe(""); + // The active model's own roles are untouched. + expect(settings.tierSlugs.brain).toBe("vendor/big"); + expect(settings.activePreset).toBe("api"); + }); + + it("ignores roles for a model that is not the one being edited", () => { + chooseEditPreset("local"); + emit({ ...LOCAL_ROLES, preset: "grok" } as DaemonEventUnion); + expect(settings.editRoles).toBeNull(); + }); + + it("choosing the active model again needs no ask", () => { + chooseEditPreset("local"); + mocks.sent = []; + chooseEditPreset("api"); + expect(mocks.sent).toEqual([]); + expect(settings.editPreset).toBeNull(); + expect(roleView().slugs.brain).toBe("vendor/big"); + }); +}); + +describe("saving a role of the chosen model", () => { + beforeEach(() => { + chooseEditPreset("local"); + emit(LOCAL_ROLES); + mocks.sent = []; + }); + + it("names the chosen model in every write", () => { + saveSlug("brain", "house/model"); + saveTierCredential("worker", "openrouter"); + saveContextWindow("validator", 65536); + expect(mocks.sent).toEqual([ + { type: "set_tier_slug", preset: "local", tier: "brain", slug: "house/model" }, + { type: "set_tier_credential", preset: "local", tier: "worker", credential: "openrouter" }, + { type: "set_tier_context_window", preset: "local", tier: "validator", context_window: 65536 }, + ]); + }); + + it("compares a window with the chosen model's, not the active one's", () => { + saveContextWindow("brain", 32768); + expect(mocks.sent).toEqual([]); + saveContextWindow("brain", 200000); + expect(mocks.sent).toHaveLength(1); + }); + + it("reads the chosen model again after the daemon acks a write", () => { + saveContextWindow("brain", 65536); + mocks.sent = []; + emit(setupState()); + expect(mocks.sent).toEqual([{ type: "get_preset_roles", name: "local" }]); + expect(editTarget()).toBe("local"); + }); + + it("lets go of a chosen model that was removed, or became the active one", () => { + emit(setupState({ presets: ["api", "grok"] })); + expect(settings.editPreset).toBeNull(); + chooseEditPreset("grok"); + emit(setupState({ active_preset: "grok" })); + expect(settings.editPreset).toBeNull(); + }); +}); + +describe("Settings → Model", () => { + function open(): HTMLElement { + const target = document.createElement("div"); + document.body.appendChild(target); + component = mount(SettingsModels, { target }); + flushSync(); + mocks.sent = []; + return target; + } + + function choose(root: HTMLElement, name: string): void { + const select = root.querySelector('select[aria-label="Model to edit"]')!; + select.value = name; + select.dispatchEvent(new Event("change", { bubbles: true })); + flushSync(); + } + + it("offers every model to edit, starting on the one new chats use", () => { + const root = open(); + const select = root.querySelector('select[aria-label="Model to edit"]')!; + expect([...select.options].map((o) => [o.value, o.textContent])).toEqual([ + ["api", "Hosted"], + ["grok", "Grok"], + ["local", "Laptop"], + ]); + expect(select.value).toBe("api"); + const brain = root.querySelector('input[aria-label="Brain model"]')!; + expect(brain.value).toBe("vendor/big"); + }); + + it("shows the chosen model's roles and switches nothing", () => { + const root = open(); + choose(root, "local"); + expect(mocks.sent).toEqual([{ type: "get_preset_roles", name: "local" }]); + emit(LOCAL_ROLES); + const brain = root.querySelector('input[aria-label="Brain model"]')!; + expect(brain.value).toBe(""); + expect(brain.placeholder).toBe("discovered from the endpoint"); + expect(root.textContent).toContain("Active model Hosted"); + expect(mocks.sent.some((m) => m.type === "set_preset" || m.type === "switch_preset")).toBe( + false, + ); + }); + + it("says a Grok model has no roles to set", () => { + const root = open(); + choose(root, "grok"); + emit({ ...LOCAL_ROLES, preset: "grok", tier_slugs: {}, tier_cards: {} } as DaemonEventUnion); + expect(root.textContent).toContain("Grok runs on the Grok CLI"); + expect(root.querySelector('input[aria-label="Brain model"]')).toBeNull(); + }); + + it("has no chooser when there is one model", () => { + emit(setupState({ presets: ["api"], preset_labels: { api: "Hosted" }, preset_engines: {} })); + const root = open(); + expect(root.querySelector('select[aria-label="Model to edit"]')).toBeNull(); + }); +}); diff --git a/ui/src/lib/model-engine.test.ts b/ui/src/lib/model-engine.test.ts new file mode 100644 index 00000000..3f837171 --- /dev/null +++ b/ui/src/lib/model-engine.test.ts @@ -0,0 +1,107 @@ +// @vitest-environment jsdom +// +// TD-5239: a model can name its engine, and a chat keeps the engine it +// started on. The picker marks a Grok model and holds a switch across +// engines; Settings shows no roles for a Grok model that has none. + +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { flushSync, mount, tick, unmount } from "svelte"; + +import ModelPicker from "./components/chat/ModelPicker.svelte"; +import { engineClash, engineMark, newChatEngine } from "./model-engine"; +import { resetSession, session } from "./session-status.svelte.js"; +import { resetSettings, settings } from "./settings.svelte.js"; + +let app: ReturnType | null = null; + +beforeEach(() => { + resetSession(); + resetSettings(); +}); + +afterEach(async () => { + if (app !== null) { + await unmount(app); + app = null; + } + document.body.replaceChildren(); + resetSession(); + resetSettings(); +}); + +describe("engineMark", () => { + it("marks only a model that names the Grok engine", () => { + const engines = { grok: "grok", api: "native" } as const; + expect(engineMark("grok", engines)).toBe("Grok"); + expect(engineMark("api", engines)).toBe(""); + expect(engineMark("local", engines)).toBe(""); + }); +}); + +describe("newChatEngine", () => { + it("is the active model's engine, else the Engine setting", () => { + expect(newChatEngine("grok", { grok: "grok" }, "native")).toBe("grok"); + expect(newChatEngine("local", { grok: "grok" }, "native")).toBe("native"); + expect(newChatEngine("local", {}, "grok")).toBe("grok"); + expect(newChatEngine(null, { grok: "grok" }, "native")).toBe("native"); + }); +}); + +describe("engineClash", () => { + const engines = { grok: "grok", api: "native" } as const; + + it("holds a model on another engine than the chat's", () => { + expect(engineClash("grok", engines, "native")).toContain("start a new chat"); + expect(engineClash("api", engines, "grok")).toContain("start a new chat"); + }); + + it("lets through the same engine, a model that names none, and a chat not yet known", () => { + expect(engineClash("grok", engines, "grok")).toBeNull(); + expect(engineClash("api", engines, "native")).toBeNull(); + expect(engineClash("local", engines, "native")).toBeNull(); + expect(engineClash("local", engines, "grok")).toBeNull(); + expect(engineClash("grok", engines, null)).toBeNull(); + }); +}); + +describe("the model picker", () => { + async function openPicker(): Promise { + session.sessionId = "s1"; + session.preset = "local"; + session.engine = "native"; + settings.hasModel = true; + settings.presets = ["api", "grok", "local"]; + settings.presetLabels = { api: "API", grok: "Grok Build", local: "Local" }; + settings.presetEngines = { grok: "grok" }; + app = mount(ModelPicker, { target: document.body }); + await tick(); + document.querySelector('[aria-label="Model and effort"]')!.click(); + flushSync(); + return document.querySelector('[role="menu"]')!; + } + + function row(menu: HTMLElement, label: string): HTMLButtonElement { + const found = [...menu.querySelectorAll('[role="menuitemradio"]')].find( + (button) => button.querySelector(".label")?.textContent?.includes(label), + ); + if (found === undefined) throw new Error(`no row for ${label}`); + return found; + } + + it("marks the Grok model and no other", async () => { + const menu = await openPicker(); + expect(row(menu, "Grok Build").querySelector(".badge")?.textContent).toBe("Grok"); + expect(row(menu, "API").querySelector(".badge")).toBeNull(); + expect(row(menu, "Local").querySelector(".badge")).toBeNull(); + }); + + it("says the Grok model opens a new chat for a chat on TST Desk's own loop", async () => { + const menu = await openPicker(); + const grok = row(menu, "Grok Build"); + // TD-5235: no longer held. It cannot move this chat, so it opens one. + expect(grok.disabled).toBe(false); + expect(grok.title).toContain("Opens a new chat"); + expect(grok.querySelector(".sub")?.textContent).toBe("Opens a new chat"); + expect(row(menu, "API").title).toBe(""); + }); +}); diff --git a/ui/src/lib/model-engine.ts b/ui/src/lib/model-engine.ts new file mode 100644 index 00000000..b083e01e --- /dev/null +++ b/ui/src/lib/model-engine.ts @@ -0,0 +1,37 @@ +// TD-5239: a model in the catalog can name its engine, and a chat keeps the +// engine it started on. The daemon sends which models name one +// (`setup_state.preset_engines`) and each chat's engine; these say what the +// picker shows for them. The daemon still refuses a switch across engines; +// the picker only says so before the click. + +export type Engine = "native" | "grok"; + +export type PresetEngines = Readonly>; + +/** The mark on a model's row: "Grok" for one that runs on the Grok CLI. */ +export function engineMark(preset: string, engines: PresetEngines): string { + return engines[preset] === "grok" ? "Grok" : ""; +} + +/** The engine a new chat on `active` would run on: its own, else the setting. */ +export function newChatEngine( + active: string | null, + engines: PresetEngines, + setting: Engine, +): Engine { + return (active !== null ? engines[active] : undefined) ?? setting; +} + +/** Why the chat cannot switch to `preset`, or null. A model that names no + * engine suits a chat on either. */ +export function engineClash( + preset: string, + engines: PresetEngines, + chatEngine: Engine | null, +): string | null { + const wanted = engines[preset]; + if (wanted === undefined || chatEngine === null || wanted === chatEngine) return null; + return wanted === "grok" + ? "Runs on the Grok CLI. A chat keeps the engine it started on, so start a new chat to use it." + : "Runs on TST Desk's own loop. This chat is on the Grok CLI, so start a new chat to use it."; +} diff --git a/ui/src/lib/model-pick.ts b/ui/src/lib/model-pick.ts new file mode 100644 index 00000000..8e294240 --- /dev/null +++ b/ui/src/lib/model-pick.ts @@ -0,0 +1,73 @@ +// TD-5235, TD-5236: what picking a model does, in one place. The picker's +// rows and the quick-switch shortcut (⌘⌥1…9, Ctrl+Alt+1…9) both come here, +// so a key press and a click mean the same thing. + +import { isBridged } from "./bridged-id"; +import { engineClash } from "./model-engine"; +import { modelAtSlot } from "./model-sections"; +import { session, switchModel, useForNewChats } from "./session-status.svelte.js"; +import { newSession } from "./sessions.svelte.js"; +import { settings } from "./settings.svelte.js"; + +export const BUSY = "Cannot change model while a turn is running"; +export const AFTER_TURN = "Applies when this turn finishes"; +export const NEW_CHAT = "Opens a new chat"; + +/** A chat on another computer switches through its hub, which keeps the + * older verb and its rules. */ +function bridged(): boolean { + return isBridged(session.sessionId); +} + +/** TD-5239: `name` is on another engine than this chat's, so the chat + * cannot move to it. TD-5235: its row opens a new chat on it instead. */ +export function opensNewChat(name: string): boolean { + return ( + name !== session.preset && + engineClash(name, settings.presetEngines, session.engine) !== null + ); +} + +/** The row cannot be used right now: another computer's chat, mid-turn. */ +export function held(name: string): boolean { + return name !== session.preset && session.turnActive && bridged(); +} + +/** Whether "Only this chat" is offered for `name`. */ +export function offersOnlyHere(name: string): boolean { + return name !== session.preset && !opensNewChat(name) && !bridged(); +} + +/** The row's tooltip: why it is held, or what the click will do. */ +export function pickHint(name: string): string { + if (name === session.preset) return ""; + const reason = engineClash(name, settings.presetEngines, session.engine); + if (reason !== null) return `${NEW_CHAT}. ${reason}`; + if (held(name)) return BUSY; + return session.turnActive ? AFTER_TURN : ""; +} + +/** A click, or the model's shortcut: this chat moves to `name` and new + * chats use it too. A model on the other engine opens a new chat. */ +export function pickModel(name: string): void { + if (held(name)) return; + if (opensNewChat(name)) { + useForNewChats(name); + newSession(name); + } else if (name !== session.preset || name !== settings.activePreset) { + switchModel(name, { onlyThisChat: false }); + } +} + +/** "Only this chat": the model new chats use stays as it is. */ +export function pickOnlyHere(name: string): void { + if (offersOnlyHere(name)) switchModel(name, { onlyThisChat: true }); +} + +/** The quick-switch shortcut. False when no model has that slot. */ +export function pickSlot(slot: number): boolean { + const name = modelAtSlot(slot, settings.presetSlots); + if (name === null) return false; + pickModel(name); + return true; +} diff --git a/ui/src/lib/model-picker.test.ts b/ui/src/lib/model-picker.test.ts index fa9c58d9..e161bbee 100644 --- a/ui/src/lib/model-picker.test.ts +++ b/ui/src/lib/model-picker.test.ts @@ -30,6 +30,8 @@ function fakeClient(): ProtocolClient { sent.push({ type: "set_ultracode", session_id: sessionId, on }), setSessionPreset: (sessionId: string, name: string) => sent.push({ type: "set_session_preset", session_id: sessionId, name }), + // TD-5235: the picker's click goes out as one switch_preset. + send: (msg: unknown) => (sent.push(msg), true), } as unknown as ProtocolClient; } @@ -110,7 +112,10 @@ describe("model picker", () => { await click(item("More models")); expect(texts(".item")).toEqual(["Claude Opus", "Grok"]); await click(item("Grok")); - expect(sent).toEqual([{ type: "set_session_preset", session_id: "s1", name: "sub-grok" }]); + // TD-5235: a click moves this chat and the model new chats use. + expect(sent).toEqual([ + { type: "switch_preset", name: "sub-grok", session_id: "s1", make_default: true }, + ]); }); it("shows Teams only when a team exists", async () => { diff --git a/ui/src/lib/model-ready.svelte.ts b/ui/src/lib/model-ready.svelte.ts new file mode 100644 index 00000000..859a1af7 --- /dev/null +++ b/ui/src/lib/model-ready.svelte.ts @@ -0,0 +1,36 @@ +// TD-5240: whether each model on the picker can run right now, as the +// daemon last said. The picker asks when it opens (`check_presets`) and +// shows a model's problem on its row, before the click: no key stored, its +// local server not answering, the Grok CLI not found. A row the daemon has +// said nothing about shows nothing; the window does not guess. + +import { onEvent, sendToDaemon } from "./connection-status.svelte.js"; +import type { DaemonEventUnion, PresetReady } from "./protocol"; + +export const readiness = $state<{ rows: Record }>({ rows: {} }); + +/** Keep the daemon's latest answer. Any other event is ignored. */ +export function ingestReadiness(event: DaemonEventUnion): void { + if (event.type !== "preset_readiness") return; + readiness.rows = Object.fromEntries(event.rows.map((row) => [row.preset, row])); +} + +/** What is wrong with `preset`, in one sentence, or "" when nothing is known to be. */ +export function problemWith(preset: string): string { + const row = readiness.rows[preset]; + return row === undefined || row.status === "ready" ? "" : (row.detail ?? ""); +} + +export function resetReadiness(): void { + readiness.rows = {}; +} + +/** Listen for the daemon's answers while the picker is mounted. */ +export function watchReadiness(): () => void { + return onEvent(ingestReadiness); +} + +/** Ask the daemon again. The picker calls this each time it opens. */ +export function askReadiness(): void { + sendToDaemon({ type: "check_presets" }); +} diff --git a/ui/src/lib/model-ready.test.ts b/ui/src/lib/model-ready.test.ts new file mode 100644 index 00000000..79d61480 --- /dev/null +++ b/ui/src/lib/model-ready.test.ts @@ -0,0 +1,133 @@ +// @vitest-environment jsdom +// +// TD-5240: the picker asks the daemon, each time it opens, which models can +// run now, and shows a model's problem on its row before the click. + +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, tick, unmount } from "svelte"; +import type { ClientMessageUnion, DaemonEventUnion } from "./protocol"; + +const mocks = vi.hoisted(() => ({ + sent: [] as ClientMessageUnion[], + subs: new Set<(event: DaemonEventUnion) => void>(), +})); + +vi.mock("./connection-status.svelte.js", () => { + const onDaemonEvent = (handler: (event: DaemonEventUnion) => void) => { + mocks.subs.add(handler); + return () => mocks.subs.delete(handler); + }; + return { + onEvent: onDaemonEvent, + onDaemonEvent, + sendToDaemon: (msg: ClientMessageUnion) => (mocks.sent.push(msg), true), + }; +}); +vi.mock("./sessions.svelte.js", () => ({ newSession: () => true })); + +import ModelPicker from "./components/chat/ModelPicker.svelte"; +import { problemWith, readiness, resetReadiness } from "./model-ready.svelte.js"; +import { resetSession, session } from "./session-status.svelte.js"; +import { resetSettings, settings } from "./settings.svelte.js"; + +let app: ReturnType | null = null; + +function emit(event: Partial & { type: string }): void { + for (const sub of [...mocks.subs]) sub(event as DaemonEventUnion); + flushSync(); +} + +const chip = (): HTMLButtonElement => + document.querySelector('[aria-label="Model and effort"]')!; + +function row(label: string): HTMLButtonElement { + const found = [...document.querySelectorAll('[role="menuitemradio"]')].find( + (button) => button.querySelector(".label")?.textContent?.includes(label), + ); + if (found === undefined) throw new Error(`no row for ${label}`); + return found; +} + +beforeEach(async () => { + resetSession(); + resetSettings(); + resetReadiness(); + mocks.sent = []; + session.sessionId = "s1"; + session.preset = "api"; + session.engine = "native"; + settings.hasModel = true; + settings.activePreset = "api"; + settings.presets = ["api", "local"]; + settings.presetLabels = { api: "Hosted", local: "Laptop" }; + settings.presetDescriptions = { local: "Served on this computer" }; + app = mount(ModelPicker, { target: document.body }); + await tick(); +}); + +afterEach(async () => { + if (app !== null) await unmount(app); + app = null; + document.body.replaceChildren(); + mocks.subs.clear(); + resetReadiness(); + resetSession(); + resetSettings(); +}); + +describe("the picker's readiness check", () => { + it("asks each time the picker opens, and not when it closes", () => { + chip().click(); + flushSync(); + expect(mocks.sent).toEqual([{ type: "check_presets" }]); + chip().click(); + flushSync(); + expect(mocks.sent).toHaveLength(1); + chip().click(); + flushSync(); + expect(mocks.sent).toEqual([{ type: "check_presets" }, { type: "check_presets" }]); + }); + + it("shows a model's problem on its row in place of its description", () => { + chip().click(); + flushSync(); + expect(row("Laptop").querySelector(".sub")?.textContent).toBe("Served on this computer"); + emit({ + type: "preset_readiness", + rows: [ + { preset: "api", status: "ready", detail: "" }, + { + preset: "local", + status: "server_down", + detail: "Nothing is answering at 127.0.0.1:8000. Start the model server.", + }, + ], + }); + const sub = row("Laptop").querySelector(".sub"); + expect(sub?.textContent).toContain("Nothing is answering at 127.0.0.1:8000"); + expect(sub?.classList.contains("sub--problem")).toBe(true); + expect(row("Hosted").querySelector(".sub--problem")).toBeNull(); + // A warning, not a wall: the model can still be picked. + expect(row("Laptop").disabled).toBe(false); + }); + + it("clears the problem when the daemon next says the model is ready", () => { + chip().click(); + emit({ + type: "preset_readiness", + rows: [{ preset: "local", status: "no_key", detail: "No key is stored for House." }], + }); + expect(problemWith("local")).toBe("No key is stored for House."); + emit({ type: "preset_readiness", rows: [{ preset: "local", status: "ready" }] }); + expect(problemWith("local")).toBe(""); + expect(row("Laptop").querySelector(".sub")?.textContent).toBe("Served on this computer"); + }); + + it("says nothing about a model the daemon said nothing about", () => { + emit({ type: "preset_readiness", rows: [] }); + expect(readiness.rows).toEqual({}); + expect(problemWith("api")).toBe(""); + emit({ type: "setup_state" }); + expect(readiness.rows).toEqual({}); + }); +}); diff --git a/ui/src/lib/model-sections.test.ts b/ui/src/lib/model-sections.test.ts new file mode 100644 index 00000000..450f6ea0 --- /dev/null +++ b/ui/src/lib/model-sections.test.ts @@ -0,0 +1,206 @@ +// @vitest-environment jsdom +// +// TD-5236: the picker's main list in sections, and the quick-switch slots. +// The daemon says which section each model is in and which slot it has; a +// slot's shortcut does what a click on that model's row does. + +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, tick, unmount } from "svelte"; +import type { ClientMessageUnion } from "./protocol"; + +const mocks = vi.hoisted(() => ({ newChats: [] as (string | undefined)[] })); + +vi.mock("./sessions.svelte.js", () => ({ + newSession: (preset?: string) => { + mocks.newChats.push(preset); + return true; + }, +})); + +import ModelPicker from "./components/chat/ModelPicker.svelte"; +import type { ProtocolClient } from "./client"; +import { pickSlot } from "./model-pick"; +import { modelAtSlot, sectionModels } from "./model-sections"; +import { bindClient, resetSession, session } from "./session-status.svelte.js"; +import { resetSettings, settings } from "./settings.svelte.js"; +import { modelSlot, resolveShortcut, sessionSlot } from "./shortcuts"; + +const ORDER = ["Local", "API", "Grok"]; +let app: ReturnType | null = null; +let sent: ClientMessageUnion[] = []; + +beforeEach(() => { + resetSession(); + resetSettings(); + sent = []; + mocks.newChats = []; + bindClient({ + send: (msg: ClientMessageUnion) => (sent.push(msg), true), + } as unknown as ProtocolClient); + session.sessionId = "s1"; + session.preset = "local"; + session.engine = "native"; + settings.hasModel = true; + settings.activePreset = "local"; + settings.presets = ["api", "grok", "lab", "local"]; + settings.presetLabels = { api: "Hosted", grok: "Grok Build", lab: "Lab box", local: "Laptop" }; + settings.presetEngines = { grok: "grok" }; + settings.presetGroups = { api: "API", grok: "Grok", lab: "Trying out", local: "Local" }; + settings.pickerGroups = ORDER; + settings.presetSlots = { local: 1, api: 2, grok: 3 }; +}); + +afterEach(async () => { + if (app !== null) { + await unmount(app); + app = null; + } + document.body.replaceChildren(); + bindClient(null); + resetSession(); + resetSettings(); +}); + +describe("sectionModels", () => { + it("puts the built-in sections first, in the daemon's order, then the rest by name", () => { + const groups = { a: "Grok", b: "Zeta", c: "API", d: "Local", e: "Alpha", f: "API" }; + expect(sectionModels(["a", "b", "c", "d", "e", "f"], groups, ORDER)).toEqual([ + { title: "Local", names: ["d"] }, + { title: "API", names: ["c", "f"] }, + { title: "Grok", names: ["a"] }, + { title: "Alpha", names: ["e"] }, + { title: "Zeta", names: ["b"] }, + ]); + }); + + it("gives a single section no heading", () => { + expect(sectionModels(["a", "b"], { a: "API", b: "API" }, ORDER)).toEqual([ + { title: "", names: ["a", "b"] }, + ]); + }); + + it("lists everything once when the daemon sent no sections", () => { + expect(sectionModels(["a", "b"], {}, [])).toEqual([{ title: "", names: ["a", "b"] }]); + }); + + it("keeps a model the daemon gave no section, ahead of the titled ones", () => { + expect(sectionModels(["a", "b"], { b: "API" }, ORDER)).toEqual([ + { title: "", names: ["a"] }, + { title: "API", names: ["b"] }, + ]); + }); +}); + +describe("the model slot chord", () => { + const press = (over: Partial[0]>) => ({ + key: "1", + code: "Digit1", + metaKey: true, + ctrlKey: false, + altKey: true, + shiftKey: false, + ...over, + }); + const idle = { workspaceMenuOpen: false, paletteOpen: false, modalOpen: false, turnLive: false }; + + it("is ⌘⌥ or Ctrl+Alt and a digit, read off the physical key", () => { + expect(modelSlot(press({}))).toBe(1); + expect(modelSlot(press({ metaKey: false, ctrlKey: true, code: "Digit9", key: "9" }))).toBe(9); + // On a Mac, Option+1 types "¡": the key no longer says which digit. + expect(modelSlot(press({ key: "¡" }))).toBe(1); + expect(modelSlot(press({ code: undefined, key: "4" }))).toBe(4); + }); + + it("is nothing without Option, with Shift, or off the digit row", () => { + expect(modelSlot(press({ altKey: false }))).toBeNull(); + expect(modelSlot(press({ shiftKey: true }))).toBeNull(); + expect(modelSlot(press({ metaKey: false }))).toBeNull(); + expect(modelSlot(press({ code: "Digit0", key: "0" }))).toBeNull(); + expect(modelSlot(press({ code: "KeyA", key: "a" }))).toBeNull(); + }); + + it("does not take the session slots, and they do not take it", () => { + expect(resolveShortcut(press({}), idle)).toBe("pick-model"); + expect(sessionSlot(press({}))).toBeNull(); + expect(resolveShortcut(press({ altKey: false }), idle)).toBe("pick-session"); + }); + + it("does nothing over a modal", () => { + expect(resolveShortcut(press({}), { ...idle, modalOpen: true })).toBeNull(); + }); +}); + +describe("a slot's shortcut", () => { + it("names the model in that slot", () => { + expect(modelAtSlot(2, settings.presetSlots)).toBe("api"); + expect(modelAtSlot(7, settings.presetSlots)).toBeNull(); + }); + + it("does what a click on that model does", () => { + expect(pickSlot(2)).toBe(true); + expect(sent).toEqual([ + { type: "switch_preset", name: "api", session_id: "s1", make_default: true }, + ]); + }); + + it("opens a new chat for a model on the other engine", () => { + expect(pickSlot(3)).toBe(true); + expect(sent).toEqual([{ type: "switch_preset", name: "grok", make_default: true }]); + expect(mocks.newChats).toEqual(["grok"]); + }); + + it("does nothing for the model already in use, or an empty slot", () => { + expect(pickSlot(1)).toBe(true); + expect(pickSlot(7)).toBe(false); + expect(sent).toEqual([]); + }); +}); + +describe("the picker", () => { + async function openPicker(): Promise { + app = mount(ModelPicker, { target: document.body }); + await tick(); + document.querySelector('[aria-label="Model and effort"]')!.click(); + flushSync(); + return document.querySelector('[role="menu"]')!; + } + + it("lists the models under Local, API, Grok, then a model's own section", async () => { + const menu = await openPicker(); + // A label's own text, without the engine mark that sits inside it. + const own = (el: Element): string => (el.firstChild?.textContent ?? "").trim(); + const seen = [...menu.querySelectorAll(".section, [role='menuitemradio'] .label")].map( + (el) => (el.classList.contains("section") ? `# ${el.textContent}` : own(el)), + ); + expect(seen).toEqual([ + "# Local", + "Laptop", + "# API", + "Hosted", + "# Grok", + "Grok Build", + "# Trying out", + "Lab box", + ]); + }); + + it("shows each model's slot beside it", async () => { + const menu = await openPicker(); + const slots = [...menu.querySelectorAll("kbd.slot")].map((el) => [ + el.textContent, + el.title, + ]); + expect(slots).toEqual([ + ["1", "Quick switch: ⌘⌥1 (Ctrl+Alt+1)"], + ["2", "Quick switch: ⌘⌥2 (Ctrl+Alt+2)"], + ["3", "Quick switch: ⌘⌥3 (Ctrl+Alt+3)"], + ]); + }); + + it("shows no headings when every model is in one section", async () => { + settings.presetGroups = { api: "API", grok: "API", lab: "API", local: "API" }; + const menu = await openPicker(); + expect(menu.querySelectorAll(".section")).toHaveLength(0); + expect(menu.querySelectorAll('[role="menuitemradio"]')).toHaveLength(4); + }); +}); diff --git a/ui/src/lib/model-sections.ts b/ui/src/lib/model-sections.ts new file mode 100644 index 00000000..107aa953 --- /dev/null +++ b/ui/src/lib/model-sections.ts @@ -0,0 +1,44 @@ +// TD-5236: the picker's main list in sections: Local, API, Grok, then any +// section a model names itself. The daemon says which section each model is +// in (`setup_state.preset_groups`) and the order of the built-in ones; this +// only arranges what it was sent. + +export interface ModelSection { + /** The section's heading, or "" when the list has a single section and + * so needs none. */ + title: string; + names: string[]; +} + +/** `names` in sections. Built-in sections come first, in the daemon's + * order; others follow by name. A model the daemon gave no section (an + * older daemon) goes in an untitled one at the top. One section in all + * means no headings: a short list stays the plain list it was. */ +export function sectionModels( + names: readonly string[], + groups: Readonly>, + order: readonly string[], +): ModelSection[] { + const bySection = new Map(); + for (const name of names) { + const title = groups[name] ?? ""; + bySection.set(title, [...(bySection.get(title) ?? []), name]); + } + const rank = (title: string): number => { + if (title === "") return -1; + const at = order.indexOf(title); + return at === -1 ? order.length : at; + }; + const sections = [...bySection.entries()] + .sort(([a], [b]) => rank(a) - rank(b) || a.localeCompare(b)) + .map(([title, members]) => ({ title, names: members })); + return sections.length === 1 ? [{ title: "", names: sections[0].names }] : sections; +} + +/** The model in quick-switch slot `slot`, or null. */ +export function modelAtSlot( + slot: number, + slots: Readonly>, +): string | null { + return Object.keys(slots).find((name) => slots[name] === slot) ?? null; +} diff --git a/ui/src/lib/model-switch.test.ts b/ui/src/lib/model-switch.test.ts new file mode 100644 index 00000000..6d978d4e --- /dev/null +++ b/ui/src/lib/model-switch.test.ts @@ -0,0 +1,189 @@ +// @vitest-environment jsdom +// +// TD-5235: the picker's click. It moves this chat and makes the model the +// one new chats use; "Only this chat" moves just the chat; a model picked +// while a turn runs waits for the turn; a model on the other engine opens a +// new chat on it. + +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, tick, unmount } from "svelte"; +import type { ClientMessageUnion } from "./protocol"; + +const mocks = vi.hoisted(() => ({ newChats: [] as (string | undefined)[] })); + +vi.mock("./sessions.svelte.js", () => ({ + newSession: (preset?: string) => { + mocks.newChats.push(preset); + return true; + }, +})); + +import ModelPicker from "./components/chat/ModelPicker.svelte"; +import type { ProtocolClient } from "./client"; +import { bindClient, ingestEvent, resetSession, session } from "./session-status.svelte.js"; +import { resetSettings, settings } from "./settings.svelte.js"; + +let app: ReturnType | null = null; +let sent: ClientMessageUnion[] = []; + +function fakeClient(): ProtocolClient { + return { + send: (msg: ClientMessageUnion) => (sent.push(msg), true), + setSessionPreset: (sessionId: string, name: string) => + sent.push({ type: "set_session_preset", session_id: sessionId, name }), + } as unknown as ProtocolClient; +} + +beforeEach(() => { + resetSession(); + resetSettings(); + sent = []; + mocks.newChats = []; + bindClient(fakeClient()); + session.sessionId = "s1"; + session.preset = "local"; + session.engine = "native"; + settings.hasModel = true; + settings.activePreset = "local"; + settings.presets = ["api", "grok", "local"]; + settings.presetLabels = { api: "API", grok: "Grok Build", local: "Local" }; + settings.presetEngines = { grok: "grok" }; +}); + +afterEach(async () => { + if (app !== null) { + await unmount(app); + app = null; + } + document.body.replaceChildren(); + bindClient(null); + resetSession(); + resetSettings(); +}); + +async function openPicker(): Promise { + app = mount(ModelPicker, { target: document.body }); + await tick(); + chip().click(); + flushSync(); + return document.querySelector('[role="menu"]')!; +} + +function chip(): HTMLButtonElement { + return document.querySelector('[aria-label="Model and effort"]')!; +} + +function row(menu: HTMLElement, label: string): HTMLButtonElement { + const found = [...menu.querySelectorAll('[role="menuitemradio"]')].find( + (button) => button.querySelector(".label")?.textContent?.includes(label), + ); + if (found === undefined) throw new Error(`no row for ${label}`); + return found; +} + +function only(menu: HTMLElement, label: string): HTMLButtonElement | null { + return menu.querySelector(`[aria-label="Only this chat: ${label}"]`); +} + +describe("picking a model", () => { + it("moves this chat and makes it the model new chats use", async () => { + const menu = await openPicker(); + row(menu, "API").click(); + expect(sent).toEqual([ + { type: "switch_preset", name: "api", session_id: "s1", make_default: true }, + ]); + expect(mocks.newChats).toEqual([]); + }); + + it("Only this chat leaves the model new chats use alone", async () => { + const menu = await openPicker(); + only(menu, "API")!.click(); + expect(sent).toEqual([ + { type: "switch_preset", name: "api", session_id: "s1", make_default: false }, + ]); + }); + + it("offers Only this chat on every model but the current one and the other engine's", async () => { + const menu = await openPicker(); + expect(only(menu, "API")).not.toBeNull(); + expect(only(menu, "Local")).toBeNull(); + expect(only(menu, "Grok Build")).toBeNull(); + }); + + it("does nothing for the model the chat and new chats already use", async () => { + const menu = await openPicker(); + row(menu, "Local").click(); + expect(sent).toEqual([]); + }); + + it("makes the chat's own model the default when new chats use another", async () => { + settings.activePreset = "api"; + const menu = await openPicker(); + row(menu, "Local").click(); + expect(sent).toEqual([ + { type: "switch_preset", name: "local", session_id: "s1", make_default: true }, + ]); + }); +}); + +describe("picking a model on the other engine", () => { + it("opens a new chat on it and makes it the model new chats use", async () => { + const menu = await openPicker(); + row(menu, "Grok Build").click(); + expect(sent).toEqual([{ type: "switch_preset", name: "grok", make_default: true }]); + expect(mocks.newChats).toEqual(["grok"]); + }); +}); + +describe("picking a model while a turn runs", () => { + it("is allowed, and says it applies when the turn finishes", async () => { + session.turnActive = true; + const menu = await openPicker(); + const api = row(menu, "API"); + expect(api.disabled).toBe(false); + expect(api.title).toBe("Applies when this turn finishes"); + api.click(); + expect(sent).toEqual([ + { type: "switch_preset", name: "api", session_id: "s1", make_default: true }, + ]); + }); + + it("shows on the chip what is waiting, and clears when it lands", async () => { + app = mount(ModelPicker, { target: document.body }); + await tick(); + const state = { + type: "tier_state", + seq: 2, + session_id: "s1", + tier: "brain", + model_slugs: {}, + preset: "local", + pending_preset: "api", + } as const; + ingestEvent(state as never); + flushSync(); + expect(session.pendingPreset).toBe("api"); + expect(chip().textContent).toContain("Local Medium"); + expect(chip().textContent).toContain("→ API after this turn"); + ingestEvent({ ...state, seq: 3, preset: "api", pending_preset: null } as never); + flushSync(); + expect(session.pendingPreset).toBeNull(); + expect(chip().textContent).not.toContain("after this turn"); + expect(chip().textContent).toContain("API Medium"); + }); +}); + +describe("a chat on another computer", () => { + it("keeps the older verb, which its hub forwards, and its own rules", async () => { + session.sessionId = "s1:mch_0123456789ab"; + session.turnActive = true; + const menu = await openPicker(); + expect(row(menu, "API").disabled).toBe(true); + expect(row(menu, "API").title).toBe("Cannot change model while a turn is running"); + expect(only(menu, "API")).toBeNull(); + session.turnActive = false; + flushSync(); + row(menu, "API").click(); + expect(sent).toEqual([{ type: "set_session_preset", session_id: "s1:mch_0123456789ab", name: "api" }]); + }); +}); diff --git a/ui/src/lib/notifications.svelte.ts b/ui/src/lib/notifications.svelte.ts index ad0c008e..2f407155 100644 --- a/ui/src/lib/notifications.svelte.ts +++ b/ui/src/lib/notifications.svelte.ts @@ -28,6 +28,7 @@ import { type NoticeSpec, } from "./error-copy"; import { claimedInline } from "./inline-errors"; +import { phoneChangeNotice } from "./phone-permissions"; import type { DaemonEventUnion } from "./protocol"; import { redact } from "./redact"; import { session } from "./session-status.svelte.js"; @@ -155,6 +156,11 @@ export function notifyEvent(event: DaemonEventUnion): void { case "checkpoint_notice": notify(`checkpoint:${event.code}`, checkpointNoticeCopy(event.code, event.message)); break; + case "phone_change": + // TD-5279: a paired phone changed this computer. One banner per + // change, kept until dismissed: nobody may be watching when it lands. + notify(`phone:${event.device_name}:${event.summary}`, phoneChangeNotice(event)); + break; case "error": // A form already showed it, in words for that flow (TD-5015). if (claimedInline(event)) break; diff --git a/ui/src/lib/pair-device.svelte.ts b/ui/src/lib/pair-device.svelte.ts index 5633cf6b..0c8ac709 100644 --- a/ui/src/lib/pair-device.svelte.ts +++ b/ui/src/lib/pair-device.svelte.ts @@ -63,8 +63,13 @@ export function closePairDialog(): void { } /** Forget one phone. Acked with setup_state, which redraws the list. */ -export function removeRemoteDevice(deviceId: string): void { - sendToDaemon({ type: "remove_remote_device", device_id: deviceId }); +export function removeRemoteDevice(deviceId: string, removeAdded = false): void { + sendToDaemon({ + type: "remove_remote_device", + device_id: deviceId, + // TD-5279: also take away the MCP servers that phone added or changed. + ...(removeAdded ? { remove_added: true } : {}), + }); } /** Ask for the device list now. The daemon pushes presence after that, so diff --git a/ui/src/lib/phone-permissions.test.ts b/ui/src/lib/phone-permissions.test.ts new file mode 100644 index 00000000..41a8cd3c --- /dev/null +++ b/ui/src/lib/phone-permissions.test.ts @@ -0,0 +1,419 @@ +// @vitest-environment jsdom +// +// TD-5279: what a paired phone may change is switched on at the computer, +// after its disclosure. These pin the disclosure's words (so they cannot be +// softened unnoticed), what the block sends, and what a phone's own pages +// show while a permission is off. + +import { afterEach, describe, expect, it, vi } from "vitest"; +import { flushSync, mount, unmount, type Component } from "svelte"; +import type { ClientMessageUnion, DaemonEventUnion, RemoteDeviceSummary } from "./protocol"; + +const mocks = vi.hoisted(() => ({ sent: [] as ClientMessageUnion[] })); + +vi.mock("./connection-status.svelte.js", () => ({ + onEvent: () => () => undefined, + sendToDaemon: (msg: ClientMessageUnion) => (mocks.sent.push(msg), true), +})); + +import PhonePermissions from "./components/PhonePermissions.svelte"; +import PairedDevices from "./components/PairedDevices.svelte"; +import SettingsMcp from "./components/SettingsMcp.svelte"; +import PolicyRuleList from "./components/PolicyRuleList.svelte"; +import DoctorPane from "./components/DoctorPane.svelte"; +import { settings, type McpServerRow } from "./settings.svelte.js"; +import { doctor, buildDoctorReport } from "./doctor.svelte.js"; +import { banners, clearNotifications, notifyEvent, toasts } from "./notifications.svelte.js"; +import { + PHONE_PERMISSIONS, + isGranted, + originWords, + permissionCopy, + phoneChangeNotice, + stateWords, +} from "./phone-permissions"; + +let component: ReturnType | null = null; + +function show(what: Component, props: Record = {}): HTMLElement { + const target = document.createElement("div"); + document.body.appendChild(target); + component = mount(what, { target, props }); + flushSync(); + return target; +} + +function press(root: ParentNode, label: string): void { + const button = [...root.querySelectorAll("button")].find( + (b) => (b.getAttribute("aria-label") ?? b.textContent?.trim()) === label, + ); + if (!button) throw new Error(`no button "${label}"`); + button.click(); + flushSync(); +} + +afterEach(() => { + if (component) unmount(component); + component = null; + document.body.innerHTML = ""; + mocks.sent = []; + settings.phonePermissions = {}; + settings.remoteDevices = []; + settings.mcpServers = []; + doctor.open = false; + doctor.checks = []; + clearNotifications(); +}); + +const NOW = Date.parse("2026-11-20T18:00:00Z"); +const TITLE = "Let paired phones add and change MCP servers"; + +describe("the words", () => { + it("the MCP disclosure says what it lets a phone do, in full", () => { + expect(permissionCopy("mcp_servers")).toEqual({ + capability: "mcp_servers", + title: "Let paired phones add and change MCP servers", + lead: + "An MCP server is a program this computer starts and keeps running. With this on, " + + "anyone holding a paired phone can make this computer run any command as you, " + + "immediately and without an approval prompt here.", + points: [ + "Servers added from a phone stay after you remove the phone. Removing a phone does not undo what it changed.", + "These programs run outside the approval system and are not sandboxed.", + "Turning this off later stops new changes; it does not remove servers already added.", + ], + advice: "Leave this off unless you need to manage MCP servers while away from the computer.", + offOnPhone: + "Adding or changing MCP servers from a phone is turned off. You can turn it on under " + + "Settings, Phones and devices on the computer.", + }); + }); + + it("the approvals disclosure says what it lets a phone do, in full", () => { + expect(permissionCopy("approval_switches")).toEqual({ + capability: "approval_switches", + title: "Let paired phones turn approvals off", + lead: + "With this on, a paired phone can turn off approval prompts for every session on this " + + "computer. Commands and file changes then run without anyone confirming them, " + + "including ones outside the project folder.", + points: [ + "The setting stays on until someone turns it off, even after the phone is removed.", + "A phone can already approve one request at a time. This is the same power with nobody looking at each request.", + "A phone can always turn approval prompts back on; that needs no permission.", + ], + advice: + "Leave this off unless you run long jobs you trust and need to unblock them while away.", + offOnPhone: + "Turning approvals off from a phone is turned off. You can turn it on under " + + "Settings, Phones and devices on the computer.", + }); + }); + + it("lists exactly the permissions the daemon knows", () => { + expect(PHONE_PERMISSIONS.map((p) => p.capability)).toEqual(["mcp_servers", "approval_switches"]); + }); + + it("every permission has a full disclosure", () => { + for (const p of PHONE_PERMISSIONS) { + expect(p.title.length).toBeGreaterThan(10); + expect(p.lead).toContain("With this on"); + expect(p.points.length).toBeGreaterThanOrEqual(2); + expect(p.offOnPhone).toContain("on the computer"); + } + }); + + it("absent, off, unreadable and expired all read as off", () => { + expect(isGranted({}, "mcp_servers", NOW)).toBe(false); + expect(isGranted({ mcp_servers: { enabled: false } }, "mcp_servers", NOW)).toBe(false); + expect(isGranted({ other: { enabled: true } }, "mcp_servers", NOW)).toBe(false); + const timed = { mcp_servers: { enabled: true, expires_at: "2026-11-20T19:00:00Z" } }; + expect(isGranted(timed, "mcp_servers", NOW)).toBe(true); + expect(isGranted(timed, "mcp_servers", NOW + 3_600_000)).toBe(false); + const broken = { mcp_servers: { enabled: true, expires_at: "soon" } }; + expect(isGranted(broken, "mcp_servers", NOW)).toBe(false); + expect(isGranted({ mcp_servers: { enabled: true, expires_at: null } }, "mcp_servers", NOW)).toBe(true); + }); + + it("says how long it is on for", () => { + expect(stateWords({}, "mcp_servers", NOW)).toBe("Off"); + expect(stateWords({ mcp_servers: { enabled: true } }, "mcp_servers", NOW)).toBe( + "On until you turn it off", + ); + const timed = { mcp_servers: { enabled: true, expires_at: "2026-11-20T19:00:00Z" } }; + expect(stateWords(timed, "mcp_servers", NOW)).toMatch(/^On until \S/); + expect(stateWords(timed, "mcp_servers", NOW + 3_600_000)).toBe("Off"); + }); + + it("says which phone a server came from", () => { + expect(originWords("", "")).toBe(""); + expect(originWords(undefined, undefined)).toBe(""); + expect(originWords("Pixel 8", "not a date")).toBe("Added or changed from Pixel 8"); + expect(originWords("Pixel 8", "2026-11-20T18:02:11Z")).toMatch( + /^Added or changed from Pixel 8 on .*20/, + ); + }); +}); + +describe("Settings → Devices, what paired phones may change", () => { + it("lists the permission as off and sends nothing until the disclosure is answered", () => { + const root = show(PhonePermissions); + expect(root.textContent).toContain("What paired phones may change"); + expect(root.querySelector(".state")?.textContent?.trim()).toBe("Off"); + expect(root.querySelector('[role="dialog"]')).toBeNull(); + press(root, `Turn on: ${TITLE}`); + expect(mocks.sent).toEqual([]); + const dialog = root.querySelector('[role="dialog"]'); + expect(dialog).not.toBeNull(); + const text = dialog?.textContent ?? ""; + const copy = permissionCopy("mcp_servers"); + expect(text).toContain(copy?.lead); + for (const point of copy?.points ?? []) expect(text).toContain(point); + expect(text).toContain(copy?.advice); + const buttons = [...(dialog?.querySelectorAll("button") ?? [])].map((b) => b.textContent?.trim()); + expect(buttons).toEqual(["Keep off", "Turn on for 1 hour", "Turn on until I turn it off"]); + }); + + it("Keep off sends nothing and closes", () => { + const root = show(PhonePermissions); + press(root, `Turn on: ${TITLE}`); + press(root, "Keep off"); + expect(mocks.sent).toEqual([]); + expect(root.querySelector('[role="dialog"]')).toBeNull(); + }); + + it("Escape is Keep off", () => { + const root = show(PhonePermissions); + press(root, `Turn on: ${TITLE}`); + root + .querySelector('[role="dialog"]') + ?.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true })); + flushSync(); + expect(mocks.sent).toEqual([]); + expect(root.querySelector('[role="dialog"]')).toBeNull(); + }); + + it("turns it on for an hour, or until turned off", () => { + const root = show(PhonePermissions); + press(root, `Turn on: ${TITLE}`); + press(root, "Turn on for 1 hour"); + press(root, `Turn on: ${TITLE}`); + press(root, "Turn on until I turn it off"); + expect(mocks.sent).toEqual([ + { type: "set_phone_permission", capability: "mcp_servers", enabled: true, expires_in_seconds: 3600 }, + { type: "set_phone_permission", capability: "mcp_servers", enabled: true }, + ]); + }); + + it("shows it on and turns it off in one press", () => { + settings.phonePermissions = { mcp_servers: { enabled: true, expires_at: null } }; + const root = show(PhonePermissions); + expect(root.querySelector(".state--on")?.textContent?.trim()).toBe("On until you turn it off"); + press(root, `Turn off: ${TITLE}`); + expect(root.querySelector('[role="dialog"]')).toBeNull(); + expect(mocks.sent).toEqual([ + { type: "set_phone_permission", capability: "mcp_servers", enabled: false }, + ]); + }); +}); + +const SERVERS = [ + { + id: "probe", + transport: "stdio", + command: ["/usr/bin/touch", "/tmp/x"], + url: "", + enabled: true, + originDevice: "Pixel 8", + originAt: "2026-11-20T18:02:11Z", + excludedTools: [], + values: [], + }, + { id: "mine", transport: "stdio", command: ["true"], url: "", enabled: true, excludedTools: [], values: [] }, +] as unknown as McpServerRow[]; + +describe("Settings → MCP servers", () => { + it("off the computer with the permission off: the list, the reason, nothing to press", () => { + settings.mcpServers = SERVERS; + const root = show(SettingsMcp, { canManage: false }); + expect(root.querySelectorAll("button")).toHaveLength(0); + expect(root.querySelectorAll("input")).toHaveLength(0); + expect(root.textContent).toContain( + "Adding or changing MCP servers from a phone is turned off. You can turn it on under Settings, Phones and devices on the computer.", + ); + expect(root.textContent).toContain("/usr/bin/touch /tmp/x"); + }); + + it("marks a server that came from a phone, and only that one", () => { + settings.mcpServers = SERVERS; + const root = show(SettingsMcp, { canManage: true }); + const marks = [...root.querySelectorAll(".origin")].map((el) => el.textContent?.trim() ?? ""); + expect(marks).toHaveLength(1); + expect(marks[0]).toMatch(/^Added or changed from Pixel 8 on /); + }); +}); + +const PHONE: RemoteDeviceSummary = { + id: "dev_1", + name: "Pixel 8", + paired_at: "2026-11-01T10:00:00Z", + last_seen: null, + connected: false, + kind: "phone", + mcp_servers_added: ["probe", "walk"], +}; + +describe("removing a phone", () => { + it("lists what it added and leaves it unless asked", () => { + settings.remoteDevices = [PHONE]; + const root = show(PairedDevices, { canManage: true }); + mocks.sent = []; + press(root, "Remove Pixel 8"); + expect((root.textContent ?? "").replace(/\s+/g, " ")).toContain( + "This phone added or changed MCP servers that stay after it is removed: probe, walk.", + ); + const box = root.querySelector('input[type="checkbox"]'); + expect(box?.checked).toBe(false); + press(root, "Remove"); + expect(mocks.sent).toEqual([{ type: "remove_remote_device", device_id: "dev_1" }]); + }); + + it("takes them with it when the box is ticked", () => { + settings.remoteDevices = [PHONE]; + const root = show(PairedDevices, { canManage: true }); + mocks.sent = []; + press(root, "Remove Pixel 8"); + root.querySelector('input[type="checkbox"]')?.click(); + flushSync(); + press(root, "Remove"); + expect(mocks.sent).toEqual([ + { type: "remove_remote_device", device_id: "dev_1", remove_added: true }, + ]); + }); + + it("asks nothing extra about a phone that added nothing", () => { + settings.remoteDevices = [{ ...PHONE, mcp_servers_added: [] }]; + const root = show(PairedDevices, { canManage: true }); + mocks.sent = []; + press(root, "Remove Pixel 8"); + expect(root.querySelector('input[type="checkbox"]')).toBeNull(); + press(root, "Remove"); + expect(mocks.sent).toEqual([{ type: "remove_remote_device", device_id: "dev_1" }]); + }); + + it("the box does not carry over from one phone to the next", () => { + settings.remoteDevices = [PHONE, { ...PHONE, id: "dev_2", name: "Tablet", mcp_servers_added: ["x"] }]; + const root = show(PairedDevices, { canManage: true }); + mocks.sent = []; + press(root, "Remove Pixel 8"); + root.querySelector('input[type="checkbox"]')?.click(); + flushSync(); + press(root, "Keep"); + press(root, "Remove Tablet"); + expect(root.querySelector('input[type="checkbox"]')?.checked).toBe(false); + }); +}); + +describe("Settings → Policy, the skip-all switch", () => { + function skipSwitch(root: ParentNode): HTMLButtonElement { + const found = root.querySelector('button[role="switch"]'); + if (!found) throw new Error("no switch"); + return found; + } + + it("on a phone without the permission it cannot be turned on, and says where to allow it", () => { + settings.skipAllApprovals = false; + const root = show(PolicyRuleList, { sessionId: null, canSkipAll: false }); + const button = skipSwitch(root); + expect(button.disabled).toBe(true); + button.click(); + flushSync(); + expect(mocks.sent).toEqual([]); + expect(root.textContent).toContain( + "Turning approvals off from a phone is turned off. You can turn it on under Settings, Phones and devices on the computer.", + ); + }); + + it("a phone can always turn approvals back on", () => { + settings.skipAllApprovals = true; + const root = show(PolicyRuleList, { sessionId: null, canSkipAll: false }); + const button = skipSwitch(root); + expect(button.disabled).toBe(false); + expect(root.textContent).not.toContain("from a phone is turned off"); + button.click(); + flushSync(); + expect(mocks.sent).toEqual([{ type: "set_skip_all_approvals", enabled: false }]); + settings.skipAllApprovals = false; + }); + + it("on the computer, and on a phone that was allowed, it works as before", () => { + settings.skipAllApprovals = false; + const root = show(PolicyRuleList, { sessionId: null }); + const button = skipSwitch(root); + expect(button.disabled).toBe(false); + expect(root.textContent).not.toContain("from a phone is turned off"); + button.click(); + flushSync(); + expect(mocks.sent).toEqual([{ type: "set_skip_all_approvals", enabled: true }]); + }); +}); + +describe("when a phone uses a permission", () => { + const event = { + type: "phone_change", + seq: 1, + device_name: "Pixel 8", + capability: "mcp_servers", + summary: "added MCP server 'probe': /usr/bin/touch /tmp/x", + } as const; + + it("the computer's window keeps a banner naming the phone and the change", () => { + expect(phoneChangeNotice(event)).toEqual({ + severity: "banner", + title: "Pixel 8 changed this computer", + body: "Pixel 8 added MCP server 'probe': /usr/bin/touch /tmp/x", + }); + notifyEvent(event as DaemonEventUnion); + expect(toasts).toHaveLength(0); + expect(banners.map((b) => b.body)).toEqual([ + "Pixel 8 added MCP server 'probe': /usr/bin/touch /tmp/x", + ]); + // A second, different change is a second banner; the same one is not. + notifyEvent({ ...event, summary: "removed MCP server 'probe'." } as DaemonEventUnion); + notifyEvent(event as DaemonEventUnion); + expect(banners).toHaveLength(2); + }); + + it("a refusal on the phone says where the switch is", () => { + notifyEvent({ + type: "error", + code: "phone_permission_off", + message: "Turn on 'MCP servers' under Settings, Phones and devices on the computer to do this from a phone.", + } as DaemonEventUnion); + expect(toasts.map((t) => [t.title, t.body])).toEqual([ + [ + "Turned off for phones", + "Turn on 'MCP servers' under Settings, Phones and devices on the computer to do this from a phone.", + ], + ]); + }); +}); + +describe("Doctor", () => { + const row = { + name: "phone_permissions", + status: "warn", + detail: "paired phones may change: MCP servers ON (until turned off)", + } as const; + + it("shows a phone permission left on as a warning, in the pane and the copied report", () => { + doctor.open = true; + doctor.checks = [row]; + const root = show(DoctorPane); + expect(root.querySelector(".mark--warn")).not.toBeNull(); + expect(root.textContent).toContain("MCP servers ON (until turned off)"); + expect(buildDoctorReport([row])).toContain( + "! phone_permissions — paired phones may change: MCP servers ON (until turned off)", + ); + }); +}); diff --git a/ui/src/lib/phone-permissions.ts b/ui/src/lib/phone-permissions.ts new file mode 100644 index 00000000..4a111d20 --- /dev/null +++ b/ui/src/lib/phone-permissions.ts @@ -0,0 +1,115 @@ +// What a paired phone may change (TD-5279): the words, and the small +// decisions around them. No store and no DOM, so the wording is testable +// on its own: it is the disclosure a person reads before switching a +// permission on, and a softer sentence here would be a weaker warning. + +import type { NoticeSpec } from "./error-copy"; +import type { PhoneChange, PhonePermissionState } from "./protocol"; + +export interface PhonePermissionCopy { + capability: string; + /** The row's name on the Devices page, and the dialog's heading. */ + title: string; + /** What it is, and the plainest statement of what switching it on allows. */ + lead: string; + /** What stays true afterwards. Each is something a person would not guess. */ + points: string[]; + /** When it is reasonable to turn on. */ + advice: string; + /** What a phone's own page says while this is off. */ + offOnPhone: string; +} + +export const ONE_HOUR_SECONDS = 3600; + +export const PHONE_PERMISSIONS: PhonePermissionCopy[] = [ + { + capability: "mcp_servers", + title: "Let paired phones add and change MCP servers", + lead: + "An MCP server is a program this computer starts and keeps running. With this on, " + + "anyone holding a paired phone can make this computer run any command as you, " + + "immediately and without an approval prompt here.", + points: [ + "Servers added from a phone stay after you remove the phone. Removing a phone does not undo what it changed.", + "These programs run outside the approval system and are not sandboxed.", + "Turning this off later stops new changes; it does not remove servers already added.", + ], + advice: "Leave this off unless you need to manage MCP servers while away from the computer.", + offOnPhone: + "Adding or changing MCP servers from a phone is turned off. You can turn it on under " + + "Settings, Phones and devices on the computer.", + }, + { + capability: "approval_switches", + title: "Let paired phones turn approvals off", + lead: + "With this on, a paired phone can turn off approval prompts for every session on this " + + "computer. Commands and file changes then run without anyone confirming them, " + + "including ones outside the project folder.", + points: [ + "The setting stays on until someone turns it off, even after the phone is removed.", + "A phone can already approve one request at a time. This is the same power with nobody looking at each request.", + "A phone can always turn approval prompts back on; that needs no permission.", + ], + advice: + "Leave this off unless you run long jobs you trust and need to unblock them while away.", + offOnPhone: + "Turning approvals off from a phone is turned off. You can turn it on under " + + "Settings, Phones and devices on the computer.", + }, +]; + +export function permissionCopy(capability: string): PhonePermissionCopy | undefined { + return PHONE_PERMISSIONS.find((p) => p.capability === capability); +} + +/** May a phone use it right now? Unknown, absent and expired all read as no. */ +export function isGranted( + permissions: Record, + capability: string, + now: number, +): boolean { + const state = permissions[capability]; + if (state === undefined || state.enabled !== true) return false; + if (state.expires_at === null || state.expires_at === undefined) return true; + const until = Date.parse(state.expires_at); + return Number.isFinite(until) && now < until; +} + +/** "Off", "On until you turn it off", or "On until 19:00". */ +export function stateWords( + permissions: Record, + capability: string, + now: number, +): string { + if (!isGranted(permissions, capability, now)) return "Off"; + const expires = permissions[capability]?.expires_at; + if (expires === null || expires === undefined) return "On until you turn it off"; + const until = new Date(expires); + const sameDay = new Date(now).toDateString() === until.toDateString(); + const time = until.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }); + return sameDay ? `On until ${time}` : `On until ${until.toLocaleDateString()} ${time}`; +} + +/** "Added or changed from Pixel 8 on 20 Nov", or "" for the computer's own. */ +export function originWords(device: string | undefined, at: string | undefined): string { + if (!device) return ""; + const when = at ? new Date(at) : null; + if (when === null || Number.isNaN(when.getTime())) return `Added or changed from ${device}`; + const day = when.toLocaleDateString([], { day: "numeric", month: "short" }); + return `Added or changed from ${device} on ${day}`; +} + +/** + * What the computer's window shows when a phone used a permission. A banner, + * not a toast: the person may be away from the screen when it happens, and + * it stays until they dismiss it. + */ +export function phoneChangeNotice(event: PhoneChange): NoticeSpec { + return { + severity: "banner", + title: `${event.device_name} changed this computer`, + body: `${event.device_name} ${event.summary}`, + }; +} diff --git a/ui/src/lib/preview-pane-sandbox.test.ts b/ui/src/lib/preview-pane-sandbox.test.ts new file mode 100644 index 00000000..cbf0e5f2 --- /dev/null +++ b/ui/src/lib/preview-pane-sandbox.test.ts @@ -0,0 +1,37 @@ +// @vitest-environment jsdom +// +// TD-5266: the Grok preview's frame really carries its sandbox once the +// component is on the page. A source check is not enough: Svelte can drop +// an empty attribute when it is written as an expression. + +import { afterEach, describe, expect, it } from "vitest"; +import { flushSync, mount, unmount } from "svelte"; + +import PreviewPane from "./components/PreviewPane.svelte"; +import { grok } from "./grok.svelte.js"; + +let app: ReturnType | null = null; + +afterEach(async () => { + if (app !== null) await unmount(app); + app = null; + document.body.replaceChildren(); + grok.preview = null; +}); + +describe("the Grok preview frame", () => { + it.each(["url", "html", "pdf"] as const)("is sandboxed with nothing allowed for a %s", (kind) => { + grok.preview = { + kind, + title: "Page", + url: "http://127.0.0.1:5173/", + path: null, + } as unknown as typeof grok.preview; + app = mount(PreviewPane, { target: document.body }); + flushSync(); + const frame = document.querySelector("iframe"); + expect(frame).not.toBeNull(); + expect(frame!.hasAttribute("sandbox")).toBe(true); + expect(frame!.getAttribute("sandbox")).toBe(""); + }); +}); diff --git a/ui/src/lib/protocol-fixtures.json b/ui/src/lib/protocol-fixtures.json index 1a5619bf..751a8634 100644 --- a/ui/src/lib/protocol-fixtures.json +++ b/ui/src/lib/protocol-fixtures.json @@ -638,6 +638,38 @@ "type": "set_preset", "name": "tst-default" }, + "duplicate_preset": { + "type": "duplicate_preset", + "source": "tst-default", + "label": "My copy" + }, + "set_preset_label": { + "type": "set_preset_label", + "name": "tst-default", + "label": "House model", + "description": "General coding and chat" + }, + "delete_preset": { + "type": "delete_preset", + "name": "budget" + }, + "check_presets": { + "type": "check_presets" + }, + "get_preset_roles": { + "type": "get_preset_roles", + "name": "budget" + }, + "switch_preset": { + "type": "switch_preset", + "name": "budget", + "session_id": "sess-1", + "make_default": true + }, + "add_grok_preset": { + "type": "add_grok_preset", + "label": "Grok" + }, "set_engine": { "type": "set_engine", "kind": "grok" @@ -780,7 +812,30 @@ }, "remove_remote_device": { "type": "remove_remote_device", - "device_id": "dev_0123456789ab" + "device_id": "dev_0123456789ab", + "remove_added": false + }, + "preview_mcp_import": { + "type": "preview_mcp_import", + "text": "{\"mcpServers\": {\"files\": {\"command\": \"uvx\"}}}" + }, + "import_mcp_servers": { + "type": "import_mcp_servers", + "text": "{\"mcpServers\": {\"files\": {\"command\": \"uvx\"}}}", + "servers": [ + { + "source_name": "files", + "id": "files", + "allow_remote": false, + "inputs": {} + } + ] + }, + "set_phone_permission": { + "type": "set_phone_permission", + "capability": "mcp_servers", + "enabled": true, + "expires_in_seconds": 3600 }, "add_machine": { "type": "add_machine", @@ -1311,7 +1366,8 @@ "decision_class": "C", "summary": "Write to test.txt", "reason": "decision class C requires approval", - "proposed_always_allow": null + "proposed_always_allow": null, + "worker": null }, "approval_request_always_allow": { "type": "approval_request", @@ -1330,7 +1386,8 @@ "args": "npm test", "effect": "auto", "exact": "npm test" - } + }, + "worker": null }, "decision_logged": { "type": "decision_logged", @@ -1470,6 +1527,7 @@ "validator": "test-validator-slug" }, "preset": "test", + "pending_preset": null, "hosts": { "brain": "mock.local", "worker": "mock.local", @@ -1497,6 +1555,7 @@ "validator": "test-validator-slug" }, "preset": "test", + "pending_preset": null, "hosts": { "brain": "mock.local", "worker": "mock.local", @@ -1524,6 +1583,7 @@ "validator": "test-validator-slug" }, "preset": "test", + "pending_preset": null, "hosts": { "brain": "mock.local", "worker": "mock.local", @@ -1551,6 +1611,7 @@ "validator": "test-validator-slug" }, "preset": "test", + "pending_preset": null, "hosts": { "brain": "mock.local", "worker": "mock.local", @@ -2035,6 +2096,7 @@ "validator": "test-validator-slug" }, "preset": "test", + "pending_preset": null, "hosts": { "brain": "backup.local", "worker": "mock.local", @@ -2182,6 +2244,10 @@ "team_presets": [ "budget" ], + "preset_engines": {}, + "preset_groups": {}, + "picker_groups": [], + "preset_slots": {}, "has_model": true, "skip_all_approvals": false, "load_global_memory": false, @@ -2256,9 +2322,12 @@ "stored": false } ], - "headers": [] + "headers": [], + "origin_device": "", + "origin_at": "" } ], + "phone_permissions": {}, "speech_enabled": false, "speech_ready": false, "email": { @@ -2317,9 +2386,48 @@ "paired_at": "2026-10-14T10:41:00Z", "last_seen": null, "connected": false, - "kind": "phone" + "kind": "phone", + "mcp_servers_added": [] } }, + "mcp_import_preview": { + "type": "mcp_import_preview", + "seq": 1, + "servers": [ + { + "source_name": "GitHub", + "id": "github", + "transport": "stdio", + "command": [ + "npx", + "-y", + "server-github" + ], + "url": "", + "off_box": false, + "exists": false, + "values": [ + { + "name": "GITHUB_TOKEN", + "target": "env", + "disposition": "keychain", + "reason": "" + } + ], + "not_imported": [], + "notes": [], + "refused": "" + } + ], + "error": "" + }, + "phone_change": { + "type": "phone_change", + "seq": 1, + "device_name": "Pixel", + "capability": "mcp_servers", + "summary": "added MCP server 'files': npx files-server" + }, "machine_list": { "type": "machine_list", "seq": 1, @@ -2418,6 +2526,40 @@ "reply": "I am working.", "detail": "" }, + "preset_roles": { + "type": "preset_roles", + "seq": 1, + "preset": "budget", + "tier_slugs": { + "brain": "vendor/model-a", + "worker": null + }, + "tier_credentials": { + "brain": "openrouter", + "worker": null + }, + "tier_loopback": { + "brain": false, + "worker": true + }, + "tier_cards": {} + }, + "preset_readiness": { + "type": "preset_readiness", + "seq": 1, + "rows": [ + { + "preset": "tst-default", + "status": "ready", + "detail": "" + }, + { + "preset": "local", + "status": "server_down", + "detail": "Nothing is answering at 127.0.0.1:11434. Start the model server." + } + ] + }, "config_editor": { "type": "config_editor", "seq": 1, diff --git a/ui/src/lib/protocol.ts b/ui/src/lib/protocol.ts index 9cf6ce66..a7b5dd7e 100644 --- a/ui/src/lib/protocol.ts +++ b/ui/src/lib/protocol.ts @@ -322,6 +322,38 @@ export interface CancelPairing extends ClientMessage { export interface RemoveRemoteDevice extends ClientMessage { type: "remove_remote_device"; device_id: string; + /** TD-5279: also remove the MCP servers this device added or changed. Computer only. */ + remove_added?: boolean; +} + +/** Read MCP servers out of pasted JSON (TD-5253). Saves nothing. */ +export interface PreviewMcpImport extends ClientMessage { + type: "preview_mcp_import"; + text: string; +} + +export interface McpImportChoice { + source_name: string; + id: string; + allow_remote?: boolean; + /** Values the preview asked for, by name. They go to the keychain. */ + inputs?: Record; +} + +/** Save the chosen servers from the same pasted JSON (TD-5253). */ +export interface ImportMcpServers extends ClientMessage { + type: "import_mcp_servers"; + text: string; + servers: McpImportChoice[]; +} + +/** Switch what a paired phone may change (TD-5279). Loopback only; a phone + * can never send it. No `expires_in_seconds` means until switched off. */ +export interface SetPhonePermission extends ClientMessage { + type: "set_phone_permission"; + capability: string; + enabled: boolean; + expires_in_seconds?: number | null; } /** Pair another computer (TD-5015). Loopback only; runs as a task, answered @@ -655,6 +687,9 @@ export interface SearchSessions extends ClientMessage { export interface NewSession extends ClientMessage { type: "new_session"; session_id: string; + /** TD-5235: the catalog entry the new chat opens on, with its engine. + * Omitted: the model new chats use. */ + preset?: string | null; } /** File a session away, or restore it (TD-1715). Filing, never killing: an @@ -890,6 +925,62 @@ export interface SetPreset extends ClientMessage { name: string; } +// TD-5237: copy a catalog entry under a new label. The daemon derives the +// new entry's key and writes every role out in full. Acked with setup_state. +export interface DuplicatePreset extends ClientMessage { + type: "duplicate_preset"; + source: string; + label: string; +} + +// TD-5237: a new label for an entry. Its key never changes. `description` +// omitted keeps the stored one; empty removes it. Acked with setup_state. +export interface SetPresetLabel extends ClientMessage { + type: "set_preset_label"; + name: string; + label: string; + description?: string | null; +} + +// TD-5235: one switch. `session_id` switches that chat (now, or when its +// running turn completes); `make_default` also makes it the model new chats +// use. Checked whole before anything changes. Acked with setup_state when +// the default changed, else session_list. +export interface SwitchPreset extends ClientMessage { + type: "switch_preset"; + name: string; + session_id?: string | null; + make_default?: boolean; +} + +// TD-5241: ask for one model's roles, to edit it in Settings → Model. +// Read-only. Answered with preset_roles. +export interface GetPresetRoles extends ClientMessage { + type: "get_preset_roles"; + name: string; +} + +// TD-5240: ask whether each model on the picker can run right now. Sent +// when the picker opens. Answered with preset_readiness. +export interface CheckPresets extends ClientMessage { + type: "check_presets"; +} + +// TD-5258: put the Grok CLI on the model list as an entry that is +// `engine: grok` and names no model or key. Refused when the CLI is not +// found or an entry already names the Grok engine. Acked with setup_state. +export interface AddGrokPreset extends ClientMessage { + type: "add_grok_preset"; + label?: string; +} + +// TD-5237: remove a catalog entry. This computer's window only. A chat +// already on it keeps its model until switched. Acked with setup_state. +export interface DeletePreset extends ClientMessage { + type: "delete_preset"; + name: string; +} + /** Pick native TST loop or Grok Build ACP for new sessions. */ export interface SetEngine extends ClientMessage { type: "set_engine"; @@ -1366,6 +1457,13 @@ export type ClientMessageUnion = | AddModel | TestModel | SetPreset + | DuplicatePreset + | SwitchPreset + | CheckPresets + | GetPresetRoles + | AddGrokPreset + | SetPresetLabel + | DeletePreset | SetEngine | SetGrokMode | RunGrokCommand @@ -1400,6 +1498,9 @@ export type ClientMessageUnion = | StartPairing | CancelPairing | RemoveRemoteDevice + | SetPhonePermission + | PreviewMcpImport + | ImportMcpServers | AddMachine | RemoveMachine | ListMachines @@ -1716,6 +1817,22 @@ export interface PolicyRuleSummary { exact?: string | null; } +/** TD-5268: which child is asking, on a child's approval card. */ +export interface ApprovalWorker { + id: string; + parent_call_id: string; + via: "delegate" | "workflow"; + kind: "worker" | "research" | "check"; + agent?: string | null; + /** A workflow task's number, from 1. */ + index?: number | null; + label?: string; + task?: string; + tier?: string; + /** Set when the child works in a worktree of its own. */ + worktree_branch?: string | null; +} + export interface ApprovalRequest extends DaemonEvent { type: "approval_request"; session_id: string; @@ -1726,6 +1843,8 @@ export interface ApprovalRequest extends DaemonEvent { summary: string; reason: string; proposed_always_allow?: PolicyRuleSummary | null; + /** TD-5268: the child that is asking; absent for the session's own agent. */ + worker?: ApprovalWorker | null; } export interface QuestionOption { @@ -1839,6 +1958,8 @@ export interface TierState extends DaemonEvent { model_slugs: Record; /** Preset this session opened with. Omitted by older daemons. */ preset?: string; + /** TD-5235: a switch waiting for the running turn to complete. */ + pending_preset?: string | null; /** Tier → hostname:port the client will call. Omitted by older daemons. */ hosts?: Record; /** Plan mode (brain lock). Omitted by older daemons — treat as off. */ @@ -2238,6 +2359,13 @@ export interface SetupState extends DaemonEvent { // (false on a fresh install: the UI starts "Add a model"). preset_descriptions?: Record; team_presets?: string[]; + /** TD-5239: the engine of each preset that names one. */ + preset_engines?: Record; + /** TD-5236: every preset's picker section, the order the built-in + * sections come in, and each preset's quick-switch slot (1 to 9). */ + preset_groups?: Record; + picker_groups?: string[]; + preset_slots?: Record; has_model?: boolean; // TD-804: skip-all approvals. Additive, default off. skip_all_approvals?: boolean; @@ -2287,6 +2415,8 @@ export interface SetupState extends DaemonEvent { judgments_typesafe_key_stored?: boolean; // TD-4403: listed MCP servers. Additive, default empty. Never a secret. mcp_servers?: McpServerSummary[]; + // TD-5279: what a paired phone may change, by capability. Additive. + phone_permissions?: Record; // TD-4701: hold-to-talk. Additive, default off. The URL never arrives. speech_enabled?: boolean; speech_ready?: boolean; @@ -2374,6 +2504,14 @@ export interface RemoteDeviceSummary { connected: boolean; /** TD-5015: a phone or another computer's hub. Missing = phone. */ kind?: "phone" | "machine"; + /** TD-5279: MCP servers this device added or changed that are still listed. */ + mcp_servers_added?: string[]; +} + +/** One phone permission (TD-5279). `enabled` is "may a phone use it right now". */ +export interface PhonePermissionState { + enabled: boolean; + expires_at?: string | null; } /** A paired computer's link, as the hub reports it (TD-5015). */ @@ -2436,6 +2574,44 @@ export interface PairingCompleted extends DaemonEvent { device: RemoteDeviceSummary; } +/** One env var or header in an import preview (TD-5253). Never its value. */ +export interface McpImportValue { + name: string; + target: "env" | "header"; + disposition: "keychain" | "literal" | "needs_input"; + reason?: string; +} + +export interface McpImportServer { + source_name: string; + id: string; + transport: "stdio" | "http"; + command: string[]; + url: string; + off_box: boolean; + exists: boolean; + values: McpImportValue[]; + not_imported: string[]; + notes: string[]; + /** Why this server cannot be imported; empty when it can. */ + refused: string; +} + +/** Reply to preview_mcp_import (TD-5253). `error` set: the text was not readable. */ +export interface McpImportPreview extends DaemonEvent { + type: "mcp_import_preview"; + servers: McpImportServer[]; + error: string; +} + +/** A paired phone used a phone permission (TD-5279). Loopback windows only. */ +export interface PhoneChange extends DaemonEvent { + type: "phone_change"; + device_name: string; + capability: string; + summary: string; +} + export interface McpServerSummary { id: string; transport: "stdio" | "http"; @@ -2447,6 +2623,9 @@ export interface McpServerSummary { /** TD-5110: env vars (stdio) and headers (http), names only. Additive. */ env?: McpValueSummary[]; headers?: McpValueSummary[]; + /** TD-5279: the phone this server was last added or changed from, and when. */ + origin_device?: string; + origin_at?: string; } /** One env var or header of an MCP server (TD-5110). Never its value. @@ -2526,6 +2705,31 @@ export interface LocalServers extends DaemonEvent { } // TD-5172: reply to test_model. +/** TD-5241: reply to get_preset_roles. The four maps setup_state carries + * for the active model, for model `preset`. */ +export interface PresetRoles extends DaemonEvent { + type: "preset_roles"; + preset: string; + tier_slugs?: Record; + tier_credentials?: Record; + tier_loopback?: Record; + tier_cards?: Record; +} + +/** TD-5240: whether one model can run right now. `detail` is one sentence + * for the picker's row, empty when ready. */ +export interface PresetReady { + preset: string; + status: "ready" | "no_key" | "server_down" | "no_grok"; + detail?: string; +} + +/** TD-5240: reply to check_presets, one row per model. */ +export interface PresetReadiness extends DaemonEvent { + type: "preset_readiness"; + rows: PresetReady[]; +} + export interface ModelTested extends DaemonEvent { type: "model_tested"; seq: number; @@ -2593,10 +2797,11 @@ export interface ApiKeyValidated extends DaemonEvent { // TD-1104 doctor: one row per check. `skip` means not applicable (no key // to validate, no workspace open) — not a failure. `fix` is the concrete -// remedy, present exactly when status is "fail". +// remedy, present exactly when status is "fail". `warn` (TD-5279) is a +// setting that works as chosen and is worth knowing about. export interface DiagnosticCheck { name: string; - status: "ok" | "fail" | "skip"; + status: "ok" | "fail" | "skip" | "warn"; detail: string; fix?: string | null; } @@ -3360,11 +3565,15 @@ export type DaemonEventUnion = | SetupState | PairingCode | PairingCompleted + | PhoneChange + | McpImportPreview | MachineList | ApiKeyValidated | EndpointModels | LocalServers | ModelTested + | PresetReadiness + | PresetRoles | ConfigEditor | ConfigValueRejected | DiagnosticsReport diff --git a/ui/src/lib/session-status.svelte.ts b/ui/src/lib/session-status.svelte.ts index ba95cdc3..616e4b05 100644 --- a/ui/src/lib/session-status.svelte.ts +++ b/ui/src/lib/session-status.svelte.ts @@ -75,6 +75,9 @@ export const session = $state({ engine: null as "native" | "grok" | null, /** Preset this session opened with (TD-1720). Empty until tier_state. */ preset: "" as string, + /** TD-5235: a model picked while a turn runs; it applies when the turn + * completes. Null when nothing is waiting. */ + pendingPreset: null as string | null, /** Tier → hostname:port the daemon will call (TD-1720). */ hosts: {} as Record, /** Tiers on a backup endpoint this turn → its 1-based position (TD-5120). @@ -133,6 +136,7 @@ export function resetSession(): void { session.fastNote = ""; session.ultracode = false; session.ultracodeAvailable = false; + session.pendingPreset = null; session.vision = false; session.turnActive = false; session.context = null; @@ -191,6 +195,7 @@ export function ingestEvent(event: DaemonEventUnion): void { session.tierOverride = event.override ?? null; session.modelSlugs = event.model_slugs; session.preset = event.preset ?? ""; + session.pendingPreset = event.pending_preset ?? null; session.hosts = event.hosts ?? {}; session.fallbacks = event.fallbacks ?? {}; session.planMode = event.plan ?? false; @@ -293,6 +298,7 @@ export function focusSession( session.fastNote = ""; session.ultracode = false; session.ultracodeAvailable = false; + session.pendingPreset = null; session.vision = false; session.turnActive = false; session.context = null; @@ -363,6 +369,29 @@ export function setSessionPreset(name: string): void { client?.setSessionPreset(session.sessionId, name); } +/** TD-5235: the picker's click. This chat moves to `name`, and unless + * `onlyThisChat`, new chats use it too. A chat on another computer keeps + * the older verb, which its hub forwards; that computer's default is its own. */ +export function switchModel(name: string, opts: { onlyThisChat: boolean }): void { + const sessionId = session.sessionId; + if (sessionId === null) return; + if (isBridged(sessionId)) { + client?.setSessionPreset(sessionId, name); + return; + } + client?.send({ + type: "switch_preset", + name, + session_id: sessionId, + make_default: !opts.onlyThisChat, + }); +} + +/** TD-5235: make `name` the model new chats use, leaving every open chat alone. */ +export function useForNewChats(name: string): void { + client?.send({ type: "switch_preset", name, make_default: true }); +} + /** Display name for the workspace row: basename of the path. */ export function workspaceName(path: string): string { const parts = path.split(/[\\/]/).filter((p) => p.length > 0); diff --git a/ui/src/lib/sessions.svelte.ts b/ui/src/lib/sessions.svelte.ts index 2cd57f3b..15c797a8 100644 --- a/ui/src/lib/sessions.svelte.ts +++ b/ui/src/lib/sessions.svelte.ts @@ -425,7 +425,7 @@ export function boundTurn(): BoundTurn { * anchor works. With no rows at all (fresh install) there is nothing to * anchor on, so the wired directory picker runs instead of silently * no-op'ing — the chosen path goes through new_session_in_workspace. */ -export function newSession(): boolean { +export function newSession(preset?: string): boolean { if (pendingNewAnchor() !== null) return false; // A bound session on another computer grows there; unbound, the fallback // anchor is this computer's own newest row (TD-5015). @@ -443,7 +443,12 @@ export function newSession(): boolean { // A computer that isn't connected can't answer; the chat pane already // says it isn't reachable. if (!sessionMachineUp(anchor)) return false; - if (!sendToDaemon({ type: "new_session", session_id: anchor })) return false; + // TD-5235: on a named model, with its engine, when the picker asks for one. + const message = + preset === undefined + ? ({ type: "new_session", session_id: anchor } as const) + : ({ type: "new_session", session_id: anchor, preset } as const); + if (!sendToDaemon(message)) return false; setPendingNew(anchor); return true; } diff --git a/ui/src/lib/settings.svelte.ts b/ui/src/lib/settings.svelte.ts index 37173494..b7b5b9ca 100644 --- a/ui/src/lib/settings.svelte.ts +++ b/ui/src/lib/settings.svelte.ts @@ -21,6 +21,7 @@ import type { GooglePhase, MicrosoftPhase, PolicyRuleSummary, + PhonePermissionState, RemoteDeviceSummary, TierCard, } from "./protocol"; @@ -65,6 +66,9 @@ export type McpServerRow = { command: string[]; url: string; enabled: boolean; + /** The phone it was last added or changed from, and when (TD-5279). */ + originDevice?: string; + originAt?: string; /** Tools left out at the last load (TD-5104). */ excludedTools?: { name: string; reason: string }[]; /** Env vars (stdio) or headers (http) by name (TD-5110). Never a value. */ @@ -96,6 +100,13 @@ export const settings = $state({ presetDescriptions: {} as Record, /** Presets the picker lists under "Teams" (config `picker: team`). */ teamPresets: [] as string[], + // TD-5239: the engine of each model that names one. + presetEngines: {} as Record, + // TD-5236: each model's picker section, the built-in sections in order, + // and each model's quick-switch slot. All as the daemon sent them. + presetGroups: {} as Record, + pickerGroups: [] as string[], + presetSlots: {} as Record, /** False until a model is set up: the first run starts "Add a model". */ hasModel: true, activePreset: null as string | null, @@ -114,6 +125,11 @@ export const settings = $state({ tierLoopback: {} as Record, /** TD-5185: stored prices and limits per tier, and the last card refresh. */ tierCards: {} as Record, + // TD-5241: the model Settings → Model is editing when it is not the one + // new chats use, and that model's roles as the daemon gave them + // (`preset_roles`). Null: the page edits the active model, above. + editPreset: null as string | null, + editRoles: null as RoleView | null, /** A model-card refresh is in flight; cleared by setup_state or an error. */ refreshingCards: false, // TD-5189: the tier whose context window is being saved. @@ -142,6 +158,8 @@ export const settings = $state({ remotePort: null as number | null, /** Paired phones (TD-5014). From setup_state; never a token or hash. */ remoteDevices: [] as RemoteDeviceSummary[], + /** What a paired phone may change (TD-5279). From setup_state; off when absent. */ + phonePermissions: {} as Record, /** Agent engine for new sessions. */ engine: "native" as "native" | "grok", grokAvailable: false, @@ -232,6 +250,10 @@ export function resetSettings(): void { settings.morePresets = []; settings.presetDescriptions = {}; settings.teamPresets = []; + settings.presetEngines = {}; + settings.presetGroups = {}; + settings.pickerGroups = []; + settings.presetSlots = {}; settings.hasModel = true; settings.activePreset = null; settings.tierSlugs = {}; @@ -242,6 +264,8 @@ export function resetSettings(): void { settings.tierCredentials = {}; settings.tierLoopback = {}; settings.tierCards = {}; + settings.editPreset = null; + settings.editRoles = null; settings.refreshingCards = false; settings.savingWindowTier = null; settings.savingCredentialTier = null; @@ -257,6 +281,7 @@ export function resetSettings(): void { settings.remoteBind = null; settings.remotePort = null; settings.remoteDevices = []; + settings.phonePermissions = {}; settings.engine = "native"; settings.grokAvailable = false; settings.grokBinary = null; @@ -405,12 +430,24 @@ function reduce(event: DaemonEventUnion): void { settings.refreshingCards = false; settings.savingWindowTier = null; } + if (event.type === "preset_roles" && event.preset === settings.editPreset) { + settings.editRoles = { + slugs: event.tier_slugs ?? {}, + credentials: event.tier_credentials ?? {}, + loopback: event.tier_loopback ?? {}, + cards: event.tier_cards ?? {}, + }; + } if (event.type === "setup_state") { settings.presets = event.presets; settings.presetLabels = event.preset_labels ?? {}; settings.morePresets = event.more_presets ?? []; settings.presetDescriptions = event.preset_descriptions ?? {}; settings.teamPresets = event.team_presets ?? []; + settings.presetEngines = event.preset_engines ?? {}; + settings.presetGroups = event.preset_groups ?? {}; + settings.pickerGroups = event.picker_groups ?? []; + settings.presetSlots = event.preset_slots ?? {}; settings.hasModel = event.has_model ?? true; settings.activePreset = event.active_preset; // A daemon too old to send the field leaves the model section empty @@ -428,6 +465,15 @@ function reduce(event: DaemonEventUnion): void { settings.savingWindowTier = null; settings.savingTier = null; settings.savingCredentialTier = null; + // TD-5241: setup_state describes the active model. One being edited + // that is not it is read again, or let go if it has been removed. + if (settings.editPreset !== null) { + const gone = !settings.presets.includes(settings.editPreset); + if (gone || settings.editPreset === settings.activePreset) { + settings.editPreset = null; + settings.editRoles = null; + } else sendToDaemon({ type: "get_preset_roles", name: settings.editPreset }); + } settings.skipAllApprovals = event.skip_all_approvals ?? false; settings.loadGlobalMemory = event.load_global_memory ?? false; settings.coworkerEnabled = event.coworker_enabled ?? true; @@ -438,6 +484,7 @@ function reduce(event: DaemonEventUnion): void { settings.remoteBind = event.remote_bind ?? null; settings.remotePort = event.remote_port ?? null; settings.remoteDevices = event.remote_devices ?? []; + settings.phonePermissions = event.phone_permissions ?? {}; settings.engine = event.engine ?? "native"; settings.grokAvailable = event.grok_available ?? false; settings.grokBinary = event.grok_binary ?? null; @@ -463,6 +510,8 @@ function reduce(event: DaemonEventUnion): void { command: row.command ?? [], url: row.url ?? "", enabled: row.enabled ?? true, + originDevice: row.origin_device ?? "", + originAt: row.origin_at ?? "", excludedTools: row.excluded_tools ?? [], values: [...(row.env ?? []), ...(row.headers ?? [])].map((v) => ({ name: v.name, @@ -569,12 +618,54 @@ export function setEngine(kind: "native" | "grok"): void { * save, and saving would pin a model the user meant to leave floating. */ export function isDiscovered(tier: string): boolean { - return settings.tierSlugs[tier] === null; + return roleView().slugs[tier] === null; +} + +/** One model's roles: each one's model, bound key, whether it is served on + * this computer, and its stored numbers. */ +export interface RoleView { + slugs: Record; + credentials: Record; + loopback: Record; + cards: Record; +} + +const NO_ROLES: RoleView = { slugs: {}, credentials: {}, loopback: {}, cards: {} }; + +/** TD-5241: the model Settings → Model edits: the chosen one, else the one + * new chats use. */ +export function editTarget(): string | null { + return settings.editPreset ?? settings.activePreset; +} + +/** The roles of the model being edited. Empty while the daemon's answer for + * a chosen model is still on its way: nothing is shown that was not sent. */ +export function roleView(): RoleView { + if (settings.editPreset === null) { + return { + slugs: settings.tierSlugs, + credentials: settings.tierCredentials, + loopback: settings.tierLoopback, + cards: settings.tierCards, + }; + } + return settings.editRoles ?? NO_ROLES; +} + +/** Choose the model Settings → Model edits. The active one needs no ask. */ +export function chooseEditPreset(name: string): void { + settings.editRoles = null; + if (name === settings.activePreset || !settings.presets.includes(name)) { + settings.editPreset = null; + return; + } + settings.editPreset = name; + sendToDaemon({ type: "get_preset_roles", name }); } /** Persist a tier's slug. No-ops on a blank value or with no active preset. */ export function saveSlug(tier: string, slug: string): void { - const preset = settings.activePreset; + const preset = editTarget(); const trimmed = slug.trim(); if (preset === null || trimmed === "") return; settings.savingTier = tier; @@ -584,7 +675,7 @@ export function saveSlug(tier: string, slug: string): void { /** TD-5185: re-read the active preset's prices and limits from each host. */ export function refreshModelCards(): void { - const preset = settings.activePreset; + const preset = editTarget(); if (preset === null || settings.refreshingCards) return; settings.refreshingCards = true; const sent = sendToDaemon({ type: "refresh_model_cards", preset }); @@ -594,9 +685,9 @@ export function refreshModelCards(): void { /** TD-5189: set a tier's context window by hand. Ignores a non-number or * an unchanged value; the daemon refuses one too small for the reply. */ export function saveContextWindow(tier: string, window: number): void { - const preset = settings.activePreset; + const preset = editTarget(); if (preset === null || !Number.isInteger(window) || window < 1024) return; - if (settings.tierCards[tier]?.context_window === window) return; + if (roleView().cards[tier]?.context_window === window) return; settings.savingWindowTier = tier; const sent = sendToDaemon({ type: "set_tier_context_window", preset, tier, context_window: window }); if (!sent) settings.savingWindowTier = null; @@ -610,7 +701,7 @@ export function credentialHost(credentialId: string): string | null { /** Persist a tier's named key. Empty string unbinds. */ export function saveTierCredential(tier: string, credential: string): void { - const preset = settings.activePreset; + const preset = editTarget(); if (preset === null) return; settings.savingCredentialTier = tier; const sent = sendToDaemon({ @@ -624,9 +715,10 @@ export function saveTierCredential(tier: string, credential: string): void { /** Selected catalog id for the picker: bound, else implicit openrouter on remote. */ export function selectedCredential(tier: string): string { - const bound = settings.tierCredentials[tier]; + const roles = roleView(); + const bound = roles.credentials[tier]; if (bound) return bound; - if (settings.tierLoopback[tier]) return ""; + if (roles.loopback[tier]) return ""; return "openrouter"; } @@ -736,6 +828,21 @@ export function setRemoteAttach(enabled: boolean): void { sendToDaemon({ type: "set_remote_attach", enabled }); } +/** Switch what a paired phone may change (TD-5279). The daemon takes this + * from the computer's own window only. `null` seconds: until switched off. */ +export function setPhonePermission( + capability: string, + enabled: boolean, + expiresInSeconds: number | null, +): void { + sendToDaemon({ + type: "set_phone_permission", + capability, + enabled, + ...(enabled && expiresInSeconds !== null ? { expires_in_seconds: expiresInSeconds } : {}), + }); +} + /** New phone token (TD-5005). Acked with setup_state; the token stays in the file. */ export function rotateRemoteToken(): void { sendToDaemon({ type: "rotate_remote_token" }); diff --git a/ui/src/lib/settings.test.ts b/ui/src/lib/settings.test.ts index 4b275d18..1b31cd4e 100644 --- a/ui/src/lib/settings.test.ts +++ b/ui/src/lib/settings.test.ts @@ -855,6 +855,9 @@ describe("mcp section", () => { command: ["python", "-m", "some_mcp"], url: "", enabled: true, + // TD-5279: no phone touched it. + originDevice: "", + originAt: "", excludedTools: [], values: [], }, diff --git a/ui/src/lib/shortcuts.ts b/ui/src/lib/shortcuts.ts index 68c95488..ab67e079 100644 --- a/ui/src/lib/shortcuts.ts +++ b/ui/src/lib/shortcuts.ts @@ -13,6 +13,9 @@ export interface ShortcutEvent { shiftKey?: boolean; /** ⌘⌥↑ / ⌘⌥↓ step between sessions (navigation round, 2026-09). */ altKey?: boolean; + /** The physical key (`Digit1`). Option changes what a digit key types on + * a Mac, so the model slots read this (TD-5236). */ + code?: string; } export interface ShortcutContext { @@ -37,7 +40,8 @@ export type ShortcutAction = | "stop-computer-use" | "prev-session" | "next-session" - | "pick-session"; + | "pick-session" + | "pick-model"; /** The 1-based rail slot a ⌘1…⌘9 / Ctrl+1…9 press names, or null. Shift * and Option are excluded so ⌘⇧1-style system combos and the characters @@ -49,6 +53,17 @@ export function sessionSlot(event: ShortcutEvent): number | null { return Number(event.key); } +/** The 1-based model slot a ⌘⌥1…⌘⌥9 / Ctrl+Alt+1…9 press names, or null + * (TD-5236). Option is what sets it apart from ⌘1…9, the session slots. + * Read from `code`, with `key` for an event that has none: on a Mac, + * Option+1 types "¡". Shift stays out of it. */ +export function modelSlot(event: ShortcutEvent): number | null { + if (!(event.metaKey || event.ctrlKey) || event.altKey !== true) return null; + if (event.shiftKey === true) return null; + const digit = /^Digit([1-9])$/.exec(event.code ?? "")?.[1] ?? event.key; + return /^[1-9]$/.test(digit) ? Number(digit) : null; +} + /** Map a keydown to one app-level action, or null when nothing applies. */ export function resolveShortcut( event: ShortcutEvent, @@ -91,6 +106,9 @@ export function resolveShortcut( if (event.key === "ArrowDown") return "next-session"; if (event.key === "ArrowUp") return "prev-session"; } + // ⌘⌥1…⌘⌥9 — the model in that quick-switch slot (TD-5236). Not over a + // modal: the chat it would switch is behind it. + if (modelSlot(event) !== null) return context.modalOpen ? null : "pick-model"; // ⌘1…⌘9 — straight to a rail slot, counted from the top. The shell // reads the digit back with `sessionSlot`. if (sessionSlot(event) !== null) return "pick-session"; diff --git a/ui/vite.config.ts b/ui/vite.config.ts index 9fb96b65..4a06f854 100644 --- a/ui/vite.config.ts +++ b/ui/vite.config.ts @@ -39,6 +39,11 @@ export function contentSecurityPolicy(mode: string): NonNullable